76 lines
1.6 KiB
TypeScript
76 lines
1.6 KiB
TypeScript
import { Kysely, sql } from "kysely";
|
|
|
|
import type { Database } from "../tables.js";
|
|
|
|
/**
|
|
* Adds invitation tracking to orders and seeds sample catalog items.
|
|
*/
|
|
export async function up(db: Kysely<Database>): Promise<void> {
|
|
await db.schema
|
|
.alterTable("orders")
|
|
.addColumn("invitation_identifier", "text")
|
|
.execute();
|
|
|
|
await db.schema
|
|
.createIndex("idx_orders_invitation_identifier")
|
|
.on("orders")
|
|
.column("invitation_identifier")
|
|
.execute();
|
|
|
|
// Hard-Insert items for demo purposes.
|
|
const seedItems = [
|
|
{
|
|
name: "Cola",
|
|
description: "Classic cola drink",
|
|
price: 1000,
|
|
quantity: 10,
|
|
image: "",
|
|
},
|
|
{
|
|
name: "Chips",
|
|
description: "Salted potato chips",
|
|
price: 1500,
|
|
quantity: 8,
|
|
image: "",
|
|
},
|
|
{
|
|
name: "Water",
|
|
description: "Still spring water",
|
|
price: 800,
|
|
quantity: 15,
|
|
image: "",
|
|
},
|
|
{
|
|
name: "Chocolate Bar",
|
|
description: "Milk chocolate bar",
|
|
price: 1200,
|
|
quantity: 12,
|
|
image: "",
|
|
},
|
|
];
|
|
|
|
for (const item of seedItems) {
|
|
const existing = await db
|
|
.selectFrom("items")
|
|
.select("id")
|
|
.where("name", "=", item.name)
|
|
.executeTakeFirst();
|
|
|
|
if (!existing) {
|
|
await db.insertInto("items").values(item).execute();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Removes invitation column and seed is left in place.
|
|
*/
|
|
export async function down(db: Kysely<Database>): Promise<void> {
|
|
await sql`DROP INDEX IF EXISTS idx_orders_invitation_identifier`.execute(db);
|
|
|
|
await db.schema
|
|
.alterTable("orders")
|
|
.dropColumn("invitation_identifier")
|
|
.execute();
|
|
}
|