feat(mysql): try creating database if not exist
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.
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 ?
@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.