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

npx sequelize-cli db:migrate command don't read the environment variables with PM2 #1514

Open
aobregonmx opened this issue Jul 12, 2024 · 3 comments

Comments

@aobregonmx
Copy link

aobregonmx commented Jul 12, 2024

What you are doing?

npx sequelize-cli db:migrate command don't read the environment variables

This command works without problems in my local environment on macOS Sonoma 14.2.1 but when I try to run it in a development environment (PM2 version 5.3.1) it does not work

.sequelizerc

const path = require('path');

module.exports = {
    'config': path.resolve('src/db/config', 'config.cjs'),
    'models-path': '../src/db/models/',
    'migrations-path': path.resolve('src/db', 'migrations'),
    'seeders-path': path.resolve('src/db', 'seeders'),
}

config.cjs

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

module.exports = {
  development: {
    username: config.dbUser,
    password: config.dbPassword,
    database: config.dbName,
    host: config.dbHost,
    dialect: 'postgres',
    migrationStorageTableSchema: 'vic',
    logging: !config.isProd,
  },
  test: {
    username: 'root',
    password: null,
    database: 'database_test',
    host: '127.0.0.1',
    dialect: 'postgres',
  },
  production: {
    username: 'root',
    password: null,
    database: 'database_production',
    host: '127.0.0.1',
    dialect: 'postgres',
  },
};

common/config.js

require('dotenv').config();

const config = {
  env: process.env.NODE_ENV || 'dev',
  host: process.env.HOST || 'localhost',
  port: process.env.PORT || 3000,
  dbHost: process.env.DB_HOST,
  dbPort: process.env.DB_PORT,
  dbName: process.env.DB_NAME,
  dbUser: process.env.DB_USER,
  dbPassword: process.env.DB_PASSWORD,
  isProd: process.env.NODE_ENV === 'production',
};

module.exports = config;

What do you expect to happen?

Get a message similar to the one obtained in my local macOS environment

> js-backend@0.0.0 migrations:run
> sequelize-cli db:migrate


Sequelize CLI [Node: 20.11.0, CLI: 6.6.2, ORM: 6.37.3]

Loaded configuration file "src/db/config/config.cjs".
Using environment "development".
Executing (default): SELECT 1+1 AS result
Executing (default): CREATE SCHEMA IF NOT EXISTS "vic";
Executing (default): SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_type LIKE '%TABLE' AND table_name != 'spatial_ref_sys';
Executing (default): SELECT pk.constraint_type as "Constraint",c.column_name as "Field", c.column_default as "Default",c.is_nullable as "Null", (CASE WHEN c.udt_name = 'hstore' THEN c.udt_name ELSE c.data_type END) || (CASE WHEN c.character_maximum_length IS NOT NULL THEN '(' || c.character_maximum_length || ')' ELSE '' END) as "Type", (SELECT array_agg(e.enumlabel) FROM pg_catalog.pg_type t JOIN pg_catalog.pg_enum e ON t.oid=e.enumtypid WHERE t.typname=c.udt_name) AS "special", (SELECT pgd.description FROM pg_catalog.pg_statio_all_tables AS st INNER JOIN pg_catalog.pg_description pgd on (pgd.objoid=st.relid) WHERE c.ordinal_position=pgd.objsubid AND c.table_name=st.relname) AS "Comment" FROM information_schema.columns c LEFT JOIN (SELECT tc.table_schema, tc.table_name, cu.column_name, tc.constraint_type FROM information_schema.TABLE_CONSTRAINTS tc JOIN information_schema.KEY_COLUMN_USAGE  cu ON tc.table_schema=cu.table_schema and tc.table_name=cu.table_name and tc.constraint_name=cu.constraint_name and tc.constraint_type='PRIMARY KEY') pk ON pk.table_schema=c.table_schema AND pk.table_name=c.table_name AND pk.column_name=c.column_name WHERE c.table_name = 'SequelizeMeta' AND c.table_schema = 'public'
Executing (default): SELECT table_name FROM information_schema.tables WHERE table_schema = 'vic' AND table_name = 'SequelizeMeta'
Executing (default): SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a, pg_namespace s WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND t.relkind = 'r' and t.relname = 'SequelizeMeta' AND s.oid = t.relnamespace AND s.nspname = 'vic' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;
Executing (default): SELECT "name" FROM "vic"."SequelizeMeta" AS "SequelizeMeta" ORDER BY "SequelizeMeta"."name" ASC;

What is actually happening?

I am getting this message in the terminal, where it is not reading the environment variables and is not creating the models in the database

Sequelize CLI [Node: 20.11.0, CLI: 6.6.2, ORM: 6.37.3]

Loaded configuration file "src/db/config/config.cjs".
Using environment "development".

ERROR: connect ECONNREFUSED 127.0.0.1:5432

I tried printing the variables and either way I'm getting undefined

Dialect: postgres
Database version: PostgreSQL 12.19 (Ubuntu 12.19-0ubuntu0.20.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.4.0-1ubuntu1~20.04.3) 9.4.0, 64-bit
Sequelize CLI version: 6.6.2
Sequelize version: 6.37.3
PM2 version: 5.3.1

@aobregonmx aobregonmx changed the title npx sequelize-cli db:migrate command don't read the environment variables npx sequelize-cli db:migrate command don't read the environment variables with PM2 Jul 24, 2024
@aobregonmx
Copy link
Author

Workaround
My application runs inside PM2 and the environment variables are injected from the ecosystem.config.js configuration file

After creating the .env file with the environment variables, the sequelize-cli db:migrate command ran without errors

I think sequelize-cli can't read environment variables injected by PM2

@himharsh1997
Copy link

himharsh1997 commented Aug 23, 2024

@aobregonmx I try to run sequelize-cli command in the ec2 instance with inline environment variables but it doesn't work. To resolve the issue i need to set env globally for the current shell session using the command
export <ENV_NAME>=<ENV_VALUE>
<COMMAND_TO_RUN>

Is the sequelize-cli not able to use an inline environment variable.

@aobregonmx
Copy link
Author

I think sequelize-cli runs outside of the PM2 environment and that's why it doesn't read the injected variables. When I have implemented CI it will carry out the process of creating the .env file as indicated in the comments of the official documentation Environment Variables Management

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants