-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy path23-polls-awnsers.ts
More file actions
41 lines (36 loc) · 1.26 KB
/
23-polls-awnsers.ts
File metadata and controls
41 lines (36 loc) · 1.26 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
import { sql, type Kysely } from "kysely";
export async function up(db: Kysely<any>) {
await db.schema
.createTable("pollAnswers")
.addColumn("id", "integer", c => c.primaryKey().autoIncrement())
.addColumn("optionId", "integer", c =>
c.references("pollOptions.id").notNull().onDelete("cascade"),
)
.addColumn("userId", "text", c => c.notNull())
.addColumn("createdAt", "timestamp", c => c.notNull().defaultTo(sql`current_timestamp`))
.addColumn("updatedAt", "timestamp", c => c.notNull().defaultTo(sql`current_timestamp`))
.execute();
await db.schema
.createIndex("pollAnswers_optionId_userId_index")
.on("pollAnswers")
.columns(["optionId", "userId"])
.unique()
.execute();
await createUpdatedAtTrigger(db, "pollAnswers");
}
function createUpdatedAtTrigger(db: Kysely<any>, tableName: string) {
return sql
.raw(`
create trigger ${tableName}_updatedAt
after update on ${tableName} for each row
begin
update ${tableName}
set updatedAt = current_timestamp
where id = old.id;
end;
`)
.execute(db);
}
export async function down(_db: Kysely<any>) {
throw new Error("Not supported lol");
}