| 1 |
// Package dbutil provides a simple framework for in-process database |
| 2 |
// migrations. You provide the SQL files and they are run to upgrade |
| 3 |
// the database. A versions table is automatically created in the |
| 4 |
// database to track which migrations have been applied. There is |
| 5 |
// support for multiple migration pathways, for example v0->v2 versus |
| 6 |
// v0->v1->v2, and the shorter one is prioritized if both are |
| 7 |
// provided. |
| 8 |
// |
| 9 |
// Example usage from Go: |
| 10 |
// |
| 11 |
// package main |
| 12 |
// |
| 13 |
// import ( |
| 14 |
// "context" |
| 15 |
// "database/sql" |
| 16 |
// "embed" |
| 17 |
// |
| 18 |
// "go.mau.fi/util/dbutil" |
| 19 |
// ) |
| 20 |
// |
| 21 |
// //go:embed *.sql |
| 22 |
// var upgrades embed.FS |
| 23 |
// |
| 24 |
// func mainE() error { |
| 25 |
// ctx := context.Background() |
| 26 |
// rawDB, err := sql.Open("sqlite3", "./hotdogs.db") |
| 27 |
// if err != nil { |
| 28 |
// return err |
| 29 |
// } |
| 30 |
// db, err := dbutil.NewWithDB(rawDB, "sqlite3") |
| 31 |
// if err != nil { |
| 32 |
// return err |
| 33 |
// } |
| 34 |
// table := dbutil.UpgradeTable{} |
| 35 |
// table.RegisterFS(upgrades) |
| 36 |
// err = db.Upgrade(ctx) |
| 37 |
// if err != nil { |
| 38 |
// return err |
| 39 |
// } |
| 40 |
// // db has been upgraded to latest version |
| 41 |
// return nil |
| 42 |
// } |
| 43 |
// |
| 44 |
// In dbutil, the database is understood to have a monotonic integer |
| 45 |
// sequence of versions starting at v0, v1, v2, etc. By providing |
| 46 |
// migrations you define a directed acyclic graph (DAG) that allows |
| 47 |
// dbutil to find a path from the current recorded database version to |
| 48 |
// the latest version available. |
| 49 |
// |
| 50 |
// Each SQL migration file has a mandatory comment header that |
| 51 |
// identifies which database versions it upgrades between. For example |
| 52 |
// this is a migration that upgrades from v0 to v2: |
| 53 |
// |
| 54 |
// -- v0 -> v2: Do some things |
| 55 |
// |
| 56 |
// You can omit the first version for the common case of upgrading to |
| 57 |
// a version from the previous version. For example this is a |
| 58 |
// migration that upgrades from v1 to v2: |
| 59 |
// |
| 60 |
// -- v2: Do fewer things |
| 61 |
// |
| 62 |
// By providing "v1" and "v2" migrations, a v0 database would be |
| 63 |
// upgraded to v1 and then v2, while by providing an additional "v0 -> |
| 64 |
// v2" migration a v0 database would be upgraded directly to v2 as it |
| 65 |
// is a more direct path. With that migration provided the "v1" |
| 66 |
// migration is no longer needed. |
| 67 |
// |
| 68 |
// By default, when running migrations, if a more recent database |
| 69 |
// version is live than the current code knows about (for example, |
| 70 |
// from running a previous version of the application), dbutil will |
| 71 |
// error out. However, many database migrations are backwards |
| 72 |
// compatible. You can therefore indicate this when writing a |
| 73 |
// migration, and previous versions of the application will accept a |
| 74 |
// database with that migration applied, even if they are unaware of |
| 75 |
// its contents. For example, if the migration from v1 to v2 was |
| 76 |
// backwards compatible, you could provide this migration: |
| 77 |
// |
| 78 |
// -- v2 (compatible with v1+): Do fewer things |
| 79 |
// |
| 80 |
// When applying the migration, the compatibility level (v1) is saved |
| 81 |
// to the versions table in the database, so that older versions of |
| 82 |
// the application which only know about v1 will see that v2 of the |
| 83 |
// database is still OK to use. If the compatibility level is not set, |
| 84 |
// then it defaults to the same as the target version for the |
| 85 |
// migration, which achieves the default behavior described in the |
| 86 |
// previous paragraph. |
| 87 |
// |
| 88 |
// You can provide additional flags immediately following the header |
| 89 |
// line. To disable wrapping the upgrade in a single transaction, put |
| 90 |
// "transaction: off" on the second line. |
| 91 |
// |
| 92 |
// -- v5: Upgrade without transaction |
| 93 |
// -- transaction: off |
| 94 |
// // do dangerous stuff |
| 95 |
// |
| 96 |
// Within migrations, there is special syntax that can be used to |
| 97 |
// filter parts of the SQL to apply only with specific dialects. To |
| 98 |
// limit the next line to one dialect: |
| 99 |
// |
| 100 |
// -- only: postgres |
| 101 |
// |
| 102 |
// To limit the next N lines: |
| 103 |
// |
| 104 |
// -- only: sqlite for next 123 lines |
| 105 |
// |
| 106 |
// To limit a block of code, fenced by another directive: |
| 107 |
// |
| 108 |
// -- only: sqlite until "end only" |
| 109 |
// QUERY; |
| 110 |
// ANOTHER QUERY; |
| 111 |
// -- end only sqlite |
| 112 |
// |
| 113 |
// If the single-line limit is on the second line of the file, the |
| 114 |
// whole file is limited to that dialect. |
| 115 |
// |
| 116 |
// If the filter ends with `(lines commented)`, then ALL lines chosen |
| 117 |
// by the filter will be uncommented. The `--` comment prefix must be |
| 118 |
// at the beginning of the line with no whitespace ahead of it. |
| 119 |
package dbutil |