Skip to content

Commit

Permalink
Add initial DB migration for PostgreSQL
Browse files Browse the repository at this point in the history
PostgreSQL does not support AUTOINCREMENT. Instead it has the SERIAL type.

To generate the migration `aerich init-db` together with PostgreSQL 16.3
was used.
  • Loading branch information
easybe committed Aug 30, 2024
1 parent 2857da0 commit 1963af2
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
4 changes: 4 additions & 0 deletions goosebit/db/migrations/models/0_20240828095320_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@


async def upgrade(db: BaseDBAsyncClient) -> str:

if db.schema_generator.DIALECT == "postgres":
return ""

return """
CREATE TABLE IF NOT EXISTS "hardware" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Expand Down
75 changes: 75 additions & 0 deletions goosebit/db/migrations/models/0_20240830054046_init_postgres.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from tortoise import BaseDBAsyncClient


async def upgrade(db: BaseDBAsyncClient) -> str:

if db.schema_generator.DIALECT != "postgres":
return ""

return """
CREATE TABLE IF NOT EXISTS "hardware" (
"id" SERIAL NOT NULL PRIMARY KEY,
"model" VARCHAR(255) NOT NULL,
"revision" VARCHAR(255) NOT NULL
);
CREATE TABLE IF NOT EXISTS "software" (
"id" SERIAL NOT NULL PRIMARY KEY,
"uri" VARCHAR(255) NOT NULL,
"size" BIGINT NOT NULL,
"hash" VARCHAR(255) NOT NULL,
"version" VARCHAR(255) NOT NULL
);
CREATE TABLE IF NOT EXISTS "device" (
"uuid" VARCHAR(255) NOT NULL PRIMARY KEY,
"name" VARCHAR(255),
"force_update" BOOL NOT NULL DEFAULT False,
"sw_version" VARCHAR(255),
"feed" VARCHAR(255) NOT NULL DEFAULT 'default',
"update_mode" SMALLINT NOT NULL DEFAULT 3,
"last_state" SMALLINT NOT NULL DEFAULT 1,
"progress" INT,
"log_complete" BOOL NOT NULL DEFAULT False,
"last_log" TEXT,
"last_seen" BIGINT,
"last_ip" VARCHAR(15),
"last_ipv6" VARCHAR(40),
"assigned_software_id" INT REFERENCES "software" ("id") ON DELETE SET NULL,
"hardware_id" INT NOT NULL REFERENCES "hardware" ("id") ON DELETE CASCADE
);
COMMENT ON COLUMN "device"."update_mode" IS 'NONE: 0\nLATEST: 1\nPINNED: 2\nROLLOUT: 3\nASSIGNED: 4';
COMMENT ON COLUMN "device"."last_state" IS 'NONE: 0\nUNKNOWN: 1\nREGISTERED: 2\nRUNNING: 3\nERROR: 4\nFINISHED: 5';
CREATE TABLE IF NOT EXISTS "rollout" (
"id" SERIAL NOT NULL PRIMARY KEY,
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
"name" VARCHAR(255),
"feed" VARCHAR(255) NOT NULL DEFAULT 'default',
"paused" BOOL NOT NULL DEFAULT False,
"success_count" INT NOT NULL DEFAULT 0,
"failure_count" INT NOT NULL DEFAULT 0,
"software_id" INT NOT NULL REFERENCES "software" ("id") ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS "tag" (
"id" SERIAL NOT NULL PRIMARY KEY,
"name" VARCHAR(255) NOT NULL
);
CREATE TABLE IF NOT EXISTS "aerich" (
"id" SERIAL NOT NULL PRIMARY KEY,
"version" VARCHAR(255) NOT NULL,
"app" VARCHAR(100) NOT NULL,
"content" JSONB NOT NULL
);
CREATE TABLE IF NOT EXISTS "software_compatibility" (
"software_id" INT NOT NULL REFERENCES "software" ("id") ON DELETE CASCADE,
"hardware_id" INT NOT NULL REFERENCES "hardware" ("id") ON DELETE CASCADE
);
CREATE UNIQUE INDEX IF NOT EXISTS "uidx_software_co_softwar_2683cc" ON "software_compatibility" ("software_id", "hardware_id");
CREATE TABLE IF NOT EXISTS "device_tags" (
"device_id" VARCHAR(255) NOT NULL REFERENCES "device" ("uuid") ON DELETE CASCADE,
"tag_id" INT NOT NULL REFERENCES "tag" ("id") ON DELETE CASCADE
);
CREATE UNIQUE INDEX IF NOT EXISTS "uidx_device_tags_device__2ab77e" ON "device_tags" ("device_id", "tag_id");"""


async def downgrade(db: BaseDBAsyncClient) -> str:
return """
"""

0 comments on commit 1963af2

Please sign in to comment.