ionian/packages/server/migrations/initial.ts

76 lines
2.7 KiB
TypeScript
Raw Normal View History

import { Kysely, sql } from 'kysely';
export async function up(db: Kysely<any>) {
await db.schema
.createTable('schema_migration')
.addColumn('version', 'char(16)')
.addColumn('dirty', 'boolean')
.execute();
await db.schema
.createTable('users')
.addColumn('username', 'varchar(256)', col => col.primaryKey())
.addColumn('password_hash', 'varchar(64)', col => col.notNull())
.addColumn('password_salt', 'varchar(64)', col => col.notNull())
.execute();
await db.schema
.createTable('document')
.addColumn('id', 'serial', col => col.primaryKey())
.addColumn('title', 'varchar(512)', col => col.notNull())
.addColumn('content_type', 'varchar(16)', col => col.notNull())
.addColumn('basepath', 'varchar(256)', col => col.notNull())
.addColumn('filename', 'varchar(512)', col => col.notNull())
.addColumn('content_hash', 'varchar')
.addColumn('additional', 'json')
.addColumn("pagenum", "integer", col => col.notNull())
.addColumn('created_at', 'integer', col => col.notNull())
.addColumn('modified_at', 'integer', col => col.notNull())
.addColumn('deleted_at', 'integer')
.execute();
await db.schema
.createTable('tags')
.addColumn('name', 'varchar', col => col.primaryKey())
.addColumn('description', 'text')
.execute();
await db.schema
.createTable('doc_tag_relation')
.addColumn('doc_id', 'integer', col => col.notNull())
.addColumn('tag_name', 'varchar', col => col.notNull())
.addForeignKeyConstraint('doc_id_fk', ['doc_id'], 'document', ['id'])
.addForeignKeyConstraint('tag_name_fk', ['tag_name'], 'tags', ['name'])
.addPrimaryKeyConstraint('doc_tag_relation_pk', ['doc_id', 'tag_name'])
.execute();
await db.schema
.createTable('permissions')
.addColumn('username', 'varchar', col => col.notNull())
.addColumn('name', 'varchar', col => col.notNull())
.addPrimaryKeyConstraint('permissions_pk', ['username', 'name'])
.addForeignKeyConstraint('username_fk', ['username'], 'users', ['username'])
.execute();
// create admin account.
await db
.insertInto('users')
.values({
username: 'admin',
password_hash: 'unchecked',
password_salt: 'unchecked',
})
.execute();
await db
.insertInto('schema_migration')
.values({
version: '0.0.1',
dirty: false,
})
.execute();
}
export async function down(db: Kysely<any>) {
throw new Error('Downward migrations are not supported. Restore from backup.');
}