-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcreate-verification-table.ts
More file actions
72 lines (64 loc) · 1.96 KB
/
create-verification-table.ts
File metadata and controls
72 lines (64 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env node
import 'dotenv/config'
import { captureError } from '@repo/error/node'
import { logger } from '@repo/utils/logger/server'
import { Pool } from 'pg'
import { env } from '../src/lib/env.js'
const pool = new Pool({ connectionString: env.DATABASE_URL })
async function createVerificationTable() {
try {
// Check if table exists
const checkResult = await pool.query(`
SELECT EXISTS (
SELECT FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = 'verification'
)
`)
if (checkResult.rows[0]?.exists) {
logger.info('Verification table already exists')
await pool.end()
return
}
// Create the table
await pool.query(`
CREATE TABLE "verification" (
"id" text PRIMARY KEY NOT NULL,
"identifier" text NOT NULL,
"value" text NOT NULL,
"expires_at" timestamp NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL
);
`)
// Create indexes
await pool.query(`
CREATE INDEX "verification_identifier_idx" ON "verification" USING btree ("identifier");
`)
await pool.query(`
CREATE INDEX "verification_expires_at_idx" ON "verification" USING btree ("expires_at");
`)
logger.info('Verification table created successfully')
} catch (error) {
captureError({
code: 'INTERNAL_ERROR',
error: error instanceof Error ? error : new Error(String(error)),
logger,
label: 'createVerificationTable failed',
tags: { app: 'api', module: 'db-migration' },
})
throw error
} finally {
await pool.end()
}
}
createVerificationTable().catch(error => {
captureError({
code: 'INTERNAL_ERROR',
error: error instanceof Error ? error : new Error(String(error)),
logger,
label: 'createVerificationTable script failed',
tags: { app: 'api', module: 'db-migration' },
})
process.exit(1)
})