Files
linux-odyssey/packages/database/migrations/app/20261011T0821_init_schema/migration.ts
T
2026-10-11 19:30:38 +11:00

269 lines
9.8 KiB
JavaScript
Executable File

#!/usr/bin/env -S node
import type { Contract as End } from '../../snapshots/6fecb00f7a40489f8c1a9a4dad506cc6d4b95789cbb32519634132f006530c04/contract';
import endContract from '../../snapshots/6fecb00f7a40489f8c1a9a4dad506cc6d4b95789cbb32519634132f006530c04/contract.json' with { type: 'json' };
import {
Migration,
MigrationCLI,
checkExpression,
col,
primaryKey,
} from '@prisma/orm-postgres/migration';
export default class M extends Migration<never, End> {
override readonly endContractJson = endContract;
override get operations() {
return [
this.createSchema({ schema: 'public' }),
this.createTable({
schema: 'public',
table: 'Command',
columns: [
col('command', 'text', { notNull: true, codecRef: { codecId: 'pg/text@1' } }),
col('error', 'text', { codecRef: { codecId: 'pg/text@1' } }),
col('id', 'BIGSERIAL', { notNull: true, codecRef: { codecId: 'pg/int8@1' } }),
col('output', 'text', { codecRef: { codecId: 'pg/text@1' } }),
col('pwd', 'text', { notNull: true, codecRef: { codecId: 'pg/text@1' } }),
col('sessionId', 'int8', { notNull: true, codecRef: { codecId: 'pg/int8@1' } }),
col('stage', 'text', { codecRef: { codecId: 'pg/text@1' } }),
col('timestamp', 'timestamptz', {
notNull: true,
codecRef: { codecId: 'pg/timestamptz-temporal@1' },
}),
],
constraints: [primaryKey(['id'])],
}),
this.createTable({
schema: 'public',
table: 'LoginAttempt',
columns: [
col('id', 'BIGSERIAL', { notNull: true, codecRef: { codecId: 'pg/int8@1' } }),
col('ip', 'text', { notNull: true, codecRef: { codecId: 'pg/text@1' } }),
col('loginMethod', 'text', { notNull: true, codecRef: { codecId: 'pg/text@1' } }),
col('message', 'text', { codecRef: { codecId: 'pg/text@1' } }),
col('success', 'bool', { notNull: true, codecRef: { codecId: 'pg/bool@1' } }),
col('time', 'timestamptz', {
notNull: true,
codecRef: { codecId: 'pg/timestamptz-temporal@1' },
}),
col('userAgent', 'text', { notNull: true, codecRef: { codecId: 'pg/text@1' } }),
col('userId', 'int8', { codecRef: { codecId: 'pg/int8@1' } }),
col('username', 'text', { notNull: true, codecRef: { codecId: 'pg/text@1' } }),
],
constraints: [
primaryKey(['id']),
checkExpression(
'LoginAttempt_loginMethod_check_f8485f11',
`"loginMethod" IN ('password', 'google', 'github')`,
),
],
}),
this.createTable({
schema: 'public',
table: 'Session',
columns: [
col('completedEvents', 'text[]', {
notNull: true,
codecRef: { codecId: 'pg/text@1', many: true },
}),
col('containerId', 'text', { codecRef: { codecId: 'pg/text@1' } }),
col('containerName', 'text', { codecRef: { codecId: 'pg/text@1' } }),
col('createdAt', 'timestamptz', {
notNull: true,
codecRef: { codecId: 'pg/timestamptz-temporal@1' },
}),
col('finishedAt', 'timestamptz', { codecRef: { codecId: 'pg/timestamptz-temporal@1' } }),
col('graph', 'jsonb', { notNull: true, codecRef: { codecId: 'pg/jsonb@1' } }),
col('id', 'BIGSERIAL', { notNull: true, codecRef: { codecId: 'pg/int8@1' } }),
col('lastActivityAt', 'timestamptz', {
notNull: true,
codecRef: { codecId: 'pg/timestamptz-temporal@1' },
}),
col('locale', 'text', { notNull: true, codecRef: { codecId: 'pg/text@1' } }),
col('quest', 'text', { notNull: true, codecRef: { codecId: 'pg/text@1' } }),
col('status', 'text', { notNull: true, codecRef: { codecId: 'pg/text@1' } }),
col('userId', 'int8', { notNull: true, codecRef: { codecId: 'pg/int8@1' } }),
],
constraints: [
primaryKey(['id']),
checkExpression(
'Session_completedEvents_elem_not_null_b55d23f4',
'array_position("completedEvents", NULL) IS NULL',
),
checkExpression(
'Session_status_check_8fa5bbc6',
`"status" IN ('active', 'finished', 'inactive')`,
),
],
}),
this.createTable({
schema: 'public',
table: 'User',
columns: [
col('createdAt', 'timestamptz', {
notNull: true,
codecRef: { codecId: 'pg/timestamptz-temporal@1' },
}),
col('email', 'text', { notNull: true, codecRef: { codecId: 'pg/text@1' } }),
col('id', 'BIGSERIAL', { notNull: true, codecRef: { codecId: 'pg/int8@1' } }),
col('isGuest', 'bool', { notNull: true, codecRef: { codecId: 'pg/bool@1' } }),
col('name', 'text', { codecRef: { codecId: 'pg/text@1' } }),
col('updatedAt', 'timestamptz', {
notNull: true,
codecRef: { codecId: 'pg/timestamptz-temporal@1' },
}),
col('username', 'text', { notNull: true, codecRef: { codecId: 'pg/text@1' } }),
],
constraints: [primaryKey(['id'])],
}),
this.createTable({
schema: 'public',
table: 'UserLoginMethod',
columns: [
col('createdAt', 'timestamptz', {
notNull: true,
codecRef: { codecId: 'pg/timestamptz-temporal@1' },
}),
col('displayName', 'text', { codecRef: { codecId: 'pg/text@1' } }),
col('hashedPassword', 'text', { codecRef: { codecId: 'pg/text@1' } }),
col('oauthId', 'text', { codecRef: { codecId: 'pg/text@1' } }),
col('type', 'text', { notNull: true, codecRef: { codecId: 'pg/text@1' } }),
col('updatedAt', 'timestamptz', {
notNull: true,
codecRef: { codecId: 'pg/timestamptz-temporal@1' },
}),
col('userId', 'int8', { notNull: true, codecRef: { codecId: 'pg/int8@1' } }),
],
constraints: [
primaryKey(['userId', 'type']),
checkExpression(
'UserLoginMethod_type_check_6814314f',
`"type" IN ('password', 'google', 'github')`,
),
],
}),
this.createTable({
schema: 'public',
table: 'UserProgress',
columns: [
col('finishedAt', 'timestamptz', { codecRef: { codecId: 'pg/timestamptz-temporal@1' } }),
col('quest', 'text', { notNull: true, codecRef: { codecId: 'pg/text@1' } }),
col('startedAt', 'timestamptz', {
notNull: true,
codecRef: { codecId: 'pg/timestamptz-temporal@1' },
}),
col('userId', 'int8', { notNull: true, codecRef: { codecId: 'pg/int8@1' } }),
],
constraints: [primaryKey(['userId', 'quest'])],
}),
this.addUnique({
schema: 'public',
table: 'User',
constraint: 'User_email_key',
columns: ['email'],
}),
this.addUnique({
schema: 'public',
table: 'User',
constraint: 'User_username_key',
columns: ['username'],
}),
this.addUnique({
schema: 'public',
table: 'UserLoginMethod',
constraint: 'UserLoginMethod_type_oauthId_key',
columns: ['type', 'oauthId'],
}),
this.createIndex({
schema: 'public',
table: 'Command',
index: 'Command_sessionId_idx_29f415d4',
columns: ['sessionId'],
}),
this.createIndex({
schema: 'public',
table: 'LoginAttempt',
index: 'LoginAttempt_userId_idx_a489d58a',
columns: ['userId'],
}),
this.createIndex({
schema: 'public',
table: 'Session',
index: 'Session_userId_idx_a489d58a',
columns: ['userId'],
}),
this.createIndex({
schema: 'public',
table: 'Session',
index: 'Session_userId_quest_idx_b13626bf',
columns: ['userId', 'quest'],
}),
this.createIndex({
schema: 'public',
table: 'Session',
index: 'Session_userId_status_idx_e4a128ba',
columns: ['userId', 'status'],
}),
this.createIndex({
schema: 'public',
table: 'UserLoginMethod',
index: 'UserLoginMethod_userId_idx_a489d58a',
columns: ['userId'],
}),
this.createIndex({
schema: 'public',
table: 'UserProgress',
index: 'UserProgress_userId_idx_a489d58a',
columns: ['userId'],
}),
this.addForeignKey({
schema: 'public',
table: 'Command',
foreignKey: {
name: 'Command_sessionId_fkey',
columns: ['sessionId'],
references: { schema: 'public', table: 'Session', columns: ['id'] },
},
}),
this.addForeignKey({
schema: 'public',
table: 'LoginAttempt',
foreignKey: {
name: 'LoginAttempt_userId_fkey',
columns: ['userId'],
references: { schema: 'public', table: 'User', columns: ['id'] },
},
}),
this.addForeignKey({
schema: 'public',
table: 'Session',
foreignKey: {
name: 'Session_userId_fkey',
columns: ['userId'],
references: { schema: 'public', table: 'User', columns: ['id'] },
},
}),
this.addForeignKey({
schema: 'public',
table: 'UserLoginMethod',
foreignKey: {
name: 'UserLoginMethod_userId_fkey',
columns: ['userId'],
references: { schema: 'public', table: 'User', columns: ['id'] },
},
}),
this.addForeignKey({
schema: 'public',
table: 'UserProgress',
foreignKey: {
name: 'UserProgress_userId_fkey',
columns: ['userId'],
references: { schema: 'public', table: 'User', columns: ['id'] },
},
}),
];
}
}
MigrationCLI.run(import.meta.url, M);