import type { Kysely } from 'kysely'; import { sql } from 'kysely'; import type { Database } from '../types.js'; export async function up(db: Kysely): Promise { // Create person table await db.schema .createTable('person') .addColumn('id', 'serial', (col) => col.primaryKey()) .addColumn('first_name', 'varchar(255)', (col) => col.notNull()) .addColumn('last_name', 'varchar(255)') .addColumn('gender', 'varchar(10)', (col) => col.notNull().check(sql`gender IN ('man', 'woman', 'other')`) ) .addColumn('created_at', 'timestamp', (col) => col.defaultTo(sql`CURRENT_TIMESTAMP`).notNull() ) .addColumn('metadata', 'jsonb', (col) => col.notNull()) .execute(); // Create pet table await db.schema .createTable('pet') .addColumn('id', 'serial', (col) => col.primaryKey()) .addColumn('name', 'varchar(255)', (col) => col.notNull()) .addColumn('owner_id', 'integer', (col) => col.notNull().references('person.id').onDelete('cascade') ) .addColumn('species', 'varchar(10)', (col) => col.notNull().check(sql`species IN ('dog', 'cat')`) ) .execute(); // Create index on pet.owner_id for better query performance await db.schema.createIndex('pet_owner_id_index').on('pet').column('owner_id').execute(); } export async function down(db: Kysely): Promise { // Drop tables in reverse order (drop dependent tables first) await db.schema.dropTable('pet').execute(); await db.schema.dropTable('person').execute(); }