Skip to content

Commit 00fd84e

Browse files
committed
fix: sync print_templates with default_print_templates on migration
Fresh installs were showing outdated surgery templates because migration 010 created print_templates with old structures, while migrations 013/014/016 only updated default_print_templates. Migration 017 now syncs the latest template structures from default_print_templates to print_templates, ensuring fresh installs have all conditional blocks (inward management, discharge plan, referral).
1 parent 303d6f4 commit 00fd84e

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/main/db/migrations.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import * as m_013_update_default_templates_inward_referral from './migrations/01
1515
import * as m_014_sync_default_templates from './migrations/014_sync_default_templates'
1616
import * as m_015_add_discharge_plan from './migrations/015_add_discharge_plan'
1717
import * as m_016_update_default_templates_discharge_plan from './migrations/016_update_default_templates_discharge_plan'
18+
import * as m_017_sync_print_templates_with_defaults from './migrations/017_sync_print_templates_with_defaults'
1819

1920
export default {
2021
'000_init': m_000_init,
@@ -33,5 +34,6 @@ export default {
3334
'013_update_default_templates_inward_referral': m_013_update_default_templates_inward_referral,
3435
'014_sync_default_templates': m_014_sync_default_templates,
3536
'015_add_discharge_plan': m_015_add_discharge_plan,
36-
'016_update_default_templates_discharge_plan': m_016_update_default_templates_discharge_plan
37+
'016_update_default_templates_discharge_plan': m_016_update_default_templates_discharge_plan,
38+
'017_sync_print_templates_with_defaults': m_017_sync_print_templates_with_defaults
3739
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
import { Kysely } from 'kysely'
3+
4+
/**
5+
* Sync print_templates with default_print_templates.
6+
*
7+
* This migration fixes a bug where the user-facing print_templates table
8+
* was not being updated when default_print_templates was updated.
9+
*
10+
* Migrations 013, 014, and 016 correctly updated default_print_templates
11+
* with new fields (inward_management, discharge_plan, referral), but the
12+
* user-facing print_templates table was never updated.
13+
*
14+
* This migration copies the latest structure from default_print_templates
15+
* to the matching default entries in print_templates.
16+
*/
17+
export async function up(db: Kysely<any>): Promise<void> {
18+
// Get all default templates
19+
const defaults = await db
20+
.selectFrom('default_print_templates')
21+
.select(['key', 'type', 'structure'])
22+
.execute()
23+
24+
const now = Date.now()
25+
26+
// Update matching print_templates entries (only default templates)
27+
for (const defaultTemplate of defaults) {
28+
await db
29+
.updateTable('print_templates')
30+
.set({
31+
structure: defaultTemplate.structure,
32+
updated_at: now
33+
})
34+
.where('type', '=', defaultTemplate.type)
35+
.where('is_default', '=', 1)
36+
.execute()
37+
}
38+
}
39+
40+
export async function down(_db: Kysely<any>): Promise<void> {
41+
// No-op: This is a data sync migration, not reversible without storing old data
42+
// Users who need the old templates can manually reset or the app will work with either version
43+
}

0 commit comments

Comments
 (0)