mirror of
https://github.com/docmost/docmost.git
synced 2025-11-17 12:41:10 +10:00
Kysely - WIP
* create database migration files * kysely codegen * kysely migrate
This commit is contained in:
121
apps/server/src/kysely/migrations/20240324T086600-comments.ts
Normal file
121
apps/server/src/kysely/migrations/20240324T086600-comments.ts
Normal file
@ -0,0 +1,121 @@
|
||||
import { Kysely, sql } from 'kysely';
|
||||
|
||||
export async function up(db: Kysely<any>): Promise<void> {
|
||||
await db.schema
|
||||
.createTable('comments')
|
||||
.addColumn('id', 'uuid', (col) =>
|
||||
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
|
||||
)
|
||||
.addColumn('content', 'jsonb', (col) => col)
|
||||
.addColumn('selection', 'varchar', (col) => col)
|
||||
.addColumn('type', 'varchar', (col) => col)
|
||||
.addColumn('creatorId', 'uuid', (col) => col.notNull())
|
||||
.addColumn('pageId', 'uuid', (col) => col.notNull())
|
||||
.addColumn('parentCommentId', 'uuid', (col) => col)
|
||||
.addColumn('resolvedById', 'uuid', (col) => col)
|
||||
.addColumn('resolvedAt', 'timestamp', (col) => col)
|
||||
.addColumn('spaceId', 'uuid', (col) => col.notNull())
|
||||
.addColumn('workspaceId', 'uuid', (col) => col.notNull())
|
||||
.addColumn('createdAt', 'timestamp', (col) =>
|
||||
col.notNull().defaultTo(sql`now()`),
|
||||
)
|
||||
.addColumn('editedAt', 'timestamp', (col) => col)
|
||||
.addColumn('deletedAt', 'timestamp', (col) => col)
|
||||
.execute();
|
||||
|
||||
// foreign key relations
|
||||
await db.schema
|
||||
.alterTable('comments')
|
||||
.addForeignKeyConstraint(
|
||||
'FK_comments_users_creatorId',
|
||||
['creatorId'],
|
||||
'users',
|
||||
['id'],
|
||||
)
|
||||
.execute();
|
||||
|
||||
await db.schema
|
||||
.alterTable('comments')
|
||||
.addForeignKeyConstraint('FK_comments_pages_pageId', ['pageId'], 'pages', [
|
||||
'id',
|
||||
])
|
||||
.onDelete('cascade')
|
||||
.execute();
|
||||
|
||||
await db.schema
|
||||
.alterTable('comments')
|
||||
.addForeignKeyConstraint(
|
||||
'FK_comments_comments_parentCommentId',
|
||||
['parentCommentId'],
|
||||
'comments',
|
||||
['id'],
|
||||
)
|
||||
.onDelete('cascade')
|
||||
.execute();
|
||||
|
||||
await db.schema
|
||||
.alterTable('comments')
|
||||
.addForeignKeyConstraint(
|
||||
'FK_comments_users_resolvedById',
|
||||
['resolvedById'],
|
||||
'users',
|
||||
['id'],
|
||||
)
|
||||
.execute();
|
||||
|
||||
await db.schema
|
||||
.alterTable('comments')
|
||||
.addForeignKeyConstraint(
|
||||
'FK_comments_spaces_spaceId',
|
||||
['spaceId'],
|
||||
'spaces',
|
||||
['id'],
|
||||
)
|
||||
.onDelete('cascade')
|
||||
.execute();
|
||||
|
||||
await db.schema
|
||||
.alterTable('comments')
|
||||
.addForeignKeyConstraint(
|
||||
'FK_comments_workspaces_workspaceId',
|
||||
['workspaceId'],
|
||||
'workspaces',
|
||||
['id'],
|
||||
)
|
||||
.onDelete('cascade')
|
||||
.execute();
|
||||
}
|
||||
|
||||
export async function down(db: Kysely<any>): Promise<void> {
|
||||
await db.schema
|
||||
.alterTable('comments')
|
||||
.dropConstraint('FK_comments_users_creatorId')
|
||||
.execute();
|
||||
|
||||
await db.schema
|
||||
.alterTable('comments')
|
||||
.dropConstraint('FK_comments_pages_pageId')
|
||||
.execute();
|
||||
|
||||
await db.schema
|
||||
.alterTable('comments')
|
||||
.dropConstraint('FK_comments_comments_parentCommentId')
|
||||
.execute();
|
||||
|
||||
await db.schema
|
||||
.alterTable('comments')
|
||||
.dropConstraint('FK_comments_users_resolvedById')
|
||||
.execute();
|
||||
|
||||
await db.schema
|
||||
.alterTable('comments')
|
||||
.dropConstraint('FK_comments_spaces_spaceId')
|
||||
.execute();
|
||||
|
||||
await db.schema
|
||||
.alterTable('comments')
|
||||
.dropConstraint('FK_comments_workspaces_workspaceId')
|
||||
.execute();
|
||||
|
||||
await db.schema.dropTable('comments').execute();
|
||||
}
|
||||
Reference in New Issue
Block a user