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

`db-migrate db:create` not working as intended #468

Closed
LeeDownshift opened this issue Mar 21, 2017 · 11 comments · May be fixed by #644

Comments

@LeeDownshift
Copy link

commented Mar 21, 2017

I'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.


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@c100k

This comment has been minimized.

Copy link

commented Apr 20, 2017

Just had the same problem. Maybe you should rename the issue because that is a big issue.
db:create tries to connect to db before creating it.

@deegale

This comment has been minimized.

Copy link

commented Jun 9, 2017

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, then db-migrate db:create mydatabase works fine. But then none of the migrations will work until you put the database name back in database.json

What I expected was:

  • running db-migrate db:create (no database name specified) will attempt to create a database with the name as specified in database.json If there is nothing specified, it will error out.
  • running 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 in database.json

EDIT: I'm using MySql and I don't know if this same issue applies to the other database servers.

@stale

This comment has been minimized.

Copy link

commented Nov 23, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Nov 23, 2017

@stale stale bot closed this Nov 30, 2017

@bernardocs

This comment has been minimized.

Copy link

commented Dec 5, 2017

I'm also having the same issue.

If I don't specify the database at database.json and run db:create [db name] it will create successfully. But if any database is specified at the configuration it'll try to connect to it before running anything.

I think the expected behavior should be like @deegale described.

@zaidqureshi2

This comment has been minimized.

Copy link

commented Dec 6, 2017

I am having an issue where when I do db-migrate db:create <dbname> it errors out saying database "zaidq" does not exist where I think "zaidq" is my pc user

@oroce

This comment has been minimized.

Copy link

commented Dec 7, 2017

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

First I have the following .env:

MYSQL_URL=mysql://root:root@localhost/myapp

and the database.json:

{
  "defaultEnv": "mysql",
  "mysql": {
      "driver": "mysql",
      "url": { "ENV": "MYSQL_URL" },
      "multipleStatements": true
   }
}

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 of CONNECT_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:

{
  "defaultEnv": "mysql",
  "mysql": {
      "driver": "mysql",
      "url": { "ENV": "MYSQL_URL" },
      "flags": { "ENV": "MYSQL_FLAGS" },
      "multipleStatements": true
   }
}

The commands I use:

  • db creation: MYSQL_FLAGS="-CONNECT_WITH_DB" db-migrate sync
  • db sync: db-migrate sync

Since the creation and syncing are two separate steps a new connection will be created everytime.

Hope this helps you!

@Ajaxy

This comment has been minimized.

Copy link

commented May 8, 2018

Any update on this?
Or a solution for non-mysql engines?

@chamiz

This comment has been minimized.

Copy link

commented Aug 11, 2018

@oroce
After doing your changes I was able to create db using blow commend
MYSQL_FLAGS="-CONNECT_WITH_DB" db-migrate db:create myapp

Then no issues in
db-migrate up

Thanks for help

Lercerss added a commit to Lercerss/SOEN343 that referenced this issue Sep 25, 2018
Fixed database creation using db-migrate
Due to issue db-migrate/node-db-migrate#468 we
have to make sure not to specify a database when running `db-migrate
db:create`
Lercerss added a commit to Lercerss/SOEN343 that referenced this issue Sep 25, 2018
Fixed database creation using db-migrate #9
Due to issue db-migrate/node-db-migrate#468 we
have to make sure not to specify a database when running `db-migrate
db:create`
@aprakash-sovrn

This comment has been minimized.

Copy link

commented Apr 17, 2019

This occurs when trying to use Postgres as well. My driver is "pg", and I've installed db-migrate-pg.

>  db-migrate db:create testdb -e local
[ERROR] AssertionError [ERR_ASSERTION]: ifError got unwanted exception: database "testdb" does not exist
    at /usr/local/lib/node_modules/db-migrate/lib/commands/db.js:17:12
    at /usr/local/lib/node_modules/db-migrate/lib/driver/index.js:95:9
    at /usr/local/lib/node_modules/db-migrate-pg/index.js:662:7

I have tried with and without adding "flags": "-CONNECT_WITH_DB" to my database.json config, in case that wasn't a setting specific to MySql.

On db-migrate 0.11.3, btw.

@RenWenshan

This comment has been minimized.

Copy link

commented Aug 14, 2019

Experiencing the same issue. I guess it tries to connect first using the config, then run db:create

RenWenshan pushed a commit to RenWenshan/node-db-migrate that referenced this issue Aug 14, 2019
RenWenshan added a commit to RenWenshan/node-db-migrate that referenced this issue Aug 14, 2019
fix: db-migrate#468
* avoid connecting to database before creation.

Signed-off-by: Wenshan Ren <renws1990@gmail.com>
@RenWenshan

This comment has been minimized.

Copy link

commented Aug 15, 2019

I've added a fix, ready for review @wzrdtales

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
10 participants
You can’t perform that action at this time.