ilyakatz / data-migrate

Migrate and update data alongside your database structure.
MIT License
1.4k stars 192 forks source link

Using db:migrate:with_data breaks db:seed for new database when run together #306

Closed maurobender closed 4 months ago

maurobender commented 4 months ago

When creating, migrating and seeding the database for the first time, if we run

bundle exec rake db:create db:migrate:with_data db:seed

The execution ends with an error referring to a undefined column. This column is one that was deleted after a data migration using it, see the following redacted output:

== 20240404103431 AddNewColumnsToTable: migrating
-- add_column(:table, :new_column1, :integer, {:null=>true})
   -> 0.0090s
-- add_column(:table, :new_column2, :string, {:null=>true})
   -> 0.0007s
== 20240404103431 AddNewColumnToTable: migrated (0.0098s)

== 20240404105505 MigrateDataFromOldColumnToNewColumns: migrating
== 20240404105505 MigrateDataFromOldColumnToNewColumns: migrated (0.0173s)

== 20240404105940 RemoveOldColumn: migrating ===============
-- remove_column(:table, :old_column, :string, {:null=>true})
   -> 0.0070s
== 20240404105940 RemoveOldColumn: migrated (0.0071s) ======

... OTHER MIGRATIONS

Running seeds...
rails aborted!
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR:  column "old_column" of relation "table" does not exist
LINE 1: ...other_column1", "other_column2", "old_col...
                                                             ^
/srv/app/db/seeds.rb:10:in `<main>'

Caused by:
PG::UndefinedColumn: ERROR:  column "old_column" of relation "table" does not exist
LINE 1: ...other_column1", "other_column2", "old_col...
                                                             ^
/srv/app/db/seeds.rb:10:in `<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)

This error is only throw if we run db:migrate:with_data instead of db:migrate, for example the following command runs without any error:

bundle exec rake db:create db:migrate db:seed

Or if we split the command in two, first creating and migrating the database and then seeding in another command it works too without any issues:

bundle exec rake db:create db:migrate
bundle exec rake db:seed

The problem seems to be related to run the seeds along the migration with data.

Here is some information about the versions:

Please let me know if you need any more information in order to look into this issue.