Skip to content

Commit

Permalink
setup migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
amalshaji committed Mar 25, 2024
1 parent 16562f9 commit 7a7acc3
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 4 deletions.
11 changes: 11 additions & 0 deletions admin/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
init-migrations:
aerich init -t src.portr_admin.db.TORTOISE_ORM

init-db:
aerich init-db

create-migrations:
aerich migrate --name $(name)

run-migrations:
aerich upgrade
88 changes: 88 additions & 0 deletions admin/migrations/models/0_20240325204945_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from tortoise import BaseDBAsyncClient


async def upgrade(db: BaseDBAsyncClient) -> str:
return """
CREATE TABLE IF NOT EXISTS "aerich" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"version" VARCHAR(255) NOT NULL,
"app" VARCHAR(100) NOT NULL,
"content" JSON NOT NULL
);
CREATE TABLE IF NOT EXISTS "user" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
"email" VARCHAR(255) NOT NULL UNIQUE,
"first_name" VARCHAR(255),
"last_name" VARCHAR(255),
"is_superuser" INT NOT NULL DEFAULT 0
);
CREATE TABLE IF NOT EXISTS "session" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
"token" VARCHAR(255) NOT NULL UNIQUE,
"expires_at" TIMESTAMP NOT NULL,
"user_id" INT NOT NULL REFERENCES "user" ("id") ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS "idx_session_expires_823c67" ON "session" ("expires_at");
CREATE TABLE IF NOT EXISTS "githubuser" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"github_id" BIGINT NOT NULL UNIQUE,
"github_access_token" VARCHAR(255) NOT NULL,
"github_avatar_url" VARCHAR(255) NOT NULL,
"user_id" INT NOT NULL UNIQUE REFERENCES "user" ("id") ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS "idx_githubuser_github__f7df59" ON "githubuser" ("github_id");
CREATE TABLE IF NOT EXISTS "team" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
"name" VARCHAR(255) NOT NULL UNIQUE,
"slug" VARCHAR(255) NOT NULL UNIQUE
);
CREATE INDEX IF NOT EXISTS "idx_team_slug_b2d3a8" ON "team" ("slug");
CREATE TABLE IF NOT EXISTS "team_users" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
"secret_key" VARCHAR(42) NOT NULL UNIQUE,
"role" VARCHAR(255) NOT NULL DEFAULT 'member',
"team_id" INT NOT NULL REFERENCES "team" ("id") ON DELETE CASCADE,
"user_id" INT NOT NULL REFERENCES "user" ("id") ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS "idx_team_users_secret__22c341" ON "team_users" ("secret_key");
CREATE TABLE IF NOT EXISTS "instancesettings" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
"smtp_enabled" INT NOT NULL DEFAULT 0,
"smtp_host" VARCHAR(255),
"smtp_port" INT,
"smtp_username" VARCHAR(255),
"smtp_password" BLOB,
"from_address" VARCHAR(255),
"add_user_email_subject" VARCHAR(255),
"add_user_email_body" TEXT,
"updated_by_id" INT REFERENCES "user" ("id") ON DELETE SET NULL
);
CREATE TABLE IF NOT EXISTS "connection" (
"created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
"id" VARCHAR(26) NOT NULL PRIMARY KEY,
"type" VARCHAR(255) NOT NULL,
"subdomain" VARCHAR(255),
"port" INT,
"status" VARCHAR(255) NOT NULL DEFAULT 'reserved',
"started_at" TIMESTAMP,
"closed_at" TIMESTAMP,
"created_by_id" INT NOT NULL REFERENCES "team_users" ("id") ON DELETE CASCADE,
"team_id" INT NOT NULL REFERENCES "team" ("id") ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS "idx_connection_status_7d03b9" ON "connection" ("status");"""


async def downgrade(db: BaseDBAsyncClient) -> str:
return """
"""
6 changes: 6 additions & 0 deletions admin/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies = [
"email-validator>=2.1.0.post1",
"aiosmtplib>=3.0.1",
"cryptography>=42.0.5",
"aerich>=0.7.2",
]
readme = "README.md"
requires-python = ">= 3.8"
Expand Down Expand Up @@ -46,3 +47,8 @@ allow-direct-references = true

[tool.hatch.build.targets.wheel]
packages = ["src/portr_admin"]

[tool.aerich]
tortoise_orm = "src.portr_admin.db.TORTOISE_ORM"
location = "./migrations"
src_folder = "./."
9 changes: 9 additions & 0 deletions admin/requirements-dev.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
# with-sources: false

-e file:.
aerich==0.7.2
# via portr-admin
aiosmtplib==3.0.1
# via portr-admin
aiosqlite==0.17.0
Expand All @@ -31,9 +33,12 @@ cffi==1.16.0
cfgv==3.4.0
# via pre-commit
click==8.1.7
# via aerich
# via uvicorn
cryptography==42.0.5
# via portr-admin
dictdiffer==0.9.0
# via aerich
distlib==0.3.8
# via virtualenv
dnspython==2.6.1
Expand Down Expand Up @@ -84,6 +89,7 @@ pre-commit==3.6.2
pycparser==2.21
# via cffi
pydantic==2.6.1
# via aerich
# via fastapi
# via portr-admin
# via pydantic-settings
Expand Down Expand Up @@ -122,7 +128,10 @@ starlette==0.36.3
# via fastapi
text-unidecode==1.3
# via python-slugify
tomlkit==0.12.4
# via aerich
tortoise-orm==0.20.0
# via aerich
# via portr-admin
typing-extensions==4.9.0
# via aiosqlite
Expand Down
9 changes: 9 additions & 0 deletions admin/requirements.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
# with-sources: false

-e file:.
aerich==0.7.2
# via portr-admin
aiosmtplib==3.0.1
# via portr-admin
aiosqlite==0.17.0
Expand All @@ -27,9 +29,12 @@ certifi==2024.2.2
cffi==1.16.0
# via cryptography
click==8.1.7
# via aerich
# via uvicorn
cryptography==42.0.5
# via portr-admin
dictdiffer==0.9.0
# via aerich
dnspython==2.6.1
# via email-validator
email-validator==2.1.0.post1
Expand Down Expand Up @@ -58,6 +63,7 @@ nanoid==2.0.0
pycparser==2.21
# via cffi
pydantic==2.6.1
# via aerich
# via fastapi
# via portr-admin
# via pydantic-settings
Expand Down Expand Up @@ -85,7 +91,10 @@ starlette==0.36.3
# via fastapi
text-unidecode==1.3
# via python-slugify
tomlkit==0.12.4
# via aerich
tortoise-orm==0.20.0
# via aerich
# via portr-admin
typing-extensions==4.9.0
# via aiosqlite
Expand Down
21 changes: 17 additions & 4 deletions admin/scripts/pre-deploy.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import asyncio
from aerich import Command # type: ignore


from portr_admin.db import connect_db, disconnect_db
from portr_admin.db import TORTOISE_ORM, connect_db, disconnect_db
from portr_admin.services.settings import populate_instance_settings


async def main():
await connect_db(generate_schemas=True)
command = Command(tortoise_config=TORTOISE_ORM)


async def run_migrations():
await command.init()
await command.upgrade(run_in_transaction=True)


async def populate_settings():
await connect_db()
await populate_instance_settings()
await disconnect_db()


async def main():
await run_migrations()
await populate_settings()


asyncio.run(main())
11 changes: 11 additions & 0 deletions admin/src/portr_admin/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@
from portr_admin.config import settings

TORTOISE_MODELS = [
"aerich.models",
"portr_admin.models.auth",
"portr_admin.models.user",
"portr_admin.models.settings",
"portr_admin.models.connection",
]

TORTOISE_ORM = {
"connections": {"default": settings.db_url},
"apps": {
"models": {
"models": TORTOISE_MODELS,
"default_connection": "default",
},
},
}


async def connect_db(generate_schemas: bool = False):
await Tortoise.init(
Expand Down

0 comments on commit 7a7acc3

Please sign in to comment.