Files
sync-server-v2/source/services/storage/migrations/001-resources.ts
T
2026-07-27 10:20:12 +00:00

38 lines
1.3 KiB
TypeScript

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<DatabaseTables>): Promise<void> => {
// 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<DatabaseTables>): Promise<void> => {
await db.schema.dropTable('resource_data').ifExists().execute();
};