53 lines
2.0 KiB
TypeScript
53 lines
2.0 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))
|
|
.addColumn('signature', 'text', (col) => col.notNull())
|
|
.addPrimaryKeyConstraint('pk_resource_data', [ 'resource_id', 'public_key' ])
|
|
.execute();
|
|
|
|
// Table for authed requests
|
|
// We will store the signature and the timestamp of the request, and we will clear out rows that are older than our msTimeout for our auth
|
|
await db.schema
|
|
.createTable('authed_requests')
|
|
.ifNotExists()
|
|
.addColumn('signature', 'text', (col) => col.notNull())
|
|
.addColumn('timestamp', 'integer', (col) => col.notNull().defaultTo(millisecondTime))
|
|
.addPrimaryKeyConstraint('pk_authed_requests', [ 'signature' ])
|
|
.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();
|
|
|
|
await db.schema.dropTable('authed_requests').ifExists()
|
|
.execute();
|
|
};
|