mattes / migrate

Database migrations. CLI and Golang library.
Other
2.29k stars 326 forks source link

feat(mysql): try creating database if not exist #279

Open JensRantil opened 7 years ago

JensRantil commented 7 years ago

Background: Our scenario is that we'd like to be able to create schema from scratch for a brand new staging environment. This new staging environment doesn't have the database created.

Problem: Having a first migration doing CREATE DATABASE xxx doesn't work because a connection to mysql://tcp(a.b.c.d)/xxx fails when database driver is trying to switch to the database xxx.

Proposed solution: When Open(...)ing a MySQL Driver, we always try to create the database if it doesn't exist and then we reestablish a new database connection and ask the MySQL database/sql/driver.Driver to make the switch to the database.

Cred to @LarsAlmgren for helping out with this.

mattes commented 7 years ago

I think there is more that needs to be done when setting up a database, i.e. roles and permissions. I'm unsure about this PR. I believe creating the database in the first place might be out of the scope of this migration. But then rails db:setup would create a database as well. What's your thinking, @JensRantil ?

JensRantil commented 6 years ago

@mattes Sorry for late answer here. I understand your concern. I definitely think roles and permissions are outside of the scope for migrate. In terms of creating a database if it doesn't exist, I think it's worth adding since it's such a common task when setting up a local development environment.