github.com/scraniel/migrate@v0.0.0-20230320185700-339088f36cee/database/mysql/README.md (about)

     1  # MySQL
     2  
     3  `mysql://user:password@tcp(host:port)/dbname?query`
     4  
     5  | URL Query  | WithInstance Config | Description |
     6  |------------|---------------------|-------------|
     7  | `x-migrations-table` | `MigrationsTable` | Name of the migrations table |
     8  | `x-no-lock` | `NoLock` | Set to `true` to skip `GET_LOCK`/`RELEASE_LOCK` statements. Useful for [multi-master MySQL flavors](https://www.percona.com/doc/percona-xtradb-cluster/LATEST/features/pxc-strict-mode.html#explicit-table-locking). Only run migrations from one host when this is enabled. |
     9  | `x-statement-timeout` | `StatementTimeout` | Abort any statement that takes more than the specified number of milliseconds, functionally similar to [Server-side SELECT statement timeouts](https://dev.mysql.com/blog-archive/server-side-select-statement-timeouts/) but enforced by the client. Available for all versions of MySQL, not just >=5.7. | 
    10  | `dbname` | `DatabaseName` | The name of the database to connect to |
    11  | `user` | | The user to sign in as |
    12  | `password` | | The user's password | 
    13  | `host` | | The host to connect to. |
    14  | `port` | | The port to bind to. |
    15  | `tls`  | | TLS / SSL encrypted connection parameter; see [go-sql-driver](https://github.com/go-sql-driver/mysql#tls). Use any name (e.g. `migrate`) if you want to use a custom TLS config (`x-tls-` queries). |
    16  | `x-tls-ca` | | The location of the CA (certificate authority) file. |
    17  | `x-tls-cert` | | The location of the client certicicate file. Must be used with `x-tls-key`. |
    18  | `x-tls-key` | | The location of the private key file. Must be used with `x-tls-cert`. |
    19  | `x-tls-insecure-skip-verify` | | Whether or not to use SSL (true\|false) | 
    20  
    21  ## Use with existing client
    22  
    23  If you use the MySQL driver with existing database client, you must create the client with parameter `multiStatements=true`:
    24  
    25  ```go
    26  package main
    27  
    28  import (
    29      "database/sql"
    30      
    31      _ "github.com/go-sql-driver/mysql"
    32      "github.com/golang-migrate/migrate/v4"
    33      "github.com/golang-migrate/migrate/v4/database/mysql"
    34      _ "github.com/golang-migrate/migrate/v4/source/file"
    35  )
    36  
    37  func main() {
    38      db, _ := sql.Open("mysql", "user:password@tcp(host:port)/dbname?multiStatements=true")
    39      driver, _ := mysql.WithInstance(db, &mysql.Config{})
    40      m, _ := migrate.NewWithDatabaseInstance(
    41          "file:///migrations",
    42          "mysql", 
    43          driver,
    44      )
    45      
    46      m.Steps(2)
    47  }
    48  ```
    49  
    50  ## Upgrading from v1
    51  
    52  1. Write down the current migration version from schema_migrations
    53  1. `DROP TABLE schema_migrations`
    54  2. Wrap your existing migrations in transactions ([BEGIN/COMMIT](https://dev.mysql.com/doc/refman/5.7/en/commit.html)) if you use multiple statements within one migration.
    55  3. Download and install the latest migrate version.
    56  4. Force the current migration version with `migrate force <current_version>`.