| @@ -4,6 +4,12 @@ ChangeLog | |||||||
|  |  | ||||||
| 0.1 | 0.1 | ||||||
| === | === | ||||||
|  | 0.1.9 | ||||||
|  | ----- | ||||||
|  | - Fix default_connection when upgrade | ||||||
|  | - Find default app instead of default. | ||||||
|  | - Diff MySQL ddl. | ||||||
|  | - Check tortoise config. | ||||||
|  |  | ||||||
| 0.1.8 | 0.1.8 | ||||||
| ----- | ----- | ||||||
|   | |||||||
| @@ -1 +1 @@ | |||||||
| __version__ = "0.1.8" | __version__ = "0.1.9" | ||||||
|   | |||||||
| @@ -32,7 +32,7 @@ parser = ConfigParser() | |||||||
| @click.option( | @click.option( | ||||||
|     "-c", "--config", default="aerich.ini", show_default=True, help="Config file.", |     "-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( | @click.option( | ||||||
|     "-n", |     "-n", | ||||||
|     "--name", |     "--name", | ||||||
| @@ -45,7 +45,6 @@ async def cli(ctx: Context, config, app, name): | |||||||
|     ctx.ensure_object(dict) |     ctx.ensure_object(dict) | ||||||
|     ctx.obj["config_file"] = config |     ctx.obj["config_file"] = config | ||||||
|     ctx.obj["name"] = name |     ctx.obj["name"] = name | ||||||
|     ctx.obj["app"] = app |  | ||||||
|  |  | ||||||
|     invoked_subcommand = ctx.invoked_subcommand |     invoked_subcommand = ctx.invoked_subcommand | ||||||
|     if invoked_subcommand != "init": |     if invoked_subcommand != "init": | ||||||
| @@ -57,9 +56,12 @@ async def cli(ctx: Context, config, app, name): | |||||||
|         tortoise_orm = parser[name]["tortoise_orm"] |         tortoise_orm = parser[name]["tortoise_orm"] | ||||||
|  |  | ||||||
|         tortoise_config = get_tortoise_config(ctx, tortoise_orm) |         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["config"] = tortoise_config | ||||||
|         ctx.obj["location"] = location |         ctx.obj["location"] = location | ||||||
|  |         ctx.obj["app"] = app | ||||||
|  |  | ||||||
|         if invoked_subcommand != "init-db": |         if invoked_subcommand != "init-db": | ||||||
|             await Migrate.init_with_old_models(tortoise_config, app, location) |             await Migrate.init_with_old_models(tortoise_config, app, location) | ||||||
|   | |||||||
| @@ -8,17 +8,17 @@ from tortoise.fields import Field, JSONField, TextField, UUIDField | |||||||
| class BaseDDL: | class BaseDDL: | ||||||
|     schema_generator_cls: Type[BaseSchemaGenerator] = BaseSchemaGenerator |     schema_generator_cls: Type[BaseSchemaGenerator] = BaseSchemaGenerator | ||||||
|     DIALECT = "sql" |     DIALECT = "sql" | ||||||
|     _DROP_TABLE_TEMPLATE = "DROP TABLE IF EXISTS {table_name}" |     _DROP_TABLE_TEMPLATE = 'DROP TABLE IF EXISTS "{table_name}"' | ||||||
|     _ADD_COLUMN_TEMPLATE = "ALTER TABLE {table_name} ADD {column}" |     _ADD_COLUMN_TEMPLATE = 'ALTER TABLE "{table_name}" ADD {column}' | ||||||
|     _DROP_COLUMN_TEMPLATE = "ALTER TABLE {table_name} DROP COLUMN {column_name}" |     _DROP_COLUMN_TEMPLATE = 'ALTER TABLE "{table_name}" DROP COLUMN "{column_name}"' | ||||||
|     _ADD_INDEX_TEMPLATE = ( |     _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}" |     _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}" |     _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}" |     _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};" |     _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}" |     _MODIFY_COLUMN_TEMPLATE = 'ALTER TABLE "{table_name}" MODIFY COLUMN {column}' | ||||||
|  |  | ||||||
|     def __init__(self, client: "BaseDBAsyncClient"): |     def __init__(self, client: "BaseDBAsyncClient"): | ||||||
|         self.client = client |         self.client = client | ||||||
|   | |||||||
| @@ -6,3 +6,14 @@ from aerich.ddl import BaseDDL | |||||||
| class MysqlDDL(BaseDDL): | class MysqlDDL(BaseDDL): | ||||||
|     schema_generator_cls = MySQLSchemaGenerator |     schema_generator_cls = MySQLSchemaGenerator | ||||||
|     DIALECT = MySQLSchemaGenerator.DIALECT |     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}" | ||||||
|   | |||||||
| @@ -174,7 +174,10 @@ class Migrate: | |||||||
|         temp_config = deepcopy(config) |         temp_config = deepcopy(config) | ||||||
|         path = os.path.join(location, app, cls.old_models) |         path = os.path.join(location, app, cls.old_models) | ||||||
|         path = path.replace("/", ".").lstrip(".") |         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 |         return temp_config | ||||||
|  |  | ||||||
|     @classmethod |     @classmethod | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| [tool.poetry] | [tool.poetry] | ||||||
| name = "aerich" | name = "aerich" | ||||||
| version = "0.1.8" | version = "0.1.9" | ||||||
| description = "A database migrations tool for Tortoise ORM." | description = "A database migrations tool for Tortoise ORM." | ||||||
| authors = ["long2ice <long2ice@gmail.com>"] | authors = ["long2ice <long2ice@gmail.com>"] | ||||||
|  |  | ||||||
|   | |||||||
| @@ -48,63 +48,76 @@ COMMENT ON COLUMN "category"."user_id" IS 'User';""" | |||||||
|  |  | ||||||
| def test_drop_table(): | def test_drop_table(): | ||||||
|     ret = Migrate.ddl.drop_table(Category) |     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(): | def test_add_column(): | ||||||
|     ret = Migrate.ddl.add_column(Category, Category._meta.fields_map.get("name")) |     ret = Migrate.ddl.add_column(Category, Category._meta.fields_map.get("name")) | ||||||
|     if isinstance(Migrate.ddl, MysqlDDL): |     if isinstance(Migrate.ddl, MysqlDDL): | ||||||
|         assert ret == "ALTER TABLE category ADD `name` VARCHAR(200) NOT NULL" |         assert ret == "ALTER TABLE `category` ADD `name` VARCHAR(200) NOT NULL" | ||||||
|     elif isinstance(Migrate.ddl, PostgresDDL): |     else: | ||||||
|         assert ret == 'ALTER TABLE category ADD "name" VARCHAR(200) NOT NULL' |         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' |  | ||||||
|  |  | ||||||
|  |  | ||||||
| def test_modify_column(): | def test_modify_column(): | ||||||
|     ret = Migrate.ddl.modify_column(Category, Category._meta.fields_map.get("name")) |     ret = Migrate.ddl.modify_column(Category, Category._meta.fields_map.get("name")) | ||||||
|     if isinstance(Migrate.ddl, MysqlDDL): |     if isinstance(Migrate.ddl, MysqlDDL): | ||||||
|         assert ret == "ALTER TABLE category MODIFY COLUMN `name` VARCHAR(200) NOT NULL" |         assert ret == "ALTER TABLE `category` MODIFY COLUMN `name` VARCHAR(200) NOT NULL" | ||||||
|     elif isinstance(Migrate.ddl, PostgresDDL): |     else: | ||||||
|         assert ret == 'ALTER TABLE category MODIFY COLUMN "name" VARCHAR(200) NOT NULL' |         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' |  | ||||||
|  |  | ||||||
|  |  | ||||||
| def test_drop_column(): | def test_drop_column(): | ||||||
|     ret = Migrate.ddl.drop_column(Category, "name") |     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(): | def test_add_index(): | ||||||
|     index = Migrate.ddl.add_index(Category, ["name"]) |     index = Migrate.ddl.add_index(Category, ["name"]) | ||||||
|     index_u = Migrate.ddl.add_index(Category, ["name"], True) |     index_u = Migrate.ddl.add_index(Category, ["name"], True) | ||||||
|     if isinstance(Migrate.ddl, MysqlDDL): |     if isinstance(Migrate.ddl, MysqlDDL): | ||||||
|         assert index == "ALTER TABLE category ADD  INDEX idx_category_name_8b0cb9 (`name`)" |         assert index == "ALTER TABLE `category` ADD  INDEX `idx_category_name_8b0cb9` (`name`)" | ||||||
|  |         assert ( | ||||||
|         assert index_u == "ALTER TABLE category ADD UNIQUE INDEX uid_category_name_8b0cb9 (`name`)" |             index_u == "ALTER TABLE `category` ADD UNIQUE INDEX `uid_category_name_8b0cb9` (`name`)" | ||||||
|  |         ) | ||||||
|     elif isinstance(Migrate.ddl, SqliteDDL): |     else: | ||||||
|         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 ( | ||||||
|         assert index_u == 'ALTER TABLE category ADD UNIQUE INDEX uid_category_name_8b0cb9 ("name")' |             index_u == 'ALTER TABLE "category" ADD UNIQUE INDEX "uid_category_name_8b0cb9" ("name")' | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |  | ||||||
| def test_drop_index(): | def test_drop_index(): | ||||||
|     ret = Migrate.ddl.drop_index(Category, ["name"]) |     ret = Migrate.ddl.drop_index(Category, ["name"]) | ||||||
|     assert ret == "ALTER TABLE category DROP INDEX idx_category_name_8b0cb9" |     if isinstance(Migrate.ddl, MysqlDDL): | ||||||
|     ret = Migrate.ddl.drop_index(Category, ["name"], True) |         assert ret == "ALTER TABLE `category` DROP INDEX `idx_category_name_8b0cb9`" | ||||||
|     assert ret == "ALTER TABLE category DROP INDEX uid_category_name_8b0cb9" |     else: | ||||||
|  |         assert ret == 'ALTER TABLE "category" DROP INDEX "idx_category_name_8b0cb9"' | ||||||
|  |  | ||||||
|  |  | ||||||
| def test_add_fk(): | def test_add_fk(): | ||||||
|     ret = Migrate.ddl.add_fk(Category, Category._meta.fields_map.get("user")) |     ret = Migrate.ddl.add_fk(Category, Category._meta.fields_map.get("user")) | ||||||
|  |     if isinstance(Migrate.ddl, MysqlDDL): | ||||||
|         assert ( |         assert ( | ||||||
|             ret |             ret | ||||||
|         == "ALTER TABLE category ADD CONSTRAINT `fk_category_user_e2e3874c` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE" |             == "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(): | def test_drop_fk(): | ||||||
|     ret = Migrate.ddl.drop_fk(Category, Category._meta.fields_map.get("user")) |     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"' | ||||||
|   | |||||||
| @@ -1,8 +1,6 @@ | |||||||
| from tortoise import Tortoise | from tortoise import Tortoise | ||||||
|  |  | ||||||
| from aerich.ddl.mysql import MysqlDDL | from aerich.ddl.mysql import MysqlDDL | ||||||
| from aerich.ddl.postgres import PostgresDDL |  | ||||||
| from aerich.ddl.sqlite import SqliteDDL |  | ||||||
| from aerich.migrate import Migrate | from aerich.migrate import Migrate | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -14,28 +12,19 @@ def test_migrate(): | |||||||
|     Migrate.diff_models(models, diff_models, False) |     Migrate.diff_models(models, diff_models, False) | ||||||
|     if isinstance(Migrate.ddl, MysqlDDL): |     if isinstance(Migrate.ddl, MysqlDDL): | ||||||
|         assert Migrate.upgrade_operators == [ |         assert Migrate.upgrade_operators == [ | ||||||
|             "ALTER TABLE category ADD `name` VARCHAR(200) NOT NULL", |             "ALTER TABLE `category` ADD `name` VARCHAR(200) NOT NULL", | ||||||
|             "ALTER TABLE user ADD UNIQUE INDEX uid_user_usernam_9987ab (`username`)", |             "ALTER TABLE `user` ADD UNIQUE INDEX `uid_user_usernam_9987ab` (`username`)", | ||||||
|         ] |         ] | ||||||
|         assert Migrate.downgrade_operators == [ |         assert Migrate.downgrade_operators == [ | ||||||
|             "ALTER TABLE category DROP COLUMN name", |             "ALTER TABLE `category` DROP COLUMN `name`", | ||||||
|             "ALTER TABLE user DROP INDEX uid_user_usernam_9987ab", |             "ALTER TABLE `user` DROP INDEX `uid_user_usernam_9987ab`", | ||||||
|         ] |         ] | ||||||
|     elif isinstance(Migrate.ddl, SqliteDDL): |     else: | ||||||
|         assert Migrate.upgrade_operators == [ |         assert Migrate.upgrade_operators == [ | ||||||
|             'ALTER TABLE category ADD "name" VARCHAR(200) NOT NULL', |             'ALTER TABLE "category" ADD "name" VARCHAR(200) NOT NULL', | ||||||
|             'ALTER TABLE user ADD UNIQUE INDEX uid_user_usernam_9987ab ("username")', |             'ALTER TABLE "user" ADD UNIQUE INDEX "uid_user_usernam_9987ab" ("username")', | ||||||
|         ] |         ] | ||||||
|         assert Migrate.downgrade_operators == [ |         assert Migrate.downgrade_operators == [ | ||||||
|             "ALTER TABLE category DROP COLUMN name", |             'ALTER TABLE "category" DROP COLUMN "name"', | ||||||
|             "ALTER TABLE user DROP INDEX uid_user_usernam_9987ab", |             '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", |  | ||||||
|         ] |         ] | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user