import type { Kysely } from 'kysely'; import { sql } from 'kysely'; import type { DatabaseTables } from '../tables.ts'; /** * Helper for converting the current time to a millisecond timestamp. * * @returns SQLite expression producing the current time in milliseconds. */ const millisecondTime = sql`(CAST(unixepoch('subsec') * 1000 AS INTEGER))`; /** * Creates the resource_data table. * * @param db - Kysely database to apply the migration against. */ export const up = async (db: Kysely): Promise => { // Composite primary key enforces one blob slot per (resource, public key). await db.schema .createTable('resource_data') .ifNotExists() .addColumn('resource_id', 'text', (col) => col.notNull()) .addColumn('public_key', 'text', (col) => col.notNull()) .addColumn('blob', 'blob', (col) => col.notNull()) .addColumn('timestamp', 'integer', (col) => col.notNull().defaultTo(millisecondTime)) .addPrimaryKeyConstraint('pk_resource_data', ['resource_id', 'public_key']) .execute(); }; /** * Drops the resource_data table. * * @param db - Kysely database to apply the rollback against. */ export const down = async (db: Kysely): Promise => { await db.schema.dropTable('resource_data').ifExists().execute(); };