-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix support for default knexfile in working dir (#2941)
* Fix support for default knexfile in working dir * Bump version * Remove incorrect comment
- Loading branch information
Showing
9 changed files
with
155 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const { DEFAULT_EXT } = require('./constants'); | ||
const { resolveClientNameWithAliases } = require('../../lib/helpers'); | ||
const fs = require('fs'); | ||
|
||
function mkConfigObj(opts) { | ||
if (!opts.client) { | ||
const path = resolveDefaultKnexfilePath(); | ||
throw new Error( | ||
`No default configuration file '${path}' found and no commandline connection parameters passed` | ||
); | ||
} | ||
|
||
const envName = opts.env || process.env.NODE_ENV || 'development'; | ||
const resolvedClientName = resolveClientNameWithAliases(opts.client); | ||
const useNullAsDefault = resolvedClientName === 'sqlite3'; | ||
return { | ||
ext: DEFAULT_EXT, | ||
[envName]: { | ||
useNullAsDefault, | ||
client: opts.client, | ||
connection: opts.connection, | ||
migrations: { | ||
directory: opts.migrationsDirectory, | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
function tryLoadingDefaultConfiguration() { | ||
const path = resolveDefaultKnexfilePath(); | ||
if (fs.existsSync(path)) { | ||
return require(path); | ||
} | ||
} | ||
|
||
function resolveDefaultKnexfilePath() { | ||
return process.cwd() + '/knexfile.js'; | ||
} | ||
|
||
module.exports = { | ||
mkConfigObj, | ||
tryLoadingDefaultConfiguration, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const DEFAULT_EXT = 'js'; | ||
|
||
module.exports = { | ||
DEFAULT_EXT, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#!/usr/bin/env jake | ||
'use strict'; | ||
/* eslint-disable no-undef */ | ||
/* eslint-disable no-console */ | ||
|
||
const path = require('path'); | ||
const { | ||
assertExec, | ||
assertExecError, | ||
test, | ||
} = require('../../jake-util/helpers/migration-test-helper'); | ||
const { assert } = require('chai'); | ||
const fs = require('fs'); | ||
const rimraf = require('rimraf'); | ||
|
||
const KNEX = path.normalize(__dirname + '/../../../bin/cli.js'); | ||
|
||
const taskList = []; | ||
/* * * TESTS * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | ||
|
||
test(taskList, 'Run migrations with knexfile passed', (temp) => { | ||
return assertExec( | ||
`node ${KNEX} migrate:latest --knexfile=test/jake-util/knexfile/knexfile.js --knexpath=../knex.js` | ||
); | ||
}); | ||
|
||
test(taskList, 'Throws informative error when no knexfile is found', (temp) => { | ||
return assertExecError( | ||
`node ${KNEX} migrate:latest --knexpath=../knex.js` | ||
).then((err) => { | ||
assert.include(err, 'No default configuration file'); | ||
}); | ||
}); | ||
|
||
test( | ||
taskList, | ||
'Resolves default knexfile in working directory correctly', | ||
(temp) => { | ||
const path = process.cwd() + '/knexfile.js'; | ||
let error; | ||
fs.writeFileSync( | ||
path, | ||
` | ||
module.exports = { | ||
client: 'sqlite3', | ||
connection: { | ||
filename: __dirname + '/test/jake-util/test.sqlite3', | ||
}, | ||
migrations: { | ||
directory: __dirname + '/test//jake-util/knexfile_migrations', | ||
}, | ||
}; | ||
` | ||
); | ||
return assertExec(`node ${KNEX} migrate:latest --knexpath=../knex.js`) | ||
.catch((err) => { | ||
error = err; | ||
}) | ||
.then(() => { | ||
rimraf.sync(path); | ||
if (error) { | ||
throw error; | ||
} | ||
}); | ||
} | ||
); | ||
|
||
test(taskList, 'resolves knexfile correctly with cwd specified', (temp) => { | ||
return assertExec( | ||
`node ${KNEX} migrate:latest --cwd=test/jake-util/knexfile --knexfile=knexfile.js` | ||
); | ||
}); | ||
|
||
module.exports = { | ||
taskList, | ||
}; |
This file was deleted.
Oops, something went wrong.
File renamed without changes.