Compare commits
46 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
63e8d06157 | ||
|
68ef8ac676 | ||
|
8b5cf6faa0 | ||
|
fac00d45cc | ||
|
6f7893d376 | ||
|
b1521c4cc7 | ||
|
24c1f4cb7d | ||
|
661f241dac | ||
|
01787558d6 | ||
|
699b0321a4 | ||
|
4a83021892 | ||
|
af63221875 | ||
|
359525716c | ||
|
7d3eb2e151 | ||
|
d8abf79449 | ||
|
aa9f40ae27 | ||
|
79b7ae343a | ||
|
6f5a9ab78c | ||
|
1e5a83c281 | ||
|
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 |
2
.github/FUNDING.yml
vendored
2
.github/FUNDING.yml
vendored
@@ -1 +1 @@
|
||||
custom: ["https://sponsor.long2ice.cn"]
|
||||
custom: ["https://sponsor.long2ice.io"]
|
||||
|
14
.github/workflows/ci.yml
vendored
14
.github/workflows/ci.yml
vendored
@@ -1,5 +1,11 @@
|
||||
name: ci
|
||||
on: [ push, pull_request ]
|
||||
on:
|
||||
push:
|
||||
branches-ignore:
|
||||
- master
|
||||
pull_request:
|
||||
branches-ignore:
|
||||
- master
|
||||
jobs:
|
||||
ci:
|
||||
runs-on: ubuntu-latest
|
||||
@@ -20,9 +26,9 @@ jobs:
|
||||
with:
|
||||
python-version: '3.x'
|
||||
- name: Install and configure Poetry
|
||||
uses: snok/install-poetry@v1.1.1
|
||||
with:
|
||||
virtualenvs-create: false
|
||||
run: |
|
||||
pip install -U pip poetry
|
||||
poetry config virtualenvs.create false
|
||||
- name: CI
|
||||
env:
|
||||
MYSQL_PASS: root
|
||||
|
6
.github/workflows/pypi.yml
vendored
6
.github/workflows/pypi.yml
vendored
@@ -12,9 +12,9 @@ jobs:
|
||||
with:
|
||||
python-version: '3.x'
|
||||
- name: Install and configure Poetry
|
||||
uses: snok/install-poetry@v1.1.1
|
||||
with:
|
||||
virtualenvs-create: false
|
||||
run: |
|
||||
pip install -U pip poetry
|
||||
poetry config virtualenvs.create false
|
||||
- name: Build dists
|
||||
run: make build
|
||||
- name: Pypi Publish
|
||||
|
37
CHANGELOG.md
37
CHANGELOG.md
@@ -1,7 +1,44 @@
|
||||
# ChangeLog
|
||||
|
||||
## 0.6
|
||||
|
||||
### 0.6.0
|
||||
|
||||
- Change default config file from `aerich.ini` to `pyproject.toml`. (#197)
|
||||
|
||||
**Upgrade note:**
|
||||
1. Run `aerich init -t config.TORTOISE_ORM`.
|
||||
2. Remove `aerich.ini`.
|
||||
- Remove `pydantic` dependency. (#198)
|
||||
- `inspectdb` support `DATE`. (#215)
|
||||
|
||||
## 0.5
|
||||
|
||||
### 0.5.8
|
||||
|
||||
- Support `indexes` change. (#193)
|
||||
|
||||
### 0.5.7
|
||||
|
||||
- Fix no module found error. (#188) (#189)
|
||||
|
||||
### 0.5.6
|
||||
|
||||
- Add `Command` class. (#148) (#141) (#123) (#106)
|
||||
- Fix: migrate doesn't use source_field in unique_together. (#181)
|
||||
|
||||
### 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)
|
||||
|
2
Makefile
2
Makefile
@@ -12,7 +12,7 @@ up:
|
||||
@poetry update
|
||||
|
||||
deps:
|
||||
@poetry install -E asyncpg -E asyncmy -E aiomysql
|
||||
@poetry install -E asyncpg -E asyncmy
|
||||
|
||||
style: deps
|
||||
isort -src $(checkfiles)
|
||||
|
49
README.md
49
README.md
@@ -1,9 +1,9 @@
|
||||
# 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:ci)
|
||||
[](https://github.com/tortoise/aerich)
|
||||
[](https://github.com/tortoise/aerich/actions?query=workflow:pypi)
|
||||
[](https://github.com/tortoise/aerich/actions?query=workflow:ci)
|
||||
|
||||
## Introduction
|
||||
|
||||
@@ -15,7 +15,7 @@ it\'s own migration solution.
|
||||
Just install from pypi:
|
||||
|
||||
```shell
|
||||
> pip install aerich
|
||||
pip install aerich
|
||||
```
|
||||
|
||||
## Quick Start
|
||||
@@ -26,10 +26,9 @@ Just install from pypi:
|
||||
Usage: aerich [OPTIONS] COMMAND [ARGS]...
|
||||
|
||||
Options:
|
||||
-c, --config TEXT Config file. [default: aerich.ini]
|
||||
--app TEXT Tortoise-ORM app name. [default: models]
|
||||
-n, --name TEXT Name of section in .ini file to use for aerich config.
|
||||
[default: aerich]
|
||||
-V, --version Show the version and exit.
|
||||
-c, --config TEXT Config file. [default: pyproject.toml]
|
||||
--app TEXT Tortoise-ORM app name.
|
||||
-h, --help Show this message and exit.
|
||||
|
||||
Commands:
|
||||
@@ -40,7 +39,7 @@ 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
|
||||
@@ -69,9 +68,10 @@ 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]
|
||||
-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.
|
||||
```
|
||||
|
||||
@@ -81,7 +81,7 @@ Initialize the config file and migrations location:
|
||||
> aerich init -t tests.backends.mysql.TORTOISE_ORM
|
||||
|
||||
Success create migrate location ./migrations
|
||||
Success generate config file aerich.ini
|
||||
Success write config to pyproject.toml
|
||||
```
|
||||
|
||||
### Init db
|
||||
@@ -124,7 +124,7 @@ Now your db is migrated to latest.
|
||||
### Downgrade to specified version
|
||||
|
||||
```shell
|
||||
> aerich init -h
|
||||
> aerich downgrade -h
|
||||
|
||||
Usage: aerich downgrade [OPTIONS]
|
||||
|
||||
@@ -208,6 +208,29 @@ tortoise_orm = {
|
||||
|
||||
You only need to specify `aerich.models` in one app, and must specify `--app` when running `aerich migrate` and so on.
|
||||
|
||||
## Restore `aerich` workflow
|
||||
|
||||
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.
|
||||
|
||||
## Use `aerich` in application
|
||||
|
||||
You can use `aerich` out of cli by use `Command` class.
|
||||
|
||||
```python
|
||||
from aerich import Command
|
||||
|
||||
command = Command(tortoise_config=config, app='models')
|
||||
await command.init()
|
||||
await command.migrate('test')
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
This project is licensed under the
|
||||
|
@@ -1 +1,138 @@
|
||||
__version__ = "0.5.3"
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
from tortoise import Tortoise, generate_schema_for_client
|
||||
from tortoise.exceptions import OperationalError
|
||||
from tortoise.transactions import in_transaction
|
||||
from tortoise.utils import get_schema_sql
|
||||
|
||||
from aerich.exceptions import DowngradeError
|
||||
from aerich.inspectdb import InspectDb
|
||||
from aerich.migrate import Migrate
|
||||
from aerich.models import Aerich
|
||||
from aerich.utils import (
|
||||
get_app_connection,
|
||||
get_app_connection_name,
|
||||
get_models_describe,
|
||||
get_version_content_from_file,
|
||||
write_version_file,
|
||||
)
|
||||
|
||||
|
||||
class Command:
|
||||
def __init__(
|
||||
self,
|
||||
tortoise_config: dict,
|
||||
app: str = "models",
|
||||
location: str = "./migrations",
|
||||
):
|
||||
self.tortoise_config = tortoise_config
|
||||
self.app = app
|
||||
self.location = location
|
||||
Migrate.app = app
|
||||
|
||||
async def init(self):
|
||||
await Migrate.init(self.tortoise_config, self.app, self.location)
|
||||
|
||||
async def upgrade(self):
|
||||
migrated = []
|
||||
for version_file in Migrate.get_all_version_files():
|
||||
try:
|
||||
exists = await Aerich.exists(version=version_file, app=self.app)
|
||||
except OperationalError:
|
||||
exists = False
|
||||
if not exists:
|
||||
async with in_transaction(
|
||||
get_app_connection_name(self.tortoise_config, self.app)
|
||||
) as conn:
|
||||
file_path = Path(Migrate.migrate_location, version_file)
|
||||
content = get_version_content_from_file(file_path)
|
||||
upgrade_query_list = content.get("upgrade")
|
||||
for upgrade_query in upgrade_query_list:
|
||||
await conn.execute_script(upgrade_query)
|
||||
await Aerich.create(
|
||||
version=version_file,
|
||||
app=self.app,
|
||||
content=get_models_describe(self.app),
|
||||
)
|
||||
migrated.append(version_file)
|
||||
return migrated
|
||||
|
||||
async def downgrade(self, version: int, delete: bool):
|
||||
ret = []
|
||||
if version == -1:
|
||||
specified_version = await Migrate.get_last_version()
|
||||
else:
|
||||
specified_version = await Aerich.filter(
|
||||
app=self.app, version__startswith=f"{version}_"
|
||||
).first()
|
||||
if not specified_version:
|
||||
raise DowngradeError("No specified version found")
|
||||
if version == -1:
|
||||
versions = [specified_version]
|
||||
else:
|
||||
versions = await Aerich.filter(app=self.app, pk__gte=specified_version.pk)
|
||||
for version in versions:
|
||||
file = version.version
|
||||
async with in_transaction(
|
||||
get_app_connection_name(self.tortoise_config, self.app)
|
||||
) as conn:
|
||||
file_path = Path(Migrate.migrate_location, file)
|
||||
content = get_version_content_from_file(file_path)
|
||||
downgrade_query_list = content.get("downgrade")
|
||||
if not downgrade_query_list:
|
||||
raise DowngradeError("No downgrade items found")
|
||||
for downgrade_query in downgrade_query_list:
|
||||
await conn.execute_query(downgrade_query)
|
||||
await version.delete()
|
||||
if delete:
|
||||
os.unlink(file_path)
|
||||
ret.append(file)
|
||||
return ret
|
||||
|
||||
async def heads(self):
|
||||
ret = []
|
||||
versions = Migrate.get_all_version_files()
|
||||
for version in versions:
|
||||
if not await Aerich.exists(version=version, app=self.app):
|
||||
ret.append(version)
|
||||
return ret
|
||||
|
||||
async def history(self):
|
||||
ret = []
|
||||
versions = Migrate.get_all_version_files()
|
||||
for version in versions:
|
||||
ret.append(version)
|
||||
return ret
|
||||
|
||||
async def inspectdb(self, tables: List[str]):
|
||||
connection = get_app_connection(self.tortoise_config, self.app)
|
||||
inspect = InspectDb(connection, tables)
|
||||
await inspect.inspect()
|
||||
|
||||
async def migrate(self, name: str = "update"):
|
||||
return await Migrate.migrate(name)
|
||||
|
||||
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)
|
||||
|
241
aerich/cli.py
241
aerich/cli.py
@@ -1,34 +1,25 @@
|
||||
import asyncio
|
||||
import os
|
||||
import sys
|
||||
from configparser import ConfigParser
|
||||
from functools import wraps
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
import click
|
||||
import tomlkit
|
||||
from click import Context, UsageError
|
||||
from tortoise import Tortoise, generate_schema_for_client
|
||||
from tortoise.exceptions import OperationalError
|
||||
from tortoise.transactions import in_transaction
|
||||
from tortoise.utils import get_schema_sql
|
||||
from tomlkit.exceptions import NonExistentKey
|
||||
from tortoise import Tortoise
|
||||
|
||||
from aerich.inspectdb import InspectDb
|
||||
from aerich.migrate import Migrate
|
||||
from aerich.utils import (
|
||||
get_app_connection,
|
||||
get_app_connection_name,
|
||||
get_models_describe,
|
||||
get_tortoise_config,
|
||||
get_version_content_from_file,
|
||||
write_version_file,
|
||||
)
|
||||
from aerich.exceptions import DowngradeError
|
||||
from aerich.utils import add_src_path, get_tortoise_config
|
||||
|
||||
from . import __version__
|
||||
from . import Command
|
||||
from .enums import Color
|
||||
from .models import Aerich
|
||||
from .version import __version__
|
||||
|
||||
parser = ConfigParser()
|
||||
CONFIG_DEFAULT_VALUES = {
|
||||
"src_folder": ".",
|
||||
}
|
||||
|
||||
|
||||
def coro(f):
|
||||
@@ -49,42 +40,42 @@ 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="pyproject.toml",
|
||||
show_default=True,
|
||||
help="Config file.",
|
||||
)
|
||||
@click.option("--app", required=False, help="Tortoise-ORM app name.")
|
||||
@click.option(
|
||||
"-n",
|
||||
"--name",
|
||||
default="aerich",
|
||||
show_default=True,
|
||||
help="Name of section in .ini file to use for aerich config.",
|
||||
)
|
||||
@click.pass_context
|
||||
@coro
|
||||
async def cli(ctx: Context, config, app, name):
|
||||
async def cli(ctx: Context, config, app):
|
||||
ctx.ensure_object(dict)
|
||||
ctx.obj["config_file"] = config
|
||||
ctx.obj["name"] = name
|
||||
|
||||
invoked_subcommand = ctx.invoked_subcommand
|
||||
if invoked_subcommand != "init":
|
||||
if not Path(config).exists():
|
||||
raise UsageError("You must exec init first", ctx=ctx)
|
||||
parser.read(config)
|
||||
|
||||
location = parser[name]["location"]
|
||||
tortoise_orm = parser[name]["tortoise_orm"]
|
||||
|
||||
with open(config, "r") as f:
|
||||
content = f.read()
|
||||
doc = tomlkit.parse(content)
|
||||
try:
|
||||
tool = doc["tool"]["aerich"]
|
||||
location = tool["location"]
|
||||
tortoise_orm = tool["tortoise_orm"]
|
||||
src_folder = tool.get("src_folder", CONFIG_DEFAULT_VALUES["src_folder"])
|
||||
except NonExistentKey:
|
||||
raise UsageError("You need run aerich init again when upgrade to 0.6.0+")
|
||||
add_src_path(src_folder)
|
||||
tortoise_config = get_tortoise_config(ctx, tortoise_orm)
|
||||
app = app or list(tortoise_config.get("apps").keys())[0]
|
||||
ctx.obj["config"] = tortoise_config
|
||||
ctx.obj["location"] = location
|
||||
ctx.obj["app"] = app
|
||||
Migrate.app = app
|
||||
command = Command(tortoise_config=tortoise_config, app=app, location=location)
|
||||
ctx.obj["command"] = command
|
||||
if invoked_subcommand != "init-db":
|
||||
if not Path(location, app).exists():
|
||||
raise UsageError("You must exec init-db first", ctx=ctx)
|
||||
await Migrate.init(tortoise_config, app, location)
|
||||
await command.init()
|
||||
|
||||
|
||||
@cli.command(help="Generate migrate changes file.")
|
||||
@@ -92,7 +83,8 @@ async def cli(ctx: Context, config, app, name):
|
||||
@click.pass_context
|
||||
@coro
|
||||
async def migrate(ctx: Context, name):
|
||||
ret = await Migrate.migrate(name)
|
||||
command = ctx.obj["command"]
|
||||
ret = await command.migrate(name)
|
||||
if not ret:
|
||||
return click.secho("No changes detected", fg=Color.yellow)
|
||||
click.secho(f"Success migrate {ret}", fg=Color.green)
|
||||
@@ -102,28 +94,13 @@ async def migrate(ctx: Context, name):
|
||||
@click.pass_context
|
||||
@coro
|
||||
async def upgrade(ctx: Context):
|
||||
config = ctx.obj["config"]
|
||||
app = ctx.obj["app"]
|
||||
migrated = False
|
||||
for version_file in Migrate.get_all_version_files():
|
||||
try:
|
||||
exists = await Aerich.exists(version=version_file, app=app)
|
||||
except OperationalError:
|
||||
exists = False
|
||||
if not exists:
|
||||
async with in_transaction(get_app_connection_name(config, app)) as conn:
|
||||
file_path = Path(Migrate.migrate_location, version_file)
|
||||
content = get_version_content_from_file(file_path)
|
||||
upgrade_query_list = content.get("upgrade")
|
||||
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),
|
||||
)
|
||||
click.secho(f"Success upgrade {version_file}", fg=Color.green)
|
||||
migrated = True
|
||||
command = ctx.obj["command"]
|
||||
migrated = await command.upgrade()
|
||||
if not migrated:
|
||||
click.secho("No upgrade items found", fg=Color.yellow)
|
||||
else:
|
||||
for version_file in migrated:
|
||||
click.secho(f"Success upgrade {version_file}", fg=Color.green)
|
||||
|
||||
|
||||
@cli.command(help="Downgrade to specified version.")
|
||||
@@ -149,59 +126,37 @@ async def upgrade(ctx: Context):
|
||||
)
|
||||
@coro
|
||||
async def downgrade(ctx: Context, version: int, delete: bool):
|
||||
app = ctx.obj["app"]
|
||||
config = ctx.obj["config"]
|
||||
if version == -1:
|
||||
specified_version = await Migrate.get_last_version()
|
||||
else:
|
||||
specified_version = await Aerich.filter(app=app, version__startswith=f"{version}_").first()
|
||||
if not specified_version:
|
||||
return click.secho("No specified version found", fg=Color.yellow)
|
||||
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:
|
||||
file_path = Path(Migrate.migrate_location, file)
|
||||
content = get_version_content_from_file(file_path)
|
||||
downgrade_query_list = content.get("downgrade")
|
||||
if not downgrade_query_list:
|
||||
click.secho("No downgrade items found", fg=Color.yellow)
|
||||
return
|
||||
for downgrade_query in downgrade_query_list:
|
||||
await conn.execute_query(downgrade_query)
|
||||
await version.delete()
|
||||
if delete:
|
||||
os.unlink(file_path)
|
||||
click.secho(f"Success downgrade {file}", fg=Color.green)
|
||||
command = ctx.obj["command"]
|
||||
try:
|
||||
files = await command.downgrade(version, delete)
|
||||
except DowngradeError as e:
|
||||
return click.secho(str(e), fg=Color.yellow)
|
||||
for file in files:
|
||||
click.secho(f"Success downgrade {file}", fg=Color.green)
|
||||
|
||||
|
||||
@cli.command(help="Show current available heads in migrate location.")
|
||||
@click.pass_context
|
||||
@coro
|
||||
async def heads(ctx: Context):
|
||||
app = ctx.obj["app"]
|
||||
versions = Migrate.get_all_version_files()
|
||||
is_heads = False
|
||||
for version in versions:
|
||||
if not await Aerich.exists(version=version, app=app):
|
||||
click.secho(version, fg=Color.green)
|
||||
is_heads = True
|
||||
if not is_heads:
|
||||
click.secho("No available heads,try migrate first", fg=Color.green)
|
||||
command = ctx.obj["command"]
|
||||
head_list = await command.heads()
|
||||
if not head_list:
|
||||
return click.secho("No available heads, try migrate first", fg=Color.green)
|
||||
for version in head_list:
|
||||
click.secho(version, fg=Color.green)
|
||||
|
||||
|
||||
@cli.command(help="List all migrate items.")
|
||||
@click.pass_context
|
||||
@coro
|
||||
async def history(ctx: Context):
|
||||
versions = Migrate.get_all_version_files()
|
||||
command = ctx.obj["command"]
|
||||
versions = await command.history()
|
||||
if not versions:
|
||||
return click.secho("No history, try migrate", fg=Color.green)
|
||||
for version in versions:
|
||||
click.secho(version, fg=Color.green)
|
||||
if not versions:
|
||||
click.secho("No history,try migrate", fg=Color.green)
|
||||
|
||||
|
||||
@cli.command(help="Init config file and generate root migrate location.")
|
||||
@@ -212,29 +167,49 @@ 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)
|
||||
|
||||
parser.add_section(name)
|
||||
parser.set(name, "tortoise_orm", tortoise_orm)
|
||||
parser.set(name, "location", location)
|
||||
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
|
||||
|
||||
with open(config_file, "w", encoding="utf-8") as f:
|
||||
parser.write(f)
|
||||
# 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)
|
||||
|
||||
with open(config_file, "r") as f:
|
||||
content = f.read()
|
||||
doc = tomlkit.parse(content)
|
||||
table = tomlkit.table()
|
||||
table["tortoise_orm"] = tortoise_orm
|
||||
table["location"] = location
|
||||
table["src_folder"] = src_folder
|
||||
doc["tool"]["aerich"] = table
|
||||
|
||||
with open(config_file, "w") as f:
|
||||
f.write(tomlkit.dumps(doc))
|
||||
|
||||
Path(location).mkdir(parents=True, exist_ok=True)
|
||||
|
||||
click.secho(f"Success create migrate location {location}", fg=Color.green)
|
||||
click.secho(f"Success generate config file {config_file}", fg=Color.green)
|
||||
click.secho(f"Success write config to {config_file}", fg=Color.green)
|
||||
|
||||
|
||||
@cli.command(help="Generate schema and generate app migrate location.")
|
||||
@@ -248,53 +223,35 @@ async def init(
|
||||
@click.pass_context
|
||||
@coro
|
||||
async def init_db(ctx: Context, safe):
|
||||
config = ctx.obj["config"]
|
||||
location = ctx.obj["location"]
|
||||
app = ctx.obj["app"]
|
||||
|
||||
dirname = Path(location, app)
|
||||
command = ctx.obj["command"]
|
||||
app = command.app
|
||||
dirname = Path(command.location, app)
|
||||
try:
|
||||
dirname.mkdir(parents=True)
|
||||
await command.init_db(safe)
|
||||
click.secho(f"Success create app migrate location {dirname}", fg=Color.green)
|
||||
click.secho(f'Success generate schema for app "{app}"', 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)
|
||||
|
||||
|
||||
@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
|
||||
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()
|
||||
command = ctx.obj["command"]
|
||||
await command.inspectdb(table)
|
||||
|
||||
|
||||
def main():
|
||||
sys.path.insert(0, ".")
|
||||
cli()
|
||||
|
||||
|
||||
|
@@ -78,11 +78,15 @@ 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",
|
||||
] or is_default_function(default):
|
||||
if (
|
||||
field_describe.get("field_type")
|
||||
in [
|
||||
"UUIDField",
|
||||
"TextField",
|
||||
"JSONField",
|
||||
]
|
||||
or is_default_function(default)
|
||||
):
|
||||
default = ""
|
||||
else:
|
||||
try:
|
||||
@@ -115,7 +119,9 @@ 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 "",
|
||||
|
@@ -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.")
|
||||
|
||||
|
@@ -2,3 +2,9 @@ class NotSupportError(Exception):
|
||||
"""
|
||||
raise when features not support
|
||||
"""
|
||||
|
||||
|
||||
class DowngradeError(Exception):
|
||||
"""
|
||||
raise when downgrade error
|
||||
"""
|
||||
|
@@ -16,6 +16,7 @@ class InspectDb:
|
||||
"TEXT": " {field} = fields.TextField({null}{default}{comment})",
|
||||
"DATETIME": " {field} = fields.DatetimeField({null}{default}{comment})",
|
||||
"FLOAT": " {field} = fields.FloatField({null}{default}{comment})",
|
||||
"DATE": " {field} = fields.DateField({null}{default}{comment})",
|
||||
}
|
||||
|
||||
def __init__(self, conn: BaseDBAsyncClient, tables: Optional[List[str]] = None):
|
||||
|
@@ -113,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
|
||||
@@ -138,21 +138,21 @@ class Migrate:
|
||||
return await cls._generate_diff_sql(name)
|
||||
|
||||
@classmethod
|
||||
def _add_operator(cls, operator: str, upgrade=True, fk_m2m=False):
|
||||
def _add_operator(cls, operator: str, upgrade=True, fk_m2m_index=False):
|
||||
"""
|
||||
add operator,differentiate fk because fk is order limit
|
||||
:param operator:
|
||||
:param upgrade:
|
||||
:param fk_m2m:
|
||||
:param fk_m2m_index:
|
||||
:return:
|
||||
"""
|
||||
if upgrade:
|
||||
if fk_m2m:
|
||||
if fk_m2m_index:
|
||||
cls._upgrade_fk_m2m_index_operators.append(operator)
|
||||
else:
|
||||
cls.upgrade_operators.append(operator)
|
||||
else:
|
||||
if fk_m2m:
|
||||
if fk_m2m_index:
|
||||
cls._downgrade_fk_m2m_index_operators.append(operator)
|
||||
else:
|
||||
cls.downgrade_operators.append(operator)
|
||||
@@ -192,7 +192,8 @@ class Migrate:
|
||||
new_unique_together = set(
|
||||
map(lambda x: tuple(x), new_model_describe.get("unique_together"))
|
||||
)
|
||||
|
||||
old_indexes = set(map(lambda x: tuple(x), old_model_describe.get("indexes", [])))
|
||||
new_indexes = set(map(lambda x: tuple(x), new_model_describe.get("indexes", [])))
|
||||
old_pk_field = old_model_describe.get("pk_field")
|
||||
new_pk_field = new_model_describe.get("pk_field")
|
||||
# pk field
|
||||
@@ -205,6 +206,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
|
||||
@@ -222,7 +225,7 @@ class Migrate:
|
||||
new_models.get(change[0][1].get("model_name")),
|
||||
),
|
||||
upgrade,
|
||||
fk_m2m=True,
|
||||
fk_m2m_index=True,
|
||||
)
|
||||
elif action == "remove":
|
||||
add = False
|
||||
@@ -233,18 +236,19 @@ class Migrate:
|
||||
cls._downgrade_m2m.append(table)
|
||||
add = True
|
||||
if add:
|
||||
cls._add_operator(cls.drop_m2m(table), upgrade, fk_m2m=True)
|
||||
cls._add_operator(cls.drop_m2m(table), upgrade, 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)
|
||||
# add indexes
|
||||
for index in new_indexes.difference(old_indexes):
|
||||
cls._add_operator(cls._add_index(model, index, False), upgrade, True)
|
||||
# remove indexes
|
||||
for index in old_indexes.difference(new_indexes):
|
||||
cls._add_operator(cls._drop_index(model, index, False), upgrade, True)
|
||||
old_data_fields = old_model_describe.get("data_fields")
|
||||
new_data_fields = new_model_describe.get("data_fields")
|
||||
|
||||
@@ -266,7 +270,11 @@ class Migrate:
|
||||
# rename field
|
||||
if (
|
||||
changes[0]
|
||||
== ("change", "name", (old_data_field_name, new_data_field_name),)
|
||||
== (
|
||||
"change",
|
||||
"name",
|
||||
(old_data_field_name, new_data_field_name),
|
||||
)
|
||||
and changes[1]
|
||||
== (
|
||||
"change",
|
||||
@@ -297,15 +305,23 @@ class Migrate:
|
||||
and cls._db_version.startswith("5.")
|
||||
):
|
||||
cls._add_operator(
|
||||
cls._modify_field(model, new_data_field), upgrade,
|
||||
cls._change_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(
|
||||
@@ -340,11 +356,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_index=True,
|
||||
)
|
||||
# drop fk
|
||||
for old_fk_field_name in set(old_fk_fields_name).difference(
|
||||
set(new_fk_fields_name)
|
||||
@@ -352,13 +371,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_index=True,
|
||||
)
|
||||
# change fields
|
||||
for field_name in set(new_data_fields_name).intersection(set(old_data_fields_name)):
|
||||
old_data_field = next(
|
||||
@@ -375,11 +395,11 @@ 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.":
|
||||
# continue since repeated with others
|
||||
@@ -394,14 +414,15 @@ class Migrate:
|
||||
)
|
||||
elif option == "unique":
|
||||
# because indexed include it
|
||||
pass
|
||||
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,
|
||||
cls._modify_field(model, new_data_field),
|
||||
upgrade,
|
||||
)
|
||||
|
||||
for old_model in old_models:
|
||||
@@ -432,7 +453,10 @@ class Migrate:
|
||||
def _resolve_fk_fields_name(cls, model: Type[Model], fields_name: Tuple[str]):
|
||||
ret = []
|
||||
for field_name in fields_name:
|
||||
if field_name in model._meta.fk_fields:
|
||||
field = model._meta.fields_map[field_name]
|
||||
if field.source_field:
|
||||
ret.append(field.source_field)
|
||||
elif field_name in model._meta.fk_fields:
|
||||
ret.append(field_name + "_id")
|
||||
else:
|
||||
ret.append(field_name)
|
||||
|
@@ -1,11 +1,30 @@
|
||||
import importlib
|
||||
import os
|
||||
import re
|
||||
from typing import Dict
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Dict, Union
|
||||
|
||||
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
|
||||
@@ -17,7 +36,8 @@ 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 +61,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:
|
||||
@@ -62,7 +81,7 @@ _UPGRADE = "-- upgrade --\n"
|
||||
_DOWNGRADE = "-- downgrade --\n"
|
||||
|
||||
|
||||
def get_version_content_from_file(version_file: str) -> Dict:
|
||||
def get_version_content_from_file(version_file: Union[str, Path]) -> Dict:
|
||||
"""
|
||||
get version content
|
||||
:param version_file:
|
||||
@@ -84,7 +103,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 +114,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(";"):
|
||||
|
1
aerich/version.py
Normal file
1
aerich/version.py
Normal file
@@ -0,0 +1 @@
|
||||
__version__ = "0.6.0"
|
861
poetry.lock
generated
861
poetry.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,13 +1,13 @@
|
||||
[tool.poetry]
|
||||
name = "aerich"
|
||||
version = "0.5.3"
|
||||
version = "0.6.0"
|
||||
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,18 +16,18 @@ include = ["CHANGELOG.md", "LICENSE", "README.md"]
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = "^3.7"
|
||||
tortoise-orm = "*"
|
||||
tortoise-orm = "^0.17.7"
|
||||
click = "*"
|
||||
pydantic = "*"
|
||||
aiomysql = { version = "*", optional = true }
|
||||
asyncpg = { version = "*", optional = true }
|
||||
asyncmy = { version = "*", optional = true }
|
||||
ddlparse = "*"
|
||||
dictdiffer = "*"
|
||||
tomlkit = "*"
|
||||
|
||||
[tool.poetry.dev-dependencies]
|
||||
flake8 = "*"
|
||||
isort = "*"
|
||||
black = "19.10b0"
|
||||
black = "*"
|
||||
pytest = "*"
|
||||
pytest-xdist = "*"
|
||||
pytest-asyncio = "*"
|
||||
@@ -38,7 +38,6 @@ cryptography = "*"
|
||||
[tool.poetry.extras]
|
||||
asyncmy = ["asyncmy"]
|
||||
asyncpg = ["asyncpg"]
|
||||
aiomysql = ["aiomysql"]
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry>=0.12"]
|
||||
|
@@ -56,13 +56,16 @@ class Product(Model):
|
||||
view_num = fields.IntField(description="View Num", default=0)
|
||||
sort = fields.IntField()
|
||||
is_reviewed = fields.BooleanField(description="Is Reviewed")
|
||||
type = fields.IntEnumField(ProductType, description="Product Type")
|
||||
type = fields.IntEnumField(
|
||||
ProductType, description="Product Type", source_field="type_db_alias"
|
||||
)
|
||||
pic = fields.CharField(max_length=200)
|
||||
body = fields.TextField()
|
||||
created_at = fields.DatetimeField(auto_now_add=True)
|
||||
|
||||
class Meta:
|
||||
unique_together = (("name", "type"),)
|
||||
indexes = (("name", "type"),)
|
||||
|
||||
|
||||
class Config(Model):
|
||||
|
@@ -50,7 +50,9 @@ class Product(Model):
|
||||
view_num = fields.IntField(description="View Num")
|
||||
sort = fields.IntField()
|
||||
is_reviewed = fields.BooleanField(description="Is Reviewed")
|
||||
type = fields.IntEnumField(ProductType, description="Product Type")
|
||||
type = fields.IntEnumField(
|
||||
ProductType, description="Product Type", source_field="type_db_alias"
|
||||
)
|
||||
image = fields.CharField(max_length=200)
|
||||
body = fields.TextField()
|
||||
created_at = fields.DatetimeField(auto_now_add=True)
|
||||
|
@@ -50,7 +50,9 @@ class Product(Model):
|
||||
view_num = fields.IntField(description="View Num")
|
||||
sort = fields.IntField()
|
||||
is_reviewed = fields.BooleanField(description="Is Reviewed")
|
||||
type = fields.IntEnumField(ProductType, description="Product Type")
|
||||
type = fields.IntEnumField(
|
||||
ProductType, description="Product Type", source_field="type_db_alias"
|
||||
)
|
||||
image = fields.CharField(max_length=200)
|
||||
body = fields.TextField()
|
||||
created_at = fields.DatetimeField(auto_now_add=True)
|
||||
|
@@ -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
|
||||
|
||||
@@ -144,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):
|
||||
|
@@ -17,6 +17,7 @@ old_models_describe = {
|
||||
"description": None,
|
||||
"docstring": None,
|
||||
"unique_together": [],
|
||||
"indexes": [],
|
||||
"pk_field": {
|
||||
"name": "id",
|
||||
"field_type": "IntField",
|
||||
@@ -151,6 +152,7 @@ old_models_describe = {
|
||||
"description": None,
|
||||
"docstring": None,
|
||||
"unique_together": [],
|
||||
"indexes": [],
|
||||
"pk_field": {
|
||||
"name": "id",
|
||||
"field_type": "IntField",
|
||||
@@ -242,6 +244,7 @@ old_models_describe = {
|
||||
"description": None,
|
||||
"docstring": None,
|
||||
"unique_together": [],
|
||||
"indexes": [],
|
||||
"pk_field": {
|
||||
"name": "id",
|
||||
"field_type": "IntField",
|
||||
@@ -334,6 +337,7 @@ old_models_describe = {
|
||||
"description": None,
|
||||
"docstring": None,
|
||||
"unique_together": [],
|
||||
"indexes": [],
|
||||
"pk_field": {
|
||||
"name": "id",
|
||||
"field_type": "IntField",
|
||||
@@ -413,7 +417,7 @@ old_models_describe = {
|
||||
{
|
||||
"name": "type",
|
||||
"field_type": "IntEnumFieldInstance",
|
||||
"db_column": "type",
|
||||
"db_column": "type_db_alias",
|
||||
"python_type": "int",
|
||||
"generated": False,
|
||||
"nullable": False,
|
||||
@@ -512,6 +516,7 @@ old_models_describe = {
|
||||
"description": None,
|
||||
"docstring": None,
|
||||
"unique_together": [],
|
||||
"indexes": [],
|
||||
"pk_field": {
|
||||
"name": "id",
|
||||
"field_type": "IntField",
|
||||
@@ -681,6 +686,7 @@ old_models_describe = {
|
||||
"description": None,
|
||||
"docstring": None,
|
||||
"unique_together": [],
|
||||
"indexes": [],
|
||||
"pk_field": {
|
||||
"name": "id",
|
||||
"field_type": "IntField",
|
||||
@@ -793,9 +799,9 @@ def test_migrate(mocker: MockerFixture):
|
||||
"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 `product` ADD INDEX `idx_product_name_869427` (`name`, `type_db_alias`)",
|
||||
"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` ADD UNIQUE INDEX `uid_product_name_869427` (`name`, `type_db_alias`)",
|
||||
"ALTER TABLE `product` ALTER COLUMN `view_num` SET DEFAULT 0",
|
||||
"ALTER TABLE `user` DROP COLUMN `avatar`",
|
||||
"ALTER TABLE `user` MODIFY COLUMN `password` VARCHAR(100) NOT NULL",
|
||||
@@ -817,9 +823,9 @@ def test_migrate(mocker: MockerFixture):
|
||||
"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 `product` DROP INDEX `idx_product_name_869427`",
|
||||
"ALTER TABLE `email` DROP INDEX `idx_email_email_4a1a33`",
|
||||
"ALTER TABLE `product` DROP INDEX `uid_product_name_f14935`",
|
||||
"ALTER TABLE `product` DROP INDEX `uid_product_name_869427`",
|
||||
"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`",
|
||||
@@ -837,41 +843,41 @@ def test_migrate(mocker: MockerFixture):
|
||||
'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 "configs" RENAME TO "config"',
|
||||
'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 "user" DROP COLUMN "avatar"',
|
||||
'ALTER TABLE "product" RENAME COLUMN "image" TO "pic"',
|
||||
'ALTER TABLE "user" ALTER COLUMN "password" TYPE VARCHAR(100) USING "password"::VARCHAR(100)',
|
||||
'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_user_usernam_9987ab" ON "user" ("username")',
|
||||
'ALTER TABLE "user" DROP COLUMN "avatar"',
|
||||
'CREATE INDEX "idx_product_name_869427" ON "product" ("name", "type_db_alias")',
|
||||
'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_869427" ON "product" ("name", "type_db_alias")',
|
||||
'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 "user" ALTER COLUMN "password" TYPE VARCHAR(200) USING "password"::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 "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',
|
||||
'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_product_name_869427"',
|
||||
'DROP INDEX "idx_email_email_4a1a33"',
|
||||
'DROP INDEX "idx_user_usernam_9987ab"',
|
||||
'DROP INDEX "uid_product_name_f14935"',
|
||||
'DROP INDEX "uid_product_name_869427"',
|
||||
'DROP TABLE IF EXISTS "email_user"',
|
||||
'DROP TABLE IF EXISTS "newmodel"',
|
||||
]
|
||||
|
Reference in New Issue
Block a user