Merge pull request #248 from isaquealves/feature/load_ddl_class_per_dialect
feat: Add support for dynamically load DDL classes
This commit is contained in:
		| @@ -5,6 +5,8 @@ | |||||||
| ### 0.6.4 | ### 0.6.4 | ||||||
|  |  | ||||||
| - Improve `inspectdb` adding support to `postgresql::numeric` data type | - Improve `inspectdb` adding support to `postgresql::numeric` data type | ||||||
|  | - Add support for dynamically load DDL classes easing to add support to | ||||||
|  |   new databases without changing `Migrate` class logic | ||||||
|  |  | ||||||
| ### 0.6.3 | ### 0.6.3 | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,3 +1,4 @@ | |||||||
|  | import importlib | ||||||
| import os | import os | ||||||
| from datetime import datetime | from datetime import datetime | ||||||
| from hashlib import md5 | from hashlib import md5 | ||||||
| @@ -63,6 +64,11 @@ class Migrate: | |||||||
|             ret = await connection.execute_query(sql) |             ret = await connection.execute_query(sql) | ||||||
|             cls._db_version = ret[1][0].get("version") |             cls._db_version = ret[1][0].get("version") | ||||||
|  |  | ||||||
|  |     @classmethod | ||||||
|  |     async def load_ddl_class(cls): | ||||||
|  |         ddl_dialect_module = importlib.import_module(f"aerich.ddl.{cls.dialect}") | ||||||
|  |         return getattr(ddl_dialect_module, f"{cls.dialect.capitalize()}DDL") | ||||||
|  |  | ||||||
|     @classmethod |     @classmethod | ||||||
|     async def init(cls, config: dict, app: str, location: str): |     async def init(cls, config: dict, app: str, location: str): | ||||||
|         await Tortoise.init(config=config) |         await Tortoise.init(config=config) | ||||||
| @@ -74,18 +80,8 @@ class Migrate: | |||||||
|  |  | ||||||
|         connection = get_app_connection(config, app) |         connection = get_app_connection(config, app) | ||||||
|         cls.dialect = connection.schema_generator.DIALECT |         cls.dialect = connection.schema_generator.DIALECT | ||||||
|         if cls.dialect == "mysql": |         cls.ddl_class = await cls.load_ddl_class() | ||||||
|             from aerich.ddl.mysql import MysqlDDL |         cls.ddl = cls.ddl_class(connection) | ||||||
|  |  | ||||||
|             cls.ddl = MysqlDDL(connection) |  | ||||||
|         elif cls.dialect == "sqlite": |  | ||||||
|             from aerich.ddl.sqlite import SqliteDDL |  | ||||||
|  |  | ||||||
|             cls.ddl = SqliteDDL(connection) |  | ||||||
|         elif cls.dialect == "postgres": |  | ||||||
|             from aerich.ddl.postgres import PostgresDDL |  | ||||||
|  |  | ||||||
|             cls.ddl = PostgresDDL(connection) |  | ||||||
|         await cls._get_db_version(connection) |         await cls._get_db_version(connection) | ||||||
|  |  | ||||||
|     @classmethod |     @classmethod | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user