async def init_with_old_models(cls, config: dict, app: str, location: str): await Tortoise.init(config=config) last_version = await cls.get_last_version() cls.app = app cls.migrate_location = os.path.join(location, app) if last_version: content = last_version.content with open(cls.get_old_model_file(app, location), "w", encoding="utf-8") as f: f.write(content) migrate_config = cls._get_migrate_config(config, app, location) cls.migrate_config = migrate_config await Tortoise.init(config=migrate_config) connection = get_app_connection(config, app) cls.dialect = connection.schema_generator.DIALECT if cls.dialect == "mysql": from aerich.ddl.mysql import MysqlDDL 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)
async def init_db(ctx: Context, safe): config = ctx.obj["config"] location = ctx.obj["location"] app = ctx.obj["app"] dirname = os.path.join(location, app) if not os.path.isdir(dirname): os.mkdir(dirname) click.secho(f"Success create app migrate location {dirname}", fg=Color.green) else: return click.secho(f"Inited {app} already", fg=Color.yellow) await Tortoise.init(config=config) connection = get_app_connection(config, app) await generate_schema_for_client(connection, safe) schema = get_schema_sql(connection, safe) version = await Migrate.generate_version() await Aerich.create(version=version, app=app, content=Migrate.get_models_content( config, app, location)) with open(os.path.join(dirname, version), "w", encoding="utf-8") as f: content = { "upgrade": [schema], } json.dump(content, f, ensure_ascii=False, indent=2) return click.secho(f'Success generate schema for app "{app}"', fg=Color.green)
async def init_db(ctx: Context, safe): config = ctx.obj["config"] location = ctx.obj["location"] app = ctx.obj["app"] dirname = Path(location, app) try: dirname.mkdir(parents=True) click.secho(f"Success create app migrate location {dirname}", fg=Color.green) except FileExistsError: return click.secho( f"Inited {app} already, or delete {dirname} and try again.", fg=Color.yellow) await Tortoise.init(config=config) connection = get_app_connection(config, app) await generate_schema_for_client(connection, safe) schema = get_schema_sql(connection, safe) version = await Migrate.generate_version() await Aerich.create( version=version, app=app, content=get_models_describe(app), ) content = { "upgrade": [schema], } write_version_file(Path(dirname, version), content) click.secho(f'Success generate schema for app "{app}"', fg=Color.green)
async def inspectdb(ctx: Context, table: List[str]): config = ctx.obj["config"] app = ctx.obj["app"] connection = get_app_connection(config, app) inspect = InspectDb(connection, table) await inspect.inspect()
async def inspectdb(self, tables: List[str] = None) -> str: connection = get_app_connection(self.tortoise_config, self.app) dialect = connection.schema_generator.DIALECT if dialect == "mysql": cls = InspectMySQL elif dialect == "postgres": cls = InspectPostgres elif dialect == "sqlite": cls = InspectSQLite else: raise NotImplementedError(f"{dialect} is not supported") inspect = cls(connection, tables) return await inspect.inspect()
async def init_with_old_models(cls, config: dict, app: str, location: str): migrate_config = cls._get_migrate_config(config, app, location) cls.app = app cls.migrate_config = migrate_config cls.migrate_location = os.path.join(location, app) await Tortoise.init(config=migrate_config) connection = get_app_connection(config, app) if connection.schema_generator is MySQLSchemaGenerator: cls.ddl = MysqlDDL(connection) else: raise NotImplementedError("Current only support MySQL")
async def init_db(ctx: Context, safe): config = ctx.obj["config"] location = ctx.obj["location"] app = ctx.obj["app"] dirname = os.path.join(location, app) if not os.path.isdir(dirname): os.mkdir(dirname) click.secho(f"Success create app migrate location {dirname}", fg=Color.green) else: return click.secho(f'Already inited app "{app}"', fg=Color.yellow) Migrate.write_old_models(config, app, location) await Tortoise.init(config=config) connection = get_app_connection(config, app) await generate_schema_for_client(connection, safe) return click.secho(f'Success generate schema for app "{app}"', fg=Color.green)
async def init_with_old_models(cls, config: dict, app: str, location: str): migrate_config = cls._get_migrate_config(config, app, location) cls.app = app cls.migrate_config = migrate_config cls.migrate_location = os.path.join(location, app) await Tortoise.init(config=migrate_config) connection = get_app_connection(config, app) if connection.schema_generator.DIALECT == "mysql": from aerich.ddl.mysql import MysqlDDL cls.ddl = MysqlDDL(connection) elif connection.schema_generator.DIALECT == "sqlite": from aerich.ddl.sqlite import SqliteDDL cls.ddl = SqliteDDL(connection) elif connection.schema_generator.DIALECT == "postgres": from aerich.ddl.postgres import PostgresDDL cls.ddl = PostgresDDL(connection)
async def init_db(self, safe: bool): location = self.location app = self.app dirname = Path(location, app) dirname.mkdir(parents=True) await Tortoise.init(config=self.tortoise_config) connection = get_app_connection(self.tortoise_config, app) await generate_schema_for_client(connection, safe) schema = get_schema_sql(connection, safe) version = await Migrate.generate_version() await Aerich.create( version=version, app=app, content=get_models_describe(app), ) content = { "upgrade": [schema], } write_version_file(Path(dirname, version), content)
async def init(cls, config: dict, app: str, location: str): await Tortoise.init(config=config) last_version = await cls.get_last_version() cls.app = app cls.migrate_location = Path(location, app) if last_version: cls._last_version_content = last_version.content connection = get_app_connection(config, app) cls.dialect = connection.schema_generator.DIALECT if cls.dialect == "mysql": from aerich.ddl.mysql import MysqlDDL 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)