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

feat: added db:info command #1172

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ insert_final_newline = true
[*.md]
trim_trailing_whitespace = false
indent_size = 4

[{Makefile,**.mk}]
# Use tabs for indentation (Makefiles require tabs)
indent_style = tab
36 changes: 36 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"development": {
"username": "root",
"password": "secret_pass",
"database": "database_development",
"host": "127.0.0.1",
"dialect": "mysql",
"pool": {
"maxConnections": 1,
"some": {
"other": {
"nested": {
"object": "here"
}
}
}
}
},
"env": {
"use_env_variable": "postgresql://[user[:password]@][netlocation][:port][/dbname]"
},
"test": {
"username": "root",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "mysql"
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
}
}
30 changes: 30 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
build:
npm run build

info:
@echo "INFO" && \
lib/sequelize db:info

fail:
@echo "FAIL" && \
NODE_ENV=nope lib/sequelize db:info

raw:
@echo "RAW" && lib/sequelize db:info --raw

show:
@echo "PWD" && lib/sequelize db:info --show-password

env:
@echo "ENV" && NODE_ENV=env lib/sequelize db:info

env-pass:
@echo "ENV" && NODE_ENV=env lib/sequelize db:info --show-password

run: build show
@echo "----\n"

loop:
while inotifywait -q -e modify src/**; \
do clear; make run; done;

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"pretty": "prettier src test --write",
"prepare": "husky install && npm run build",
"test-raw": "mocha 'test/**/*.test.js'",
"test": "npm run lint && npm run build && npm run test-raw"
"test": "npm run lint && npm run build && npm run test-raw",
"test-one": "mocha 'test/db/db-info.test.js' --timeout 5000"
},
"repository": {
"type": "git",
Expand Down
65 changes: 65 additions & 0 deletions src/commands/database_info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import process from 'process';
import { _baseOptions } from '../core/yargs';

import helpers from '../helpers';
import { pick } from 'lodash';
import clc from 'cli-color';

exports.builder = (yargs) => {
_baseOptions(yargs)
.option('show-password', {
describe: 'Will show password as plain text',
type: 'boolean',
default: false,
})
.option('raw', {
describe: 'Display the raw object',
type: 'boolean',
default: false,
}).argv;
};

exports.handler = async function (args) {
const command = args._[0];

// legacy, gulp used to do this
await helpers.config.init();

const options = pick(args, ['showPassword', 'raw']);

switch (command) {
case 'db:info':
displayConfigObject(options);
break;
}

process.exit(0);
};

function displayConfigObject(options) {
let config = null;

try {
config = helpers.config.readConfig();
} catch (e) {
helpers.view.error(e);
}

if (!options.showPassword) {
config.password = '***';
}

helpers.view.log();
helpers.view.log(clc.bold('Sequelize configuration:'));

options.raw
? helpers.view.log(JSON.stringify(config, null, 2))
: display(config);
}

function display(i, p = '') {
Object.entries(i).forEach(([k, v]) => {
if (typeof v == 'object' && v !== null) display(v, `${p}${k}.`);
else helpers.view.log(`${p}${k}:`, v);
});
}
2 changes: 2 additions & 0 deletions src/sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import migrationGenerate from './commands/migration_generate';
import modelGenerate from './commands/model_generate';
import seedGenerate from './commands/seed_generate';
import database from './commands/database';
import databaseInfo from './commands/database_info';

import helpers from './helpers/index';

Expand All @@ -35,6 +36,7 @@ yargs
.command('db:seed:undo', 'Deletes data from the database', seedOne)
.command('db:seed:all', 'Run every seeder', seed)
.command('db:seed:undo:all', 'Deletes data from the database', seed)
.command('db:info', 'Displays the database configuration', databaseInfo)
.command('db:create', 'Create database specified by configuration', database)
.command('db:drop', 'Drop database specified by configuration', database)
.command('init', 'Initializes project', init)
Expand Down
74 changes: 74 additions & 0 deletions test/db/db-info.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const expect = require('expect.js');
const Support = require(__dirname + '/../support');
const helpers = require(__dirname + '/../support/helpers');
const gulp = require('gulp');
const _ = require('lodash');

['db:info'].forEach((flag) => {
describe(Support.getTestDialectTeaser(flag), function () {
this.timeout(5000);

if (Support.dialectIsPostgres() || Support.dialectIsMySQL()) {
console.log("we're testing");
const combineFlags = function (flags) {
let result = flag;

_.forEach(flags || {}, (value, key) => {
result = result + ' --' + key;
});

return result;
};

const prepare = function (options, callback) {
options = _.assign(
{
flags: {},
cli: { pipeStdout: true },
},
options || {}
);

console.log('fl', combineFlags(options.flags));

const configPath = 'config/config.json';
const config = _.assign(
{ password: 'secret_password' },
helpers.getTestConfig(),
options.config
);
const configContent = JSON.stringify(config);

gulp
.src(Support.resolveSupportPath('tmp'))
.pipe(helpers.clearDirectory())
.pipe(helpers.runCli('init'))
.pipe(helpers.runCli('db:create'))
.pipe(
// helpers.overwriteFile(helpers.getTestConfig(), 'config/config.json')
helpers.overwriteFile(configContent, configPath)
)
.pipe(helpers.runCli(flag, { pipeStdout: true }))
// .pipe(helpers.runCli(combineFlags(options.flags), options.cli))
.pipe(helpers.teardown(callback));
};

describe('show-password', function () {
it('hides the password', function (done) {
prepare({}, (err, stdout) => {
expect(stdout).to.contain('password: ***');
done();
});
});

it.only('reveals the password', function (done) {
prepare({ flags: { 'show-password': true } }, done);
// prepare({ flags: { showPassword: true } }, (err, stdout) => {
// expect(stdout).not.to.contain('password: ***');
// done();
// });
});
});
}
});
});