mirror of
https://github.com/docmost/docmost.git
synced 2026-08-28 17:27:06 +08:00
55 lines
1.7 KiB
TypeScript
55 lines
1.7 KiB
TypeScript
import { Kysely, sql } from 'kysely';
|
|
|
|
export async function up(db: Kysely<any>): Promise<void> {
|
|
await db.schema
|
|
.createTable('page_analytics')
|
|
.ifNotExists()
|
|
.addColumn('id', 'uuid', (col) =>
|
|
col.primaryKey().defaultTo(sql`gen_uuid_v7()`),
|
|
)
|
|
.addColumn('workspace_id', 'uuid', (col) =>
|
|
col.references('workspaces.id').onDelete('cascade').notNull(),
|
|
)
|
|
.addColumn('page_id', 'uuid', (col) =>
|
|
col.references('pages.id').onDelete('cascade').notNull(),
|
|
)
|
|
.addColumn('space_id', 'uuid', (col) =>
|
|
col.references('spaces.id').onDelete('cascade'),
|
|
)
|
|
.addColumn('user_id', 'uuid', (col) =>
|
|
col.references('users.id').onDelete('set null'),
|
|
)
|
|
.addColumn('share_id', 'uuid')
|
|
.addColumn('visitor_id', 'varchar', (col) => col.notNull())
|
|
.addColumn('view_date', 'varchar', (col) => col.notNull())
|
|
.addColumn('hits', 'int8', (col) => col.notNull().defaultTo(1))
|
|
.addColumn('last_viewed_at', 'timestamptz', (col) =>
|
|
col.notNull().defaultTo(sql`now()`),
|
|
)
|
|
.addColumn('created_at', 'timestamptz', (col) =>
|
|
col.notNull().defaultTo(sql`now()`),
|
|
)
|
|
.execute();
|
|
|
|
await db.schema
|
|
.createIndex('idx_page_analytics_workspace_page_date')
|
|
.ifNotExists()
|
|
.on('page_analytics')
|
|
.columns(['workspace_id', 'page_id', 'view_date'])
|
|
.execute();
|
|
|
|
await sql`
|
|
CREATE UNIQUE INDEX IF NOT EXISTS
|
|
uq_page_analytics_workspace_page_identity
|
|
ON page_analytics (
|
|
workspace_id,
|
|
page_id,
|
|
COALESCE(user_id::text, visitor_id)
|
|
)
|
|
`.execute(db);
|
|
}
|
|
|
|
export async function down(db: Kysely<any>): Promise<void> {
|
|
await db.schema.dropTable('page_analytics').ifExists().execute();
|
|
}
|