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")
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
def test_missed_db(create_command): with cleaner("alembic_env"): try: make_migrations = create_command.make_migrations() make_migrations() except Exception as e: assert "Invalid configuration: Missed required parameter DATABASES" in str( e)
def test_missed_not_migrated_history(create_command): with cleaner("alembic_env"): try: show_history = create_command.show_history() show_history() except Exception as e: assert ( "You can not run show history command before migrations not created" in str(e))
def test_missed_not_migrated(create_command): with cleaner("alembic_env"): try: migrate = create_command.migrate() migrate() except Exception as e: assert ( "You can not run migrate command before migrations not created" in str(e))
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")
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)
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")
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)
async def test_initial_migrations_apps(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, apps=True) create_all = create_command.create_all() create_all() check = await check_table_exists(create_command.default_connection, "customer_b") assert check is True customer = await checking_query() assert customer["bio"] == "Python Core Developer" assert customer["discount"] == 4 drop_all = create_command.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_one/migrations") shutil.rmtree("app_two/migrations") make_migrations() migrate = create_command.migrate() migrate() check = await check_table_exists(create_command.default_connection, "customer_b") assert check is True check = await check_table_exists(create_command.default_connection, "orders") assert check is False customer = await checking_query() assert customer["bio"] == "Python Core Developer" assert customer["discount"] == 4
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", )