diff --git a/apps/backend/src/migrations/2025-08-17_14-54-18_add_refresh_token.ts b/apps/backend/src/migrations/2025-08-17_14-54-18_add_refresh_token.ts new file mode 100644 index 0000000..360c869 --- /dev/null +++ b/apps/backend/src/migrations/2025-08-17_14-54-18_add_refresh_token.ts @@ -0,0 +1,33 @@ +import { Kysely, sql } from 'kysely'; + +export async function up(db: Kysely): Promise { + await db.schema + .createTable('refresh_tokens') + .addColumn('id', 'bigserial', (col) => col.primaryKey()) + .addColumn('user_id', 'bigserial', (col) => col.notNull()) + .addForeignKeyConstraint( + 'fk_user_id_refresh_tokens_users', + ['user_id'], + 'users', + ['id'], + (col) => col.onDelete('cascade'), + ) + .addColumn('token', 'text', (col) => col.notNull()) + .addUniqueConstraint('uniq_user_id_tokens_refresh_tokens', [ + 'user_id', + 'token', + ]) + .addColumn('valid_until', 'timestamptz', (col) => col.notNull()) + .addColumn('created_at', 'timestamptz', (col) => + col.notNull().defaultTo(sql`now()`), + ) + .addColumn('updated_at', 'timestamptz', (col) => + col.notNull().defaultTo(sql`now()`), + ) + .addColumn('deleted_at', 'timestamptz') + .execute(); +} + +export async function down(db: Kysely): Promise { + await db.schema.dropTable('refresh_tokens').execute(); +}