From 13f5952336ecd8785759d7bb764b014802eabc0d Mon Sep 17 00:00:00 2001 From: Kevin McConnell Date: Wed, 2 Oct 2024 13:47:05 +0100 Subject: [PATCH] Fix migrations that can no longer be applied The migrations could no longer be applied on an empty database, mainly because they referred to models that no longer existed. Replaced the model references with direct table access that will always be correct at the time the migration is applied. Also removed conflicting indexes in table renames. --- db/migrate/20240813200622_rename_organizations_to_accounts.rb | 3 --- db/migrate/20240916191616_create_projects.rb | 2 -- db/migrate/20240916193936_add_project_to_bubbles.rb | 3 ++- db/migrate/20241001200953_add_account_to_tags.rb | 3 ++- 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/db/migrate/20240813200622_rename_organizations_to_accounts.rb b/db/migrate/20240813200622_rename_organizations_to_accounts.rb index e6ec31e42..a0fbe2ec2 100644 --- a/db/migrate/20240813200622_rename_organizations_to_accounts.rb +++ b/db/migrate/20240813200622_rename_organizations_to_accounts.rb @@ -1,9 +1,6 @@ class RenameOrganizationsToAccounts < ActiveRecord::Migration[8.0] def change rename_table :organizations, :accounts - add_index :accounts, :name, unique: true - rename_column :users, :organization_id, :account_id - add_index :users, :account_id end end diff --git a/db/migrate/20240916191616_create_projects.rb b/db/migrate/20240916191616_create_projects.rb index c537d2f40..9d12b0066 100644 --- a/db/migrate/20240916191616_create_projects.rb +++ b/db/migrate/20240916191616_create_projects.rb @@ -7,7 +7,5 @@ class CreateProjects < ActiveRecord::Migration[8.0] t.timestamps end - - Account.first.projects.create! name: "Writebook", creator: User.first end end diff --git a/db/migrate/20240916193936_add_project_to_bubbles.rb b/db/migrate/20240916193936_add_project_to_bubbles.rb index 58069b98f..5ec29cbba 100644 --- a/db/migrate/20240916193936_add_project_to_bubbles.rb +++ b/db/migrate/20240916193936_add_project_to_bubbles.rb @@ -4,7 +4,8 @@ class AddProjectToBubbles < ActiveRecord::Migration[8.0] t.references :project, null: true end - Bubble.update_all project_id: Project.first.id + execute "update bubbles set project_id = (select id from projects limit 1)" + change_column_null :bubbles, :project_id, false end end diff --git a/db/migrate/20241001200953_add_account_to_tags.rb b/db/migrate/20241001200953_add_account_to_tags.rb index 6a20b6836..56243f0d1 100644 --- a/db/migrate/20241001200953_add_account_to_tags.rb +++ b/db/migrate/20241001200953_add_account_to_tags.rb @@ -4,7 +4,8 @@ class AddAccountToTags < ActiveRecord::Migration[8.0] t.references :account, null: true end - Tag.update_all account_id: Account.first.id + execute "update tags set account_id = (select id from accounts limit 1)" + change_column_null :tags, :account_id, false end end