Draft: Resolve "squash migrations script"
What does this MR do and why?
In this MR, we are introducing an automated way to squash old migrations.
How to run the script:
cd gitlab-
scripts/squash-migrations MIGRATION_VERSION-> This will squash all migrations where<= MIGRATION_VERSION.
Script steps:
-
bundle exec rake db:drop db:create db:migrate VERSION=20190909141517-> Drops the database; Creates a new database; Migrates to a specific version -
bundle exec rake db:structure:dump SCHEMA=db/init_structure.sql-> Creates a new structure file with the nameinit_structure.sql -
rake remove_old_migrations[20190909141517]-> Removes all migration files + specs (*) -
bundle exec rake db:drop db:create db:migrate-> Creates a new database without using the old migrations
(*) We are not removing the init schema migration.
Note:
This only works if we change the content of the InitSchema class to something like this:
class InitSchema < ActiveRecord::Migration[6.0]
def up
execute(IO.read("db/init_structure.sql"))
end
def down
raise ActiveRecord::IrreversibleMigration, "The initial migration is not revertable"
end
end
Questions:
- Is it OK to always keep the
/db/migrate/20181228175414_init_schema.rbfile? I guess the most common strategy is to rename the last file that we want to squash to init schema. -
execute(IO.read("db/init_structure.sql"))-> I am not sure if this creates a security issue, since we are reading sql instructions from a file. - I am not sure if we can use the plain
db/init_structure.sqlfile. I need to double-check this.
What do you think @abrandl @pbair?
Btw: This is just a draft. I would like to hear your opinion first before I start to refactor and add tests to the code :)
Describe in detail what your merge request does and why.
Screenshots or screen recordings
These are strongly recommended to assist reviewers and reduce the time to merge your change.
How to set up and validate locally
Numbered steps to set up and validate the change are strongly suggested.
MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
-
I have evaluated the MR acceptance checklist for this MR.
Related to #331248 (closed)