Compare commits
52 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
180420843d | ||
|
58f66b91cf | ||
|
064d7ff675 | ||
|
2da794d823 | ||
|
77005f3793 | ||
|
5a873b8b69 | ||
|
3989b7c674 | ||
|
694b05356f | ||
|
919d56c936 | ||
|
7bcf9b2fed | ||
|
9f663299cf | ||
|
28dbdf2663 | ||
|
e71a4b60a5 | ||
|
62840136be | ||
|
185514f711 | ||
|
8e783e031e | ||
|
10b7272ca8 | ||
|
0c763c6024 | ||
|
c6371a5c16 | ||
|
1dbf9185b6 | ||
|
9bf2de0b9a | ||
|
bf1cf21324 | ||
|
8b08329493 | ||
|
5bc7d23d95 | ||
|
a253aa96cb | ||
|
15a6e874dd | ||
|
19a5dcbf3f | ||
|
922e3eef16 | ||
|
44fd2fe6ae | ||
|
b147859960 | ||
|
793cf2532c | ||
|
fa85e05d1d | ||
|
3f52ac348b | ||
|
f8aa7a8f34 | ||
|
44d520cc82 | ||
|
364735f804 | ||
|
505d361597 | ||
|
a19edd3a35 | ||
|
84d1f78019 | ||
|
8fb07a6c9e | ||
|
54da8b22af | ||
|
4c0308ff22 | ||
|
38c4a15661 | ||
|
52151270e0 | ||
|
49897dc4fd | ||
|
d4ad0e270f | ||
|
e74fc304a5 | ||
|
14d20455e6 | ||
|
bd9ecfd6e1 | ||
|
de8500b9a1 | ||
|
90b47c5af7 | ||
|
02fe5a9d31 |
1
.github/FUNDING.yml
vendored
Normal file
1
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1 @@
|
||||
custom: ["https://sponsor.long2ice.cn"]
|
@@ -1,7 +1,13 @@
|
||||
name: test
|
||||
on: [ push, pull_request ]
|
||||
name: ci
|
||||
on:
|
||||
push:
|
||||
branches-ignore:
|
||||
- master
|
||||
pull_request:
|
||||
branches-ignore:
|
||||
- master
|
||||
jobs:
|
||||
testall:
|
||||
ci:
|
||||
runs-on: ubuntu-latest
|
||||
services:
|
||||
postgres:
|
1
.gitignore
vendored
1
.gitignore
vendored
@@ -146,3 +146,4 @@ aerich.ini
|
||||
src
|
||||
.vscode
|
||||
.DS_Store
|
||||
.python-version
|
30
CHANGELOG.md
30
CHANGELOG.md
@@ -2,6 +2,36 @@
|
||||
|
||||
## 0.5
|
||||
|
||||
### 0.5.5
|
||||
|
||||
- Fix KeyError: 'src_folder' after upgrading aerich to 0.5.4. (#176)
|
||||
- Fix MySQL 5.X rename column.
|
||||
- Fix `db_constraint` when fk changed. (#179)
|
||||
|
||||
### 0.5.4
|
||||
|
||||
- Fix incorrect index creation order. (#151)
|
||||
- Not catch exception when import config. (#164)
|
||||
- Support `drop column` for sqlite. (#40)
|
||||
|
||||
### 0.5.3
|
||||
|
||||
- Fix postgre alter null. (#142)
|
||||
- Fix default function when migrate. (#147)
|
||||
|
||||
### 0.5.2
|
||||
|
||||
- Fix rename field on the field add. (#134)
|
||||
- Fix postgres field type change error. (#135)
|
||||
- Fix inspectdb for `FloatField`. (#138)
|
||||
- Support `rename table`. (#139)
|
||||
|
||||
### 0.5.1
|
||||
|
||||
- Fix tortoise connections not being closed properly. (#120)
|
||||
- Fix bug for field change. (#119)
|
||||
- Fix drop model in the downgrade. (#132)
|
||||
|
||||
### 0.5.0
|
||||
|
||||
- Refactor core code, now has no limitation for everything.
|
||||
|
14
Makefile
14
Makefile
@@ -8,23 +8,11 @@ POSTGRES_HOST ?= "127.0.0.1"
|
||||
POSTGRES_PORT ?= 5432
|
||||
POSTGRES_PASS ?= "123456"
|
||||
|
||||
help:
|
||||
@echo "Aerich development makefile"
|
||||
@echo
|
||||
@echo "usage: make <target>"
|
||||
@echo "Targets:"
|
||||
@echo " up Updates dev/test dependencies"
|
||||
@echo " deps Ensure dev/test dependencies are installed"
|
||||
@echo " check Checks that build is sane"
|
||||
@echo " lint Reports all linter violations"
|
||||
@echo " test Runs all tests"
|
||||
@echo " style Auto-formats the code"
|
||||
|
||||
up:
|
||||
@poetry update
|
||||
|
||||
deps:
|
||||
@poetry install -E dbdrivers
|
||||
@poetry install -E asyncpg -E asyncmy -E aiomysql
|
||||
|
||||
style: deps
|
||||
isort -src $(checkfiles)
|
||||
|
68
README.md
68
README.md
@@ -1,18 +1,14 @@
|
||||
# Aerich
|
||||
|
||||
[](https://pypi.python.org/pypi/aerich)
|
||||
[](https://github.com/long2ice/aerich)
|
||||
[](https://github.com/long2ice/aerich/actions?query=workflow:pypi)
|
||||
[](https://github.com/long2ice/aerich/actions?query=workflow:test)
|
||||
[](https://github.com/tortoise/aerich)
|
||||
[](https://github.com/tortoise/aerich/actions?query=workflow:pypi)
|
||||
[](https://github.com/tortoise/aerich/actions?query=workflow:ci)
|
||||
|
||||
## Introduction
|
||||
|
||||
Aerich is a database migrations tool for Tortoise-ORM, which like alembic for SQLAlchemy, or Django ORM with it\'s own
|
||||
migrations solution.
|
||||
|
||||
~~**Important: You can only use absolutely import in your `models.py` to make `aerich` work.**~~
|
||||
|
||||
From version `v0.5.0`, there is no such limitation now.
|
||||
Aerich is a database migrations tool for Tortoise-ORM, which is like alembic for SQLAlchemy, or like Django ORM with
|
||||
it\'s own migration solution.
|
||||
|
||||
## Install
|
||||
|
||||
@@ -30,10 +26,12 @@ Just install from pypi:
|
||||
Usage: aerich [OPTIONS] COMMAND [ARGS]...
|
||||
|
||||
Options:
|
||||
-V, --version Show the version and exit.
|
||||
-c, --config TEXT Config file. [default: aerich.ini]
|
||||
--app TEXT Tortoise-ORM app name. [default: models]
|
||||
--app TEXT Tortoise-ORM app name.
|
||||
-n, --name TEXT Name of section in .ini file to use for aerich config.
|
||||
[default: aerich]
|
||||
|
||||
-h, --help Show this message and exit.
|
||||
|
||||
Commands:
|
||||
@@ -44,12 +42,12 @@ Commands:
|
||||
init-db Generate schema and generate app migrate location.
|
||||
inspectdb Introspects the database tables to standard output as...
|
||||
migrate Generate migrate changes file.
|
||||
upgrade Upgrade to latest version.
|
||||
upgrade Upgrade to specified version.
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
You need add `aerich.models` to your `Tortoise-ORM` config first, example:
|
||||
You need add `aerich.models` to your `Tortoise-ORM` config first. Example:
|
||||
|
||||
```python
|
||||
TORTOISE_ORM = {
|
||||
@@ -72,14 +70,16 @@ Usage: aerich init [OPTIONS]
|
||||
|
||||
Init config file and generate root migrate location.
|
||||
|
||||
Options:
|
||||
-t, --tortoise-orm TEXT Tortoise-ORM config module dict variable, like settings.TORTOISE_ORM.
|
||||
[required]
|
||||
OOptions:
|
||||
-t, --tortoise-orm TEXT Tortoise-ORM config module dict variable, like
|
||||
settings.TORTOISE_ORM. [required]
|
||||
|
||||
--location TEXT Migrate store location. [default: ./migrations]
|
||||
-s, --src_folder TEXT Folder of the source, relative to the project root.
|
||||
-h, --help Show this message and exit.
|
||||
```
|
||||
|
||||
Init config file and location:
|
||||
Initialize the config file and migrations location:
|
||||
|
||||
```shell
|
||||
> aerich init -t tests.backends.mysql.TORTOISE_ORM
|
||||
@@ -97,8 +97,8 @@ Success create app migrate location ./migrations/models
|
||||
Success generate schema for app "models"
|
||||
```
|
||||
|
||||
If your Tortoise-ORM app is not default `models`, you must specify
|
||||
`--app` like `aerich --app other_models init-db`.
|
||||
If your Tortoise-ORM app is not the default `models`, you must specify the correct app via `--app`,
|
||||
e.g. `aerich --app other_models init-db`.
|
||||
|
||||
### Update models and make migrate
|
||||
|
||||
@@ -111,9 +111,9 @@ Success migrate 1_202029051520102929_drop_column.sql
|
||||
Format of migrate filename is
|
||||
`{version_num}_{datetime}_{name|update}.sql`.
|
||||
|
||||
And if `aerich` guess you are renaming a column, it will ask `Rename {old_column} to {new_column} [True]`, you can
|
||||
choice `True` to rename column without column drop, or choice `False` to drop column then create, note that the after
|
||||
maybe lose data.
|
||||
If `aerich` guesses you are renaming a column, it will ask `Rename {old_column} to {new_column} [True]`. You can choose
|
||||
`True` to rename column without column drop, or choose `False` to drop the column then create. Note that the latter may
|
||||
lose data.
|
||||
|
||||
### Upgrade to latest version
|
||||
|
||||
@@ -128,7 +128,7 @@ Now your db is migrated to latest.
|
||||
### Downgrade to specified version
|
||||
|
||||
```shell
|
||||
> aerich init -h
|
||||
> aerich downgrade -h
|
||||
|
||||
Usage: aerich downgrade [OPTIONS]
|
||||
|
||||
@@ -149,7 +149,7 @@ Options:
|
||||
Success downgrade 1_202029051520102929_drop_column.sql
|
||||
```
|
||||
|
||||
Now your db rollback to specified version.
|
||||
Now your db is rolled back to the specified version.
|
||||
|
||||
### Show history
|
||||
|
||||
@@ -169,7 +169,7 @@ Now your db rollback to specified version.
|
||||
|
||||
### Inspect db tables to TortoiseORM model
|
||||
|
||||
Currently, only support MySQL.
|
||||
Currently `inspectdb` only supports MySQL.
|
||||
|
||||
```shell
|
||||
Usage: aerich inspectdb [OPTIONS]
|
||||
@@ -187,14 +187,13 @@ Inspect all tables and print to console:
|
||||
aerich --app models inspectdb
|
||||
```
|
||||
|
||||
Inspect a specified table in default app and redirect to `models.py`:
|
||||
Inspect a specified table in the default app and redirect to `models.py`:
|
||||
|
||||
```shell
|
||||
aerich inspectdb -t user > models.py
|
||||
```
|
||||
|
||||
Note that this command is restricted, which is not supported in some solutions, such as `IntEnumField`
|
||||
and `ForeignKeyField` and so on.
|
||||
Note that this command is limited and cannot infer some fields, such as `IntEnumField`, `ForeignKeyField`, and others.
|
||||
|
||||
### Multiple databases
|
||||
|
||||
@@ -211,13 +210,18 @@ tortoise_orm = {
|
||||
}
|
||||
```
|
||||
|
||||
You need only specify `aerich.models` in one app, and must specify `--app` when run `aerich migrate` and so on.
|
||||
You only need to specify `aerich.models` in one app, and must specify `--app` when running `aerich migrate` and so on.
|
||||
|
||||
## Support this project
|
||||
## Restore `aerich` workflow
|
||||
|
||||
| AliPay | WeChatPay | PayPal |
|
||||
| -------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- | ---------------------------------------------------------------- |
|
||||
| <img width="200" src="https://github.com/long2ice/aerich/raw/dev/images/alipay.jpeg"/> | <img width="200" src="https://github.com/long2ice/aerich/raw/dev/images/wechatpay.jpeg"/> | [PayPal](https://www.paypal.me/long2ice) to my account long2ice. |
|
||||
In some cases, such as broken changes from upgrade of `aerich`, you can't run `aerich migrate` or `aerich upgrade`, you
|
||||
can make the following steps:
|
||||
|
||||
1. drop `aerich` table.
|
||||
2. delete `migrations/{app}` directory.
|
||||
3. rerun `aerich init-db`.
|
||||
|
||||
Note that these actions is safe, also you can do that to reset your migrations if your migration files is too many.
|
||||
|
||||
## License
|
||||
|
||||
|
@@ -1 +1 @@
|
||||
__version__ = "0.5.0"
|
||||
__version__ = "0.5.5"
|
||||
|
@@ -1,6 +1,5 @@
|
||||
import asyncio
|
||||
import os
|
||||
import sys
|
||||
from configparser import ConfigParser
|
||||
from functools import wraps
|
||||
from pathlib import Path
|
||||
@@ -16,6 +15,7 @@ from tortoise.utils import get_schema_sql
|
||||
from aerich.inspectdb import InspectDb
|
||||
from aerich.migrate import Migrate
|
||||
from aerich.utils import (
|
||||
add_src_path,
|
||||
get_app_connection,
|
||||
get_app_connection_name,
|
||||
get_models_describe,
|
||||
@@ -30,12 +30,22 @@ from .models import Aerich
|
||||
|
||||
parser = ConfigParser()
|
||||
|
||||
CONFIG_DEFAULT_VALUES = {
|
||||
"src_folder": ".",
|
||||
}
|
||||
|
||||
|
||||
def coro(f):
|
||||
@wraps(f)
|
||||
def wrapper(*args, **kwargs):
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.run_until_complete(f(*args, **kwargs))
|
||||
|
||||
# Close db connections at the end of all all but the cli group function
|
||||
try:
|
||||
loop.run_until_complete(f(*args, **kwargs))
|
||||
finally:
|
||||
if f.__name__ != "cli":
|
||||
loop.run_until_complete(Tortoise.close_connections())
|
||||
|
||||
return wrapper
|
||||
|
||||
@@ -43,11 +53,7 @@ def coro(f):
|
||||
@click.group(context_settings={"help_option_names": ["-h", "--help"]})
|
||||
@click.version_option(__version__, "-V", "--version")
|
||||
@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", required=False, help="Tortoise-ORM app name.")
|
||||
@click.option(
|
||||
@@ -72,6 +78,10 @@ async def cli(ctx: Context, config, app, name):
|
||||
|
||||
location = parser[name]["location"]
|
||||
tortoise_orm = parser[name]["tortoise_orm"]
|
||||
src_folder = parser[name].get("src_folder", CONFIG_DEFAULT_VALUES["src_folder"])
|
||||
|
||||
# Add specified source folder to path
|
||||
add_src_path(src_folder)
|
||||
|
||||
tortoise_config = get_tortoise_config(ctx, tortoise_orm)
|
||||
app = app or list(tortoise_config.get("apps").keys())[0]
|
||||
@@ -116,9 +126,7 @@ async def upgrade(ctx: Context):
|
||||
for upgrade_query in upgrade_query_list:
|
||||
await conn.execute_script(upgrade_query)
|
||||
await Aerich.create(
|
||||
version=version_file,
|
||||
app=app,
|
||||
content=get_models_describe(app),
|
||||
version=version_file, app=app, content=get_models_describe(app),
|
||||
)
|
||||
click.secho(f"Success upgrade {version_file}", fg=Color.green)
|
||||
migrated = True
|
||||
@@ -212,26 +220,37 @@ async def history(ctx: Context):
|
||||
help="Tortoise-ORM config module dict variable, like settings.TORTOISE_ORM.",
|
||||
)
|
||||
@click.option(
|
||||
"--location",
|
||||
default="./migrations",
|
||||
show_default=True,
|
||||
help="Migrate store location.",
|
||||
"--location", default="./migrations", show_default=True, help="Migrate store location.",
|
||||
)
|
||||
@click.option(
|
||||
"-s",
|
||||
"--src_folder",
|
||||
default=CONFIG_DEFAULT_VALUES["src_folder"],
|
||||
show_default=False,
|
||||
help="Folder of the source, relative to the project root.",
|
||||
)
|
||||
@click.pass_context
|
||||
@coro
|
||||
async def init(
|
||||
ctx: Context,
|
||||
tortoise_orm,
|
||||
location,
|
||||
):
|
||||
async def init(ctx: Context, tortoise_orm, location, src_folder):
|
||||
config_file = ctx.obj["config_file"]
|
||||
name = ctx.obj["name"]
|
||||
if Path(config_file).exists():
|
||||
return click.secho("You have inited", fg=Color.yellow)
|
||||
return click.secho("Configuration file already created", fg=Color.yellow)
|
||||
|
||||
if os.path.isabs(src_folder):
|
||||
src_folder = os.path.relpath(os.getcwd(), src_folder)
|
||||
# Add ./ so it's clear that this is relative path
|
||||
if not src_folder.startswith("./"):
|
||||
src_folder = "./" + src_folder
|
||||
|
||||
# check that we can find the configuration, if not we can fail before the config file gets created
|
||||
add_src_path(src_folder)
|
||||
get_tortoise_config(ctx, tortoise_orm)
|
||||
|
||||
parser.add_section(name)
|
||||
parser.set(name, "tortoise_orm", tortoise_orm)
|
||||
parser.set(name, "location", location)
|
||||
parser.set(name, "src_folder", src_folder)
|
||||
|
||||
with open(config_file, "w", encoding="utf-8") as f:
|
||||
parser.write(f)
|
||||
@@ -274,9 +293,7 @@ async def init_db(ctx: Context, safe):
|
||||
|
||||
version = await Migrate.generate_version()
|
||||
await Aerich.create(
|
||||
version=version,
|
||||
app=app,
|
||||
content=get_models_describe(app),
|
||||
version=version, app=app, content=get_models_describe(app),
|
||||
)
|
||||
content = {
|
||||
"upgrade": [schema],
|
||||
@@ -287,11 +304,7 @@ async def init_db(ctx: Context, safe):
|
||||
|
||||
@cli.command(help="Introspects the database tables to standard output as TortoiseORM model.")
|
||||
@click.option(
|
||||
"-t",
|
||||
"--table",
|
||||
help="Which tables to inspect.",
|
||||
multiple=True,
|
||||
required=False,
|
||||
"-t", "--table", help="Which tables to inspect.", multiple=True, required=False,
|
||||
)
|
||||
@click.pass_context
|
||||
@coro
|
||||
@@ -305,7 +318,6 @@ async def inspectdb(ctx: Context, table: List[str]):
|
||||
|
||||
|
||||
def main():
|
||||
sys.path.insert(0, ".")
|
||||
cli()
|
||||
|
||||
|
||||
|
@@ -4,6 +4,8 @@ from typing import List, Type
|
||||
from tortoise import BaseDBAsyncClient, Model
|
||||
from tortoise.backends.base.schema_generator import BaseSchemaGenerator
|
||||
|
||||
from aerich.utils import is_default_function
|
||||
|
||||
|
||||
class BaseDDL:
|
||||
schema_generator_cls: Type[BaseSchemaGenerator] = BaseSchemaGenerator
|
||||
@@ -26,6 +28,7 @@ class BaseDDL:
|
||||
_CHANGE_COLUMN_TEMPLATE = (
|
||||
'ALTER TABLE "{table_name}" CHANGE {old_column_name} {new_column_name} {new_column_type}'
|
||||
)
|
||||
_RENAME_TABLE_TEMPLATE = 'ALTER TABLE "{old_table_name}" RENAME TO "{new_table_name}"'
|
||||
|
||||
def __init__(self, client: "BaseDBAsyncClient"):
|
||||
self.client = client
|
||||
@@ -75,7 +78,11 @@ class BaseDDL:
|
||||
auto_now_add = field_describe.get("auto_now_add", False)
|
||||
auto_now = field_describe.get("auto_now", False)
|
||||
if default is not None or auto_now_add:
|
||||
if field_describe.get("field_type") in ["UUIDField", "TextField", "JSONField"]:
|
||||
if field_describe.get("field_type") in [
|
||||
"UUIDField",
|
||||
"TextField",
|
||||
"JSONField",
|
||||
] or is_default_function(default):
|
||||
default = ""
|
||||
else:
|
||||
try:
|
||||
@@ -108,9 +115,7 @@ class BaseDDL:
|
||||
nullable="NOT NULL" if not field_describe.get("nullable") else "",
|
||||
unique="UNIQUE" if field_describe.get("unique") else "",
|
||||
comment=self.schema_generator._column_comment_generator(
|
||||
table=db_table,
|
||||
column=db_column,
|
||||
comment=field_describe.get("description"),
|
||||
table=db_table, column=db_column, comment=field_describe.get("description"),
|
||||
)
|
||||
if description
|
||||
else "",
|
||||
@@ -226,7 +231,13 @@ class BaseDDL:
|
||||
)
|
||||
|
||||
def alter_column_null(self, model: "Type[Model]", field_describe: dict):
|
||||
raise NotImplementedError
|
||||
return self.modify_column(model, field_describe)
|
||||
|
||||
def set_comment(self, model: "Type[Model]", field_describe: dict):
|
||||
raise NotImplementedError
|
||||
return self.modify_column(model, field_describe)
|
||||
|
||||
def rename_table(self, model: "Type[Model]", old_table_name: str, new_table_name: str):
|
||||
db_table = model._meta.db_table
|
||||
return self._RENAME_TABLE_TEMPLATE.format(
|
||||
table_name=db_table, old_table_name=old_table_name, new_table_name=new_table_name
|
||||
)
|
||||
|
@@ -1,10 +1,6 @@
|
||||
from typing import Type
|
||||
|
||||
from tortoise import Model
|
||||
from tortoise.backends.mysql.schema_generator import MySQLSchemaGenerator
|
||||
|
||||
from aerich.ddl import BaseDDL
|
||||
from aerich.exceptions import NotSupportError
|
||||
|
||||
|
||||
class MysqlDDL(BaseDDL):
|
||||
@@ -28,9 +24,4 @@ class MysqlDDL(BaseDDL):
|
||||
_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}"
|
||||
|
||||
def alter_column_null(self, model: "Type[Model]", field_describe: dict):
|
||||
raise NotSupportError("Alter column null is unsupported in MySQL.")
|
||||
|
||||
def set_comment(self, model: "Type[Model]", field_describe: dict):
|
||||
raise NotSupportError("Alter column comment is unsupported in MySQL.")
|
||||
_RENAME_TABLE_TEMPLATE = "ALTER TABLE `{old_table_name}` RENAME TO `{new_table_name}`"
|
||||
|
@@ -12,7 +12,9 @@ class PostgresDDL(BaseDDL):
|
||||
_ADD_INDEX_TEMPLATE = 'CREATE {unique}INDEX "{index_name}" ON "{table_name}" ({column_names})'
|
||||
_DROP_INDEX_TEMPLATE = 'DROP INDEX "{index_name}"'
|
||||
_ALTER_NULL_TEMPLATE = 'ALTER TABLE "{table_name}" ALTER COLUMN "{column}" {set_drop} NOT NULL'
|
||||
_MODIFY_COLUMN_TEMPLATE = 'ALTER TABLE "{table_name}" ALTER COLUMN "{column}" TYPE {datatype}'
|
||||
_MODIFY_COLUMN_TEMPLATE = (
|
||||
'ALTER TABLE "{table_name}" ALTER COLUMN "{column}" TYPE {datatype}{using}'
|
||||
)
|
||||
_SET_COMMENT_TEMPLATE = 'COMMENT ON COLUMN "{table_name}"."{column}" IS {comment}'
|
||||
_DROP_FK_TEMPLATE = 'ALTER TABLE "{table_name}" DROP CONSTRAINT "{fk_name}"'
|
||||
|
||||
@@ -27,10 +29,13 @@ class PostgresDDL(BaseDDL):
|
||||
def modify_column(self, model: "Type[Model]", field_describe: dict, is_pk: bool = False):
|
||||
db_table = model._meta.db_table
|
||||
db_field_types = field_describe.get("db_field_types")
|
||||
db_column = field_describe.get("db_column")
|
||||
datatype = db_field_types.get(self.DIALECT) or db_field_types.get("")
|
||||
return self._MODIFY_COLUMN_TEMPLATE.format(
|
||||
table_name=db_table,
|
||||
column=field_describe.get("db_column"),
|
||||
datatype=db_field_types.get(self.DIALECT) or db_field_types.get(""),
|
||||
column=db_column,
|
||||
datatype=datatype,
|
||||
using=f' USING "{db_column}"::{datatype}',
|
||||
)
|
||||
|
||||
def set_comment(self, model: "Type[Model]", field_describe: dict):
|
||||
|
@@ -11,9 +11,6 @@ class SqliteDDL(BaseDDL):
|
||||
schema_generator_cls = SqliteSchemaGenerator
|
||||
DIALECT = SqliteSchemaGenerator.DIALECT
|
||||
|
||||
def drop_column(self, model: "Type[Model]", column_name: str):
|
||||
raise NotSupportError("Drop column is unsupported in SQLite.")
|
||||
|
||||
def modify_column(self, model: "Type[Model]", field_object: dict, is_pk: bool = True):
|
||||
raise NotSupportError("Modify column is unsupported in SQLite.")
|
||||
|
||||
|
@@ -15,6 +15,7 @@ class InspectDb:
|
||||
"LONGTEXT": " {field} = fields.TextField({null}{default}{comment})",
|
||||
"TEXT": " {field} = fields.TextField({null}{default}{comment})",
|
||||
"DATETIME": " {field} = fields.DatetimeField({null}{default}{comment})",
|
||||
"FLOAT": " {field} = fields.FloatField({null}{default}{comment})",
|
||||
}
|
||||
|
||||
def __init__(self, conn: BaseDBAsyncClient, tables: Optional[List[str]] = None):
|
||||
|
@@ -10,7 +10,12 @@ from tortoise.exceptions import OperationalError
|
||||
|
||||
from aerich.ddl import BaseDDL
|
||||
from aerich.models import MAX_VERSION_LENGTH, Aerich
|
||||
from aerich.utils import get_app_connection, get_models_describe, write_version_file
|
||||
from aerich.utils import (
|
||||
get_app_connection,
|
||||
get_models_describe,
|
||||
is_default_function,
|
||||
write_version_file,
|
||||
)
|
||||
|
||||
|
||||
class Migrate:
|
||||
@@ -108,8 +113,8 @@ class Migrate:
|
||||
if version_file.startswith(version.split("_")[0]):
|
||||
os.unlink(Path(cls.migrate_location, version_file))
|
||||
content = {
|
||||
"upgrade": cls.upgrade_operators,
|
||||
"downgrade": cls.downgrade_operators,
|
||||
"upgrade": list(dict.fromkeys(cls.upgrade_operators)),
|
||||
"downgrade": list(dict.fromkeys(cls.downgrade_operators)),
|
||||
}
|
||||
write_version_file(Path(cls.migrate_location, version), content)
|
||||
return version
|
||||
@@ -169,10 +174,18 @@ class Migrate:
|
||||
model = cls._get_model(new_model_describe.get("name").split(".")[1])
|
||||
|
||||
if new_model_str not in old_models.keys():
|
||||
cls._add_operator(cls.add_model(model), upgrade)
|
||||
if upgrade:
|
||||
cls._add_operator(cls.add_model(model), upgrade)
|
||||
else:
|
||||
# we can't find origin model when downgrade, so skip
|
||||
pass
|
||||
else:
|
||||
old_model_describe = old_models.get(new_model_str)
|
||||
|
||||
# rename table
|
||||
new_table = new_model_describe.get("table")
|
||||
old_table = old_model_describe.get("table")
|
||||
if new_table != old_table:
|
||||
cls._add_operator(cls.rename_table(model, old_table, new_table), upgrade)
|
||||
old_unique_together = set(
|
||||
map(lambda x: tuple(x), old_model_describe.get("unique_together"))
|
||||
)
|
||||
@@ -192,6 +205,8 @@ class Migrate:
|
||||
old_m2m_fields = old_model_describe.get("m2m_fields")
|
||||
new_m2m_fields = new_model_describe.get("m2m_fields")
|
||||
for action, option, change in diff(old_m2m_fields, new_m2m_fields):
|
||||
if change[0][0] == "db_constraint":
|
||||
continue
|
||||
table = change[0][1].get("through")
|
||||
if action == "add":
|
||||
add = False
|
||||
@@ -223,16 +238,10 @@ class Migrate:
|
||||
cls._add_operator(cls.drop_m2m(table), upgrade, fk_m2m=True)
|
||||
# add unique_together
|
||||
for index in new_unique_together.difference(old_unique_together):
|
||||
cls._add_operator(
|
||||
cls._add_index(model, index, True),
|
||||
upgrade,
|
||||
)
|
||||
cls._add_operator(cls._add_index(model, index, True), upgrade, True)
|
||||
# remove unique_together
|
||||
for index in old_unique_together.difference(new_unique_together):
|
||||
cls._add_operator(
|
||||
cls._drop_index(model, index, True),
|
||||
upgrade,
|
||||
)
|
||||
cls._add_operator(cls._drop_index(model, index, True), upgrade, True)
|
||||
|
||||
old_data_fields = old_model_describe.get("data_fields")
|
||||
new_data_fields = new_model_describe.get("data_fields")
|
||||
@@ -253,14 +262,19 @@ class Migrate:
|
||||
old_data_field_name = old_data_field.get("name")
|
||||
if len(changes) == 2:
|
||||
# rename field
|
||||
if changes[0] == (
|
||||
"change",
|
||||
"name",
|
||||
(old_data_field_name, new_data_field_name),
|
||||
) and changes[1] == (
|
||||
"change",
|
||||
"db_column",
|
||||
(old_data_field.get("db_column"), new_data_field.get("db_column")),
|
||||
if (
|
||||
changes[0]
|
||||
== ("change", "name", (old_data_field_name, new_data_field_name),)
|
||||
and changes[1]
|
||||
== (
|
||||
"change",
|
||||
"db_column",
|
||||
(
|
||||
old_data_field.get("db_column"),
|
||||
new_data_field.get("db_column"),
|
||||
),
|
||||
)
|
||||
and old_data_field_name not in new_data_fields_name
|
||||
):
|
||||
if upgrade:
|
||||
is_rename = click.prompt(
|
||||
@@ -282,22 +296,17 @@ class Migrate:
|
||||
):
|
||||
cls._add_operator(
|
||||
cls._change_field(
|
||||
model, new_data_field, old_data_field
|
||||
model, old_data_field, new_data_field
|
||||
),
|
||||
upgrade,
|
||||
)
|
||||
else:
|
||||
cls._add_operator(
|
||||
cls._rename_field(model, *changes[1][2]),
|
||||
upgrade,
|
||||
cls._rename_field(model, *changes[1][2]), upgrade,
|
||||
)
|
||||
if not is_rename:
|
||||
cls._add_operator(
|
||||
cls._add_field(
|
||||
model,
|
||||
new_data_field,
|
||||
),
|
||||
upgrade,
|
||||
cls._add_field(model, new_data_field,), upgrade,
|
||||
)
|
||||
# remove fields
|
||||
for old_data_field_name in set(old_data_fields_name).difference(
|
||||
@@ -332,11 +341,14 @@ class Migrate:
|
||||
fk_field = next(
|
||||
filter(lambda x: x.get("name") == new_fk_field_name, new_fk_fields)
|
||||
)
|
||||
cls._add_operator(
|
||||
cls._add_fk(model, fk_field, new_models.get(fk_field.get("python_type"))),
|
||||
upgrade,
|
||||
fk_m2m=True,
|
||||
)
|
||||
if fk_field.get("db_constraint"):
|
||||
cls._add_operator(
|
||||
cls._add_fk(
|
||||
model, fk_field, new_models.get(fk_field.get("python_type"))
|
||||
),
|
||||
upgrade,
|
||||
fk_m2m=True,
|
||||
)
|
||||
# drop fk
|
||||
for old_fk_field_name in set(old_fk_fields_name).difference(
|
||||
set(new_fk_fields_name)
|
||||
@@ -344,13 +356,14 @@ class Migrate:
|
||||
old_fk_field = next(
|
||||
filter(lambda x: x.get("name") == old_fk_field_name, old_fk_fields)
|
||||
)
|
||||
cls._add_operator(
|
||||
cls._drop_fk(
|
||||
model, old_fk_field, old_models.get(old_fk_field.get("python_type"))
|
||||
),
|
||||
upgrade,
|
||||
fk_m2m=True,
|
||||
)
|
||||
if old_fk_field.get("db_constraint"):
|
||||
cls._add_operator(
|
||||
cls._drop_fk(
|
||||
model, old_fk_field, old_models.get(old_fk_field.get("python_type"))
|
||||
),
|
||||
upgrade,
|
||||
fk_m2m=True,
|
||||
)
|
||||
# change fields
|
||||
for field_name in set(new_data_fields_name).intersection(set(old_data_fields_name)):
|
||||
old_data_field = next(
|
||||
@@ -367,27 +380,43 @@ class Migrate:
|
||||
unique = new_data_field.get("unique")
|
||||
if old_new[0] is False and old_new[1] is True:
|
||||
cls._add_operator(
|
||||
cls._add_index(model, (field_name,), unique),
|
||||
upgrade,
|
||||
cls._add_index(model, (field_name,), unique), upgrade, True
|
||||
)
|
||||
else:
|
||||
cls._add_operator(
|
||||
cls._drop_index(model, (field_name,), unique),
|
||||
upgrade,
|
||||
cls._drop_index(model, (field_name,), unique), upgrade, True
|
||||
)
|
||||
elif option == "db_field_types.":
|
||||
# change column
|
||||
cls._add_operator(
|
||||
cls._change_field(model, old_data_field, new_data_field),
|
||||
upgrade,
|
||||
)
|
||||
# continue since repeated with others
|
||||
continue
|
||||
elif option == "default":
|
||||
cls._add_operator(cls._alter_default(model, new_data_field), upgrade)
|
||||
if not (
|
||||
is_default_function(old_new[0]) or is_default_function(old_new[1])
|
||||
):
|
||||
# change column default
|
||||
cls._add_operator(
|
||||
cls._alter_default(model, new_data_field), upgrade
|
||||
)
|
||||
elif option == "unique":
|
||||
# because indexed include it
|
||||
continue
|
||||
elif option == "nullable":
|
||||
# change nullable
|
||||
cls._add_operator(cls._alter_null(model, new_data_field), upgrade)
|
||||
else:
|
||||
# modify column
|
||||
cls._add_operator(
|
||||
cls._modify_field(model, new_data_field), upgrade,
|
||||
)
|
||||
|
||||
for old_model in old_models:
|
||||
if old_model not in new_models.keys():
|
||||
cls._add_operator(cls.drop_model(old_models.get(old_model).get("table")), upgrade)
|
||||
|
||||
@classmethod
|
||||
def rename_table(cls, model: Type[Model], old_table_name: str, new_table_name: str):
|
||||
return cls.ddl.rename_table(model, old_table_name, new_table_name)
|
||||
|
||||
@classmethod
|
||||
def add_model(cls, model: Type[Model]):
|
||||
return cls.ddl.create_table(model)
|
||||
|
@@ -1,10 +1,30 @@
|
||||
import importlib
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Dict
|
||||
|
||||
from click import BadOptionUsage, Context
|
||||
from click import BadOptionUsage, ClickException, Context
|
||||
from tortoise import BaseDBAsyncClient, Tortoise
|
||||
|
||||
|
||||
def add_src_path(path: str) -> str:
|
||||
"""
|
||||
add a folder to the paths so we can import from there
|
||||
:param path: path to add
|
||||
:return: absolute path
|
||||
"""
|
||||
if not os.path.isabs(path):
|
||||
# use the absolute path, otherwise some other things (e.g. __file__) won't work properly
|
||||
path = os.path.abspath(path)
|
||||
if not os.path.isdir(path):
|
||||
raise ClickException(f"Specified source folder does not exist: {path}")
|
||||
if path not in sys.path:
|
||||
sys.path.insert(0, path)
|
||||
return path
|
||||
|
||||
|
||||
def get_app_connection_name(config, app_name: str) -> str:
|
||||
"""
|
||||
get connection name
|
||||
@@ -16,8 +36,7 @@ def get_app_connection_name(config, app_name: str) -> str:
|
||||
if app:
|
||||
return app.get("default_connection", "default")
|
||||
raise BadOptionUsage(
|
||||
option_name="--app",
|
||||
message=f'Can\'t get app named "{app_name}"',
|
||||
option_name="--app", message=f'Can\'t get app named "{app_name}"',
|
||||
)
|
||||
|
||||
|
||||
@@ -41,12 +60,11 @@ def get_tortoise_config(ctx: Context, tortoise_orm: str) -> dict:
|
||||
splits = tortoise_orm.split(".")
|
||||
config_path = ".".join(splits[:-1])
|
||||
tortoise_config = splits[-1]
|
||||
|
||||
try:
|
||||
config_module = importlib.import_module(config_path)
|
||||
except (ModuleNotFoundError, AttributeError):
|
||||
raise BadOptionUsage(
|
||||
ctx=ctx, message=f'No config named "{config_path}"', option_name="--config"
|
||||
)
|
||||
except ModuleNotFoundError as e:
|
||||
raise ClickException(f"Error while importing configuration module: {e}") from None
|
||||
|
||||
config = getattr(config_module, tortoise_config, None)
|
||||
if not config:
|
||||
@@ -84,7 +102,7 @@ def get_version_content_from_file(version_file: str) -> Dict:
|
||||
return ret
|
||||
|
||||
|
||||
def write_version_file(version_file: str, content: Dict):
|
||||
def write_version_file(version_file: Path, content: Dict):
|
||||
"""
|
||||
write version file
|
||||
:param version_file:
|
||||
@@ -95,7 +113,9 @@ def write_version_file(version_file: str, content: Dict):
|
||||
f.write(_UPGRADE)
|
||||
upgrade = content.get("upgrade")
|
||||
if len(upgrade) > 1:
|
||||
f.write(";\n".join(upgrade) + ";\n")
|
||||
f.write(";\n".join(upgrade))
|
||||
if not upgrade[-1].endswith(";"):
|
||||
f.write(";\n")
|
||||
else:
|
||||
f.write(f"{upgrade[0]}")
|
||||
if not upgrade[0].endswith(";"):
|
||||
@@ -121,3 +141,7 @@ def get_models_describe(app: str) -> Dict:
|
||||
describe = model.describe()
|
||||
ret[describe.get("name")] = describe
|
||||
return ret
|
||||
|
||||
|
||||
def is_default_function(string: str):
|
||||
return re.match(r"^<function.+>$", str(string or ""))
|
||||
|
@@ -20,10 +20,7 @@ tortoise_orm = {
|
||||
"second": expand_db_url(db_url_second, True),
|
||||
},
|
||||
"apps": {
|
||||
"models": {
|
||||
"models": ["tests.models", "aerich.models"],
|
||||
"default_connection": "default",
|
||||
},
|
||||
"models": {"models": ["tests.models", "aerich.models"], "default_connection": "default"},
|
||||
"models_second": {"models": ["tests.models_second"], "default_connection": "second"},
|
||||
},
|
||||
}
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 75 KiB |
Binary file not shown.
Before Width: | Height: | Size: 76 KiB |
638
poetry.lock
generated
638
poetry.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,13 +1,13 @@
|
||||
[tool.poetry]
|
||||
name = "aerich"
|
||||
version = "0.5.0"
|
||||
version = "0.5.5"
|
||||
description = "A database migrations tool for Tortoise ORM."
|
||||
authors = ["long2ice <long2ice@gmail.com>"]
|
||||
license = "Apache-2.0"
|
||||
readme = "README.md"
|
||||
homepage = "https://github.com/long2ice/aerich"
|
||||
repository = "https://github.com/long2ice/aerich.git"
|
||||
documentation = "https://github.com/long2ice/aerich"
|
||||
homepage = "https://github.com/tortoise/aerich"
|
||||
repository = "https://github.com/tortoise/aerich.git"
|
||||
documentation = "https://github.com/tortoise/aerich"
|
||||
keywords = ["migrate", "Tortoise-ORM", "mysql"]
|
||||
packages = [
|
||||
{ include = "aerich" }
|
||||
@@ -16,7 +16,7 @@ include = ["CHANGELOG.md", "LICENSE", "README.md"]
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = "^3.7"
|
||||
tortoise-orm = "^0.16.21"
|
||||
tortoise-orm = { git = "https://github.com/tortoise/tortoise-orm.git", branch = "develop" }
|
||||
click = "*"
|
||||
pydantic = "*"
|
||||
aiomysql = { version = "*", optional = true }
|
||||
@@ -27,15 +27,18 @@ dictdiffer = "*"
|
||||
[tool.poetry.dev-dependencies]
|
||||
flake8 = "*"
|
||||
isort = "*"
|
||||
black = "^20.8b1"
|
||||
black = "19.10b0"
|
||||
pytest = "*"
|
||||
pytest-xdist = "*"
|
||||
pytest-asyncio = "*"
|
||||
bandit = "*"
|
||||
pytest-mock = "*"
|
||||
cryptography = "*"
|
||||
|
||||
[tool.poetry.extras]
|
||||
dbdrivers = ["aiomysql", "asyncpg"]
|
||||
asyncmy = ["asyncmy"]
|
||||
asyncpg = ["asyncpg"]
|
||||
aiomysql = ["aiomysql"]
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry>=0.12"]
|
||||
|
@@ -1,4 +1,5 @@
|
||||
import datetime
|
||||
import uuid
|
||||
from enum import IntEnum
|
||||
|
||||
from tortoise import Model, fields
|
||||
@@ -38,9 +39,13 @@ class Email(Model):
|
||||
users = fields.ManyToManyField("models.User")
|
||||
|
||||
|
||||
def default_name():
|
||||
return uuid.uuid4()
|
||||
|
||||
|
||||
class Category(Model):
|
||||
slug = fields.CharField(max_length=200)
|
||||
name = fields.CharField(max_length=200)
|
||||
slug = fields.CharField(max_length=100)
|
||||
name = fields.CharField(max_length=200, null=True, default=default_name)
|
||||
user = fields.ForeignKeyField("models.User", description="User")
|
||||
created_at = fields.DatetimeField(auto_now_add=True)
|
||||
|
||||
@@ -66,3 +71,7 @@ class Config(Model):
|
||||
value = fields.JSONField()
|
||||
status: Status = fields.IntEnumField(Status)
|
||||
user = fields.ForeignKeyField("models.User", description="User")
|
||||
|
||||
|
||||
class NewModel(Model):
|
||||
name = fields.CharField(max_length=50)
|
||||
|
@@ -61,3 +61,6 @@ class Config(Model):
|
||||
key = fields.CharField(max_length=20)
|
||||
value = fields.JSONField()
|
||||
status: Status = fields.IntEnumField(Status, default=Status.on)
|
||||
|
||||
class Meta:
|
||||
table = "configs"
|
||||
|
@@ -1,9 +1,6 @@
|
||||
import pytest
|
||||
|
||||
from aerich.ddl.mysql import MysqlDDL
|
||||
from aerich.ddl.postgres import PostgresDDL
|
||||
from aerich.ddl.sqlite import SqliteDDL
|
||||
from aerich.exceptions import NotSupportError
|
||||
from aerich.migrate import Migrate
|
||||
from tests.models import Category, Product, User
|
||||
|
||||
@@ -15,8 +12,8 @@ def test_create_table():
|
||||
ret
|
||||
== """CREATE TABLE IF NOT EXISTS `category` (
|
||||
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
||||
`slug` VARCHAR(200) NOT NULL,
|
||||
`name` VARCHAR(200) NOT NULL,
|
||||
`slug` VARCHAR(100) NOT NULL,
|
||||
`name` VARCHAR(200),
|
||||
`created_at` DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
|
||||
`user_id` INT NOT NULL COMMENT 'User',
|
||||
CONSTRAINT `fk_category_user_e2e3874c` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE
|
||||
@@ -28,8 +25,8 @@ def test_create_table():
|
||||
ret
|
||||
== """CREATE TABLE IF NOT EXISTS "category" (
|
||||
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
"slug" VARCHAR(200) NOT NULL,
|
||||
"name" VARCHAR(200) NOT NULL,
|
||||
"slug" VARCHAR(100) NOT NULL,
|
||||
"name" VARCHAR(200),
|
||||
"created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"user_id" INT NOT NULL REFERENCES "user" ("id") ON DELETE CASCADE /* User */
|
||||
);"""
|
||||
@@ -40,8 +37,8 @@ def test_create_table():
|
||||
ret
|
||||
== """CREATE TABLE IF NOT EXISTS "category" (
|
||||
"id" SERIAL NOT NULL PRIMARY KEY,
|
||||
"slug" VARCHAR(200) NOT NULL,
|
||||
"name" VARCHAR(200) NOT NULL,
|
||||
"slug" VARCHAR(100) NOT NULL,
|
||||
"name" VARCHAR(200),
|
||||
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"user_id" INT NOT NULL REFERENCES "user" ("id") ON DELETE CASCADE
|
||||
);
|
||||
@@ -60,9 +57,9 @@ def test_drop_table():
|
||||
def test_add_column():
|
||||
ret = Migrate.ddl.add_column(Category, Category._meta.fields_map.get("name").describe(False))
|
||||
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)"
|
||||
else:
|
||||
assert ret == 'ALTER TABLE "category" ADD "name" VARCHAR(200) NOT NULL'
|
||||
assert ret == 'ALTER TABLE "category" ADD "name" VARCHAR(200)'
|
||||
|
||||
|
||||
def test_modify_column():
|
||||
@@ -74,9 +71,12 @@ def test_modify_column():
|
||||
)
|
||||
ret1 = Migrate.ddl.modify_column(User, User._meta.fields_map.get("is_active").describe(False))
|
||||
if isinstance(Migrate.ddl, MysqlDDL):
|
||||
assert ret0 == "ALTER TABLE `category` MODIFY COLUMN `name` VARCHAR(200) NOT NULL"
|
||||
assert ret0 == "ALTER TABLE `category` MODIFY COLUMN `name` VARCHAR(200)"
|
||||
elif isinstance(Migrate.ddl, PostgresDDL):
|
||||
assert ret0 == 'ALTER TABLE "category" ALTER COLUMN "name" TYPE VARCHAR(200)'
|
||||
assert (
|
||||
ret0
|
||||
== 'ALTER TABLE "category" ALTER COLUMN "name" TYPE VARCHAR(200) USING "name"::VARCHAR(200)'
|
||||
)
|
||||
|
||||
if isinstance(Migrate.ddl, MysqlDDL):
|
||||
assert (
|
||||
@@ -84,19 +84,19 @@ def test_modify_column():
|
||||
== "ALTER TABLE `user` MODIFY COLUMN `is_active` BOOL NOT NULL COMMENT 'Is Active' DEFAULT 1"
|
||||
)
|
||||
elif isinstance(Migrate.ddl, PostgresDDL):
|
||||
assert ret1 == 'ALTER TABLE "user" ALTER COLUMN "is_active" TYPE BOOL'
|
||||
assert (
|
||||
ret1 == 'ALTER TABLE "user" ALTER COLUMN "is_active" TYPE BOOL USING "is_active"::BOOL'
|
||||
)
|
||||
|
||||
|
||||
def test_alter_column_default():
|
||||
if isinstance(Migrate.ddl, SqliteDDL):
|
||||
return
|
||||
ret = Migrate.ddl.alter_column_default(
|
||||
Category, Category._meta.fields_map.get("name").describe(False)
|
||||
)
|
||||
ret = Migrate.ddl.alter_column_default(User, User._meta.fields_map.get("intro").describe(False))
|
||||
if isinstance(Migrate.ddl, PostgresDDL):
|
||||
assert ret == 'ALTER TABLE "category" ALTER COLUMN "name" DROP DEFAULT'
|
||||
assert ret == 'ALTER TABLE "user" ALTER COLUMN "intro" SET DEFAULT \'\''
|
||||
elif isinstance(Migrate.ddl, MysqlDDL):
|
||||
assert ret == "ALTER TABLE `category` ALTER COLUMN `name` DROP DEFAULT"
|
||||
assert ret == "ALTER TABLE `user` ALTER COLUMN `intro` SET DEFAULT ''"
|
||||
|
||||
ret = Migrate.ddl.alter_column_default(
|
||||
Category, Category._meta.fields_map.get("created_at").describe(False)
|
||||
@@ -127,7 +127,7 @@ def test_alter_column_null():
|
||||
Category, Category._meta.fields_map.get("name").describe(False)
|
||||
)
|
||||
if isinstance(Migrate.ddl, PostgresDDL):
|
||||
assert ret == 'ALTER TABLE "category" ALTER COLUMN "name" SET NOT NULL'
|
||||
assert ret == 'ALTER TABLE "category" ALTER COLUMN "name" DROP NOT NULL'
|
||||
|
||||
|
||||
def test_set_comment():
|
||||
@@ -141,11 +141,7 @@ def test_set_comment():
|
||||
|
||||
|
||||
def test_drop_column():
|
||||
if isinstance(Migrate.ddl, SqliteDDL):
|
||||
with pytest.raises(NotSupportError):
|
||||
ret = Migrate.ddl.drop_column(Category, "name")
|
||||
else:
|
||||
ret = Migrate.ddl.drop_column(Category, "name")
|
||||
ret = Migrate.ddl.drop_column(Category, "name")
|
||||
if isinstance(Migrate.ddl, MysqlDDL):
|
||||
assert ret == "ALTER TABLE `category` DROP COLUMN `name`"
|
||||
elif isinstance(Migrate.ddl, PostgresDDL):
|
||||
|
@@ -146,7 +146,7 @@ old_models_describe = {
|
||||
"models.Config": {
|
||||
"name": "models.Config",
|
||||
"app": "models",
|
||||
"table": "config",
|
||||
"table": "configs",
|
||||
"abstract": False,
|
||||
"description": None,
|
||||
"docstring": None,
|
||||
@@ -768,7 +768,7 @@ def test_migrate(mocker: MockerFixture):
|
||||
- alter default: Config.status
|
||||
- rename column: Product.image -> Product.pic
|
||||
"""
|
||||
mocker.patch("click.prompt", side_effect=(False, True))
|
||||
mocker.patch("click.prompt", side_effect=(True,))
|
||||
|
||||
models_describe = get_models_describe("models")
|
||||
Migrate.app = "models"
|
||||
@@ -783,19 +783,22 @@ def test_migrate(mocker: MockerFixture):
|
||||
if isinstance(Migrate.ddl, MysqlDDL):
|
||||
assert sorted(Migrate.upgrade_operators) == sorted(
|
||||
[
|
||||
"ALTER TABLE `category` MODIFY COLUMN `name` VARCHAR(200)",
|
||||
"ALTER TABLE `category` MODIFY COLUMN `slug` VARCHAR(100) NOT NULL",
|
||||
"ALTER TABLE `config` ADD `user_id` INT NOT NULL COMMENT 'User'",
|
||||
"ALTER TABLE `config` ADD CONSTRAINT `fk_config_user_17daa970` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE",
|
||||
"ALTER TABLE `config` ALTER COLUMN `status` DROP DEFAULT",
|
||||
"ALTER TABLE `email` ADD `address` VARCHAR(200) NOT NULL",
|
||||
"ALTER TABLE `email` DROP COLUMN `user_id`",
|
||||
"ALTER TABLE `configs` RENAME TO `config`",
|
||||
"ALTER TABLE `product` RENAME COLUMN `image` TO `pic`",
|
||||
"ALTER TABLE `email` RENAME COLUMN `id` TO `email_id`",
|
||||
"ALTER TABLE `email` DROP FOREIGN KEY `fk_email_user_5b58673d`",
|
||||
"ALTER TABLE `email` ADD INDEX `idx_email_email_4a1a33` (`email`)",
|
||||
"ALTER TABLE `product` ADD UNIQUE INDEX `uid_product_name_f14935` (`name`, `type`)",
|
||||
"ALTER TABLE `product` ALTER COLUMN `view_num` SET DEFAULT 0",
|
||||
"ALTER TABLE `user` DROP COLUMN `avatar`",
|
||||
"ALTER TABLE `user` CHANGE password password VARCHAR(100)",
|
||||
"ALTER TABLE `user` MODIFY COLUMN `password` VARCHAR(100) NOT NULL",
|
||||
"CREATE TABLE IF NOT EXISTS `newmodel` (\n `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,\n `name` VARCHAR(50) NOT NULL\n) CHARACTER SET utf8mb4;",
|
||||
"ALTER TABLE `user` ADD UNIQUE INDEX `uid_user_usernam_9987ab` (`username`)",
|
||||
"CREATE TABLE `email_user` (`email_id` INT NOT NULL REFERENCES `email` (`email_id`) ON DELETE CASCADE,`user_id` INT NOT NULL REFERENCES `user` (`id`) ON DELETE CASCADE) CHARACTER SET utf8mb4",
|
||||
]
|
||||
@@ -803,68 +806,74 @@ def test_migrate(mocker: MockerFixture):
|
||||
|
||||
assert sorted(Migrate.downgrade_operators) == sorted(
|
||||
[
|
||||
"ALTER TABLE `category` MODIFY COLUMN `name` VARCHAR(200) NOT NULL",
|
||||
"ALTER TABLE `category` MODIFY COLUMN `slug` VARCHAR(200) NOT NULL",
|
||||
"ALTER TABLE `config` DROP COLUMN `user_id`",
|
||||
"ALTER TABLE `config` DROP FOREIGN KEY `fk_config_user_17daa970`",
|
||||
"ALTER TABLE `config` ALTER COLUMN `status` SET DEFAULT 1",
|
||||
"ALTER TABLE `email` ADD `user_id` INT NOT NULL",
|
||||
"ALTER TABLE `email` DROP COLUMN `address`",
|
||||
"ALTER TABLE `config` RENAME TO `configs`",
|
||||
"ALTER TABLE `product` RENAME COLUMN `pic` TO `image`",
|
||||
"ALTER TABLE `email` RENAME COLUMN `email_id` TO `id`",
|
||||
"ALTER TABLE `email` ADD CONSTRAINT `fk_email_user_5b58673d` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE",
|
||||
"ALTER TABLE `email` DROP INDEX `idx_email_email_4a1a33`",
|
||||
"ALTER TABLE `product` DROP INDEX `uid_product_name_f14935`",
|
||||
"ALTER TABLE `product` ALTER COLUMN `view_num` DROP DEFAULT",
|
||||
"ALTER TABLE `user` ADD `avatar` VARCHAR(200) NOT NULL DEFAULT ''",
|
||||
"ALTER TABLE `user` DROP INDEX `idx_user_usernam_9987ab`",
|
||||
"ALTER TABLE `user` CHANGE password password VARCHAR(200)",
|
||||
"ALTER TABLE `user` MODIFY COLUMN `password` VARCHAR(200) NOT NULL",
|
||||
"DROP TABLE IF EXISTS `email_user`",
|
||||
"DROP TABLE IF EXISTS `newmodel`",
|
||||
]
|
||||
)
|
||||
|
||||
elif isinstance(Migrate.ddl, PostgresDDL):
|
||||
assert sorted(Migrate.upgrade_operators) == sorted(
|
||||
[
|
||||
'ALTER TABLE "category" ALTER COLUMN "name" DROP NOT NULL',
|
||||
'ALTER TABLE "category" ALTER COLUMN "slug" TYPE VARCHAR(100) USING "slug"::VARCHAR(100)',
|
||||
'ALTER TABLE "config" ADD "user_id" INT NOT NULL',
|
||||
'ALTER TABLE "config" ADD CONSTRAINT "fk_config_user_17daa970" FOREIGN KEY ("user_id") REFERENCES "user" ("id") ON DELETE CASCADE',
|
||||
'ALTER TABLE "config" ALTER COLUMN "status" DROP DEFAULT',
|
||||
'ALTER TABLE "configs" RENAME TO "config"',
|
||||
'ALTER TABLE "email" ADD "address" VARCHAR(200) NOT NULL',
|
||||
'ALTER TABLE "email" DROP COLUMN "user_id"',
|
||||
'ALTER TABLE "product" RENAME COLUMN "image" TO "pic"',
|
||||
'ALTER TABLE "email" RENAME COLUMN "id" TO "email_id"',
|
||||
'ALTER TABLE "email" DROP CONSTRAINT "fk_email_user_5b58673d"',
|
||||
'CREATE INDEX "idx_email_email_4a1a33" ON "email" ("email")',
|
||||
'CREATE UNIQUE INDEX "uid_product_name_f14935" ON "product" ("name", "type")',
|
||||
'ALTER TABLE "product" ALTER COLUMN "view_num" SET DEFAULT 0',
|
||||
'ALTER TABLE "product" RENAME COLUMN "image" TO "pic"',
|
||||
'ALTER TABLE "user" ALTER COLUMN "password" TYPE VARCHAR(100) USING "password"::VARCHAR(100)',
|
||||
'ALTER TABLE "user" DROP COLUMN "avatar"',
|
||||
'ALTER TABLE "user" CHANGE password password VARCHAR(100)',
|
||||
'CREATE UNIQUE INDEX "uid_user_usernam_9987ab" ON "user" ("username")',
|
||||
'CREATE INDEX "idx_email_email_4a1a33" ON "email" ("email")',
|
||||
'CREATE TABLE "email_user" ("email_id" INT NOT NULL REFERENCES "email" ("email_id") ON DELETE CASCADE,"user_id" INT NOT NULL REFERENCES "user" ("id") ON DELETE CASCADE)',
|
||||
'CREATE TABLE IF NOT EXISTS "newmodel" (\n "id" SERIAL NOT NULL PRIMARY KEY,\n "name" VARCHAR(50) NOT NULL\n);\nCOMMENT ON COLUMN "config"."user_id" IS \'User\';',
|
||||
'CREATE UNIQUE INDEX "uid_product_name_f14935" ON "product" ("name", "type")',
|
||||
'CREATE UNIQUE INDEX "uid_user_usernam_9987ab" ON "user" ("username")',
|
||||
]
|
||||
)
|
||||
assert sorted(Migrate.downgrade_operators) == sorted(
|
||||
[
|
||||
'ALTER TABLE "category" ALTER COLUMN "name" SET NOT NULL',
|
||||
'ALTER TABLE "category" ALTER COLUMN "slug" TYPE VARCHAR(200) USING "slug"::VARCHAR(200)',
|
||||
'ALTER TABLE "config" ALTER COLUMN "status" SET DEFAULT 1',
|
||||
'ALTER TABLE "config" DROP COLUMN "user_id"',
|
||||
'ALTER TABLE "config" DROP CONSTRAINT "fk_config_user_17daa970"',
|
||||
'ALTER TABLE "config" ALTER COLUMN "status" SET DEFAULT 1',
|
||||
'ALTER TABLE "config" RENAME TO "configs"',
|
||||
'ALTER TABLE "email" ADD "user_id" INT NOT NULL',
|
||||
'ALTER TABLE "email" DROP COLUMN "address"',
|
||||
'ALTER TABLE "product" RENAME COLUMN "pic" TO "image"',
|
||||
'ALTER TABLE "email" RENAME COLUMN "email_id" TO "id"',
|
||||
'ALTER TABLE "email" ADD CONSTRAINT "fk_email_user_5b58673d" FOREIGN KEY ("user_id") REFERENCES "user" ("id") ON DELETE CASCADE',
|
||||
'DROP INDEX "idx_email_email_4a1a33"',
|
||||
'ALTER TABLE "product" ALTER COLUMN "view_num" DROP DEFAULT',
|
||||
'ALTER TABLE "product" RENAME COLUMN "pic" TO "image"',
|
||||
'ALTER TABLE "user" ADD "avatar" VARCHAR(200) NOT NULL DEFAULT \'\'',
|
||||
'ALTER TABLE "user" ALTER COLUMN "password" TYPE VARCHAR(200) USING "password"::VARCHAR(200)',
|
||||
'DROP INDEX "idx_email_email_4a1a33"',
|
||||
'DROP INDEX "idx_user_usernam_9987ab"',
|
||||
'DROP INDEX "uid_product_name_f14935"',
|
||||
'ALTER TABLE "user" CHANGE password password VARCHAR(200)',
|
||||
'DROP TABLE IF EXISTS "email_user"',
|
||||
'DROP TABLE IF EXISTS "newmodel"',
|
||||
]
|
||||
)
|
||||
elif isinstance(Migrate.ddl, SqliteDDL):
|
||||
assert Migrate.upgrade_operators == [
|
||||
'ALTER TABLE "config" ADD "user_id" INT NOT NULL /* User */',
|
||||
'ALTER TABLE "config" ADD CONSTRAINT "fk_config_user_17daa970" FOREIGN KEY ("user_id") REFERENCES "user" ("id") ON DELETE CASCADE',
|
||||
]
|
||||
assert Migrate.upgrade_operators == []
|
||||
|
||||
assert Migrate.downgrade_operators == []
|
||||
|
||||
|
Reference in New Issue
Block a user