Esempio n. 1
0
def test_make_migrations_plugin_with_alembic(config, config_dir,
                                             entry_points: Path,
                                             alembic_config, engine):

    # make sure db is up-to-date
    cli._run_migrations(alembic_config=alembic_config)

    alembic_config.set_main_option(
        "version_locations",
        os.path.join(entry_points, "versions") + " quetz:migrations/versions",
    )

    # initialize a plugin
    cli._make_migrations(
        None,
        message="test revision",
        plugin_name="quetz-plugin",
        initialize=True,
        alembic_config=alembic_config,
    )

    migration_scripts = list(
        (entry_points / "versions").glob("*_test_revision.py"))
    assert migration_scripts

    # apply migrations
    cli._run_migrations(alembic_config=alembic_config)

    # add a new revision

    class TestPluginModel(Base):
        __tablename__ = "test_plugin_table"
        pk = sa.Column(sa.String, primary_key=True)

    cli._make_migrations(
        None,
        message="new revision",
        plugin_name="quetz-plugin",
        alembic_config=alembic_config,
    )

    migration_scripts = list(
        (entry_points / "versions").glob("*_new_revision.py"))
    assert migration_scripts

    with open(migration_scripts[0]) as fid:
        content = fid.read()

    assert "test_plugin_table" in content

    # clean up

    for p in (entry_points / "versions").glob("*.py"):
        os.remove(p)

    Base.metadata.drop_all(engine)
    try:
        engine.execute("DROP TABLE alembic_version")
    except sa.exc.DatabaseError:
        pass
Esempio n. 2
0
def test_run_migrations(sql_connection, engine, database_url, alembic_config,
                        refresh_db):
    db = sql_connection
    with pytest.raises(sa.exc.DatabaseError):
        db.execute("SELECT * FROM users")

    cli._run_migrations(alembic_config=alembic_config)

    db.execute("SELECT * FROM users")
Esempio n. 3
0
def test_multi_head(config, config_dir, entry_points: Path, alembic_config,
                    engine, refresh_db):

    quetz_migrations_path = Path(config_dir) / "migrations"
    quetz_versions_path = quetz_migrations_path / "versions"

    alembic_config.config_file_name = quetz_migrations_path / "alembic.ini"

    os.makedirs(quetz_versions_path)

    plugin_versions_path = Path(entry_points) / "versions"

    with open(quetz_migrations_path / "env.py", "w") as fid:
        fid.write(alembic_env)
    with open(quetz_migrations_path / "script.py.mako", "w") as fid:
        fid.write(script_mako)
    with open(quetz_versions_path / "0000_initial.py", 'w') as fid:
        fid.write(quetz_rev)

    alembic_config.set_main_option(
        "version_locations",
        " ".join(map(str, [plugin_versions_path, quetz_versions_path])),
    )
    alembic_config.set_main_option("script_location",
                                   str(quetz_migrations_path))
    cli._run_migrations(alembic_config=alembic_config)

    # initialize a plugin
    cli._make_migrations(
        None,
        message="test revision",
        plugin_name="quetz-plugin",
        initialize=True,
        alembic_config=alembic_config,
    )
    cli._run_migrations(alembic_config=alembic_config)

    rev_file = next((plugin_versions_path).glob("*test_revision.py"))
    with open(rev_file) as fid:
        content = fid.read()
    assert 'down_revision = None' in content
    assert "depends_on = 'quetz'" in content
    import re

    m = re.search("revision = '(.*)'", content)
    assert m
    plugin_rev_1 = m.groups()[0]

    # second revision quetz
    cli._make_migrations(
        None,
        message="test revision",
        alembic_config=alembic_config,
    )
    cli._run_migrations(alembic_config=alembic_config)

    rev_file = next((quetz_versions_path).glob("*test_revision.py"))
    with open(rev_file) as fid:
        content = fid.read()
    assert "down_revision = '0000'" in content

    # second revision plugin
    cli._make_migrations(
        None,
        message="plugin rev 2",
        plugin_name="quetz-plugin",
        alembic_config=alembic_config,
    )
    rev_file = next((plugin_versions_path).glob("*plugin_rev_2.py"))

    with open(rev_file) as fid:
        content = fid.read()
    assert f"down_revision = '{plugin_rev_1}'" in content

    cli._run_migrations(alembic_config=alembic_config)

    # check heads

    script_directory = ScriptDirectory.from_config(alembic_config)
    heads = script_directory.get_revisions("heads")
    assert len(heads) == 2

    for p in (plugin_versions_path).glob("*.py"):
        os.remove(p)

    try:
        engine.execute("DROP TABLE alembic_version")
    except sa.exc.DatabaseError:
        pass