-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply.ts
More file actions
108 lines (98 loc) · 3.14 KB
/
Copy pathapply.ts
File metadata and controls
108 lines (98 loc) · 3.14 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import "dotenv/config";
import { pool } from "./server/db.ts";
async function main() {
try {
console.log("Applying missing tables...");
await pool.query(`
CREATE TABLE IF NOT EXISTS "api_usage_log" (
"id" varchar PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"repository_id" varchar,
"provider" text NOT NULL,
"model" text NOT NULL,
"tokens_in" integer DEFAULT 0 NOT NULL,
"tokens_out" integer DEFAULT 0 NOT NULL,
"cost_usd" text DEFAULT '0' NOT NULL,
"latency_ms" integer DEFAULT 0 NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL
);
`);
await pool.query(`
CREATE TABLE IF NOT EXISTS "audits" (
"id" varchar PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"repository_url" text NOT NULL,
"branch" text NOT NULL,
"framework" text DEFAULT 'asvs-5.0' NOT NULL,
"status" text DEFAULT 'pending' NOT NULL,
"started_at" timestamp DEFAULT now(),
"completed_at" timestamp,
"report_id" varchar,
"user_id" varchar
);
`);
await pool.query(`
CREATE TABLE IF NOT EXISTS "audit_reports" (
"id" varchar PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"audit_id" varchar NOT NULL,
"report_json" jsonb NOT NULL,
"report_hash" text,
"signature" text,
"pdf_path" text,
"created_at" timestamp DEFAULT now()
);
`);
await pool.query(`
CREATE TABLE IF NOT EXISTS "audit_orders" (
"id" varchar PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"audit_id" varchar NOT NULL,
"user_id" varchar NOT NULL,
"tier_id" text NOT NULL,
"price_usd" integer NOT NULL,
"status" text DEFAULT 'pending_payment' NOT NULL,
"created_at" timestamp DEFAULT now(),
"updated_at" timestamp DEFAULT now()
);
`);
await pool.query(`
ALTER TABLE "users" ADD COLUMN IF NOT EXISTS "role" text DEFAULT 'user' NOT NULL;
`);
await pool.query(`
CREATE TABLE IF NOT EXISTS "request_logs" (
"id" serial PRIMARY KEY NOT NULL,
"user_id" varchar,
"session_id" text,
"method" text NOT NULL,
"path" text NOT NULL,
"status_code" integer NOT NULL,
"response_time_ms" integer NOT NULL,
"ip_address" varchar,
"user_agent" text,
"device" text,
"browser" text,
"os" text,
"geo_country" text,
"geo_region" text,
"geo_city" text,
"geo_lat" text,
"geo_lng" text,
"timestamp" timestamp DEFAULT now() NOT NULL
);
`);
await pool.query(`
CREATE TABLE IF NOT EXISTS "admin_action_log" (
"id" serial PRIMARY KEY NOT NULL,
"admin_user_id" varchar NOT NULL,
"action_type" text NOT NULL,
"target_id" text,
"before_state" jsonb,
"after_state" jsonb,
"timestamp" timestamp DEFAULT now() NOT NULL
);
`);
console.log("Migration applied successfully.");
process.exit(0);
} catch (error) {
console.error("Migration failed:", error);
process.exit(1);
}
}
main();