knex/test/lib/schema.js

72 lines
2.1 KiB
JavaScript
Raw Normal View History

2013-05-08 20:16:39 -04:00
var When = require('when');
module.exports = function(Knex, resolver, error) {
2013-05-04 16:27:58 -04:00
var res = null;
2013-05-08 20:16:39 -04:00
return When.all([
2013-05-04 16:27:58 -04:00
Knex.Schema.dropTableIfExists('test_table_one'),
Knex.Schema.dropTableIfExists('test_table_two'),
Knex.Schema.dropTableIfExists('test_table_three'),
Knex.Schema.dropTableIfExists('accounts')
]).then(function(resp) {
2013-05-08 20:16:39 -04:00
res = [resp[0]]; // only really need one of these for the test output.
2013-05-08 20:16:39 -04:00
return When.all([
2013-05-04 02:57:12 -04:00
Knex.Schema.createTable('test_table_one', function(table) {
table.engine('InnoDB');
table.comment('A table comment.')
table.increments('id');
table.string('first_name');
table.string('last_name');
table.string('email').unique().nullable();
table.integer('logins').defaultTo(1).index().comment();
table.text('about').comment('A comment.');
table.timestamps();
}),
2013-05-04 02:57:12 -04:00
Knex.Schema.createTable('test_table_two', function(t) {
t.engine('InnoDB');
2013-05-04 02:57:12 -04:00
t.increments();
t.integer('account_id');
t.text('details');
t.tinyint('status');
}),
2013-05-04 02:57:12 -04:00
Knex.Schema.createTable('test_table_three', function(table) {
table.engine('InnoDB');
table.integer('main').primary();
table.text('paragraph').defaultTo('Lorem ipsum Qui quis qui in.');
})
2013-05-04 16:27:58 -04:00
]);
})
.then(function(resp) {
// Edit test table one
res = res.concat(resp);
return Knex.Schema.table('test_table_one', function(t) {
t.string('phone').nullable();
});
2013-05-04 16:27:58 -04:00
}).then(function(resp) {
// conditionally drops tables with `dropTableIfExists`
res.push(resp);
return Knex.Schema.dropTableIfExists('items');
})
.then(function(resp) {
res.push(resp);
return Knex.Schema.hasTable('test_table_two');
})
.then(function(resp) {
res.push(resp);
return Knex.Schema.renameTable('test_table_one', 'accounts');
})
.then(function(resp) {
res.push(resp);
return Knex.Schema.dropTable('test_table_three');
})
.then(function(resp) {
res.push(resp);
return res;
})
.then(resolver, error);
};