migrate/database/mysql
Lukas Joergensen 480a5a634a postgres: Move lock out of ensureVersionTable, for consistency with other SQL operations (#173)
* Consistently lock in ensureVersionTable and do not call ensureVersionTable from Drop across all database implementations

* Add test for dropping postgres databases

* Fix failing database tests

* Fix CockroachDb test, lock table should be created before versionTable

* Add Initialize() to Driver interface, and add integration tests for Drop() between database implementations and migrate

* Remove Initialize, document breaking behaviour of Drop

* Revert introduction of Initialize method

* Removed Initialize in Stub as well

* Remove call to non-existent Initialize and make sure to close re-initialized database connections

* Revert changes to TestDrop in database/testing

* Split Test and TestMigrate into different test entrypoints

* Remove unused import in migrate_testing

* Remove erroneous code to fix tests

* Add stub source imports to database tests

* Add Stub source to migrate tests

* Use example migrations for tests

* Add file driver to database tests

* Align database directory layout

* Add file source driver to Cassandra

* Review changes

* Minor syntactic change for cleaner diff
2019-02-26 15:56:57 -08:00
..
examples/migrations postgres: Move lock out of ensureVersionTable, for consistency with other SQL operations (#173) 2019-02-26 15:56:57 -08:00
README.md more descriptive. 2018-08-15 02:42:18 +00:00
mysql.go postgres: Move lock out of ensureVersionTable, for consistency with other SQL operations (#173) 2019-02-26 15:56:57 -08:00
mysql_test.go postgres: Move lock out of ensureVersionTable, for consistency with other SQL operations (#173) 2019-02-26 15:56:57 -08:00

README.md

MySQL

mysql://user:password@tcp(host:port)/dbname?query

URL Query WithInstance Config Description
x-migrations-table MigrationsTable Name of the migrations table
dbname DatabaseName The name of the database to connect to
user The user to sign in as
password The user's password
host The host to connect to.
port The port to bind to.
tls TLS / SSL encrypted connection parameter; see go-sql-driver. Use any name (e.g. migrate) if you want to use a custom TLS config (x-tls- queries).
x-tls-ca The location of the CA (certificate authority) file.
x-tls-cert The location of the client certicicate file. Must be used with x-tls-key.
x-tls-key The location of the private key file. Must be used with x-tls-cert.
x-tls-insecure-skip-verify Whether or not to use SSL (true|false)

Use with existing client

If you use the MySQL driver with existing database client, you must create the client with parameter multiStatements=true:

package main

import (
    "database/sql"
    
    _ "github.com/go-sql-driver/mysql"
    "github.com/golang-migrate/migrate"
    "github.com/golang-migrate/migrate/database/mysql"
    _ "github.com/golang-migrate/migrate/source/file"
)

func main() {
    db, _ := sql.Open("mysql", "user:password@tcp(host:port)/dbname?multiStatements=true")
    driver, _ := mysql.WithInstance(db, &mysql.Config{})
    m, _ := migrate.NewWithDatabaseInstance(
        "file:///migrations",
        "mysql", 
        driver,
    )
    
    m.Steps(2)
}

Upgrading from v1

  1. Write down the current migration version from schema_migrations
  2. DROP TABLE schema_migrations
  3. Wrap your existing migrations in transactions (BEGIN/COMMIT) if you use multiple statements within one migration.
  4. Download and install the latest migrate version.
  5. Force the current migration version with migrate force <current_version>.