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

Migrations with CLI and without knexfile #2884

Merged
merged 3 commits into from Nov 6, 2018
Merged

Conversation

aurium
Copy link
Contributor

@aurium aurium commented Oct 28, 2018

Allows to create and run migrations.

closes #2819

Allows to create and run migrations.

closes knex#2819
bin/cli.js Outdated
if (!env.configPath) {
exit('No knexfile found in this directory. Specify a path with --knexfile');
if (!env.configuration) {
if (commander.client) mkConfigObj(env, commander);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you run prettier formatting before commit?..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Is it necessary?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, in this particular case I wonder if it would have removed the one-liner; typically it's encouraged to always enclose then statements in { } :)

bin/cli.js Outdated
@@ -52,7 +70,10 @@ function initKnex(env) {

var environment = commander.env || process.env.NODE_ENV;
var defaultEnv = 'development';
var config = require(env.configPath);
var config =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you consider replacing all vars in this file with let/const?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could, however there are many "var's" in the same scope and upper. I could do this in another PR to isolate the changes.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough :)

bin/cli.js Outdated
@@ -52,7 +70,10 @@ function initKnex(env) {

var environment = commander.env || process.env.NODE_ENV;
var defaultEnv = 'development';
var config = require(env.configPath);
var config =
typeof env.configuration == 'object'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we depend on Lodash anyway, you could just use _.isObject

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Changing...

bin/cli.js Outdated
function mkConfigObj(env, commander) {
env.configuration = {
ext: 'js',
[commander.env || process.env.NODE_ENV || 'development']: {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is very difficult to read. Could you please split it somewhat? Inline condition inside key name resolution is heavy :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true. :-)

bin/cli.js Outdated
@@ -35,11 +35,29 @@ function checkLocalModule(env) {
}
}

function initKnex(env) {
function mkConfigObj(env, commander) {
env.configuration = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this is mutating passed params? Can this be done in a more immutable way?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

bin/cli.js Outdated
@@ -90,6 +112,12 @@ function invoke(env) {
.option('--debug', 'Run with debugging.')
.option('--knexfile [path]', 'Specify the knexfile path.')
.option('--cwd [path]', 'Specify the working directory.')
.option('--client [name]', 'Set DB client whitout a knexfile.')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"whitout " -> typo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

corrected

bin/cli.js Outdated
@@ -90,6 +112,12 @@ function invoke(env) {
.option('--debug', 'Run with debugging.')
.option('--knexfile [path]', 'Specify the knexfile path.')
.option('--cwd [path]', 'Specify the working directory.')
.option('--client [name]', 'Set DB client whitout a knexfile.')
.option('--connection [address]', 'Set DB connection whitout a knexfile.')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

bin/cli.js Outdated
@@ -136,8 +164,12 @@ function invoke(env) {
'Specify the stub extension (default js)'
)
.action(function(name) {
var instance = initKnex(env);
var ext = (argv.x || env.configPath.split('.').pop()).toLowerCase();
var instance = initKnex(env, commander);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to pass commander instance for internal calls? can't we pass resolved params instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a method to do that on commander API? Can't find.

var instance = initKnex(env);
var ext = (argv.x || env.configPath.split('.').pop()).toLowerCase();
var instance = initKnex(env, commander);
var ext = (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might make sense to extract to internal function with self-explanatory name or at least add comment with explanation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now using initKnex(env, commander.opts()). looks more explicit on its intention.

@@ -0,0 +1,42 @@
#!/bin/bash -e

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't it be simpler to just write a Jake (https://www.npmjs.com/package/jake) script within usual unit test?

@kibertoad
Copy link
Collaborator

@aurium Thank you for your contribution!

@kibertoad
Copy link
Collaborator

CI is failing with "test/migrate-cli.sh: line 4: realpath: command not found" error, btw.

Thanks to @kibertoad comments!
The bigger change was to remove the bash script to test knex's cli command, and replace it with a jake file. I believe this jake file may be used as a base for future cli tests.
@kibertoad
Copy link
Collaborator

@aurium Tests are failing on node 6 and 8 now. I think "fs.promises" is only available since Node 10, so probably you would have to promisify fs methods manually (e. g. using bluebird).

@kibertoad
Copy link
Collaborator

LGTM! @elhigu, what do you think?

@elhigu
Copy link
Member

elhigu commented Nov 6, 2018

Hey... awesome client tests! 🎉 Now we can start finishing all the other client PRs which were deferred because of missing tests 😊

@elhigu
Copy link
Member

elhigu commented Nov 6, 2018

And great work everyone!

@elhigu elhigu merged commit bb5da4b into knex:master Nov 6, 2018
mwilliammyers pushed a commit to voxjar/knex that referenced this pull request Dec 12, 2018
* Migrations with CLI and without knexfile

Allows to create and run migrations.

closes knex#2819

* Apply tweaks to migrate without knexfile

Thanks to @kibertoad comments!
The bigger change was to remove the bash script to test knex's cli command, and replace it with a jake file. I believe this jake file may be used as a base for future cli tests.

* Replace `fs.promises` with `new Promise`
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

Successfully merging this pull request may close these issues.

Create migrations with the CLI without knexfile
3 participants