Skip to content

Fix MySQL JSON default and load engine migrations#33

Merged
HamptonMakes merged 1 commit intomainfrom
fix/mysql-json-default-and-engine-migrations
Feb 27, 2026
Merged

Fix MySQL JSON default and load engine migrations#33
HamptonMakes merged 1 commit intomainfrom
fix/mysql-json-default-and-engine-migrations

Conversation

@HamptonMakes
Copy link
Collaborator

Problem

Migrations fail with:

Mysql2::Error: BLOB, TEXT, GEOMETRY or JSON column 'metadata' can't have a default value

And the engine's coplan_users migration wasn't being picked up by the app, causing a second failure when the app migration tried to INSERT into a table that didn't exist yet.

Changes

  1. Remove default: {} from JSON columns — MySQL doesn't support defaults on JSON/BLOB/TEXT columns. The model already handles this via after_initialize { self.metadata ||= {} }.

  2. Add engine migration path initializer — The engine wasn't appending its db/migrate path, so Rails never discovered the engine's migrations. Added an initializer that appends the path early enough for the migrator to see it.

  3. Remove duplicate table creation from app migration — The engine migration (20260226200000) owns the coplan_users table schema. The app migration (20260226200001) now only handles data migration from users and FK rewiring.

- Remove 'default: {}' from JSON metadata columns in both engine and app
  migrations (MySQL doesn't support defaults on JSON/BLOB/TEXT columns)
- Add initializer to append engine migration paths so Rails picks them up
- Remove duplicate table creation from app migration — engine owns the
  schema, app migration only handles data migration and FK rewiring

Amp-Thread-ID: https://ampcode.com/threads/T-019c9ba5-621e-7769-81b4-99b46ff27501
Co-authored-by: Amp <amp@ampcode.com>
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d11067b4fa

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +21 to +24
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

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 👍 / 👎.

@@ -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 👍 / 👎.

@HamptonMakes HamptonMakes merged commit 10a77ac into main Feb 27, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant