mirror of
https://github.com/knex/knex.git
synced 2025-11-11 23:54:44 +00:00
Feature/add rollback all to cli (#3187)
* Add --all flag for rollback in cli * Set up test for rollback --all cli migration command * Fix test for cli migrate:rollback --all
This commit is contained in:
parent
337639c2da
commit
75df3b6f7b
@ -206,11 +206,14 @@ function invoke(env) {
|
|||||||
|
|
||||||
commander
|
commander
|
||||||
.command('migrate:rollback')
|
.command('migrate:rollback')
|
||||||
.description(' Rollback the last set of migrations performed.')
|
.description(' Rollback the last batch of migrations performed.')
|
||||||
|
.option('--all', 'rollback all completed migrations')
|
||||||
.option('--verbose', 'verbose')
|
.option('--verbose', 'verbose')
|
||||||
.action(() => {
|
.action((cmd) => {
|
||||||
|
const { all } = cmd;
|
||||||
|
|
||||||
pending = initKnex(env, commander.opts())
|
pending = initKnex(env, commander.opts())
|
||||||
.migrate.rollback()
|
.migrate.rollback(null, all)
|
||||||
.spread((batchNo, log) => {
|
.spread((batchNo, log) => {
|
||||||
if (log.length === 0) {
|
if (log.length === 0) {
|
||||||
success(color.cyan('Already at the base migration'));
|
success(color.cyan('Already at the base migration'));
|
||||||
|
|||||||
@ -151,6 +151,142 @@ test('migrate:rollback prints verbose logs', (temp) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('migrate:rollback --all rolls back all completed migrations', (temp) => {
|
||||||
|
const migrationFile1 = '001_create_users_table.js';
|
||||||
|
const migrationFile2 = '002_add_age_column_to_users_table.js';
|
||||||
|
const migrationFile3 = '003_add_last_name_column_to_users_table.js';
|
||||||
|
const migrationFile4 = '004_add_email_to_users_table.js';
|
||||||
|
|
||||||
|
fs.writeFileSync(
|
||||||
|
`${temp}/migrations/${migrationFile1}`,
|
||||||
|
`
|
||||||
|
exports.up = (knex) => knex.schema
|
||||||
|
.createTable('users', (table) => {
|
||||||
|
table.string('first_name');
|
||||||
|
});
|
||||||
|
|
||||||
|
exports.down = (knex) => knex.schema.dropTable('users');
|
||||||
|
`
|
||||||
|
);
|
||||||
|
|
||||||
|
return assertExec(
|
||||||
|
`node ${KNEX} migrate:latest \
|
||||||
|
--client=sqlite3 \
|
||||||
|
--connection=${temp}/db \
|
||||||
|
--migrations-directory=${temp}/migrations`,
|
||||||
|
'create_users_table'
|
||||||
|
)
|
||||||
|
.then(() => {
|
||||||
|
fs.writeFileSync(
|
||||||
|
`${temp}/migrations/${migrationFile2}`,
|
||||||
|
`
|
||||||
|
exports.up = (knex) => knex.schema
|
||||||
|
.table('users', (table) => {
|
||||||
|
table.integer('age');
|
||||||
|
});
|
||||||
|
|
||||||
|
exports.down = (knex) => knex.schema
|
||||||
|
.table('users', (table) => {
|
||||||
|
table.dropColumn('age');
|
||||||
|
});
|
||||||
|
`
|
||||||
|
);
|
||||||
|
|
||||||
|
return assertExec(
|
||||||
|
`node ${KNEX} migrate:latest \
|
||||||
|
--client=sqlite3 \
|
||||||
|
--connection=${temp}/db \
|
||||||
|
--migrations-directory=${temp}/migrations`,
|
||||||
|
'add_age_column_to_users_table'
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
fs.writeFileSync(
|
||||||
|
`${temp}/migrations/${migrationFile3}`,
|
||||||
|
`
|
||||||
|
exports.up = (knex) => knex.schema
|
||||||
|
.table('users', (table) => {
|
||||||
|
table.string('last_name');
|
||||||
|
});
|
||||||
|
|
||||||
|
exports.down = (knex) => knex.schema
|
||||||
|
.table('users', (table) => {
|
||||||
|
table.dropColumn('last_name');
|
||||||
|
});
|
||||||
|
`
|
||||||
|
);
|
||||||
|
|
||||||
|
return assertExec(
|
||||||
|
`node ${KNEX} migrate:latest \
|
||||||
|
--client=sqlite3 \
|
||||||
|
--connection=${temp}/db \
|
||||||
|
--migrations-directory=${temp}/migrations`,
|
||||||
|
'add_last_name_column_to_user_table'
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
fs.writeFileSync(
|
||||||
|
`${temp}/migrations/${migrationFile4}`,
|
||||||
|
`
|
||||||
|
exports.up = (knex) => knex.schema
|
||||||
|
.table('users', (table) => {
|
||||||
|
table.string('email');
|
||||||
|
});
|
||||||
|
|
||||||
|
exports.down = (knex) => knex.schema
|
||||||
|
.table('users', (table) => {
|
||||||
|
table.dropColumn('email');
|
||||||
|
});
|
||||||
|
`
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
const db = new sqlite3.Database(`${temp}/db`);
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) =>
|
||||||
|
db.all('SELECT * FROM knex_migrations', (err, rows) => {
|
||||||
|
const migrationsWithoutMigrationTime = rows.map((row) => {
|
||||||
|
return {
|
||||||
|
id: row.id,
|
||||||
|
name: row.name,
|
||||||
|
batch: row.batch,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.includeDeepOrderedMembers(migrationsWithoutMigrationTime, [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
name: migrationFile1,
|
||||||
|
batch: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 2,
|
||||||
|
name: migrationFile2,
|
||||||
|
batch: 2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 3,
|
||||||
|
name: migrationFile3,
|
||||||
|
batch: 3,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
err ? reject(err) : resolve(rows);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
return assertExec(
|
||||||
|
`node ${KNEX} migrate:rollback --all \
|
||||||
|
--client=sqlite3 \
|
||||||
|
--connection=${temp}/db \
|
||||||
|
--migrations-directory=${temp}/migrations`
|
||||||
|
).then(({ stdout }) => {
|
||||||
|
assert.include(stdout, 'Batch 3 rolled back: 3 migrations');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
taskList,
|
taskList,
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user