github.com/olitvin/migrate/v4@v4.14.3-0.20210330111251-992b37ee04c8/README.md (about) 1 [![CircleCI - Build Status](https://img.shields.io/circleci/build/github/golang-migrate/migrate/master)](https://circleci.com/gh/golang-migrate/migrate) 2 [![GoDoc](https://pkg.go.dev/badge/github.com/olitvin/migrate)](https://pkg.go.dev/github.com/olitvin/migrate/v4) 3 [![Coverage Status](https://img.shields.io/coveralls/github/golang-migrate/migrate/master.svg)](https://coveralls.io/github/golang-migrate/migrate?branch=master) 4 [![packagecloud.io](https://img.shields.io/badge/deb-packagecloud.io-844fec.svg)](https://packagecloud.io/golang-migrate/migrate?filter=debs) 5 [![Docker Pulls](https://img.shields.io/docker/pulls/migrate/migrate.svg)](https://hub.docker.com/r/migrate/migrate/) 6 ![Supported Go Versions](https://img.shields.io/badge/Go-1.15%2C%201.16-lightgrey.svg) 7 [![GitHub Release](https://img.shields.io/github/release/golang-migrate/migrate.svg)](https://github.com/olitvin/migrate/releases) 8 [![Go Report Card](https://goreportcard.com/badge/github.com/olitvin/migrate)](https://goreportcard.com/report/github.com/olitvin/migrate) 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](database/pgx) 28 * [Redshift](database/redshift) 29 * [Ql](database/ql) 30 * [Cassandra](database/cassandra) 31 * [SQLite](database/sqlite3) ([todo #165](https://github.com/mattes/migrate/issues/165)) 32 * [SQLCipher](database/sqlcipher) 33 * [MySQL/ MariaDB](database/mysql) 34 * [Neo4j](database/neo4j) 35 * [MongoDB](database/mongodb) 36 * [CrateDB](database/crate) ([todo #170](https://github.com/mattes/migrate/issues/170)) 37 * [Shell](database/shell) ([todo #171](https://github.com/mattes/migrate/issues/171)) 38 * [Google Cloud Spanner](database/spanner) 39 * [CockroachDB](database/cockroachdb) 40 * [ClickHouse](database/clickhouse) 41 * [Firebird](database/firebird) 42 * [MS SQL Server](database/sqlserver) 43 44 ### Database URLs 45 46 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` 47 48 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) 49 50 Explicitly, the following characters need to be escaped: 51 `!`, `#`, `$`, `%`, `&`, `'`, `(`, `)`, `*`, `+`, `,`, `/`, `:`, `;`, `=`, `?`, `@`, `[`, `]` 52 53 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: 54 55 ```bash 56 $ python3 -c 'import urllib.parse; print(urllib.parse.quote(input("String to encode: "), ""))' 57 String to encode: FAKEpassword!#$%&'()*+,/:;=?@[] 58 FAKEpassword%21%23%24%25%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D 59 $ python2 -c 'import urllib; print urllib.quote(raw_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 $ 63 ``` 64 65 ## Migration Sources 66 67 Source drivers read migrations from local or remote sources. [Add a new source?](source/driver.go) 68 69 * [Filesystem](source/file) - read from filesystem 70 * [Go-Bindata](source/go_bindata) - read from embedded binary data ([jteeuwen/go-bindata](https://github.com/jteeuwen/go-bindata)) 71 * [GitHub](source/github) - read from remote GitHub repositories 72 * [GitHub Enterprise](source/github_ee) - read from remote GitHub Enterprise repositories 73 * [Bitbucket](source/bitbucket) - read from remote Bitbucket repositories 74 * [Gitlab](source/gitlab) - read from remote Gitlab repositories 75 * [AWS S3](source/aws_s3) - read from Amazon Web Services S3 76 * [Google Cloud Storage](source/google_cloud_storage) - read from Google Cloud Platform Storage 77 78 ## CLI usage 79 80 * Simple wrapper around this library. 81 * Handles ctrl+c (SIGINT) gracefully. 82 * No config search paths, no config files, no magic ENV var injections. 83 84 __[CLI Documentation](cmd/migrate)__ 85 86 ### Basic usage 87 88 ```bash 89 $ migrate -source file://path/to/migrations -database postgres://localhost:5432/database up 2 90 ``` 91 92 ### Docker usage 93 94 ```bash 95 $ docker run -v {{ migration dir }}:/migrations --network host migrate/migrate 96 -path=/migrations/ -database postgres://localhost:5432/database up 2 97 ``` 98 99 ## Use in your Go project 100 101 * API is stable and frozen for this release (v3 & v4). 102 * Uses [Go modules](https://golang.org/cmd/go/#hdr-Modules__module_versions__and_more) to manage dependencies. 103 * To help prevent database corruptions, it supports graceful stops via `GracefulStop chan bool`. 104 * Bring your own logger. 105 * Uses `io.Reader` streams internally for low memory overhead. 106 * Thread-safe and no goroutine leaks. 107 108 __[Go Documentation](https://godoc.org/github.com/olitvin/migrate)__ 109 110 ```go 111 import ( 112 "github.com/olitvin/migrate/v4" 113 _ "github.com/olitvin/migrate/v4/database/postgres" 114 _ "github.com/olitvin/migrate/v4/source/github" 115 ) 116 117 func main() { 118 m, err := migrate.New( 119 "github://mattes:personal-access-token@mattes/migrate_test", 120 "postgres://localhost:5432/database?sslmode=enable") 121 m.Steps(2) 122 } 123 ``` 124 125 Want to use an existing database client? 126 127 ```go 128 import ( 129 "database/sql" 130 _ "github.com/lib/pq" 131 "github.com/olitvin/migrate/v4" 132 "github.com/olitvin/migrate/v4/database/postgres" 133 _ "github.com/olitvin/migrate/v4/source/file" 134 ) 135 136 func main() { 137 db, err := sql.Open("postgres", "postgres://localhost:5432/database?sslmode=enable") 138 driver, err := postgres.WithInstance(db, &postgres.Config{}) 139 m, err := migrate.NewWithDatabaseInstance( 140 "file:///migrations", 141 "postgres", driver) 142 m.Steps(2) 143 } 144 ``` 145 146 ## Getting started 147 148 Go to [getting started](GETTING_STARTED.md) 149 150 ## Tutorials 151 152 * [CockroachDB](database/cockroachdb/TUTORIAL.md) 153 * [PostgreSQL](database/postgres/TUTORIAL.md) 154 155 (more tutorials to come) 156 157 ## Migration files 158 159 Each migration has an up and down migration. [Why?](FAQ.md#why-two-separate-files-up-and-down-for-a-migration) 160 161 ```bash 162 1481574547_create_users_table.up.sql 163 1481574547_create_users_table.down.sql 164 ``` 165 166 [Best practices: How to write migrations.](MIGRATIONS.md) 167 168 ## Versions 169 170 Version | Supported? | Import | Notes 171 --------|------------|--------|------ 172 **master** | :white_check_mark: | `import "github.com/olitvin/migrate/v4"` | New features and bug fixes arrive here first | 173 **v4** | :white_check_mark: | `import "github.com/olitvin/migrate/v4"` | Used for stable releases | 174 **v3** | :x: | `import "github.com/olitvin/migrate"` (with package manager) or `import "gopkg.in/golang-migrate/migrate.v3"` (not recommended) | **DO NOT USE** - No longer supported | 175 176 ## Development and Contributing 177 178 Yes, please! [`Makefile`](Makefile) is your friend, 179 read the [development guide](CONTRIBUTING.md). 180 181 Also have a look at the [FAQ](FAQ.md). 182 183 --- 184 185 Looking for alternatives? [https://awesome-go.com/#database](https://awesome-go.com/#database).