-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update alembic migration to use new database (#3841)
- Upgrade SQLAlchemy for the processing container so it matches the other containers - Upgrade Alembic for local development. - factor out "persistance_schema" into a constant to avoid naming issues. - Update alembic to support both the new and legacy ORM for now. The legacy ORM can be removed in a future migration. - Add an Alembic migration for the redesign database changes. This is will not affect the legacy database. - Modify `migrate_redesign_write` so it no longer create the new database tables. This should be handled by the automated migration processes in GHA. - Fixe the legacy ORM to match the actual database. - Update `create_db` to use the new ORM. - updated sqlalchemy in the processing container to be current with other containers. - Add Make recipe `db/check` to make it easier to detect if the ORM is different from the database. - Add a try..except block in `DatabaseProvider._drop_schema` so prevent breaking if the schema does not exist. - Add support for creating both the legacy and new database. - removing CORPORA_LOCAL_DEV from setup_dev_data.sh.
- Loading branch information
Showing
15 changed files
with
153 additions
and
47 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
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,89 @@ | ||
"""redesign | ||
Revision ID: 33_c5aaf6e2ca9e | ||
Revises: 32_c27083d1a76d | ||
Create Date: 2023-01-05 16:06:27.723131 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "33_c5aaf6e2ca9e" | ||
down_revision = "32_c27083d1a76d" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
op.execute("CREATE SCHEMA persistence_schema") | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
"Collection", | ||
sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False), | ||
sa.Column("version_id", postgresql.UUID(as_uuid=True), nullable=True), | ||
sa.Column("originally_published_at", sa.DateTime(), nullable=True), | ||
sa.Column("tombstoned", sa.BOOLEAN(), nullable=True), | ||
sa.PrimaryKeyConstraint("id"), | ||
schema="persistence_schema", | ||
) | ||
op.create_table( | ||
"CollectionVersion", | ||
sa.Column("version_id", postgresql.UUID(as_uuid=True), nullable=False), | ||
sa.Column("collection_id", postgresql.UUID(as_uuid=True), nullable=True), | ||
sa.Column("metadata", postgresql.JSON(astext_type=sa.Text()), nullable=True), | ||
sa.Column("owner", sa.String(), nullable=True), | ||
sa.Column("curator_name", sa.String(), nullable=True), | ||
sa.Column("publisher_metadata", postgresql.JSON(astext_type=sa.Text()), nullable=True), | ||
sa.Column("published_at", sa.DateTime(), nullable=True), | ||
sa.Column("created_at", sa.DateTime(), nullable=True), | ||
sa.Column("datasets", postgresql.ARRAY(postgresql.UUID(as_uuid=True)), nullable=True), | ||
sa.PrimaryKeyConstraint("version_id"), | ||
schema="persistence_schema", | ||
) | ||
op.create_table( | ||
"Dataset", | ||
sa.Column("dataset_id", postgresql.UUID(as_uuid=True), nullable=False), | ||
sa.Column("dataset_version_id", postgresql.UUID(as_uuid=True), nullable=True), | ||
sa.Column("published_at", sa.DateTime(), nullable=True), | ||
sa.PrimaryKeyConstraint("dataset_id"), | ||
schema="persistence_schema", | ||
) | ||
op.create_table( | ||
"DatasetArtifact", | ||
sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False), | ||
sa.Column("type", sa.Enum("RAW_H5AD", "H5AD", "RDS", "CXG", name="datasetartifacttype"), nullable=True), | ||
sa.Column("uri", sa.String(), nullable=True), | ||
sa.PrimaryKeyConstraint("id"), | ||
schema="persistence_schema", | ||
) | ||
op.create_table( | ||
"DatasetVersion", | ||
sa.Column("version_id", postgresql.UUID(as_uuid=True), nullable=False), | ||
sa.Column("dataset_id", postgresql.UUID(as_uuid=True), nullable=True), | ||
sa.Column("collection_id", postgresql.UUID(as_uuid=True), nullable=True), | ||
sa.Column("created_at", sa.DateTime(), nullable=True), | ||
sa.Column("metadata", postgresql.JSON(astext_type=sa.Text()), nullable=True), | ||
sa.Column("artifacts", postgresql.ARRAY(postgresql.UUID(as_uuid=True)), nullable=True), | ||
sa.Column("status", postgresql.JSON(astext_type=sa.Text()), nullable=True), | ||
sa.ForeignKeyConstraint( | ||
["dataset_id"], | ||
["persistence_schema.Dataset.dataset_id"], | ||
), | ||
sa.PrimaryKeyConstraint("version_id"), | ||
schema="persistence_schema", | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_table("DatasetVersion", schema="persistence_schema") | ||
op.drop_table("DatasetArtifact", schema="persistence_schema") | ||
op.drop_table("Dataset", schema="persistence_schema") | ||
op.drop_table("CollectionVersion", schema="persistence_schema") | ||
op.drop_table("Collection", schema="persistence_schema") | ||
# ### end Alembic commands ### | ||
sa.Enum(name="datasetartifacttype").drop(op.get_bind(), checkfirst=False) | ||
op.execute("DROP SCHEMA persistence_schema") |
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 @@ | ||
SCHEMA_NAME = "persistence_schema" |
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
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 |
---|---|---|
@@ -1,20 +1,35 @@ | ||
""" | ||
Drops and recreates all tables according to corpora_orm.py | ||
Drops and recreates all tables according to corpora_orm.py and orm.py | ||
""" | ||
|
||
from sqlalchemy import create_engine | ||
|
||
from backend.common.corpora_config import CorporaDbConfig | ||
from backend.common.corpora_orm import Base | ||
def legacy_db(): | ||
from sqlalchemy import create_engine | ||
|
||
from backend.common.corpora_config import CorporaDbConfig | ||
from backend.common.corpora_orm import Base | ||
|
||
def create_db(): | ||
engine = create_engine(CorporaDbConfig().database_uri) | ||
print("Dropping tables") | ||
print("legacy db: Dropping tables") | ||
Base.metadata.drop_all(engine) | ||
print("Recreating tables") | ||
print("legacy db: Recreating tables") | ||
Base.metadata.create_all(engine) | ||
|
||
|
||
def current_db(): | ||
from backend.layers.persistence.persistence import DatabaseProvider | ||
|
||
db_provider = DatabaseProvider() | ||
print("current db: Dropping tables") | ||
db_provider._drop_schema() | ||
print("current db: Recreating tables") | ||
db_provider._create_schema() | ||
|
||
|
||
def create_db(): | ||
legacy_db() | ||
current_db() | ||
|
||
|
||
if __name__ == "__main__": | ||
create_db() |
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
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