complete refactoring
This commit is contained in:
		
							
								
								
									
										12
									
								
								CHANGELOG.md
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								CHANGELOG.md
									
									
									
									
									
								
							| @@ -1,5 +1,17 @@ | |||||||
| # ChangeLog | # ChangeLog | ||||||
|  |  | ||||||
|  | ## 0.3 | ||||||
|  |  | ||||||
|  | ### 0.3.0 | ||||||
|  |  | ||||||
|  | - Refactoring migrate logic, and this version is not compatible with previous version. | ||||||
|  | - Now there don't need `old_models.py` and it store in database. | ||||||
|  | - Upgrade steps: | ||||||
|  |   1. Upgrade aerich version. | ||||||
|  |   2. Drop aerich model in db and recreate with new struct. | ||||||
|  |   3. Delete `migrations/{app}` folder and rerun `aerich init-db`. | ||||||
|  |   4. Update model and `aerich migrate` normally. | ||||||
|  |  | ||||||
| ## 0.2 | ## 0.2 | ||||||
|  |  | ||||||
| ### 0.2.5 | ### 0.2.5 | ||||||
|   | |||||||
							
								
								
									
										16
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								README.md
									
									
									
									
									
								
							| @@ -125,7 +125,19 @@ Success upgrade 1_202029051520102929_drop_column.json | |||||||
|  |  | ||||||
| Now your db is migrated to latest. | Now your db is migrated to latest. | ||||||
|  |  | ||||||
| ### Downgrade to previous version | ### Downgrade to specified version | ||||||
|  |  | ||||||
|  | ```shell | ||||||
|  | $ aerich init -h | ||||||
|  |  | ||||||
|  | Usage: aerich downgrade [OPTIONS] | ||||||
|  |  | ||||||
|  |   Downgrade to specified version. | ||||||
|  |  | ||||||
|  | Options: | ||||||
|  |   -v, --version INTEGER  Specified version, default to last.  [default: -1] | ||||||
|  |   -h, --help             Show this message and exit. | ||||||
|  | ``` | ||||||
|  |  | ||||||
| ```shell | ```shell | ||||||
| $ aerich downgrade | $ aerich downgrade | ||||||
| @@ -133,7 +145,7 @@ $ aerich downgrade | |||||||
| Success downgrade 1_202029051520102929_drop_column.json | Success downgrade 1_202029051520102929_drop_column.json | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| Now your db rollback to previous version. | Now your db rollback to specified version. | ||||||
|  |  | ||||||
| ### Show history | ### Show history | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1 +1 @@ | |||||||
| __version__ = "0.2.6" | __version__ = "0.3.0" | ||||||
|   | |||||||
| @@ -79,10 +79,6 @@ async def cli(ctx: Context, config, app, name): | |||||||
| @click.pass_context | @click.pass_context | ||||||
| @coro | @coro | ||||||
| async def migrate(ctx: Context, name): | async def migrate(ctx: Context, name): | ||||||
|     config = ctx.obj["config"] |  | ||||||
|     location = ctx.obj["location"] |  | ||||||
|     app = ctx.obj["app"] |  | ||||||
|  |  | ||||||
|     ret = await Migrate.migrate(name) |     ret = await Migrate.migrate(name) | ||||||
|     if not ret: |     if not ret: | ||||||
|         return click.secho("No changes detected", fg=Color.yellow) |         return click.secho("No changes detected", fg=Color.yellow) | ||||||
| @@ -90,16 +86,9 @@ async def migrate(ctx: Context, name): | |||||||
|  |  | ||||||
|  |  | ||||||
| @cli.command(help="Upgrade to specified version.") | @cli.command(help="Upgrade to specified version.") | ||||||
| @click.option( |  | ||||||
|     "--version", |  | ||||||
|     default=-1, |  | ||||||
|     type=int, |  | ||||||
|     show_default=True, |  | ||||||
|     help="Specified version, default to latest.", |  | ||||||
| ) |  | ||||||
| @click.pass_context | @click.pass_context | ||||||
| @coro | @coro | ||||||
| async def upgrade(ctx: Context, version: int): | async def upgrade(ctx: Context): | ||||||
|     config = ctx.obj["config"] |     config = ctx.obj["config"] | ||||||
|     app = ctx.obj["app"] |     app = ctx.obj["app"] | ||||||
|     location = ctx.obj["location"] |     location = ctx.obj["location"] | ||||||
| @@ -124,15 +113,18 @@ async def upgrade(ctx: Context, version: int): | |||||||
|                 ) |                 ) | ||||||
|             click.secho(f"Success upgrade {version_file}", fg=Color.green) |             click.secho(f"Success upgrade {version_file}", fg=Color.green) | ||||||
|             migrated = True |             migrated = True | ||||||
|         if version != -1 and version_file.startswith(str(version)): |  | ||||||
|             break |  | ||||||
|     if not migrated: |     if not migrated: | ||||||
|         click.secho("No migrate items", fg=Color.yellow) |         click.secho("No migrate items", fg=Color.yellow) | ||||||
|  |  | ||||||
|  |  | ||||||
| @cli.command(help="Downgrade to specified version.") | @cli.command(help="Downgrade to specified version.") | ||||||
| @click.option( | @click.option( | ||||||
|     "--version", default=-1, type=int, show_default=True, help="Specified version, default to last." |     "-v", | ||||||
|  |     "--version", | ||||||
|  |     default=-1, | ||||||
|  |     type=int, | ||||||
|  |     show_default=True, | ||||||
|  |     help="Specified version, default to last.", | ||||||
| ) | ) | ||||||
| @click.pass_context | @click.pass_context | ||||||
| @coro | @coro | ||||||
| @@ -142,10 +134,15 @@ async def downgrade(ctx: Context, version: int): | |||||||
|     if version == -1: |     if version == -1: | ||||||
|         specified_version = await Migrate.get_last_version() |         specified_version = await Migrate.get_last_version() | ||||||
|     else: |     else: | ||||||
|         specified_version = await Aerich.filter(app=app, pk=version + 1).first() |         specified_version = await Aerich.filter(app=app, version__startswith=f"{version}_").first() | ||||||
|     if not specified_version: |     if not specified_version: | ||||||
|         return click.secho("No specified version found", fg=Color.yellow) |         return click.secho("No specified version found", fg=Color.yellow) | ||||||
|     file = specified_version.version |     if version == -1: | ||||||
|  |         versions = [specified_version] | ||||||
|  |     else: | ||||||
|  |         versions = await Aerich.filter(app=app, pk__gte=specified_version.pk) | ||||||
|  |     for version in versions: | ||||||
|  |         file = version.version | ||||||
|         async with in_transaction(get_app_connection_name(config, app)) as conn: |         async with in_transaction(get_app_connection_name(config, app)) as conn: | ||||||
|             file_path = os.path.join(Migrate.migrate_location, file) |             file_path = os.path.join(Migrate.migrate_location, file) | ||||||
|             with open(file_path, "r", encoding="utf-8") as f: |             with open(file_path, "r", encoding="utf-8") as f: | ||||||
| @@ -155,9 +152,9 @@ async def downgrade(ctx: Context, version: int): | |||||||
|                     return click.secho("No downgrade item found", fg=Color.yellow) |                     return click.secho("No downgrade item found", fg=Color.yellow) | ||||||
|                 for downgrade_query in downgrade_query_list: |                 for downgrade_query in downgrade_query_list: | ||||||
|                     await conn.execute_query(downgrade_query) |                     await conn.execute_query(downgrade_query) | ||||||
|             await specified_version.delete() |                 await version.delete() | ||||||
|             os.unlink(file_path) |             os.unlink(file_path) | ||||||
|         return click.secho(f"Success downgrade {file}", fg=Color.green) |             click.secho(f"Success downgrade {file}", fg=Color.green) | ||||||
|  |  | ||||||
|  |  | ||||||
| @cli.command(help="Show current available heads in migrate location.") | @cli.command(help="Show current available heads in migrate location.") | ||||||
|   | |||||||
| @@ -13,7 +13,7 @@ class SqliteDDL(BaseDDL): | |||||||
|     DIALECT = SqliteSchemaGenerator.DIALECT |     DIALECT = SqliteSchemaGenerator.DIALECT | ||||||
|  |  | ||||||
|     def drop_column(self, model: "Type[Model]", column_name: str): |     def drop_column(self, model: "Type[Model]", column_name: str): | ||||||
|         raise NotSupportError("Drop column is not support in SQLite.") |         raise NotSupportError("Drop column is unsupported in SQLite.") | ||||||
|  |  | ||||||
|     def modify_column(self, model: "Type[Model]", field_object: Field): |     def modify_column(self, model: "Type[Model]", field_object: Field): | ||||||
|         raise NotSupportError("Modify column is not support in SQLite.") |         raise NotSupportError("Modify column is unsupported in SQLite.") | ||||||
|   | |||||||
| @@ -2,9 +2,3 @@ class NotSupportError(Exception): | |||||||
|     """ |     """ | ||||||
|     raise when features not support |     raise when features not support | ||||||
|     """ |     """ | ||||||
|  |  | ||||||
|  |  | ||||||
| class DuplicationError(Exception): |  | ||||||
|     """ |  | ||||||
|      raise when something duplication |  | ||||||
|      """ |  | ||||||
|   | |||||||
| @@ -115,6 +115,10 @@ class Migrate: | |||||||
|     @classmethod |     @classmethod | ||||||
|     async def _generate_diff_sql(cls, name): |     async def _generate_diff_sql(cls, name): | ||||||
|         version = await cls.generate_version(name) |         version = await cls.generate_version(name) | ||||||
|  |         # delete if same version exists | ||||||
|  |         for version_file in cls.get_all_version_files(): | ||||||
|  |             if version_file.startswith(version.split("_")[0]): | ||||||
|  |                 os.unlink(os.path.join(cls.migrate_location, version_file)) | ||||||
|         content = { |         content = { | ||||||
|             "upgrade": cls.upgrade_operators, |             "upgrade": cls.upgrade_operators, | ||||||
|             "downgrade": cls.downgrade_operators, |             "downgrade": cls.downgrade_operators, | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| [tool.poetry] | [tool.poetry] | ||||||
| name = "aerich" | name = "aerich" | ||||||
| version = "0.2.6" | version = "0.3.0" | ||||||
| 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>"] | ||||||
| license = "Apache-2.0" | license = "Apache-2.0" | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user