orm: Refactor INDEX generation
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
826c6f73f1
commit
13ca40eaf7
|
@ -25,6 +25,7 @@ Features:
|
|||
import dataclasses
|
||||
import datetime
|
||||
import json
|
||||
import textwrap
|
||||
import typing
|
||||
|
||||
import psycopg
|
||||
|
@ -154,15 +155,18 @@ class BaseModel:
|
|||
Returns SQL code suitable to initialize a table to store instances
|
||||
of this class.
|
||||
"""
|
||||
return "\n".join(
|
||||
[
|
||||
f"CREATE TABLE IF NOT EXISTS {cls.TABLE} (",
|
||||
",\n".join(
|
||||
f" {field.name} {_type_to_sql(field.type)}"
|
||||
for field in dataclasses.fields(cls)
|
||||
),
|
||||
");",
|
||||
f"CREATE UNIQUE INDEX IF NOT EXISTS {cls.TABLE}_pk ON {cls.TABLE} "
|
||||
f"({', '.join(cls.PK)});",
|
||||
]
|
||||
|
||||
cols = ",\n ".join(
|
||||
f"{field.name} {_type_to_sql(field.type)}"
|
||||
for field in dataclasses.fields(cls)
|
||||
)
|
||||
return textwrap.dedent(
|
||||
f"""\
|
||||
CREATE TABLE IF NOT EXISTS {cls.TABLE} (
|
||||
{cols}
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS {cls.TABLE}_pk ON {cls.TABLE} (
|
||||
{', '.join(cls.PK)}
|
||||
);
|
||||
"""
|
||||
)
|
||||
|
|
|
@ -34,5 +34,8 @@ def test_db_schema():
|
|||
response_headers jsonb NOT NULL,
|
||||
content bytea NOT NULL
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS web_page_snapshot_pk ON web_page_snapshot (url, snapshot_date);""" # noqa
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS web_page_snapshot_pk ON web_page_snapshot (
|
||||
url, snapshot_date
|
||||
);
|
||||
"""
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue