github.com/sunsingerus/migrate@v3.5.4+incompatible/README.md (about) 1 [![Build Status](https://img.shields.io/travis/golang-migrate/migrate/master.svg)](https://travis-ci.org/golang-migrate/migrate) 2 [![GoDoc](https://godoc.org/github.com/golang-migrate/migrate?status.svg)](https://godoc.org/github.com/golang-migrate/migrate) 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.10%2C%201.11-lightgrey.svg) 7 [![GitHub Release](https://img.shields.io/github/release/golang-migrate/migrate.svg)](https://github.com/golang-migrate/migrate/releases) 8 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 21 Looking for [v1](https://github.com/golang-migrate/migrate/tree/v1)? 22 23 Forked from [mattes/migrate](https://github.com/mattes/migrate) 24 25 26 ## Databases 27 28 Database drivers run migrations. [Add a new database?](database/driver.go) 29 30 * [PostgreSQL](database/postgres) 31 * [Redshift](database/redshift) 32 * [Ql](database/ql) 33 * [Cassandra](database/cassandra) 34 * [SQLite](database/sqlite3) ([todo #165](https://github.com/mattes/migrate/issues/165)) 35 * [MySQL/ MariaDB](database/mysql) 36 * [Neo4j](database/neo4j) ([todo #167](https://github.com/mattes/migrate/issues/167)) 37 * [MongoDB](database/mongodb) ([todo #169](https://github.com/mattes/migrate/issues/169)) 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 * [ClickHouse](database/clickhouse) 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?option1=true&option2=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 helpers below: 54 ```bash 55 $ python3 -c 'import urllib.parse; print(urllib.parse.quote(input("String to encode: "), ""))' 56 String to encode: FAKEpassword!#$%&'()*+,/:;=?@[] 57 FAKEpassword%21%23%24%25%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D 58 $ python2 -c 'import urllib; print urllib.quote(raw_input("String to encode: "), "")' 59 String to encode: FAKEpassword!#$%&'()*+,/:;=?@[] 60 FAKEpassword%21%23%24%25%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D 61 $ 62 ``` 63 64 ## Migration Sources 65 66 Source drivers read migrations from local or remote sources. [Add a new source?](source/driver.go) 67 68 * [Filesystem](source/file) - read from fileystem 69 * [Go-Bindata](source/go_bindata) - read from embedded binary data ([jteeuwen/go-bindata](https://github.com/jteeuwen/go-bindata)) 70 * [Github](source/github) - read from remote Github repositories 71 * [AWS S3](source/aws_s3) - read from Amazon Web Services S3 72 * [Google Cloud Storage](source/google_cloud_storage) - read from Google Cloud Platform Storage 73 74 75 76 ## CLI usage 77 78 * Simple wrapper around this library. 79 * Handles ctrl+c (SIGINT) gracefully. 80 * No config search paths, no config files, no magic ENV var injections. 81 82 __[CLI Documentation](cli)__ 83 84 ### Basic usage: 85 86 ``` 87 $ migrate -source file://path/to/migrations -database postgres://localhost:5432/database up 2 88 ``` 89 90 ### Docker usage 91 92 ``` 93 $ docker run -v {{ migration dir }}:/migrations --network host migrate/migrate 94 -path=/migrations/ -database postgres://localhost:5432/database up 2 95 ``` 96 97 ## Use in your Go project 98 99 * API is stable and frozen for this release (v3.x). 100 * Uses [dep](https://github.com/golang/dep) to manage dependencies 101 * To help prevent database corruptions, it supports graceful stops via `GracefulStop chan bool`. 102 * Bring your own logger. 103 * Uses `io.Reader` streams internally for low memory overhead. 104 * Thread-safe and no goroutine leaks. 105 106 __[Go Documentation](https://godoc.org/github.com/golang-migrate/migrate)__ 107 108 ```go 109 import ( 110 "github.com/golang-migrate/migrate" 111 _ "github.com/golang-migrate/migrate/database/postgres" 112 _ "github.com/golang-migrate/migrate/source/github" 113 ) 114 115 func main() { 116 m, err := migrate.New( 117 "github://mattes:personal-access-token@mattes/migrate_test", 118 "postgres://localhost:5432/database?sslmode=enable") 119 m.Steps(2) 120 } 121 ``` 122 123 Want to use an existing database client? 124 125 ```go 126 import ( 127 "database/sql" 128 _ "github.com/lib/pq" 129 "github.com/golang-migrate/migrate" 130 "github.com/golang-migrate/migrate/database/postgres" 131 _ "github.com/golang-migrate/migrate/source/file" 132 ) 133 134 func main() { 135 db, err := sql.Open("postgres", "postgres://localhost:5432/database?sslmode=enable") 136 driver, err := postgres.WithInstance(db, &postgres.Config{}) 137 m, err := migrate.NewWithDatabaseInstance( 138 "file:///migrations", 139 "postgres", driver) 140 m.Steps(2) 141 } 142 ``` 143 144 ## Migration files 145 146 Each migration has an up and down migration. [Why?](FAQ.md#why-two-separate-files-up-and-down-for-a-migration) 147 148 ``` 149 1481574547_create_users_table.up.sql 150 1481574547_create_users_table.down.sql 151 ``` 152 153 [Best practices: How to write migrations.](MIGRATIONS.md) 154 155 156 157 ## Development and Contributing 158 159 Yes, please! [`Makefile`](Makefile) is your friend, 160 read the [development guide](CONTRIBUTING.md). 161 162 Also have a look at the [FAQ](FAQ.md). 163 164 165 166 --- 167 168 Looking for alternatives? [https://awesome-go.com/#database](https://awesome-go.com/#database).