From 881f70f74881133c236b234a7bf52825f47f2a86 Mon Sep 17 00:00:00 2001 From: long2ice Date: Mon, 25 May 2020 23:44:42 +0800 Subject: [PATCH 1/4] Fix default_connection when upgrade --- CHANGELOG.rst | 3 +++ aerich/migrate.py | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index fc0c877..f01ad17 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,6 +4,9 @@ ChangeLog 0.1 === +0.1.9 +----- +- Fix default_connection when upgrade 0.1.8 ----- diff --git a/aerich/migrate.py b/aerich/migrate.py index b8365de..9f6fddd 100644 --- a/aerich/migrate.py +++ b/aerich/migrate.py @@ -174,7 +174,10 @@ def _get_migrate_config(cls, config: dict, app: str, location: str): temp_config = deepcopy(config) path = os.path.join(location, app, cls.old_models) path = path.replace("/", ".").lstrip(".") - temp_config["apps"][cls.diff_app] = {"models": [path]} + temp_config["apps"][cls.diff_app] = { + "models": [path], + "default_connection": config.get("apps").get(app).get("default_connection","default"), + } return temp_config @classmethod From ef3e0c11d5f7ffd33e9839329fa1d0361c07725d Mon Sep 17 00:00:00 2001 From: long2ice Date: Mon, 25 May 2020 23:46:35 +0800 Subject: [PATCH 2/4] update version --- aerich/__init__.py | 2 +- pyproject.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/aerich/__init__.py b/aerich/__init__.py index 9cb17e7..c11f861 100644 --- a/aerich/__init__.py +++ b/aerich/__init__.py @@ -1 +1 @@ -__version__ = "0.1.8" +__version__ = "0.1.9" diff --git a/pyproject.toml b/pyproject.toml index ec12ae9..ee491b1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "aerich" -version = "0.1.8" +version = "0.1.9" description = "A database migrations tool for Tortoise ORM." authors = ["long2ice "] From c09c878eaf69703ab13ccb0b07bac9592e01bac8 Mon Sep 17 00:00:00 2001 From: long2ice Date: Tue, 26 May 2020 10:22:02 +0800 Subject: [PATCH 3/4] add support modify column diff mysql ddl --- CHANGELOG.rst | 2 ++ aerich/cli.py | 6 ++-- aerich/ddl/__init__.py | 18 +++++----- aerich/ddl/mysql/__init__.py | 11 ++++++ aerich/migrate.py | 2 +- tests/test_ddl.py | 69 +++++++++++++++++++++--------------- tests/test_migrate.py | 29 +++++---------- 7 files changed, 76 insertions(+), 61 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index f01ad17..8bb539d 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -7,6 +7,8 @@ ChangeLog 0.1.9 ----- - Fix default_connection when upgrade +- Find default app instead of default. +- Diff MySQL ddl. 0.1.8 ----- diff --git a/aerich/cli.py b/aerich/cli.py index 339566f..8d9a349 100644 --- a/aerich/cli.py +++ b/aerich/cli.py @@ -32,7 +32,7 @@ class Color(str, Enum): @click.option( "-c", "--config", default="aerich.ini", show_default=True, help="Config file.", ) -@click.option("--app", default="models", show_default=True, help="Tortoise-ORM app name.") +@click.option("--app", required=False, help="Tortoise-ORM app name.") @click.option( "-n", "--name", @@ -45,7 +45,6 @@ async def cli(ctx: Context, config, app, name): ctx.ensure_object(dict) ctx.obj["config_file"] = config ctx.obj["name"] = name - ctx.obj["app"] = app invoked_subcommand = ctx.invoked_subcommand if invoked_subcommand != "init": @@ -57,9 +56,10 @@ async def cli(ctx: Context, config, app, name): tortoise_orm = parser[name]["tortoise_orm"] tortoise_config = get_tortoise_config(ctx, tortoise_orm) - + app = app or list(tortoise_config.get("apps").keys())[0] ctx.obj["config"] = tortoise_config ctx.obj["location"] = location + ctx.obj["app"] = app if invoked_subcommand != "init-db": await Migrate.init_with_old_models(tortoise_config, app, location) diff --git a/aerich/ddl/__init__.py b/aerich/ddl/__init__.py index f803f8f..09c898d 100644 --- a/aerich/ddl/__init__.py +++ b/aerich/ddl/__init__.py @@ -8,17 +8,17 @@ class BaseDDL: schema_generator_cls: Type[BaseSchemaGenerator] = BaseSchemaGenerator DIALECT = "sql" - _DROP_TABLE_TEMPLATE = "DROP TABLE IF EXISTS {table_name}" - _ADD_COLUMN_TEMPLATE = "ALTER TABLE {table_name} ADD {column}" - _DROP_COLUMN_TEMPLATE = "ALTER TABLE {table_name} DROP COLUMN {column_name}" + _DROP_TABLE_TEMPLATE = 'DROP TABLE IF EXISTS "{table_name}"' + _ADD_COLUMN_TEMPLATE = 'ALTER TABLE "{table_name}" ADD {column}' + _DROP_COLUMN_TEMPLATE = 'ALTER TABLE "{table_name}" DROP COLUMN "{column_name}"' _ADD_INDEX_TEMPLATE = ( - "ALTER TABLE {table_name} ADD {unique} INDEX {index_name} ({column_names})" + 'ALTER TABLE "{table_name}" ADD {unique} INDEX "{index_name}" ({column_names})' ) - _DROP_INDEX_TEMPLATE = "ALTER TABLE {table_name} DROP INDEX {index_name}" - _ADD_FK_TEMPLATE = "ALTER TABLE {table_name} ADD CONSTRAINT `{fk_name}` FOREIGN KEY (`{db_column}`) REFERENCES `{table}` (`{field}`) ON DELETE {on_delete}" - _DROP_FK_TEMPLATE = "ALTER TABLE {table_name} DROP FOREIGN KEY {fk_name}" - _M2M_TABLE_TEMPLATE = "CREATE TABLE {table_name} ({backward_key} {backward_type} NOT NULL REFERENCES {backward_table} ({backward_field}) ON DELETE CASCADE,{forward_key} {forward_type} NOT NULL REFERENCES {forward_table} ({forward_field}) ON DELETE CASCADE){extra}{comment};" - _MODIFY_COLUMN_TEMPLATE = "ALTER TABLE {table_name} MODIFY COLUMN {column}" + _DROP_INDEX_TEMPLATE = 'ALTER TABLE "{table_name}" DROP INDEX "{index_name}"' + _ADD_FK_TEMPLATE = 'ALTER TABLE "{table_name}" ADD CONSTRAINT "{fk_name}" FOREIGN KEY ("{db_column}") REFERENCES "{table}" ("{field}") ON DELETE {on_delete}' + _DROP_FK_TEMPLATE = 'ALTER TABLE "{table_name}" DROP FOREIGN KEY "{fk_name}"' + _M2M_TABLE_TEMPLATE = 'CREATE TABLE "{table_name}" ("{backward_key}" {backward_type} NOT NULL REFERENCES "{backward_table}" ("{backward_field}") ON DELETE CASCADE,"{forward_key}" {forward_type} NOT NULL REFERENCES "{forward_table}" ("{forward_field}") ON DELETE CASCADE){extra}{comment};' + _MODIFY_COLUMN_TEMPLATE = 'ALTER TABLE "{table_name}" MODIFY COLUMN {column}' def __init__(self, client: "BaseDBAsyncClient"): self.client = client diff --git a/aerich/ddl/mysql/__init__.py b/aerich/ddl/mysql/__init__.py index 311da7c..710583d 100644 --- a/aerich/ddl/mysql/__init__.py +++ b/aerich/ddl/mysql/__init__.py @@ -6,3 +6,14 @@ class MysqlDDL(BaseDDL): schema_generator_cls = MySQLSchemaGenerator DIALECT = MySQLSchemaGenerator.DIALECT + _DROP_TABLE_TEMPLATE = "DROP TABLE IF EXISTS `{table_name}`" + _ADD_COLUMN_TEMPLATE = "ALTER TABLE `{table_name}` ADD {column}" + _DROP_COLUMN_TEMPLATE = "ALTER TABLE `{table_name}` DROP COLUMN `{column_name}`" + _ADD_INDEX_TEMPLATE = ( + "ALTER TABLE `{table_name}` ADD {unique} INDEX `{index_name}` ({column_names})" + ) + _DROP_INDEX_TEMPLATE = "ALTER TABLE `{table_name}` DROP INDEX `{index_name}`" + _ADD_FK_TEMPLATE = "ALTER TABLE `{table_name}` ADD CONSTRAINT `{fk_name}` FOREIGN KEY (`{db_column}`) REFERENCES `{table}` (`{field}`) ON DELETE {on_delete}" + _DROP_FK_TEMPLATE = "ALTER TABLE `{table_name}` DROP FOREIGN KEY `{fk_name}`" + _M2M_TABLE_TEMPLATE = "CREATE TABLE `{table_name}` (`{backward_key}` {backward_type} NOT NULL REFERENCES `{backward_table}` (`{backward_field}`) ON DELETE CASCADE,`{forward_key}` {forward_type} NOT NULL REFERENCES `{forward_table}` (`{forward_field}`) ON DELETE CASCADE){extra}{comment};" + _MODIFY_COLUMN_TEMPLATE = "ALTER TABLE `{table_name}` MODIFY COLUMN {column}" diff --git a/aerich/migrate.py b/aerich/migrate.py index 9f6fddd..891217e 100644 --- a/aerich/migrate.py +++ b/aerich/migrate.py @@ -176,7 +176,7 @@ def _get_migrate_config(cls, config: dict, app: str, location: str): path = path.replace("/", ".").lstrip(".") temp_config["apps"][cls.diff_app] = { "models": [path], - "default_connection": config.get("apps").get(app).get("default_connection","default"), + "default_connection": config.get("apps").get(app).get("default_connection", "default"), } return temp_config diff --git a/tests/test_ddl.py b/tests/test_ddl.py index 5cbfc31..bbf6cb8 100644 --- a/tests/test_ddl.py +++ b/tests/test_ddl.py @@ -48,63 +48,76 @@ def test_create_table(): def test_drop_table(): ret = Migrate.ddl.drop_table(Category) - assert ret == "DROP TABLE IF EXISTS category" + if isinstance(Migrate.ddl, MysqlDDL): + assert ret == "DROP TABLE IF EXISTS `category`" + else: + assert ret == 'DROP TABLE IF EXISTS "category"' def test_add_column(): ret = Migrate.ddl.add_column(Category, Category._meta.fields_map.get("name")) if isinstance(Migrate.ddl, MysqlDDL): - assert ret == "ALTER TABLE category ADD `name` VARCHAR(200) NOT NULL" - elif isinstance(Migrate.ddl, PostgresDDL): - assert ret == 'ALTER TABLE category ADD "name" VARCHAR(200) NOT NULL' - elif isinstance(Migrate.ddl, SqliteDDL): - assert ret == 'ALTER TABLE category ADD "name" VARCHAR(200) NOT NULL' + assert ret == "ALTER TABLE `category` ADD `name` VARCHAR(200) NOT NULL" + else: + assert ret == 'ALTER TABLE "category" ADD "name" VARCHAR(200) NOT NULL' def test_modify_column(): ret = Migrate.ddl.modify_column(Category, Category._meta.fields_map.get("name")) if isinstance(Migrate.ddl, MysqlDDL): - assert ret == "ALTER TABLE category MODIFY COLUMN `name` VARCHAR(200) NOT NULL" - elif isinstance(Migrate.ddl, PostgresDDL): - assert ret == 'ALTER TABLE category MODIFY COLUMN "name" VARCHAR(200) NOT NULL' - elif isinstance(Migrate.ddl, SqliteDDL): - assert ret == 'ALTER TABLE category MODIFY COLUMN "name" VARCHAR(200) NOT NULL' + assert ret == "ALTER TABLE `category` MODIFY COLUMN `name` VARCHAR(200) NOT NULL" + else: + assert ret == 'ALTER TABLE "category" MODIFY COLUMN "name" VARCHAR(200) NOT NULL' def test_drop_column(): ret = Migrate.ddl.drop_column(Category, "name") - assert ret == "ALTER TABLE category DROP COLUMN name" + if isinstance(Migrate.ddl, MysqlDDL): + assert ret == "ALTER TABLE `category` DROP COLUMN `name`" + else: + assert ret == 'ALTER TABLE "category" DROP COLUMN "name"' def test_add_index(): index = Migrate.ddl.add_index(Category, ["name"]) index_u = Migrate.ddl.add_index(Category, ["name"], True) if isinstance(Migrate.ddl, MysqlDDL): - assert index == "ALTER TABLE category ADD INDEX idx_category_name_8b0cb9 (`name`)" - - assert index_u == "ALTER TABLE category ADD UNIQUE INDEX uid_category_name_8b0cb9 (`name`)" - - elif isinstance(Migrate.ddl, SqliteDDL): - assert index_u == 'ALTER TABLE category ADD UNIQUE INDEX uid_category_name_8b0cb9 ("name")' - - assert index_u == 'ALTER TABLE category ADD UNIQUE INDEX uid_category_name_8b0cb9 ("name")' + assert index == "ALTER TABLE `category` ADD INDEX `idx_category_name_8b0cb9` (`name`)" + assert ( + index_u == "ALTER TABLE `category` ADD UNIQUE INDEX `uid_category_name_8b0cb9` (`name`)" + ) + else: + assert index == 'ALTER TABLE "category" ADD INDEX "idx_category_name_8b0cb9" ("name")' + assert ( + index_u == 'ALTER TABLE "category" ADD UNIQUE INDEX "uid_category_name_8b0cb9" ("name")' + ) def test_drop_index(): ret = Migrate.ddl.drop_index(Category, ["name"]) - assert ret == "ALTER TABLE category DROP INDEX idx_category_name_8b0cb9" - ret = Migrate.ddl.drop_index(Category, ["name"], True) - assert ret == "ALTER TABLE category DROP INDEX uid_category_name_8b0cb9" + if isinstance(Migrate.ddl, MysqlDDL): + assert ret == "ALTER TABLE `category` DROP INDEX `idx_category_name_8b0cb9`" + else: + assert ret == 'ALTER TABLE "category" DROP INDEX "idx_category_name_8b0cb9"' def test_add_fk(): ret = Migrate.ddl.add_fk(Category, Category._meta.fields_map.get("user")) - assert ( - ret - == "ALTER TABLE category ADD CONSTRAINT `fk_category_user_e2e3874c` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE" - ) + if isinstance(Migrate.ddl, MysqlDDL): + assert ( + ret + == "ALTER TABLE `category` ADD CONSTRAINT `fk_category_user_e2e3874c` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE" + ) + else: + assert ( + ret + == 'ALTER TABLE "category" ADD CONSTRAINT "fk_category_user_e2e3874c" FOREIGN KEY ("user_id") REFERENCES "user" ("id") ON DELETE CASCADE' + ) def test_drop_fk(): ret = Migrate.ddl.drop_fk(Category, Category._meta.fields_map.get("user")) - assert ret == "ALTER TABLE category DROP FOREIGN KEY fk_category_user_e2e3874c" + if isinstance(Migrate.ddl, MysqlDDL): + assert ret == "ALTER TABLE `category` DROP FOREIGN KEY `fk_category_user_e2e3874c`" + else: + assert ret == 'ALTER TABLE "category" DROP FOREIGN KEY "fk_category_user_e2e3874c"' diff --git a/tests/test_migrate.py b/tests/test_migrate.py index 55baaea..e1ed4d6 100644 --- a/tests/test_migrate.py +++ b/tests/test_migrate.py @@ -1,8 +1,6 @@ from tortoise import Tortoise from aerich.ddl.mysql import MysqlDDL -from aerich.ddl.postgres import PostgresDDL -from aerich.ddl.sqlite import SqliteDDL from aerich.migrate import Migrate @@ -14,28 +12,19 @@ def test_migrate(): Migrate.diff_models(models, diff_models, False) if isinstance(Migrate.ddl, MysqlDDL): assert Migrate.upgrade_operators == [ - "ALTER TABLE category ADD `name` VARCHAR(200) NOT NULL", - "ALTER TABLE user ADD UNIQUE INDEX uid_user_usernam_9987ab (`username`)", + "ALTER TABLE `category` ADD `name` VARCHAR(200) NOT NULL", + "ALTER TABLE `user` ADD UNIQUE INDEX `uid_user_usernam_9987ab` (`username`)", ] assert Migrate.downgrade_operators == [ - "ALTER TABLE category DROP COLUMN name", - "ALTER TABLE user DROP INDEX uid_user_usernam_9987ab", + "ALTER TABLE `category` DROP COLUMN `name`", + "ALTER TABLE `user` DROP INDEX `uid_user_usernam_9987ab`", ] - elif isinstance(Migrate.ddl, SqliteDDL): + else: assert Migrate.upgrade_operators == [ - 'ALTER TABLE category ADD "name" VARCHAR(200) NOT NULL', - 'ALTER TABLE user ADD UNIQUE INDEX uid_user_usernam_9987ab ("username")', + 'ALTER TABLE "category" ADD "name" VARCHAR(200) NOT NULL', + 'ALTER TABLE "user" ADD UNIQUE INDEX "uid_user_usernam_9987ab" ("username")', ] assert Migrate.downgrade_operators == [ - "ALTER TABLE category DROP COLUMN name", - "ALTER TABLE user DROP INDEX uid_user_usernam_9987ab", - ] - elif isinstance(Migrate.ddl, PostgresDDL): - assert Migrate.upgrade_operators == [ - 'ALTER TABLE category ADD "name" VARCHAR(200) NOT NULL', - 'ALTER TABLE user ADD UNIQUE INDEX uid_user_usernam_9987ab ("username")', - ] - assert Migrate.downgrade_operators == [ - "ALTER TABLE category DROP COLUMN name", - "ALTER TABLE user DROP INDEX uid_user_usernam_9987ab", + 'ALTER TABLE "category" DROP COLUMN "name"', + 'ALTER TABLE "user" DROP INDEX "uid_user_usernam_9987ab"', ] From 125389461fdcda4c2f36c9fe965222481a42ef0a Mon Sep 17 00:00:00 2001 From: long2ice Date: Tue, 26 May 2020 14:44:55 +0800 Subject: [PATCH 4/4] check tortoise add aerich.models --- CHANGELOG.rst | 1 + aerich/cli.py | 2 ++ 2 files changed, 3 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 8bb539d..bb26271 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -9,6 +9,7 @@ ChangeLog - Fix default_connection when upgrade - Find default app instead of default. - Diff MySQL ddl. +- Check tortoise config. 0.1.8 ----- diff --git a/aerich/cli.py b/aerich/cli.py index 8d9a349..af4e19d 100644 --- a/aerich/cli.py +++ b/aerich/cli.py @@ -57,6 +57,8 @@ async def cli(ctx: Context, config, app, name): tortoise_config = get_tortoise_config(ctx, tortoise_orm) app = app or list(tortoise_config.get("apps").keys())[0] + if "aerich.models" not in tortoise_config.get("apps").get(app).get("models"): + raise UsageError("Check your tortoise config and add aerich.models to it.", ctx=ctx) ctx.obj["config"] = tortoise_config ctx.obj["location"] = location ctx.obj["app"] = app