Beispiel #1
0
def test_initial_migrations_history(create_command, capsys):
    with cleaner("alembic_env"):
        make_migrations = create_command.make_migrations()
        make_migrations()
        config = create_command.config
        assert config.get_main_option("crax_migrated") == "not migrated"
        migrate = Commands(["app_one.conf", True, {}]).migrate()
        migrate()
        migration_assertions(config)

        show_history = Commands(["app_one.conf", True, {}]).show_history()
        show_history()
        config = create_command.config
        versions = config.get_main_option("crax_latest_revisions")
        versions = json.loads(versions)
        captured = capsys.readouterr()

        captured = captured.out
        for x in versions.values():
            assert x in captured

        show_history = Commands(["app_one.conf", True, {
            "latest": True
        }]).show_history()
        show_history()
        for x in versions.values():
            assert x in captured
Beispiel #2
0
async def test_initial_migrations_and_env_deleted(create_command):
    with cleaner("alembic_env"):
        make_migrations = create_command.make_migrations()
        make_migrations()
        config = create_command.config
        migration_assertions(config)
        migrate = Commands(["app_one.conf", True, {}]).migrate()
        migrate()

        check = await check_table_exists(create_command.default_connection,
                                         "orders")
        assert check is True
        drop_all = Commands(["app_one.conf", True, {}]).drop_all()
        drop_all()
        shutil.rmtree("alembic_env")
        shutil.rmtree("app_one/migrations")
        shutil.rmtree("app_two/migrations")
        shutil.rmtree("app_three/migrations")
        if os.path.exists("../crax/auth/migrations"):
            shutil.rmtree("../crax/auth/migrations")
        assert not os.path.exists("app_one/migrations")
        assert not os.path.exists("app_two/migrations")
        assert not os.path.exists("app_three/migrations")
        make_migrations = Commands(["app_one.conf", True,
                                    {}]).make_migrations()
        make_migrations()
        assert os.path.exists("app_one/migrations")
        assert os.path.exists("app_two/migrations")
        assert os.path.exists("app_three/migrations")
Beispiel #3
0
async def test_initial_migrations(create_command):
    make_migrations = create_command.make_migrations()
    make_migrations()
    migrate = Commands(["app_one.conf", False, {}]).migrate()
    migrate()
    values = {
        "username": "******",
        "password": "******",
        "bio": "Nil!",
        "first_name": "Chris",
        "age": 27,
    }
    await CustomerB.query.insert(values=values)
    await CustomerDiscount.query.insert(
        values={
            "name": "Customer Discount",
            "percent": 10,
            "start_date": datetime.datetime.now(),
        })
    config = create_command.config
    versions = config.get_main_option("crax_latest_revisions")
    versions = json.loads(versions)
    version = [
        x for x in os.listdir("app_two/migrations")
        if x != "__pycache__" and versions["default/app_two"] not in x
    ][0][:-4]

    migrate = Commands(
        ["app_one.conf", False, {
            "down": True,
            "revision": version
        }]).migrate()
    migrate()
    try:
        await CustomerDiscount.query.insert(
            values={
                "name": "Customer Discount",
                "percent": 10,
                "start_date": datetime.datetime.now(),
            })
    except Exception as e:
        assert "start_date" in str(e)
    replacement(
        "app_two/models.py",
        "start_date = sa.Column(sa.DateTime(), nullable=True)",
        "# start_date = sa.Column(sa.DateTime(), nullable=True)",
    )
    replacement(
        "app_one/models.py",
        "age = sa.Column(sa.Integer(), nullable=True)",
        "# age = sa.Column(sa.Integer(), nullable=True)",
    )
Beispiel #4
0
async def test_initial_migrations_deleted(create_command):
    with cleaner("alembic_env"):
        make_migrations = create_command.make_migrations()
        make_migrations()
        config = create_command.config
        migration_assertions(config)
        migrate = Commands(["app_one.conf", True, {}]).migrate()
        migrate()

        check = await check_table_exists(create_command.default_connection,
                                         "orders")
        assert check is True

        connection = create_command.default_connection
        if "mysql+pymysql" in connection:
            connection = connection.replace("mysql+pymysql", "mysql")
        database = Database(connection)
        if "sqlite" not in connection:
            query = ("DROP TABLE IF EXISTS orders, customer_a, customer_b,"
                     " vendor, customer_discount, vendor_discount;")
            await database.connect()
            await database.execute(query=query)
            await database.disconnect()
        else:
            async with database.transaction():
                await database.execute(query="DROP TABLE IF EXISTS orders;")
                await database.execute(
                    query="DROP TABLE IF EXISTS customer_a; ")
                await database.execute(
                    query="DROP TABLE IF EXISTS customer_b; ")
                await database.execute(query="DROP TABLE IF EXISTS vendor; ")
                await database.execute(
                    query="DROP TABLE IF EXISTS customer_discount; ")
                await database.execute(
                    query="DROP TABLE IF EXISTS vendor_discount; ")

        shutil.rmtree("app_one/migrations")
        shutil.rmtree("app_two/migrations")
        shutil.rmtree("app_three/migrations")
        if os.path.exists("../crax/auth/migrations"):
            shutil.rmtree("../crax/auth/migrations")
        assert not os.path.exists("app_one/migrations")
        assert not os.path.exists("app_two/migrations")
        assert not os.path.exists("app_three/migrations")
        make_migrations = Commands(["app_one.conf", True,
                                    {}]).make_migrations()
        make_migrations()
        assert os.path.exists("app_one/migrations")
        assert os.path.exists("app_two/migrations")
        assert os.path.exists("app_three/migrations")
Beispiel #5
0
async def test_initial_migrations_downgrade(create_command):
    with cleaner("alembic_env"):
        make_migrations = create_command.make_migrations()
        make_migrations()
        config = create_command.config
        assert config.get_main_option("crax_migrated") == "not migrated"

        migration_assertions(config)

        migrate = Commands(["app_one.conf", True, {}]).migrate()
        migrate()

        check = await check_table_exists(create_command.default_connection,
                                         "orders")
        assert check is True
        config = create_command.config
        versions = config.get_main_option("crax_latest_revisions")
        versions = json.loads(versions)
        migrate = Commands(["app_one.conf", True, {"down": True}]).migrate()
        migrate()
        check = await check_table_exists(create_command.default_connection,
                                         "orders")
        assert check is False

        migrate = Commands(
            ["app_one.conf", True, {
                "revision": versions["default/app_two"]
            }]).migrate()
        migrate()
        check = await check_table_exists(create_command.default_connection,
                                         "customer_discount")
        assert check is True
        check = await check_table_exists(create_command.default_connection,
                                         "orders")
        assert check is False
        try:
            migrate = Commands([
                "app_one.conf", True, {
                    "down": True,
                    "revision": "wrong_revision"
                }
            ]).migrate()
            migrate()
        except Exception as e:
            assert "Can't locate revision identified by 'wrong_revision'" in str(
                e)
Beispiel #6
0
def test_initial_migrations_sql(create_command):
    with cleaner("alembic_env"):
        make_migrations = create_command.make_migrations()
        make_migrations()
        config = create_command.config
        assert config.get_main_option("crax_migrated") == "not migrated"
        migrate = Commands(["app_one.conf", True, {"sql": True}]).migrate()
        migrate()
        assert os.path.exists("app_one/migrations/sql")
        assert os.path.exists("app_two/migrations/sql")
        assert os.path.exists("app_three/migrations/sql")
Beispiel #7
0
async def test_double_migrations(create_command, capsys):
    with cleaner("alembic_env"):
        make_migrations = create_command.make_migrations()
        make_migrations()
        try:
            make_migrations = Commands(["app_one.conf", True,
                                        {}]).make_migrations()
            make_migrations()
        except:
            captured = capsys.readouterr()
            assert ("You have unapplied migrations. "
                    "Please run migrate command first" in captured.err)
Beispiel #8
0
async def test_migrations_multi(create_command):
    make_migrations = create_command.make_migrations()
    make_migrations()
    migrate = Commands([
        "app_four.conf",
        False,
        {
            "database": "custom",
            "revision": "custom/app_four@head",
            "sql": True
        },
    ]).migrate()
    migrate()
    assert os.path.exists("app_four/migrations/custom/sql")
    migrate = Commands([
        "app_four.conf",
        False,
        {
            "database": "custom",
            "revision": "custom/app_four@head"
        },
    ]).migrate()
    migrate()
    connection = create_command.default_connection
    if "mysql+pymysql" in connection:
        connection = connection.replace("mysql+pymysql", "mysql")
    database = Database(connection)
    query = ("INSERT INTO customer_b("
             "username, password, bio, first_name, age)"
             " VALUES (:username, :password, :bio, :first_name, :age)")
    await database.connect()
    values = {
        "username": "******",
        "password": "******",
        "bio": "Nil!",
        "first_name": "Chris",
        "age": 27,
    }
    await database.execute(query=query, values=values)
    config = create_command.config
    versions = config.get_main_option("crax_latest_revisions")
    versions = json.loads(versions)
    migrate = Commands([
        "app_four.conf",
        False,
        {
            "down": True,
            "revision": versions["custom/app_six"],
            "database": "custom",
        },
    ]).migrate()
    migrate()
    try:
        await CustomerDiscount.query.insert(
            values={
                "name": "Customer Discount",
                "percent": 10,
                "start_date": datetime.datetime.now(),
            })
    except Exception as e:
        assert "start_date" in str(e)
    replacement(
        "app_six/models.py",
        "start_date = sa.Column(sa.DateTime(), nullable=True)",
        "# start_date = sa.Column(sa.DateTime(), nullable=True)",
    )
    replacement(
        "app_four/models.py",
        "age = sa.Column(sa.Integer(), nullable=True)",
        "# age = sa.Column(sa.Integer(), nullable=True)",
    )

    if os.path.exists("app_one/migrations"):
        shutil.rmtree("app_one/migrations")
    if os.path.exists("app_two/migrations"):
        shutil.rmtree("app_two/migrations")
    if os.path.exists("app_three/migrations"):
        shutil.rmtree("app_three/migrations")
    if os.path.exists("app_four/migrations"):
        shutil.rmtree("app_four/migrations")
    if os.path.exists("app_five/migrations"):
        shutil.rmtree("app_five/migrations")
    if os.path.exists("app_six/migrations"):
        shutil.rmtree("app_six/migrations")
Beispiel #9
0
def create_command(request):
    return Commands(request.param)
Beispiel #10
0
async def test_initial_migrations_custom_db(create_command):
    replacement("app_two/models.py", "# database = 'custom'",
                "database = 'custom'")
    replacement("app_three/models.py", "# database = 'custom'",
                "database = 'custom'")
    replacement(
        "app_three/models.py",
        "from tests.app_one.models import CustomerB, Vendor",
        "from tests.app_five.models import CustomerB, Vendor",
    )
    with cleaner("alembic_env"):
        make_migrations = create_command.make_migrations()
        make_migrations()
        config = Config("alembic.ini")
        db_map = json.loads(config.get_main_option("crax_db_map"))["custom"]
        assert ["app_five", "app_two", "app_three"] == list(db_map)
        assert config.get_main_option("crax_migrated") == "not migrated"
        assert os.path.exists("alembic_env")
        assert os.path.isfile("alembic.ini")
        assert os.path.exists("app_three/migrations/custom")
        assert os.path.exists("app_five/migrations/custom")
        assert os.path.exists("app_two/migrations/custom")

        create_all = Commands(["app_five.conf", True, {
            "database": "custom"
        }]).create_all()
        create_all()
        check = await check_table_exists(create_command.default_connection,
                                         "customer_b")
        assert check is not False
        drop_all = Commands(["app_five.conf", True, {
            "database": "custom"
        }]).drop_all()
        drop_all()
        check = await check_table_exists(create_command.default_connection,
                                         "customer_b")
        assert check is False
        if os.path.exists("alembic_env"):
            shutil.rmtree("alembic_env")
            os.remove("alembic.ini")
        shutil.rmtree("app_five/migrations")
        shutil.rmtree("app_two/migrations")
        shutil.rmtree("app_three/migrations")
        make_migrations()
        assert os.path.exists("alembic_env")
        assert os.path.isfile("alembic.ini")
        assert os.path.exists("app_five/migrations/custom")
        assert os.path.exists("app_two/migrations/custom")
        assert os.path.exists("app_three/migrations/custom")
        migrate = Commands(["app_five.conf", True, {
            "database": "custom"
        }]).migrate()
        migrate()
        check = await check_table_exists(create_command.default_connection,
                                         "customer_b")
        assert check is True
        replacement("app_two/models.py", "database = 'custom'",
                    "# database = 'custom'")
        replacement("app_three/models.py", "database = 'custom'",
                    "# database = 'custom'")
        replacement(
            "app_three/models.py",
            "from tests.app_five.models import CustomerB, Vendor",
            "from tests.app_one.models import CustomerB, Vendor",
        )