Skip to content

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:

  1. cd gitlab
  2. scripts/squash-migrations MIGRATION_VERSION -> This will squash all migrations where <= MIGRATION_VERSION.

Script steps:

  1. bundle exec rake db:drop db:create db:migrate VERSION=20190909141517 -> Drops the database; Creates a new database; Migrates to a specific version
  2. bundle exec rake db:structure:dump SCHEMA=db/init_structure.sql -> Creates a new structure file with the name init_structure.sql
  3. rake remove_old_migrations[20190909141517] -> Removes all migration files + specs (*)
  4. 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:

  1. Is it OK to always keep the /db/migrate/20181228175414_init_schema.rb file? I guess the most common strategy is to rename the last file that we want to squash to init schema.
  2. 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.
  3. I am not sure if we can use the plain db/init_structure.sql file. 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.

Related to #331248 (closed)

Edited by Diogo Frazão

Merge request reports

Loading