Trouble Migrating Database

Skip to first unread message

Apr 11, 2023, 8:47:45 AM4/11/23
to aiidausers
Hi Everyone,

I'm trying to migrate a legacy database but I get the following error: 

(paint) daniel:~/aiida_backup$ verdi -p aiida_v1 storage migrate
Warning: Migrating your storage might take a while and is not reversible.
Warning: Before continuing, make sure you have completed the following steps:
Warning:  1. Make sure you have no active calculations and workflows.
Warning:  2. If you do, revert the code to the previous version and finish running them first.
Warning:  3. Stop the daemon using `verdi daemon stop`
Warning:  4. Make a backup of your database and repository
Warning: If you have completed the steps above and want to migrate profile "aiida_v1", type MIGRATE NOW: MIGRATE NOW
Report: Migrating to the head of the legacy sqlalchemy branch
Report: - bf591f31dd12 -> 0edcdd5a30f0
Report: - 0edcdd5a30f0 -> 7536a82b2cc4
Warning: Detected repository folders that were missing the required subfolder `path` or `raw_input`. The paths of those nodes repository folders have been written to a log file: /home/daniel/aiida_backup/migration-repository-missing-subfolder-4d_jv5r3.json
Warning: Migrated file repository to the new disk object store. The old repository has not been deleted out of safety and can be found at /home/daniel/aiida_backup/repository-aiida_v1.1/repository.
Report: - 7536a82b2cc4 -> 1feaea71bd5a
Report: - 1feaea71bd5a -> 535039300e4a
Report: - 535039300e4a -> 34a831f4286d
Report: - 34a831f4286d -> 1de112340b16
Traceback (most recent call last):
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/sqlalchemy/engine/", line 1900, in _execute_context
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/sqlalchemy/engine/", line 736, in do_execute
    cursor.execute(statement, parameters)
psycopg2.errors.ObjectInUse: cannot ALTER TABLE "db_dbnode" because it has pending trigger events

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/daniel/virtualenvs/paint/bin/verdi", line 8, in <module>
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/click/", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/click/", line 1055, in main
    rv = self.invoke(ctx)
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/click/", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/click/", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/click/", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/click/", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/home/daniel/install/paint/aiida-core/aiida/cmdline/commands/", line 78, in storage_migrate
  File "/home/daniel/install/paint/aiida-core/aiida/storage/psql_dos/", line 92, in migrate
  File "/home/daniel/install/paint/aiida-core/aiida/storage/psql_dos/", line 377, in migrate
  File "/home/daniel/install/paint/aiida-core/aiida/storage/psql_dos/", line 394, in migrate_up
    upgrade(config, version)
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/alembic/", line 378, in upgrade
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/alembic/script/", line 576, in run_env
    util.load_python_file(self.dir, "")
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/alembic/util/", line 94, in load_python_file
    module = load_module_py(module_id, path)
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/alembic/util/", line 110, in load_module_py
    spec.loader.exec_module(module)  # type: ignore
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/daniel/install/paint/aiida-core/aiida/storage/psql_dos/migrations/", line 49, in <module>
  File "/home/daniel/install/paint/aiida-core/aiida/storage/psql_dos/migrations/", line 42, in run_migrations_online
    context.run_migrations()  # pylint: disable=no-member
  File "<string>", line 8, in run_migrations
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/alembic/runtime/", line 867, in run_migrations
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/alembic/runtime/", line 624, in run_migrations
  File "/home/daniel/install/paint/aiida-core/aiida/storage/psql_dos/migrations/versions/", line 73, in upgrade
    op.alter_column('db_dbnode', 'ctime', existing_type=sa.DateTime(timezone=True), nullable=False)
  File "<string>", line 8, in alter_column
  File "<string>", line 3, in alter_column
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/alembic/operations/", line 1879, in alter_column
    return operations.invoke(alt)
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/alembic/operations/", line 401, in invoke
    return fn(self, operation)
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/alembic/operations/", line 50, in alter_column
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/alembic/ddl/", line 178, in alter_column
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/alembic/ddl/", line 229, in alter_column
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/alembic/ddl/", line 193, in _exec
    return conn.execute(  # type: ignore[call-overload]
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/sqlalchemy/future/", line 280, in execute
    return self._execute_20(
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/sqlalchemy/engine/", line 1705, in _execute_20
    return meth(self, args_10style, kwargs_10style, execution_options)
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/sqlalchemy/sql/", line 80, in _execute_on_connection
    return connection._execute_ddl(
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/sqlalchemy/engine/", line 1472, in _execute_ddl
    ret = self._execute_context(
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/sqlalchemy/engine/", line 1943, in _execute_context
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/sqlalchemy/engine/", line 2124, in _handle_dbapi_exception
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/sqlalchemy/util/", line 211, in raise_
    raise exception
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/sqlalchemy/engine/", line 1900, in _execute_context
  File "/home/daniel/virtualenvs/paint/lib/python3.10/site-packages/sqlalchemy/engine/", line 736, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (psycopg2.errors.ObjectInUse) cannot ALTER TABLE "db_dbnode" because it has pending trigger events

(Background on this error at:

This seems very similar to a previous error I once had, and was resolved here:

I was hoping to use 
$python aiida/backends/sqlalchemy/ upgrade <version>
 as a workaround, but it seems like the '' script no longer exists? Still, is there a way to run the migration 'one step at a time' as a workaround? 


Apr 12, 2023, 8:43:28 AM4/12/23
to aiidausers
I wanted to share a workaround I found for migrating a legacy database
To address the issue, I performed the following steps:

Downgraded my AiiDA installation to v1.6.9, which did not require a database migration.
Upgraded AiiDA one version at a time (e.g., v1.6.9 to v2.0.0, v2.0.0 to v2.0.1, etc.), migrating the database if necessary.
By only performing one migration at a time, I was able to successfully migrate the database. 

Best regards,

Apr 23, 2023, 3:55:54 AM4/23/23
to aiidausers
Hi Daniel,

Sorry for the slow response, I'm not much of a database wizz to be honest. Glad to hear you manage to resolve the issue, and thanks for sharing your solution! However, it shouldn't be necessary to do this, so I'll bring it up at the next AiiDA team meeting and see if we can understand what happened.


Reply all
Reply to author
0 new messages