`db-migrate db:create` not working as intended
See original GitHub issueI’ve been playing around for most of the morning with db-migrate and getting myself familiar with the ins and outs. One thing I have noticed is that the db:create command doesn’t seem to work as I would have expected it.
For example in my database.json
I have configuration for a test database and a dev database. Now, when I run the db-migrate db:create myapptest
I would expect to see that it creates a database to run my tests against. instead I get the following error:
[ERROR] Error: ER_BAD_DB_ERROR: Unknown database 'myapptest'
the whole reason for running the db:create
command is because that database doesn’t exist and I want to create it.
Yet if I create the database and then run the command passing a a random name it will happily create any database I want. It will also claim that I have created the same database multiple times even when it has in fact created it once.
<bountysource-plugin>Want to back this issue? Post a bounty on it! We accept bounties via Bountysource. </bountysource-plugin>
Issue Analytics
- State:
- Created 7 years ago
- Reactions:19
- Comments:11 (2 by maintainers)
Top GitHub Comments
I ran into this as well when trying to set up a new project using db-migrate.
If you remove the database name from
database.json
, thendb-migrate db:create mydatabase
works fine. But then none of the migrations will work until you put the database name back indatabase.json
What I expected was:
db-migrate db:create
(no database name specified) will attempt to create a database with the name as specified indatabase.json
If there is nothing specified, it will error out.db-migrate db:create nameofdb
will attempt to create “nameofdb”, if it doesn’t already exist. If it does already exist, it will report that. This will override whatever database is specified indatabase.json
EDIT: I’m using MySql and I don’t know if this same issue applies to the other database servers.
@zaidqureshi2 @bernardocs @deegale I just solved with some magic:
TLDR: Turning off the magic flag of
CONNECT_WITH_DB
: https://github.com/mysqljs/mysql#default-flagsFirst I have the following
.env
:and the database.json:
So a basic configuration. The
db-migrate db:create myapp
fails because the database does not exists. So I either remove the database from the url but then my application won’t know about which db should be used or I turn off the flag ofCONNECT_WITH_DB
in the mysql connection, but I want to do this only for the db:create, for the rest of the commands I want to have database there. So this is what I did:The commands I use:
MYSQL_FLAGS="-CONNECT_WITH_DB" db-migrate sync
db-migrate sync
Since the creation and syncing are two separate steps a new connection will be created everytime.
Hope this helps you!