Skip to content

Update database schema checks

After Execute migrations in milestone order (gitlab-org/gitlab!137190 - merged), SELECT MAX(version) AS version FROM #{schema_migrations_table_name} is no longer correct way to match the "current" schema version. Same applies for sorting the migration files by name (timestamp)- https://gitlab.com/gitlab-org/build/CNG/-/blob/master/gitlab-rails/scripts/wait-for-deps#L31.

We need to update https://gitlab.com/gitlab-org/build/CNG/-/blob/master/gitlab-rails/scripts/lib/checks/postgresql.rb (and the equivalent Omnibus check, if any) to use the new migration sorting logic.

The correct and easiest way to do this now is something like

migrations_paths = Gitlab::Database.database_base_models['main'].connection.migrations_paths;
migration_context = ActiveRecord::MigrationContext.new(migrations_paths, ActiveRecord::SchemaMigration)
migrator = ActiveRecord::Migrator.new(:up, migration_context.migrations, migration_context.schema_migration)

database_version = migrator.current_version
codebase_version = migrator.migrations.sort_by(&:version).last.version

database_version >= codebase_version

but this has to executed using rails runner, which loads the whole Rails environment, and may add some delay to the process. If this is not acceptable, we may try to extract / recreate the logic in a plain ruby script, though it will still have to load all migration files.

This new sorting logic was introduced in 17.1.