Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc/md - added usage note for golang-migrate #2482

Merged
merged 5 commits into from Apr 20, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 19 additions & 5 deletions doc/md/versioned-migrations.md
Expand Up @@ -6,13 +6,13 @@ title: Versioned Migrations
If you are using the Atlas migration engine you are able to use the versioned migrations feature of it. Instead of
applying the computed changes directly to the database, it will generate a set of migration files containing the
necessary SQL statements to migrate the database. These files can then be edited to your needs and be applied by any
tool you like (like golang-migrate, Flyway, liquibase).
tool you like (like golang-migrate, Flyway, liquibase).

![atlas-versioned-migration-process](https://entgo.io/images/assets/migrate-atlas-versioned.png)

## Generating Versioned Migration Files

### From Client
### From Client

If you want to use an instantiated Ent client to create new migration files, you have to enable the versioned
migrations feature flag in order to have Ent make the necessary changes to the generated code. Depending on how you
Expand All @@ -37,7 +37,7 @@ package main

import (
"log"

"entgo.io/ent/entc"
"entgo.io/ent/entc/gen"
)
Expand Down Expand Up @@ -161,15 +161,15 @@ migrate -source file://migrations -database mysql://root:pass@tcp(localhost:3306
## Moving from Auto-Migration to Versioned Migrations

In case you already have an Ent application in production and want to switch over from auto migration to the new
versioned migration, you need to take some extra steps.
versioned migration, you need to take some extra steps.

1. Create an initial migration file (or several files if you want) reflecting the currently deployed state.

To do this make sure your schema definition is in sync with your deployed version. Then spin up an empty database and
run the diff command once as described above. This will create the statements needed to create the current state of
your schema graph.

2. Configure the tool you use to manage migrations to consider this file as **applied**.
2. Configure the tool you use to manage migrations to consider this file as **applied**.

In case of `golang-migrate` this can be done by forcing your database version as
described [here](https://github.com/golang-migrate/migrate/blob/master/GETTING_STARTED.md#forcing-your-database-version).
Expand Down Expand Up @@ -241,3 +241,17 @@ func main() {
}
}
```

### Note for using golang-migrate

If you use golang-migrate with MySQL, you need to change the DSN as follows:

```
"user:password@tcp(host:port)/dbname?multiStatements=true"
```

**Why?**

Without the `multiStatements=true` parameter, the migration will fail if there is more than one SQL statement in the migration file.

Please note that all other options you use for your DSN must be added as well!
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd drop this sentence and change the above to something like "you need to add the multiStatements parameter to true as in the example below" and then take the DSN we used in the documents with the param applied. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with your change.