|
18 | 18 |
|
19 | 19 | import nox
|
20 | 20 |
|
| 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 | + |
21 | 63 |
|
22 | 64 | BLACK_VERSION = "black==19.10b0"
|
23 | 65 | BLACK_PATHS = ["google", "test", "noxfile.py", "setup.py"]
|
24 |
| - |
25 | 66 | DEFAULT_PYTHON_VERSION = "3.8"
|
26 | 67 |
|
27 | 68 |
|
@@ -75,3 +116,44 @@ def compliance_test(session):
|
75 | 116 | session.install("-e", ".")
|
76 | 117 | session.run("python", "create_test_database.py")
|
77 | 118 | 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") |
0 commit comments