Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions db/migrate/20260226200001_create_coplan_users_and_migrate.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
class CreateCoplanUsersAndMigrate < ActiveRecord::Migration[8.1]
def up
create_table :coplan_users, id: { type: :string, limit: 36 } do |t|
t.string :external_id, null: false
t.string :name, null: false
t.boolean :admin, default: false, null: false
t.json :metadata, default: {}
t.timestamps
end

add_index :coplan_users, :external_id, unique: true

# Migrate existing users — reuse the same id so FK columns stay valid
# Table created by engine migration; migrate existing users data
execute <<~SQL
INSERT INTO coplan_users (id, external_id, name, admin, metadata, created_at, updated_at)
SELECT id, id, name, (role = 'admin'), '{}', created_at, updated_at
Expand Down Expand Up @@ -47,7 +37,5 @@ def down
add_foreign_key :coplan_plan_collaborators, :users, column: :user_id
add_foreign_key :coplan_plan_collaborators, :users, column: :added_by_user_id
add_foreign_key :coplan_plans, :users, column: :created_by_user_id

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Make rollback clear copied users before rerunning migration

After this change, down no longer removes rows inserted into coplan_users, so a rollback/re-migrate cycle (db:rollback STEP=1 then db:migrate) will re-run the INSERT INTO coplan_users ... SELECT ... FROM users and hit duplicate primary-key/unique-key errors for existing users. This affects environments that test or rely on rollback/redeploy workflows.

Useful? React with 👍 / 👎.


drop_table :coplan_users
end
end
2 changes: 1 addition & 1 deletion engine/db/migrate/20260226200000_create_coplan_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def change
t.string :external_id, null: false
t.string :name, null: false
t.boolean :admin, default: false, null: false
t.json :metadata, default: {}
t.json :metadata
t.timestamps
end

Expand Down
7 changes: 7 additions & 0 deletions engine/lib/coplan/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class Engine < ::Rails::Engine
app.config.assets.paths << Engine.root.join("app/javascript")
end

initializer "coplan.append_migrations", before: :load_config_initializers do |app|
config.paths["db/migrate"].expanded.each do |path|
app.config.paths["db/migrate"] << path
ActiveRecord::Migrator.migrations_paths << path
Comment on lines +21 to +24

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Avoid loading engine migrations alongside installed copies

Appending engine/db/migrate at runtime makes Rails evaluate engine migration files in addition to any copied migrations from bin/rails coplan:install:migrations (as documented in docs/HOST_APP_GUIDE.md:19), which leads to duplicate migration name/version errors and blocks db:migrate in host apps that followed the documented install flow. This initializer should guard against double-registration (or choose one migration delivery mechanism) so migrations are not loaded twice.

Useful? React with 👍 / 👎.

end
end

initializer "coplan.factories", after: "factory_bot.set_factory_paths" do
if defined?(FactoryBot)
FactoryBot.definition_file_paths << Engine.root.join("spec", "factories")
Expand Down