github.com/Elate-DevOps/migrate/v4@v4.0.12/README.md (about) 1 [](https://github.com/golang-migrate/migrate/actions/workflows/ci.yaml?query=branch%3Amaster) 2 [](https://pkg.go.dev/github.com/Elate-DevOps/migrate/v4) 3 [](https://coveralls.io/github/golang-migrate/migrate?branch=master) 4 [](https://packagecloud.io/golang-migrate/migrate?filter=debs) 5 [](https://hub.docker.com/r/migrate/migrate/) 6  7 [](https://github.com/golang-migrate/migrate/releases) 8 [](https://goreportcard.com/report/github.com/Elate-DevOps/migrate/v4) 9 10 # migrate 11 12 __Database migrations written in Go. Use as [CLI](#cli-usage) or import as [library](#use-in-your-go-project).__ 13 14 * Migrate reads migrations from [sources](#migration-sources) 15 and applies them in correct order to a [database](#databases). 16 * Drivers are "dumb", migrate glues everything together and makes sure the logic is bulletproof. 17 (Keeps the drivers lightweight, too.) 18 * Database drivers don't assume things or try to correct user input. When in doubt, fail. 19 20 Forked from [mattes/migrate](https://github.com/mattes/migrate) 21 22 ## Databases 23 24 Database drivers run migrations. [Add a new database?](database/driver.go) 25 26 * [PostgreSQL](database/postgres) 27 * [PGX v4](database/pgx) 28 * [PGX v5](database/pgx/v5) 29 * [Redshift](database/redshift) 30 * [Ql](database/ql) 31 * [Cassandra](database/cassandra) 32 * [SQLite](database/sqlite) 33 * [SQLite3](database/sqlite3) ([todo #165](https://github.com/mattes/migrate/issues/165)) 34 * [SQLCipher](database/sqlcipher) 35 * [MySQL/ MariaDB](database/mysql) 36 * [Neo4j](database/neo4j) 37 * [MongoDB](database/mongodb) 38 * [CrateDB](database/crate) ([todo #170](https://github.com/mattes/migrate/issues/170)) 39 * [Shell](database/shell) ([todo #171](https://github.com/mattes/migrate/issues/171)) 40 * [Google Cloud Spanner](database/spanner) 41 * [CockroachDB](database/cockroachdb) 42 * [YugabyteDB](database/yugabytedb) 43 * [ClickHouse](database/clickhouse) 44 * [Firebird](database/firebird) 45 * [MS SQL Server](database/sqlserver) 46 47 ### Database URLs 48 49 Database connection strings are specified via URLs. The URL format is driver dependent but generally has the form: `dbdriver://username:password@host:port/dbname?param1=true¶m2=false` 50 51 Any [reserved URL characters](https://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_reserved_characters) need to be escaped. Note, the `%` character also [needs to be escaped](https://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_the_percent_character) 52 53 Explicitly, the following characters need to be escaped: 54 `!`, `#`, `$`, `%`, `&`, `'`, `(`, `)`, `*`, `+`, `,`, `/`, `:`, `;`, `=`, `?`, `@`, `[`, `]` 55 56 It's easiest to always run the URL parts of your DB connection URL (e.g. username, password, etc) through an URL encoder. See the example Python snippets below: 57 58 ```bash 59 $ python3 -c 'import urllib.parse; print(urllib.parse.quote(input("String to encode: "), ""))' 60 String to encode: FAKEpassword!#$%&'()*+,/:;=?@[] 61 FAKEpassword%21%23%24%25%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D 62 $ python2 -c 'import urllib; print urllib.quote(raw_input("String to encode: "), "")' 63 String to encode: FAKEpassword!#$%&'()*+,/:;=?@[] 64 FAKEpassword%21%23%24%25%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D 65 $ 66 ``` 67 68 ## Migration Sources 69 70 Source drivers read migrations from local or remote sources. [Add a new source?](source/driver.go) 71 72 * [Filesystem](source/file) - read from filesystem 73 * [io/fs](source/iofs) - read from a Go [io/fs](https://pkg.go.dev/io/fs#FS) 74 * [Go-Bindata](source/go_bindata) - read from embedded binary data ([jteeuwen/go-bindata](https://github.com/jteeuwen/go-bindata)) 75 * [pkger](source/pkger) - read from embedded binary data ([markbates/pkger](https://github.com/markbates/pkger)) 76 * [GitHub](source/github) - read from remote GitHub repositories 77 * [GitHub Enterprise](source/github_ee) - read from remote GitHub Enterprise repositories 78 * [Bitbucket](source/bitbucket) - read from remote Bitbucket repositories 79 * [Gitlab](source/gitlab) - read from remote Gitlab repositories 80 * [AWS S3](source/aws_s3) - read from Amazon Web Services S3 81 * [Google Cloud Storage](source/google_cloud_storage) - read from Google Cloud Platform Storage 82 83 ## CLI usage 84 85 * Simple wrapper around this library. 86 * Handles ctrl+c (SIGINT) gracefully. 87 * No config search paths, no config files, no magic ENV var injections. 88 89 __[CLI Documentation](cmd/migrate)__ 90 91 ### Basic usage 92 93 ```bash 94 $ migrate -source file://path/to/migrations -database postgres://localhost:5432/database up 2 95 ``` 96 97 ### Docker usage 98 99 ```bash 100 $ docker run -v {{ migration dir }}:/migrations --network host migrate/migrate 101 -path=/migrations/ -database postgres://localhost:5432/database up 2 102 ``` 103 104 ## Use in your Go project 105 106 * API is stable and frozen for this release (v3 & v4). 107 * Uses [Go modules](https://golang.org/cmd/go/#hdr-Modules__module_versions__and_more) to manage dependencies. 108 * To help prevent database corruptions, it supports graceful stops via `GracefulStop chan bool`. 109 * Bring your own logger. 110 * Uses `io.Reader` streams internally for low memory overhead. 111 * Thread-safe and no goroutine leaks. 112 113 __[Go Documentation](https://pkg.go.dev/github.com/Elate-DevOps/migrate/v4)__ 114 115 ```go 116 import ( 117 "github.com/Elate-DevOps/migrate/v4" 118 _ "github.com/Elate-DevOps/migrate/v4/database/postgres" 119 _ "github.com/Elate-DevOps/migrate/v4/source/github" 120 ) 121 122 func main() { 123 m, err := migrate.New( 124 "github://mattes:personal-access-token@mattes/migrate_test", 125 "postgres://localhost:5432/database?sslmode=enable") 126 m.Steps(2) 127 } 128 ``` 129 130 Want to use an existing database client? 131 132 ```go 133 import ( 134 "database/sql" 135 _ "github.com/lib/pq" 136 "github.com/Elate-DevOps/migrate/v4" 137 "github.com/Elate-DevOps/migrate/v4/database/postgres" 138 _ "github.com/Elate-DevOps/migrate/v4/source/file" 139 ) 140 141 func main() { 142 db, err := sql.Open("postgres", "postgres://localhost:5432/database?sslmode=enable") 143 driver, err := postgres.WithInstance(db, &postgres.Config{}) 144 m, err := migrate.NewWithDatabaseInstance( 145 "file:///migrations", 146 "postgres", driver) 147 m.Up() // or m.Step(2) if you want to explicitly set the number of migrations to run 148 } 149 ``` 150 151 ## Getting started 152 153 Go to [getting started](GETTING_STARTED.md) 154 155 ## Tutorials 156 157 * [CockroachDB](database/cockroachdb/TUTORIAL.md) 158 * [PostgreSQL](database/postgres/TUTORIAL.md) 159 160 (more tutorials to come) 161 162 ## Migration files 163 164 Each migration has an up and down migration. [Why?](FAQ.md#why-two-separate-files-up-and-down-for-a-migration) 165 166 ```bash 167 1481574547_create_users_table.up.sql 168 1481574547_create_users_table.down.sql 169 ``` 170 171 [Best practices: How to write migrations.](MIGRATIONS.md) 172 173 ## Coming from another db migration tool? 174 175 Check out [migradaptor](https://github.com/musinit/migradaptor/). 176 *Note: migradaptor is not affliated or supported by this project* 177 178 ## Versions 179 180 Version | Supported? | Import | Notes 181 --------|------------|--------|------ 182 **master** | :white_check_mark: | `import "github.com/Elate-DevOps/migrate/v4"` | New features and bug fixes arrive here first | 183 **v4** | :white_check_mark: | `import "github.com/Elate-DevOps/migrate/v4"` | Used for stable releases | 184 **v3** | :x: | `import "github.com/golang-migrate/migrate"` (with package manager) or `import "gopkg.in/golang-migrate/migrate.v3"` (not recommended) | **DO NOT USE** - No longer supported | 185 186 ## Development and Contributing 187 188 Yes, please! [`Makefile`](Makefile) is your friend, 189 read the [development guide](CONTRIBUTING.md). 190 191 Also have a look at the [FAQ](FAQ.md). 192 193 --- 194 195 Looking for alternatives? [https://awesome-go.com/#database](https://awesome-go.com/#database).