Track finished_at timestamp for schema migrations
What does this MR do?
This adds a timestamp column to schema_migrations
, which helps to track execution of database migrations in production.
This column defaults to NOW()
except for already existing records (we can't reason about that). For new installations bootstrapping from db/structure.sql
, all migrations are going to get NOW()
as their execution time - even though we didn't really run migrations. I think this is fine as it indicates when a certain schema "state" has been reached.
This relates to #323343.
Overall, I'd also like to be able to retroactively get the duration of a migration in production. This entails adding another started_at
timestamp. It's a bit trickier since we may need to patch ActiveRecord (e.g. here) or send an upstream change.
Overall I'm not entirely clear yet why rails doesn't track this information by default. This may be a good candidate for upstreaming later.
Up
== 20210616134905 AddTimestampToSchemaMigration: migrating ====================
-- add_column(:schema_migrations, :finished_at, :timestamptz)
-> 0.0016s
-- change_column_default(:schema_migrations, :finished_at, #<Proc:0x000055ac6eb4b020 /home/abrandl/workspace/gdk/gitlab/db/migrate/20210616134905_add_timestamp_to_schema_migration.rb:9 (lambda)>)
-> 0.0023s
== 20210616134905 AddTimestampToSchemaMigration: migrated (0.0039s) ===========
Down
== 20210616134905 AddTimestampToSchemaMigration: reverting ====================
-- remove_column(:schema_migrations, :finished_at)
-> 0.0017s
== 20210616134905 AddTimestampToSchemaMigration: reverted (0.0018s) ===========