fix transaction
update pypi.yml
This commit is contained in:
parent
559d3707e5
commit
d876c7c84b
9
.github/workflows/pypi.yml
vendored
9
.github/workflows/pypi.yml
vendored
@ -1,9 +1,10 @@
|
||||
name: pypi
|
||||
|
||||
on:
|
||||
push:
|
||||
paths:
|
||||
- 'aerich/__init__.py'
|
||||
- '.github/workflows/pypi.yml'
|
||||
release:
|
||||
types:
|
||||
- created
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
@ -4,9 +4,14 @@ ChangeLog
|
||||
|
||||
0.1
|
||||
===
|
||||
0.1.4
|
||||
-----
|
||||
- Fix transaction.
|
||||
|
||||
0.1.3
|
||||
-----
|
||||
- Support indexes and unique_together.
|
||||
|
||||
0.1.2
|
||||
-----
|
||||
- Now aerich support m2m.
|
||||
|
@ -1 +1 @@
|
||||
__version__ = "0.1.3"
|
||||
__version__ = "0.1.4"
|
||||
|
@ -6,10 +6,11 @@ from enum import Enum
|
||||
|
||||
import asyncclick as click
|
||||
from asyncclick import Context, UsageError
|
||||
from tortoise import Tortoise, generate_schema_for_client, ConfigurationError
|
||||
from tortoise import ConfigurationError, Tortoise, generate_schema_for_client
|
||||
from tortoise.transactions import in_transaction
|
||||
|
||||
from aerich.migrate import Migrate
|
||||
from aerich.utils import get_app_connection, get_tortoise_config
|
||||
from aerich.utils import get_app_connection, get_app_connection_name, get_tortoise_config
|
||||
|
||||
|
||||
class Color(str, Enum):
|
||||
@ -57,7 +58,7 @@ async def cli(ctx: Context, config, app, name):
|
||||
try:
|
||||
await Migrate.init_with_old_models(tortoise_config, app, location)
|
||||
except ConfigurationError:
|
||||
raise UsageError(ctx=ctx, message='You must exec ini-db first')
|
||||
raise UsageError(ctx=ctx, message="You must exec ini-db first")
|
||||
|
||||
|
||||
@cli.command(help="Generate migrate changes file.")
|
||||
@ -80,11 +81,10 @@ async def migrate(ctx: Context, name):
|
||||
async def upgrade(ctx: Context):
|
||||
app = ctx.obj["app"]
|
||||
config = ctx.obj["config"]
|
||||
connection = get_app_connection(config, app)
|
||||
available_versions = Migrate.get_all_version_files(is_all=False)
|
||||
if not available_versions:
|
||||
return click.secho("No migrate items", fg=Color.yellow)
|
||||
async with connection._in_transaction() as conn:
|
||||
async with in_transaction(get_app_connection_name(config, app)) as conn:
|
||||
for file in available_versions:
|
||||
file_path = os.path.join(Migrate.migrate_location, file)
|
||||
with open(file_path, "r") as f:
|
||||
@ -104,12 +104,11 @@ async def upgrade(ctx: Context):
|
||||
async def downgrade(ctx: Context):
|
||||
app = ctx.obj["app"]
|
||||
config = ctx.obj["config"]
|
||||
connection = get_app_connection(config, app)
|
||||
available_versions = Migrate.get_all_version_files()
|
||||
if not available_versions:
|
||||
return click.secho("No migrate items", fg=Color.yellow)
|
||||
|
||||
async with connection._in_transaction() as conn:
|
||||
async with in_transaction(get_app_connection_name(config, app)) as conn:
|
||||
for file in reversed(available_versions):
|
||||
file_path = os.path.join(Migrate.migrate_location, file)
|
||||
with open(file_path, "r") as f:
|
||||
@ -152,7 +151,7 @@ def history(ctx):
|
||||
)
|
||||
@click.pass_context
|
||||
async def init(
|
||||
ctx: Context, tortoise_orm, location,
|
||||
ctx: Context, tortoise_orm, location,
|
||||
):
|
||||
config = ctx.obj["config"]
|
||||
name = ctx.obj["name"]
|
||||
|
@ -137,7 +137,7 @@ class Migrate:
|
||||
|
||||
@classmethod
|
||||
def cp_models(
|
||||
cls, app: str, model_files: List[str], old_model_file,
|
||||
cls, app: str, model_files: List[str], old_model_file,
|
||||
):
|
||||
"""
|
||||
cp currents models to old_model_files
|
||||
@ -187,7 +187,7 @@ class Migrate:
|
||||
|
||||
@classmethod
|
||||
def _diff_models(
|
||||
cls, old_models: Dict[str, Type[Model]], new_models: Dict[str, Type[Model]], upgrade=True
|
||||
cls, old_models: Dict[str, Type[Model]], new_models: Dict[str, Type[Model]], upgrade=True
|
||||
):
|
||||
"""
|
||||
diff models and add operators
|
||||
@ -248,7 +248,9 @@ class Migrate:
|
||||
old_field = old_fields_map.get(new_key)
|
||||
if old_field.index and not new_field.index:
|
||||
cls._add_operator(
|
||||
cls._remove_index(old_model, [old_field.model_field_name], old_field.unique),
|
||||
cls._remove_index(
|
||||
old_model, [old_field.model_field_name], old_field.unique
|
||||
),
|
||||
upgrade,
|
||||
isinstance(old_field, (ForeignKeyFieldInstance, ManyToManyFieldInstance)),
|
||||
)
|
||||
@ -270,33 +272,25 @@ class Migrate:
|
||||
|
||||
for new_index in new_indexes:
|
||||
if new_index not in old_indexes:
|
||||
cls._add_operator(
|
||||
cls._add_index(new_model, new_index, ), upgrade
|
||||
)
|
||||
cls._add_operator(cls._add_index(new_model, new_index,), upgrade)
|
||||
for old_index in old_indexes:
|
||||
if old_index not in new_indexes:
|
||||
cls._add_operator(
|
||||
cls._remove_index(old_model, old_index), upgrade
|
||||
)
|
||||
cls._add_operator(cls._remove_index(old_model, old_index), upgrade)
|
||||
|
||||
for new_unique in new_unique_together:
|
||||
if new_unique not in old_unique_together:
|
||||
cls._add_operator(
|
||||
cls._add_index(new_model, new_unique, unique=True), upgrade
|
||||
)
|
||||
cls._add_operator(cls._add_index(new_model, new_unique, unique=True), upgrade)
|
||||
|
||||
for old_unique in old_unique_together:
|
||||
if old_unique not in new_unique_together:
|
||||
cls._add_operator(
|
||||
cls._remove_index(old_model, old_unique, unique=True), upgrade
|
||||
)
|
||||
cls._add_operator(cls._remove_index(old_model, old_unique, unique=True), upgrade)
|
||||
|
||||
@classmethod
|
||||
def _resolve_fk_fields_name(cls, model: Type[Model], fields_name: List[str]):
|
||||
ret = []
|
||||
for field_name in fields_name:
|
||||
if field_name in model._meta.fk_fields:
|
||||
ret.append(field_name + '_id')
|
||||
ret.append(field_name + "_id")
|
||||
else:
|
||||
ret.append(field_name)
|
||||
return ret
|
||||
|
@ -4,14 +4,24 @@ from asyncclick import BadOptionUsage, Context
|
||||
from tortoise import Tortoise
|
||||
|
||||
|
||||
def get_app_connection(config, app):
|
||||
def get_app_connection_name(config, app):
|
||||
"""
|
||||
get tortoise app
|
||||
get connection name
|
||||
:param config:
|
||||
:param app:
|
||||
:return:
|
||||
"""
|
||||
return Tortoise.get_connection(config.get("apps").get(app).get("default_connection"))
|
||||
return config.get("apps").get(app).get("default_connection")
|
||||
|
||||
|
||||
def get_app_connection(config, app):
|
||||
"""
|
||||
get connection name
|
||||
:param config:
|
||||
:param app:
|
||||
:return:
|
||||
"""
|
||||
return Tortoise.get_connection(get_app_connection_name(config, app))
|
||||
|
||||
|
||||
def get_tortoise_config(ctx: Context, tortoise_orm: str) -> dict:
|
||||
|
Loading…
x
Reference in New Issue
Block a user