question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

<bountysource-plugin>

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource. </bountysource-plugin>

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:19
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

15reactions
deegalecommented, 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.

8reactions
orocecommented, 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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rails db:migrate / db:create. Existed databases conflict
The problem is: When i'm running db:migrate/create/drop Rails is trying to manipulate my existing DB's instead of creation new ones.
Read more >
Rails DB Migrations [Guide + Code] — Cheatsheet
It's “correct” as it shows that the migration is one-way only and should not/cannot be reversed. This can be long if the model...
Read more >
Getting Started with Rails - Ruby on Rails Guides
Getting Started with Rails. This guide covers getting up and running with Ruby on Rails. After reading it, you should be familiar with:....
Read more >
Rails Migration A Complete Guide - Stackify
Let's take a look at Rails migration, why you might need Rails migrations, and walk through examples to show you how to troubleshoot...
Read more >
WSL2 & Dockerfile: rake db:create gives ArgumentError - Reddit
Doing a school assignment with Dockerfile with Docker running ... I kept getting an error on the rake db:create step and not sure...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found