1

I'm trying to get started with knex.js and I can't get migrations to work. Knex works fine for my API calls. Here's my setup:

knexfile.js

const env = process.env;

module.exports = {
  client: 'mysql',
  connection: {
    host: env.DB_HOST,
    database: env.DB_NAME,
    user:     env.DB_USER,
    password: env.DB_PASSWORD,
    port: env.PORT
  },
  pool: {
    min: 0,
    max: 50
  },
  migrations: {
    directory: './db/migrations',
    tableName: 'knex_migrations'
  },
  seeds: {
    directory: './db/seeds'
  }
};

knex.js

const config = require('../knexfile.js');

module.exports = require('knex')(config);

events.js

const express = require('express');
const router = express.Router();
const knex = require('../../db/knex.js');

// GET api/events
router.get('/', (req, res) => {
    knex('events')
        .then(events => { res.send(events) }
        .catch(err => { console.log(err); })
});

module.exports = router;

and then I have a file in the migrations folder with:

exports.up = function(knex) {
  return knex.schema.createTable('users', function (t) {
    t.increments('id').primary()
    t.string('username').notNullable()
    t.string('password').notNullable()
    t.timestamps(false, true)
  }).then(() => { console.log('created users table') })
    .catch((err) => { throw err} )
    .finally(() => { knex.destroy() })
};

exports.down = function(knex) {
  return knex.schema.dropTableIfExists('users')
};

When I run knex migrate:latest I get TimeoutError: Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?

I know similar questions have been asked before, but I can't seem to find any that shed light on my particular situation. I've tried adding a knex.destroy() to the end of my GET request but that doesn't seem to help (it just makes the connection unusable if I add other request handlers below).

I did try checking the knex.client.pool in a finally clause at the end of the GET request. numUsed was 0, numFree was 1, numPendingAcquires and numPendingCreates were both 0. I do find it odd that numFree was only 1 given that my knexfile specifies max 50. Any advice greatly appreciated.

user6647072
  • 131
  • 2
  • 17
  • Making your pool's min 0 has a funny smell to me. The default min is 2 from the knexfile is 2. I would make it at least 2. – technogeek1995 Aug 07 '19 at 15:03
  • Yeah, I had it at 2 originally when I first observed the problem. I saw that some people had suggested setting it to 0 for similar problems. It didn't seem to have an effect in my case. – user6647072 Aug 08 '19 at 03:48
  • Is your database on the same system that you're running Knex (eg: local host)? Can you connect through [MySQL's Shell](https://dev.mysql.com/downloads/shell/)? – technogeek1995 Aug 08 '19 at 13:20
  • It's not on the same system, no -- it's a remote database. I'm able to connect fine with knex in the GET request above. I just tried connecting through mysqlsh and the connection works, although I wasn't able to test any actual queries as I'm not familiar with mysqlsh and couldn't quite figure out how to execute them. This gives me an idea though -- do I need to add credentials to the `knex migrate` call? I was assuming it was reading from my `knexfile`, but it would make sense if not. – user6647072 Aug 08 '19 at 20:42
  • 1
    Yes it can be tricky for `knex migrate:latest`. **You have to be in the same folder as your `knexfile.js` to use the cli interface**. There is a cli option to specify a path, but usually I would recommend just `cd` to the correct directory since your troubleshooting. I'm not sure if the cli interface can read from the env file. I have seen people do this, but I've never tried it. I would see if you can console log env in your `knexfile.js` when you run `knex migrate:latest` – technogeek1995 Aug 09 '19 at 15:12

1 Answers1

1

Following @technogeek1995's comment, the answer turned out to be adding require('dotenv').config({path: '../.env'}); to knexfile.js (in retrospect, this part seems obvious), and running the cli from the same directory. Hope this helps someone else.

user6647072
  • 131
  • 2
  • 17