This repository has been archived by the owner on Jun 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrations with CLI and without knexfile (knex#2884)
* 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`
- Loading branch information
1 parent
8ded57c
commit 94c9ea1
Showing
3 changed files
with
147 additions
and
17 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
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,94 @@ | ||
#!/usr/bin/env jake | ||
"use strict"; | ||
|
||
const os = require('os'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const sqlite3 = require('sqlite3'); | ||
const assert = require('assert'); | ||
|
||
const KNEX_PATH = path.normalize(__dirname + '/../../knex.js') | ||
const KNEX = path.normalize(__dirname + '/../../bin/cli.js') | ||
|
||
/* * * HELPERS * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | ||
|
||
function assertExec(cmd, desc) { | ||
if (!desc) desc = 'Run '+cmd | ||
return new Promise((resolve, reject)=> { | ||
var stderr = ''; | ||
var bin = jake.createExec([`KNEX_PATH=${KNEX_PATH} ${cmd}`]); | ||
bin.addListener('error', (msg, code)=> reject(Error(desc +' FAIL. '+ stderr))); | ||
bin.addListener('cmdEnd', resolve); | ||
bin.addListener('stderr', (data)=> stderr += data.toString()); | ||
bin.run(); | ||
}); | ||
} | ||
|
||
var taskList = []; | ||
function test(description, func) { | ||
var tempFolder, itFails=false; | ||
tempFolder = fs.mkdtempSync(os.tmpdir() + '/knex-test-'); | ||
fs.mkdirSync(tempFolder + '/migrations'); | ||
desc(description); | ||
let taskName = description.replace(/[^a-z0-9]/g, ''); | ||
taskList.push(taskName); | ||
task(taskName, {async: true}, ()=> func(tempFolder) | ||
.then(()=> console.log('☑ '+description) ) | ||
.catch((err)=> { | ||
console.log('☒ '+err.message); | ||
itFails = true; | ||
}) | ||
.then(()=> { | ||
jake.exec(`rm -r ${tempFolder}`); | ||
if (itFails) process.exit(1); | ||
}) | ||
); | ||
} | ||
|
||
|
||
/* * * TESTS * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | ||
|
||
test('Create a migration file', (temp)=> | ||
assertExec(`${KNEX} migrate:make \ | ||
--client=sqlite3 \ | ||
--migrations-directory=${temp}/migrations \ | ||
create_rule_table`) | ||
.then(()=> | ||
assertExec(`ls ${temp}/migrations/*_create_rule_table.js`, | ||
'Find the migration file') | ||
) | ||
.then(()=> | ||
assertExec(`grep exports.up ${temp}/migrations/*_create_rule_table.js`, | ||
'Migration created with boilerplate') | ||
) | ||
); | ||
|
||
test('Run migrations', (temp)=> | ||
(new Promise((resolve, reject)=> | ||
fs.writeFile(temp+'/migrations/000_create_rule_table.js', ` | ||
exports.up = (knex)=> knex.schema.createTable('rules', (table)=> { | ||
table.string('name'); | ||
}); | ||
exports.down = (knex)=> knex.schema.dropTable('rules'); | ||
`, (err)=> err ? reject(err) : resolve()) | ||
)) | ||
.then(()=> | ||
assertExec(`${KNEX} migrate:latest \ | ||
--client=sqlite3 --connection=${temp}/db \ | ||
--migrations-directory=${temp}/migrations \ | ||
create_rule_table`) | ||
) | ||
.then(()=> assertExec(`ls ${temp}/db`, 'Find the database file') ) | ||
.then(()=> new sqlite3.Database(temp+'/db') ) | ||
.then((db)=> | ||
new Promise((resolve, reject)=> | ||
db.get( | ||
'SELECT name FROM knex_migrations', | ||
function(err, row){ err ? reject(err) : resolve(row) } | ||
) | ||
) | ||
) | ||
.then((row)=> assert.equal(row.name, '000_create_rule_table.js') ) | ||
); | ||
|
||
task('default', taskList); |