카테고리 관련 DB 스키마/코드를 Kysely 기반으로 마이그레이션

This commit is contained in:
static
2025-01-21 10:57:32 +09:00
parent 698d2455ff
commit 2a2d01b50e
8 changed files with 281 additions and 187 deletions

View File

@@ -0,0 +1,65 @@
import { Kysely } from "kysely";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const up = async (db: Kysely<any>) => {
// category.ts
await db.schema
.createTable("category")
.addColumn("id", "integer", (col) => col.primaryKey().generatedAlwaysAsIdentity())
.addColumn("parent_id", "integer", (col) => col.references("category.id").onDelete("cascade"))
.addColumn("user_id", "integer", (col) => col.references("user.id").notNull())
.addColumn("master_encryption_key_version", "integer", (col) => col.notNull())
.addColumn("encrypted_data_encryption_key", "text", (col) => col.unique().notNull())
.addColumn("data_encryption_key_version", "timestamp(3)", (col) => col.notNull())
.addColumn("encrypted_name", "json", (col) => col.notNull())
.addForeignKeyConstraint(
"category_fk01",
["user_id", "master_encryption_key_version"],
"master_encryption_key",
["user_id", "version"],
)
.execute();
await db.schema
.createTable("category_log")
.addColumn("id", "integer", (col) => col.primaryKey().generatedAlwaysAsIdentity())
.addColumn("category_id", "integer", (col) =>
col.references("category.id").onDelete("cascade").notNull(),
)
.addColumn("timestamp", "timestamp(3)", (col) => col.notNull())
.addColumn("action", "text", (col) => col.notNull())
.addColumn("new_name", "json")
.execute();
// file.ts
await db.schema
.alterTable("file_log")
.addColumn("category_id", "integer", (col) =>
col.references("category.id").onDelete("set null"),
)
.execute();
await db.schema
.createTable("file_category")
.addColumn("file_id", "integer", (col) =>
col.references("file.id").onDelete("cascade").notNull(),
)
.addColumn("category_id", "integer", (col) =>
col.references("category.id").onDelete("cascade").notNull(),
)
.addPrimaryKeyConstraint("file_category_pk", ["file_id", "category_id"])
.execute();
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const down = async (db: Kysely<any>) => {
await db
.deleteFrom("file_log")
.where((eb) =>
eb.or([eb("action", "=", "add-to-category"), eb("action", "=", "remove-from-category")]),
)
.execute();
await db.schema.dropTable("file_category").execute();
await db.schema.alterTable("file_log").dropColumn("category_id").execute();
await db.schema.dropTable("category_log").execute();
await db.schema.dropTable("category").execute();
};

View File

@@ -1,5 +1,7 @@
import * as Initial1737357000 from "./1737357000-Initial";
import * as AddFileCategory1737422340 from "./1737422340-AddFileCategory";
export default {
"1737357000-Initial": Initial1737357000,
"1737422340-AddFileCategory": AddFileCategory1737422340,
};