Cli: sequelize-cli db:migrate always using development environment

Created on 9 Oct 2019  路  8Comments  路  Source: sequelize/cli

I am using dotenv package. I reuire the package on top. I also tried changing "development" to "test" or "production", but still It's always using "development".

'use strict';
require('dotenv').config({path:'../../.env'})
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development'; // I tried changing 'development' to 'production' and 'test'
const config = require(__dirname + '/../config/config.json')[env];
const db = {};
let sequelize;
console.log(env)
if (config.use_env_variable) {
  sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  console.log(config)
  sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
  .readdirSync(__dirname)
  .filter(file => {
    return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
  })
  .forEach(file => {
    const model = sequelize['import'](path.join(__dirname, file));
    db[model.name] = model;
  });

Object.keys(db).forEach(modelName => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

Here is my .env file

//set environment for different configurations 
NODE_ENV=test

//define port
PORT=4000

NODE_CONFIG_DIR =./src/config

When I run the server, then It uses test environment.

Most helpful comment

Oops, I just use this npx sequelize db:migrate --env testing

All 8 comments

Same as u, I was wonder how to migrate db to testing server

Oops, I just use this npx sequelize db:migrate --env testing

You can just do this all time --env production, but I don't like it this way. It would make sense to just let things works as it should and use my .env file instead

Yes, exactly. I don't want passing the parameter all the time, instead, It should pick the appropriate configurations as defined in .env file.
NODE_ENV = test
In below file, as you can see it is checking for if NODE_ENV is set, if not use development.
Even I am getting this error.

WARNING: NODE_ENV value of 'test' did not match any deployment config file names.

Below is the configurations

  "test": {
    "username": "root",
    "password": null,
    "database": "database_test",
    "host": "127.0.0.1",
    "dialect": "mysql",
    "operatorsAliases": false
  },

_But why it is using development always, developer of this package should answer_

'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
  sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  config.define = {
    "createdAt":"created_on",
    "updatedAt":"updated_on"
  }
  sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
  .readdirSync(__dirname)
  .filter(file => {
    return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
  })
  .forEach(file => {
    const model = sequelize['import'](path.join(__dirname, file));
    db[model.name] = model;
  });

Object.keys(db).forEach(modelName => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

Yep, I have the same problem too.

I am running 'sequelize db:migrate --env test' to get my test database up-to-date - it'll be a fairly infrequent action, so it's not terrible, but definitely not the best way to go about this.

Did anyone figure this out?
Thanks!

The workaround that I found working is, I run a JS file to initiate my NODE_ENV and then I run the sequelize cli migration and it works. Having that said, if your server is running and you init your ENV variables, then surely sequelize will pick the correct value.

Also try to use JS configuration file instead of JSON, there you can load your .env and have it working as you like.

Phew! I have found the solution. You have to set the environment variables in the current shell.

Run

export DATABASE_URL=path_to_your_database

to export your variables one at a time. I am using bash with linux.
Better still, you can have another .env file and name it .env.local or .env.test and add the variables with the export keyword for example:

export DATABASE_URL=path_to_your_database
export NODE_ENV=development_or_test
export ANOTHER_VARIABLE=another variable

With that you can run

source .env.local

in the terminal and all the variables will be set. You can now run your migrations.

I have to do something like this in my package.json file.

 "scripts": {
    "test": "env NODE_ENV=test nyc mocha --recursive --exit --timeout 100000",
    "start": "env NODE_ENV=development nodemon -L server.js"
  },
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Jakobud picture Jakobud  路  4Comments

axetroy picture axetroy  路  3Comments

rmoura-92 picture rmoura-92  路  4Comments

KaltZK picture KaltZK  路  5Comments

OsoianMarcel picture OsoianMarcel  路  4Comments