Skip to content

Commit 71f3626

Browse files
🪲 Make add travel files up migration idempotent.
I left this migration in, instead of deleting it, as it is a very good example.
1 parent 5eee5b4 commit 71f3626

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

src/api/data/migrations/003-add-travel-fields-to-application.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,35 @@ import { Knex } from "knex"
22

33
import { DB_CONFIG } from "@/config"
44

5-
export function up(knex: Knex) {
6-
return knex.schema
7-
.withSchema(DB_CONFIG.defaultSchema)
8-
.table("application_draft", (table) => {
5+
export async function up(knex: Knex) {
6+
const schema = knex.schema.withSchema(DB_CONFIG.defaultSchema)
7+
8+
// Check and add column for 'application_draft' table
9+
const hasApplicationIdOnApplicationDraft = await schema.hasColumn(
10+
"application_draft",
11+
"application_id"
12+
)
13+
await schema.table("application_draft", (table) => {
14+
if (!hasApplicationIdOnApplicationDraft) {
915
table.integer("application_id").nullable()
10-
})
11-
.table("application", (table) => {
16+
}
17+
})
18+
19+
// Check and add columns for 'application' table
20+
const hasLastTravelOnApplication = await schema.hasColumn("application", "has_last_travel")
21+
const hasLastYearOnApplication = await schema.hasColumn("application", "last_travel_year")
22+
const hasLastMonthOnApplication = await schema.hasColumn("application", "last_travel_month")
23+
await schema.table("application", async (table) => {
24+
if (!hasLastTravelOnApplication) {
1225
table.integer("has_last_travel").nullable()
26+
}
27+
if (!hasLastYearOnApplication) {
1328
table.integer("last_travel_year").nullable()
29+
}
30+
if (!hasLastMonthOnApplication) {
1431
table.integer("last_travel_month").nullable()
15-
})
32+
}
33+
})
1634
}
1735

1836
export function down(knex: Knex) {

0 commit comments

Comments
 (0)