diff --git a/aiida/storage/psql_dos/migrator.py b/aiida/storage/psql_dos/migrator.py index b48ae7fded..cc5e11efa5 100644 --- a/aiida/storage/psql_dos/migrator.py +++ b/aiida/storage/psql_dos/migrator.py @@ -384,6 +384,7 @@ def migrate(self) -> None: # finally migrate to the main head revision MIGRATE_LOGGER.report('Migrating to the head of the main branch') self.migrate_up('main@head') + self.connection.commit() def migrate_up(self, version: str) -> None: """Migrate the database up to a specific version. diff --git a/tests/storage/psql_dos/migrations/test_all_schema.py b/tests/storage/psql_dos/migrations/test_all_schema.py index 40dedae7a7..3b57d0e687 100644 --- a/tests/storage/psql_dos/migrations/test_all_schema.py +++ b/tests/storage/psql_dos/migrations/test_all_schema.py @@ -21,6 +21,27 @@ def test_main(version, uninitialised_profile, reflect_schema, data_regression): data_regression.check(reflect_schema(uninitialised_profile)) +def test_main_initialized(uninitialised_profile): + """Test that ``migrate`` properly stamps the new schema version when updating database with existing schema.""" + migrator = PsqlDosMigrator(uninitialised_profile) + + # Initialize database at first version of main branch + migrator.migrate_up('main@main_0001') + assert migrator.get_schema_version_profile(check_legacy=False) == 'main_0001' + migrator.close() + + # Reinitialize the migrator to make sure we are fetching actual state of database and not in-memory state and then + # migrate to head schema version. + migrator = PsqlDosMigrator(uninitialised_profile) + migrator.migrate() + migrator.close() + + # Reinitialize the migrator to make sure we are fetching actual state of database and not in-memory state and then + # assert that the database version is properly set to the head schema version + migrator = PsqlDosMigrator(uninitialised_profile) + assert migrator.get_schema_version_profile(check_legacy=False) == migrator.get_schema_version_head() + + def test_head_vs_orm(uninitialised_profile, reflect_schema, data_regression): """Test that the migrations produce the same database schema as the models.""" migrator = PsqlDosMigrator(uninitialised_profile)