-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SqliteDosStorage
: Make the migrator compatible with SQLite
The majority of the `SqliteDosStorage` piggy-backs off of the `PsqlDosStorage` plugin. It also uses the `PsqlDosMigrator` as-is to perform the database migrations. This is not safe however, as PostgreSQL and SQLite do not have exactly the same syntax. An example is the `main_0002` revision which was added to drop the hashes of certain nodes. This uses the `#-` operator which is JSONB specific syntax of PostgreSQL and is not supported by SQLite. Since this migration was added before the `SqliteDosStorage` plugin was added, this has never caused a problems as all profiles would be new, would not have any nodes and therefore the SQL code of the migration would not actually be executed. In preparation for any future migrations that may need to be added, the `SqliteDosStorage` now uses the `SqliteDosMigrator`. This subclasses the `PsqlDosMigrator` as it can still use most of the functionality, but it changes a few critical things. Most notably the location of the schema versions which now are kept individually and are no longer lent from the `core.psql_dos` plugin. The initial version `main_0001_initial.py` is taken from the migration `main_0000_initial.py` of the `core.sqlite_zip` storage plugin. The only difference is that UUID fields are declared as `String(32)` instead of `CHAR(32)`. The SQLAlchemy models that are automatically generated for SQLite from the PostgreSQL-based models actually use the latter type. See `aiida.storage.sqlite_zip.models:pg_to_sqlite`.
- Loading branch information
Showing
12 changed files
with
1,354 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
"""Module with common resources related to storage migrations.""" | ||
|
||
TEMPLATE_INVALID_SCHEMA_VERSION = """ | ||
Database schema version `{schema_version_database}` is incompatible with the required schema version `{schema_version_code}`. | ||
To migrate the database schema version to the current one, run the following command: | ||
verdi -p {profile_name} storage migrate | ||
""" # noqa: E501 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
########################################################################### | ||
# Copyright (c), The AiiDA team. All rights reserved. # | ||
# This file is part of the AiiDA code. # | ||
# # | ||
# The code is hosted on GitHub at https://github.com/aiidateam/aiida-core # | ||
# For further information on the license, see the LICENSE.txt file # | ||
# For further information please visit http://www.aiida.net # | ||
########################################################################### | ||
"""Upper level SQLAlchemy migration functions.""" | ||
|
||
from alembic import context | ||
|
||
|
||
def run_migrations_online(): | ||
"""Run migrations in 'online' mode. | ||
The connection should have been passed to the config, which we use to configue the migration context. | ||
""" | ||
from aiida.storage.sqlite_zip.models import SqliteBase | ||
|
||
config = context.config | ||
|
||
connection = config.attributes.get('connection', None) | ||
aiida_profile = config.attributes.get('aiida_profile', None) | ||
on_version_apply = config.attributes.get('on_version_apply', None) | ||
|
||
if connection is None: | ||
from aiida.common.exceptions import ConfigurationError | ||
|
||
raise ConfigurationError('An initialized connection is expected for the AiiDA online migrations.') | ||
if aiida_profile is None: | ||
from aiida.common.exceptions import ConfigurationError | ||
|
||
raise ConfigurationError('An aiida_profile is expected for the AiiDA online migrations.') | ||
|
||
context.configure( | ||
connection=connection, | ||
target_metadata=SqliteBase.metadata, | ||
transaction_per_migration=True, | ||
aiida_profile=aiida_profile, | ||
on_version_apply=on_version_apply, | ||
) | ||
|
||
context.run_migrations() | ||
|
||
|
||
try: | ||
if context.is_offline_mode(): | ||
raise NotImplementedError('This feature is not currently supported.') | ||
|
||
run_migrations_online() | ||
except NameError: | ||
# This will occur in an environment that is just compiling the documentation | ||
pass |
Oops, something went wrong.