Skip to content
This repository has been archived by the owner on Jun 28, 2018. It is now read-only.

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

JensRantil
Copy link
Contributor

@JensRantil JensRantil commented Sep 17, 2017

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.

Best-practise according to `gofmt`.
According to `gofmt`.
Golang best-practise. Cleanup made by `gofmt`.
**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.
@mattes
Copy link
Owner

mattes commented Sep 18, 2017

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
Copy link
Contributor Author

@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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants