Fix duplicated semicolon in table creation DDL
This commit is contained in:
parent
b1b9cc1454
commit
7a826df43f
@ -110,25 +110,22 @@ def write_version_file(version_file: Path, content: Dict):
|
||||
:param content:
|
||||
:return:
|
||||
"""
|
||||
with open(version_file, "w", encoding="utf-8") as f:
|
||||
f.write(_UPGRADE)
|
||||
upgrade = content.get("upgrade")
|
||||
if len(upgrade) > 1:
|
||||
f.write(";\n".join(upgrade))
|
||||
if not upgrade[-1].endswith(";"):
|
||||
f.write(";\n")
|
||||
else:
|
||||
f.write(f"{upgrade[0]}")
|
||||
if not upgrade[0].endswith(";"):
|
||||
f.write(";")
|
||||
f.write("\n")
|
||||
downgrade = content.get("downgrade")
|
||||
if downgrade:
|
||||
f.write(_DOWNGRADE)
|
||||
if len(downgrade) > 1:
|
||||
f.write(";\n".join(downgrade) + ";\n")
|
||||
else:
|
||||
f.write(f"{downgrade[0]};\n")
|
||||
|
||||
def append_ddl(ddl: str):
|
||||
if not ddl.endswith(";"):
|
||||
ddl += ";"
|
||||
ddl += "\n"
|
||||
data.append(ddl)
|
||||
|
||||
data = [_UPGRADE]
|
||||
for upgrade_ddl in content.get("upgrade"):
|
||||
append_ddl(upgrade_ddl)
|
||||
downgrade = content.get("downgrade")
|
||||
if downgrade:
|
||||
data.append(_DOWNGRADE)
|
||||
for downgrade_ddl in downgrade:
|
||||
append_ddl(downgrade_ddl)
|
||||
version_file.write_text("".join(data))
|
||||
|
||||
|
||||
def get_models_describe(app: str) -> Dict:
|
||||
|
34
tests/test_utils.py
Normal file
34
tests/test_utils.py
Normal file
@ -0,0 +1,34 @@
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
from aerich.utils import write_version_file
|
||||
|
||||
|
||||
def test_write_version_file():
|
||||
content = {
|
||||
"upgrade": [
|
||||
"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;",
|
||||
"CREATE TABLE `email_user` (\n `email_id` INT NOT NULL REFERENCES `email` (`email_id`) ON DELETE CASCADE,\n `user_id` INT NOT NULL REFERENCES `user` (`id`) ON DELETE CASCADE\n) CHARACTER SET utf8mb4",
|
||||
],
|
||||
"downgrade": [
|
||||
"DROP TABLE IF EXISTS `email_user`",
|
||||
"DROP TABLE IF EXISTS `newmodel`",
|
||||
],
|
||||
}
|
||||
with tempfile.NamedTemporaryFile(mode="r", delete=True) as f:
|
||||
write_version_file(Path(f.name), content)
|
||||
result = f.read()
|
||||
assert result == (
|
||||
"-- upgrade --\n"
|
||||
"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;\n"
|
||||
"CREATE TABLE `email_user` (\n"
|
||||
" `email_id` INT NOT NULL REFERENCES `email` (`email_id`) ON DELETE CASCADE,\n"
|
||||
" `user_id` INT NOT NULL REFERENCES `user` (`id`) ON DELETE CASCADE\n"
|
||||
") CHARACTER SET utf8mb4;\n"
|
||||
"-- downgrade --\n"
|
||||
"DROP TABLE IF EXISTS `email_user`;\n"
|
||||
"DROP TABLE IF EXISTS `newmodel`;\n"
|
||||
)
|
Loading…
x
Reference in New Issue
Block a user