|
| 1 | +--- |
| 2 | +description: Run one-time migrations at startup — install FHIR packages or execute SQL using AidboxMigration and Init Bundle. |
| 3 | +--- |
| 4 | + |
| 5 | +# Migrations |
| 6 | + |
| 7 | +Aidbox supports one-time migrations that run exactly once during startup. Migrations are useful for: |
| 8 | + |
| 9 | +* Installing FHIR Implementation Guide packages |
| 10 | +* Running SQL statements (creating indexes, tables, seed data) |
| 11 | + |
| 12 | +Migrations use the `AidboxMigration` resource combined with [Init Bundle](init-bundle.md) for declarative, idempotent execution. |
| 13 | + |
| 14 | +## How it works |
| 15 | + |
| 16 | +1. You define an `AidboxMigration` resource with an action and parameters |
| 17 | +2. You wrap it in an Init Bundle with `ifNoneExist` to ensure it runs only once |
| 18 | +3. On startup, Aidbox executes the bundle — if the migration already exists, it is skipped |
| 19 | + |
| 20 | +## Install a FHIR package |
| 21 | + |
| 22 | +Use the `far-migration-fhir-package-install` action to install a FHIR IG package from the registry. |
| 23 | + |
| 24 | +```json |
| 25 | +{ |
| 26 | + "type": "transaction", |
| 27 | + "resourceType": "Bundle", |
| 28 | + "entry": [ |
| 29 | + { |
| 30 | + "request": { |
| 31 | + "method": "POST", |
| 32 | + "url": "AidboxMigration", |
| 33 | + "ifNoneExist": "id=us-core-install" |
| 34 | + }, |
| 35 | + "resource": { |
| 36 | + "resourceType": "AidboxMigration", |
| 37 | + "id": "us-core-install", |
| 38 | + "action": "far-migration-fhir-package-install", |
| 39 | + "status": "to-run", |
| 40 | + "params": { |
| 41 | + "resourceType": "Parameters", |
| 42 | + "parameter": [ |
| 43 | + { |
| 44 | + "name": "package", |
| 45 | + "valueString": "hl7.fhir.us.core@3.1.1" |
| 46 | + } |
| 47 | + ] |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + ] |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +The `package` parameter value follows the format `<package-name>@<version>`. |
| 56 | + |
| 57 | +After execution, the migration status changes to `done` and the `result` field contains the number of installed canonicals. |
| 58 | + |
| 59 | +### Uninstall a FHIR package |
| 60 | + |
| 61 | +Use `far-migration-fhir-package-uninstall` to remove a previously installed package: |
| 62 | + |
| 63 | +```json |
| 64 | +{ |
| 65 | + "resourceType": "AidboxMigration", |
| 66 | + "id": "us-core-uninstall", |
| 67 | + "action": "far-migration-fhir-package-uninstall", |
| 68 | + "status": "to-run", |
| 69 | + "params": { |
| 70 | + "resourceType": "Parameters", |
| 71 | + "parameter": [ |
| 72 | + { |
| 73 | + "name": "package", |
| 74 | + "valueString": "hl7.fhir.us.core@3.1.1" |
| 75 | + } |
| 76 | + ] |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +## Run a SQL migration |
| 82 | + |
| 83 | +Use the `aidbox-migration-run-sql` action to execute arbitrary SQL statements. |
| 84 | + |
| 85 | +{% hint style="info" %} |
| 86 | +Available since the 2602 release. |
| 87 | +{% endhint %} |
| 88 | + |
| 89 | +```json |
| 90 | +{ |
| 91 | + "type": "transaction", |
| 92 | + "resourceType": "Bundle", |
| 93 | + "entry": [ |
| 94 | + { |
| 95 | + "request": { |
| 96 | + "method": "POST", |
| 97 | + "url": "AidboxMigration", |
| 98 | + "ifNoneExist": "id=create-encounter-index" |
| 99 | + }, |
| 100 | + "resource": { |
| 101 | + "resourceType": "AidboxMigration", |
| 102 | + "id": "create-encounter-index", |
| 103 | + "action": "aidbox-migration-run-sql", |
| 104 | + "status": "to-run", |
| 105 | + "params": { |
| 106 | + "resourceType": "Parameters", |
| 107 | + "parameter": [ |
| 108 | + { |
| 109 | + "name": "sql", |
| 110 | + "valueString": "CREATE INDEX IF NOT EXISTS encounter_subject_id ON encounter ((resource #>> '{subject, id}'));" |
| 111 | + } |
| 112 | + ] |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + ] |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +After execution, the migration status changes to `done` and `result.valueBoolean` is `true`. |
| 121 | + |
| 122 | +{% hint style="warning" %} |
| 123 | +Invalid SQL causes the migration to fail with a 422 error. In a transaction bundle, this rolls back the entire transaction and prevents Aidbox from starting. |
| 124 | +{% endhint %} |
| 125 | + |
| 126 | +## Using with Init Bundle |
| 127 | + |
| 128 | +Set the `BOX_INIT_BUNDLE` environment variable to load migrations on startup: |
| 129 | + |
| 130 | +```yaml |
| 131 | +volumes: |
| 132 | + - ./init-bundle.json:/tmp/init-bundle.json |
| 133 | +environment: |
| 134 | + BOX_INIT_BUNDLE: file:///tmp/init-bundle.json |
| 135 | +``` |
| 136 | +
|
| 137 | +{% hint style="info" %} |
| 138 | +The `ifNoneExist` parameter in the bundle entry ensures idempotency — if a migration with the same `id` already exists, it is skipped. Without `ifNoneExist`, a repeated POST returns a 409 duplicate key error. |
| 139 | +{% endhint %} |
| 140 | + |
| 141 | +## Combining multiple migrations |
| 142 | + |
| 143 | +You can include multiple migrations in a single Init Bundle: |
| 144 | + |
| 145 | +```json |
| 146 | +{ |
| 147 | + "type": "transaction", |
| 148 | + "resourceType": "Bundle", |
| 149 | + "entry": [ |
| 150 | + { |
| 151 | + "request": { |
| 152 | + "method": "POST", |
| 153 | + "url": "AidboxMigration", |
| 154 | + "ifNoneExist": "id=install-us-core" |
| 155 | + }, |
| 156 | + "resource": { |
| 157 | + "resourceType": "AidboxMigration", |
| 158 | + "id": "install-us-core", |
| 159 | + "action": "far-migration-fhir-package-install", |
| 160 | + "status": "to-run", |
| 161 | + "params": { |
| 162 | + "resourceType": "Parameters", |
| 163 | + "parameter": [ |
| 164 | + { "name": "package", "valueString": "hl7.fhir.us.core@3.1.1" } |
| 165 | + ] |
| 166 | + } |
| 167 | + } |
| 168 | + }, |
| 169 | + { |
| 170 | + "request": { |
| 171 | + "method": "POST", |
| 172 | + "url": "AidboxMigration", |
| 173 | + "ifNoneExist": "id=create-custom-index" |
| 174 | + }, |
| 175 | + "resource": { |
| 176 | + "resourceType": "AidboxMigration", |
| 177 | + "id": "create-custom-index", |
| 178 | + "action": "aidbox-migration-run-sql", |
| 179 | + "status": "to-run", |
| 180 | + "params": { |
| 181 | + "resourceType": "Parameters", |
| 182 | + "parameter": [ |
| 183 | + { |
| 184 | + "name": "sql", |
| 185 | + "valueString": "CREATE INDEX IF NOT EXISTS patient_birthdate ON patient ((resource #>> '{birthDate}'));" |
| 186 | + } |
| 187 | + ] |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + ] |
| 192 | +} |
| 193 | +``` |
| 194 | + |
| 195 | +## Checking migration status |
| 196 | + |
| 197 | +List all migrations: |
| 198 | + |
| 199 | +```http |
| 200 | +GET /fhir/AidboxMigration |
| 201 | +``` |
| 202 | + |
| 203 | +Each migration has a `status` field: |
| 204 | + |
| 205 | +| Status | Description | |
| 206 | +|--------|-------------| |
| 207 | +| `to-run` | Migration is queued for execution | |
| 208 | +| `done` | Migration completed successfully | |
| 209 | + |
| 210 | +## See also |
| 211 | + |
| 212 | +{% content-ref url="init-bundle.md" %} |
| 213 | +[init-bundle.md](init-bundle.md) |
| 214 | +{% endcontent-ref %} |
| 215 | + |
| 216 | +{% content-ref url="../reference/system-resources-reference/core-module-resources.md" %} |
| 217 | +[AidboxMigration resource reference](../reference/system-resources-reference/core-module-resources.md) |
| 218 | +{% endcontent-ref %} |
| 219 | + |
| 220 | +{% content-ref url="../tutorials/artifact-registry-tutorials/upload-fhir-implementation-guide/how-to-load-fhir-ig-with-init-bundle.md" %} |
| 221 | +[Tutorial: Load FHIR IG with Init Bundle](../tutorials/artifact-registry-tutorials/upload-fhir-implementation-guide/how-to-load-fhir-ig-with-init-bundle.md) |
| 222 | +{% endcontent-ref %} |
| 223 | + |
| 224 | +{% content-ref url="../tutorials/other-tutorials/how-to-run-sql-via-init-bundle.md" %} |
| 225 | +[Tutorial: Run SQL statements via Init Bundle](../tutorials/other-tutorials/how-to-run-sql-via-init-bundle.md) |
| 226 | +{% endcontent-ref %} |
0 commit comments