github.com/mef13/migrate/v4@v4.14.3/README.md (about)

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