Skip to content

Commit

Permalink
Use alembic revision for migration
Browse files Browse the repository at this point in the history
  • Loading branch information
k-burt-uch committed Apr 11, 2023
1 parent c641de5 commit c506ec7
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 10 deletions.
22 changes: 20 additions & 2 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,15 @@
"line_number": 14
}
],
"migrations/versions/a72f117515c5_add_description_created_time_and_.py": [
{
"type": "Hex High Entropy String",
"filename": "migrations/versions/a72f117515c5_add_description_created_time_and_.py",
"hashed_secret": "7c52f05d2823f0becb8196c04678c822ac71bcdf",
"is_verified": false,
"line_number": 14
}
],
"tests/default_test_settings.py": [
{
"type": "Basic Auth Credentials",
Expand All @@ -242,6 +251,15 @@
"line_number": 18
}
],
"tests/postgres/migrations/test_a72f117515c5_add_description_created_time_and_.py": [
{
"type": "Hex High Entropy String",
"filename": "tests/postgres/migrations/test_a72f117515c5_add_description_created_time_and_.py",
"hashed_secret": "7c52f05d2823f0becb8196c04678c822ac71bcdf",
"is_verified": false,
"line_number": 31
}
],
"tests/postgres/migrations/test_legacy_schema_migration.py": [
{
"type": "Hex High Entropy String",
Expand Down Expand Up @@ -382,7 +400,7 @@
"filename": "tests/test_deprecated_aliases_endpoints.py",
"hashed_secret": "5666c088b494f26cd8f63ace013992f5fc391ce0",
"is_verified": false,
"line_number": 12
"line_number": 13
}
],
"tests/test_drs.py": [
Expand All @@ -395,5 +413,5 @@
}
]
},
"generated_at": "2023-04-10T21:30:00Z"
"generated_at": "2023-04-11T15:19:20Z"
}
3 changes: 0 additions & 3 deletions migrations/versions/15f2e9345ade_create_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ def upgrade() -> None:
sa.Column("file_name", sa.VARCHAR(), nullable=True),
sa.Column("version", sa.VARCHAR(), nullable=True),
sa.Column("uploader", sa.VARCHAR(), nullable=True),
sa.Column("description", sa.VARCHAR(), nullable=True),
sa.Column("content_created_date", sa.DateTime, nullable=True),
sa.Column("content_updated_date", sa.DateTime, nullable=True),
sa.ForeignKeyConstraint(
["baseid"],
["base_version.baseid"],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Add description, created_time and updated_time columns to IndexRecord
Revision ID: a72f117515c5
Revises: 15f2e9345ade
Create Date: 2023-04-11 10:00:59.250768
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "a72f117515c5"
down_revision = "15f2e9345ade"
branch_labels = None
depends_on = None


def upgrade() -> None:
op.add_column(
"index_record", sa.Column("content_created_date", sa.DateTime, nullable=True)
)
op.add_column(
"index_record", sa.Column("content_updated_date", sa.DateTime, nullable=True)
)
op.add_column("index_record", sa.Column("description", sa.VARCHAR(), nullable=True))


def downgrade() -> None:
op.drop_column("index_record", "content_created_date")
op.drop_column("index_record", "content_updated_date")
op.drop_column("index_record", "description")
6 changes: 1 addition & 5 deletions tests/postgres/migrations/test_15f2e9345ade_create_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,8 @@ def test_upgrade(postgres_driver):
("file_name", "character varying"),
("version", "character varying"),
("uploader", "character varying"),
("description", "character varying"),
("content_created_date", "timestamp without time zone"),
("content_updated_date", "timestamp without time zone"),
]
actual_schema = sorted([i for i in cols])
assert sorted(expected_schema) == actual_schema
assert sorted(expected_schema) == sorted([i for i in cols])


def test_downgrade(postgres_driver):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from alembic.config import main as alembic_main


def test_upgrade(postgres_driver):
conn = postgres_driver.engine.connect()
alembic_main(["--raiseerr", "upgrade", "a72f117515c5"])
cols = conn.execute(
"SELECT column_name, data_type FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'index_record'"
)
expected_schema = [
("did", "character varying"),
("baseid", "character varying"),
("rev", "character varying"),
("form", "character varying"),
("size", "bigint"),
("created_date", "timestamp without time zone"),
("updated_date", "timestamp without time zone"),
("file_name", "character varying"),
("version", "character varying"),
("uploader", "character varying"),
("description", "character varying"),
("content_created_date", "timestamp without time zone"),
("content_updated_date", "timestamp without time zone"),
]
actual_schema = sorted([i for i in cols])
assert sorted(expected_schema) == actual_schema


def test_downgrade(postgres_driver):
conn = postgres_driver.engine.connect()
alembic_main(["--raiseerr", "downgrade", "15f2e9345ade"])
cols = conn.execute(
"SELECT column_name, data_type FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'index_record'"
)
expected_schema = [
("did", "character varying"),
("baseid", "character varying"),
("rev", "character varying"),
("form", "character varying"),
("size", "bigint"),
("created_date", "timestamp without time zone"),
("updated_date", "timestamp without time zone"),
("file_name", "character varying"),
("version", "character varying"),
("uploader", "character varying"),
]
actual_schema = sorted([i for i in cols])
assert sorted(expected_schema) == actual_schema

0 comments on commit c506ec7

Please sign in to comment.