-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add new test for roundtrip downgrade isolation.
- Loading branch information
Showing
17 changed files
with
302 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
examples/test_downgrade_leaves_no_trace_failure/alembic.ini
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,36 @@ | ||
[alembic] | ||
script_location = migrations | ||
|
||
[loggers] | ||
keys = root,sqlalchemy,alembic | ||
|
||
[handlers] | ||
keys = console | ||
|
||
[formatters] | ||
keys = generic | ||
|
||
[logger_root] | ||
level = WARN | ||
handlers = console | ||
qualname = | ||
|
||
[logger_sqlalchemy] | ||
level = WARN | ||
handlers = | ||
qualname = sqlalchemy.engine | ||
|
||
[logger_alembic] | ||
level = INFO | ||
handlers = | ||
qualname = alembic | ||
|
||
[handler_console] | ||
class = StreamHandler | ||
args = (sys.stderr,) | ||
level = NOTSET | ||
formatter = generic | ||
|
||
[formatter_generic] | ||
format = %(levelname)-5.5s [%(name)s] %(message)s | ||
datefmt = %H:%M:%S |
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,3 @@ | ||
from pytest_mock_resources import create_postgres_fixture | ||
|
||
alembic_engine = create_postgres_fixture() |
25 changes: 25 additions & 0 deletions
25
examples/test_downgrade_leaves_no_trace_failure/migrations/env.py
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,25 @@ | ||
from logging.config import fileConfig | ||
|
||
from alembic import context | ||
from sqlalchemy import engine_from_config, pool | ||
|
||
from models import Base | ||
|
||
fileConfig(context.config.config_file_name) | ||
target_metadata = Base.metadata | ||
|
||
|
||
connectable = context.config.attributes.get("connection", None) | ||
|
||
if connectable is None: | ||
connectable = engine_from_config( | ||
context.config.get_section(context.config.config_ini_section), | ||
prefix="sqlalchemy.", | ||
poolclass=pool.NullPool, | ||
) | ||
|
||
with connectable.connect() as connection: | ||
context.configure(connection=connection, target_metadata=target_metadata) | ||
|
||
with context.begin_transaction(): | ||
context.run_migrations() |
24 changes: 24 additions & 0 deletions
24
examples/test_downgrade_leaves_no_trace_failure/migrations/script.py.mako
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,24 @@ | ||
"""${message} | ||
|
||
Revision ID: ${up_revision} | ||
Revises: ${down_revision | comma,n} | ||
Create Date: ${create_date} | ||
|
||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
${imports if imports else ""} | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = ${repr(up_revision)} | ||
down_revision = ${repr(down_revision)} | ||
branch_labels = ${repr(branch_labels)} | ||
depends_on = ${repr(depends_on)} | ||
|
||
|
||
def upgrade(): | ||
${upgrades if upgrades else "pass"} | ||
|
||
|
||
def downgrade(): | ||
${downgrades if downgrades else "pass"} |
19 changes: 19 additions & 0 deletions
19
...les/test_downgrade_leaves_no_trace_failure/migrations/versions/aaaaaaaaaaaa_create_foo.py
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,19 @@ | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
revision = "aaaaaaaaaaaa" | ||
down_revision = None | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
op.create_table( | ||
"foo", | ||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
|
||
|
||
def downgrade(): | ||
op.rename_table('foo', 'bar') |
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,11 @@ | ||
import sqlalchemy | ||
from sqlalchemy import Column, types | ||
from sqlalchemy.ext.declarative import declarative_base | ||
|
||
Base = declarative_base() | ||
|
||
|
||
class CreatedAt(Base): | ||
__tablename__ = "foo" | ||
|
||
id = Column(types.Integer(), autoincrement=True, primary_key=True) |
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,2 @@ | ||
[tool:pytest] | ||
pytest_alembic_include_experimental = downgrade_leaves_no_trace |
36 changes: 36 additions & 0 deletions
36
examples/test_downgrade_leaves_no_trace_success/alembic.ini
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,36 @@ | ||
[alembic] | ||
script_location = migrations | ||
|
||
[loggers] | ||
keys = root,sqlalchemy,alembic | ||
|
||
[handlers] | ||
keys = console | ||
|
||
[formatters] | ||
keys = generic | ||
|
||
[logger_root] | ||
level = WARN | ||
handlers = console | ||
qualname = | ||
|
||
[logger_sqlalchemy] | ||
level = WARN | ||
handlers = | ||
qualname = sqlalchemy.engine | ||
|
||
[logger_alembic] | ||
level = INFO | ||
handlers = | ||
qualname = alembic | ||
|
||
[handler_console] | ||
class = StreamHandler | ||
args = (sys.stderr,) | ||
level = NOTSET | ||
formatter = generic | ||
|
||
[formatter_generic] | ||
format = %(levelname)-5.5s [%(name)s] %(message)s | ||
datefmt = %H:%M:%S |
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,3 @@ | ||
from pytest_mock_resources import create_postgres_fixture | ||
|
||
alembic_engine = create_postgres_fixture() |
25 changes: 25 additions & 0 deletions
25
examples/test_downgrade_leaves_no_trace_success/migrations/env.py
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,25 @@ | ||
from logging.config import fileConfig | ||
|
||
from alembic import context | ||
from sqlalchemy import engine_from_config, pool | ||
|
||
from models import Base | ||
|
||
fileConfig(context.config.config_file_name) | ||
target_metadata = Base.metadata | ||
|
||
|
||
connectable = context.config.attributes.get("connection", None) | ||
|
||
if connectable is None: | ||
connectable = engine_from_config( | ||
context.config.get_section(context.config.config_ini_section), | ||
prefix="sqlalchemy.", | ||
poolclass=pool.NullPool, | ||
) | ||
|
||
with connectable.connect() as connection: | ||
context.configure(connection=connection, target_metadata=target_metadata) | ||
|
||
with context.begin_transaction(): | ||
context.run_migrations() |
24 changes: 24 additions & 0 deletions
24
examples/test_downgrade_leaves_no_trace_success/migrations/script.py.mako
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,24 @@ | ||
"""${message} | ||
|
||
Revision ID: ${up_revision} | ||
Revises: ${down_revision | comma,n} | ||
Create Date: ${create_date} | ||
|
||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
${imports if imports else ""} | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = ${repr(up_revision)} | ||
down_revision = ${repr(down_revision)} | ||
branch_labels = ${repr(branch_labels)} | ||
depends_on = ${repr(depends_on)} | ||
|
||
|
||
def upgrade(): | ||
${upgrades if upgrades else "pass"} | ||
|
||
|
||
def downgrade(): | ||
${downgrades if downgrades else "pass"} |
19 changes: 19 additions & 0 deletions
19
...les/test_downgrade_leaves_no_trace_success/migrations/versions/aaaaaaaaaaaa_create_foo.py
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,19 @@ | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
revision = "aaaaaaaaaaaa" | ||
down_revision = None | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
op.create_table( | ||
"foo", | ||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
|
||
|
||
def downgrade(): | ||
op.drop_table('foo') |
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,11 @@ | ||
import sqlalchemy | ||
from sqlalchemy import Column, types | ||
from sqlalchemy.ext.declarative import declarative_base | ||
|
||
Base = declarative_base() | ||
|
||
|
||
class CreatedAt(Base): | ||
__tablename__ = "foo" | ||
|
||
id = Column(types.Integer(), autoincrement=True, primary_key=True) |
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,2 @@ | ||
[tool:pytest] | ||
pytest_alembic_include_experimental = downgrade_leaves_no_trace |
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
41 changes: 41 additions & 0 deletions
41
src/pytest_alembic/tests/experimental/downgrade_leaves_no_trace.py
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,41 @@ | ||
from typing import List, Optional, Set, Tuple | ||
|
||
from sqlalchemy import MetaData | ||
|
||
from pytest_alembic.plugin.error import AlembicTestFailure | ||
from pytest_alembic.runner import MigrationContext | ||
|
||
try: | ||
from sqlalchemy.ext.declarative import DeclarativeMeta | ||
except ImportError: # pragma: no cover | ||
from sqlalchemy.declarative import DeclarativeMeta | ||
|
||
|
||
def test_downgrade_leaves_no_trace(alembic_runner: MigrationContext, alembic_engine): | ||
"""Assert that all tables defined on your `MetaData`, are imported in the `env.py`. | ||
""" | ||
original_metadata = MetaData() | ||
original_metadata.reflect(alembic_engine) | ||
|
||
alembic_runner.migrate_up_one() | ||
|
||
upgrade_metadata = MetaData() | ||
upgrade_metadata.reflect(alembic_engine) | ||
|
||
alembic_runner.migrate_down_one() | ||
|
||
downgrade_metadata = MetaData() | ||
downgrade_metadata.reflect(alembic_engine) | ||
|
||
# old_tables = {k: v for k, v in old_metadata.tables.items()} | ||
# new_tables = {k: v for k, v in new_metadata.tables.items() if k != 'alembic_version'} | ||
|
||
import pdb; pdb.set_trace() | ||
if new_tables: | ||
tables = ', '.join(new_tables.keys()) | ||
raise AlembicTestFailure( | ||
f"{new_tables}" | ||
) | ||
|
||
raise |
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