Skip to content

Commit 352c113

Browse files
author
Ilya Gurov
authored
test: add a migration nox testing session (#69)
1 parent 37f13a6 commit 352c113

File tree

2 files changed

+163
-1
lines changed

2 files changed

+163
-1
lines changed

noxfile.py

+83-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,51 @@
1818

1919
import nox
2020

21+
ALEMBIC_CONF = """
22+
[alembic]
23+
script_location = test_migration
24+
prepend_sys_path = .
25+
sqlalchemy.url = spanner:///projects/appdev-soda-spanner-staging/instances/sqlalchemy-dialect-test/databases/compliance-test
26+
[post_write_hooks]
27+
[loggers]
28+
keys = root,sqlalchemy,alembic
29+
[handlers]
30+
keys = console
31+
[formatters]
32+
keys = generic
33+
[logger_root]
34+
level = WARN
35+
handlers = console
36+
qualname =
37+
[logger_sqlalchemy]
38+
level = WARN
39+
handlers =
40+
qualname = sqlalchemy.engine
41+
[logger_alembic]
42+
level = INFO
43+
handlers =
44+
qualname = alembic
45+
[handler_console]
46+
class = StreamHandler
47+
args = (sys.stderr,)
48+
level = NOTSET
49+
formatter = generic
50+
[formatter_generic]
51+
format = %(levelname)-5.5s [%(name)s] %(message)s
52+
datefmt = %H:%M:%S
53+
"""
54+
55+
UPGRADE_CODE = """def upgrade():
56+
op.create_table(
57+
'account',
58+
sa.Column('id', sa.Integer, primary_key=True),
59+
sa.Column('name', sa.String(50), nullable=False),
60+
sa.Column('description', sa.Unicode(200)),
61+
)"""
62+
2163

2264
BLACK_VERSION = "black==19.10b0"
2365
BLACK_PATHS = ["google", "test", "noxfile.py", "setup.py"]
24-
2566
DEFAULT_PYTHON_VERSION = "3.8"
2667

2768

@@ -75,3 +116,44 @@ def compliance_test(session):
75116
session.install("-e", ".")
76117
session.run("python", "create_test_database.py")
77118
session.run("pytest", "-v")
119+
120+
121+
@nox.session(python=DEFAULT_PYTHON_VERSION)
122+
def migration_test(session):
123+
"""Migrate with SQLAlchemy and Alembic and check the result."""
124+
import glob
125+
import os
126+
import shutil
127+
128+
session.install("pytest")
129+
session.install("sqlalchemy")
130+
session.install("google-cloud-spanner")
131+
session.install("-e", ".")
132+
session.install("alembic")
133+
session.run("alembic", "init", "test_migration")
134+
135+
# setting testing configurations
136+
os.remove("alembic.ini")
137+
with open("alembic.ini", "w") as f:
138+
f.write(ALEMBIC_CONF)
139+
140+
session.run("alembic", "revision", "-m", "migration_for_test")
141+
files = glob.glob("test_migration/versions/*.py")
142+
143+
# updating the upgrade-script code
144+
with open(files[0], "r") as f:
145+
script_code = f.read()
146+
147+
script_code = script_code.replace("""def upgrade():\n pass""", UPGRADE_CODE)
148+
with open(files[0], "w") as f:
149+
f.write(script_code)
150+
151+
os.remove("test_migration/env.py")
152+
shutil.copyfile("test_migration_env.py", "test_migration/env.py")
153+
154+
# running the test migration
155+
session.run("alembic", "upgrade", "head")
156+
157+
# clearing the migration data
158+
os.remove("alembic.ini")
159+
shutil.rmtree("test_migration")

test_migration_env.py

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
from logging.config import fileConfig
2+
3+
from sqlalchemy import engine_from_config
4+
from sqlalchemy import pool
5+
6+
from alembic import context
7+
from alembic.ddl.impl import DefaultImpl
8+
9+
# this is the Alembic Config object, which provides
10+
# access to the values within the .ini file in use.
11+
config = context.config
12+
13+
# Interpret the config file for Python logging.
14+
# This line sets up loggers basically.
15+
fileConfig(config.config_file_name)
16+
17+
# add your model's MetaData object here
18+
# for 'autogenerate' support
19+
# from myapp import mymodel
20+
# target_metadata = mymodel.Base.metadata
21+
target_metadata = None
22+
23+
# other values from the config, defined by the needs of env.py,
24+
# can be acquired:
25+
# my_important_option = config.get_main_option("my_important_option")
26+
# ... etc.
27+
28+
29+
class SpannerImpl(DefaultImpl):
30+
__dialect__ = "spanner"
31+
32+
33+
def run_migrations_offline():
34+
"""Run migrations in 'offline' mode.
35+
36+
This configures the context with just a URL
37+
and not an Engine, though an Engine is acceptable
38+
here as well. By skipping the Engine creation
39+
we don't even need a DBAPI to be available.
40+
41+
Calls to context.execute() here emit the given string to the
42+
script output.
43+
44+
"""
45+
url = config.get_main_option("sqlalchemy.url")
46+
context.configure(
47+
url=url,
48+
target_metadata=target_metadata,
49+
literal_binds=True,
50+
dialect_opts={"paramstyle": "named"},
51+
)
52+
53+
with context.begin_transaction():
54+
context.run_migrations()
55+
56+
57+
def run_migrations_online():
58+
"""Run migrations in 'online' mode.
59+
60+
In this scenario we need to create an Engine
61+
and associate a connection with the context.
62+
63+
"""
64+
connectable = engine_from_config(
65+
config.get_section(config.config_ini_section),
66+
prefix="sqlalchemy.",
67+
poolclass=pool.NullPool,
68+
)
69+
70+
with connectable.connect() as connection:
71+
context.configure(connection=connection, target_metadata=target_metadata)
72+
73+
with context.begin_transaction():
74+
context.run_migrations()
75+
76+
77+
if context.is_offline_mode():
78+
run_migrations_offline()
79+
else:
80+
run_migrations_online()

0 commit comments

Comments
 (0)