Skip to content
This repository has been archived by the owner on Aug 5, 2021. It is now read-only.

Commit

Permalink
add more migration functions/options #76
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilWaldmann committed Oct 3, 2018
1 parent 38d6d4d commit d288cc3
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 6 deletions.
33 changes: 30 additions & 3 deletions docs/migrations.md
Expand Up @@ -50,7 +50,8 @@ Available methods in the `migration scope` are:
## createTable(name, options, fn)

Creates a new table. The `options` object is optional, valid values are:
* **id** Set to `false` to disable the automatic creation of an `id` column with auto increment and primary key
* **id**: Set to `false` to disable the automatic creation of an `id` column with auto increment and primary key
* **comment**: Set a table comment (if supported by your database)

`fn` is a function which provides the `table scope`!
Within the `table scope` you can add columns to the table:
Expand All @@ -60,7 +61,7 @@ this.createTable('users', function(){
// `this` => `table scope`
this.string('login', {unique: true})
this.integer('failed_login_count', {default: 0})

this.string('first_name')
this.string('last_name')
this.integer_array('role_ids') // Postgres only
Expand All @@ -72,7 +73,8 @@ The `table scope` has a method vor every [data type](./definition.md#attributena
* **primary**: Set to `true` for a primary key
* **unique**: Set to `true` to use a uniq constraint
* **default**: Set a default value
* **notnull**: Set a not null constraint
* **not_null**: **notnull** or **null**: Set a not null constraint
* **references**: Defines a foreign key for the given `table.fieldName` reference.
* **comment**: Set a field comment (if supported by your database)

## renameTable(from, to)
Expand All @@ -83,6 +85,31 @@ Rename a table

Remove a table

## createIndex(table, columns, options)

create an index for one or multiple columns.
The `options` object is optional. Valid `options` are:
* **name**: The name of the index
* **type**: The index type

## dropIndex(table, columns, options)

removes an index.
The `options` object is optional. Valid `options` are:
* **name**: The name of the index

## createUniqueIndex(table, columns, options)

create an uniqe index.
The `options` object is optional. Valid `options` are:
* **name**: The name of the index

## dropUniqueIndex(table, columns, options)

removes an uniqe index.
The `options` object is optional. Valid `options` are:
* **name**: The name of the index

## addColumn(table, fn){

Adds one or multiple new columns to a table. `fn` provides the `table scope` like in `createTable(name, fn)`
Expand Down
16 changes: 14 additions & 2 deletions lib/stores/sql/migrations/column_methods.js
Expand Up @@ -63,8 +63,20 @@ exports.migration = {
column.notNullable()
}

if (options.comment || options.note) {
column.comment(options.comment || options.note)
if (options.null === false) {
column.notNullable()
}

if (options.references) {
column.references(options.references)
}

if (options.unsigned) {
column.unsigned()
}

if (options.comment) {
column.comment(options.comment)
}
}
}
59 changes: 59 additions & 0 deletions lib/stores/sql/migrations/table_methods.js
Expand Up @@ -42,6 +42,10 @@ exports.migration = {
}
table.primary(primary)
}

if(options.comment){
table.comment(options.comment)
}
})
})

Expand All @@ -65,6 +69,61 @@ exports.migration = {
return self.connection.schema.dropTableIfExists(table)
})

return this
},

createIndex: function(tableName, columns, options){
var self = this
options = options || {}

this.queue.push(function(next) {
return self.connection.schema.alterTable(tableName, function(table){
table.index(columns, options.name, options.type)
})
})

return this
},


dropIndex: function(tableName, columns, options){
var self = this
options = options || {}

this.queue.push(function(next) {
return self.connection.schema.alterTable(tableName, function(table){
table.dropIndex(columns, options.name)
})
})

return this
},


createUniqueIndex: function(tableName, columns, options){
var self = this
options = options || {}

this.queue.push(function(next) {
return self.connection.schema.alterTable(tableName, function(table){
table.unique(columns, options.name)
})
})

return this
},


dropUniqueIndex: function(tableName, columns, options){
var self = this
options = options || {}

this.queue.push(function(next) {
return self.connection.schema.alterTable(tableName, function(table){
table.dropUnique(columns, options.name)
})
})

return this
}
}
@@ -1,5 +1,5 @@
module.exports = function() {
this.createTable('with_comments', function() {
this.createTable('with_comments', {comment: 'foobar table'}, function() {
this.integer('foo', { comment: 'foobar' })
})
}
@@ -0,0 +1,5 @@
module.exports = function() {
this.createTable('with_references', function() {
this.integer('post_id', { references: 'posts.id', unsigned: true })
})
}
@@ -0,0 +1,8 @@
module.exports = function() {
this.createTable('with_indices', function() {
this.integer('foo', { unique: true })
this.integer('bar')
})

this.createIndex('with_indices', 'bar')
}
20 changes: 20 additions & 0 deletions test/sql/__shared/migrations_fresh-test.js
Expand Up @@ -30,6 +30,8 @@ module.exports = function(title, beforeFn, afterFn, storeConf) {
store.Model('AttributeTest', function() {})
store.Model('CompoundPrimaryKey', function() {})
store.Model('WithComment', function() {})
store.Model('WithReference', function() {})
store.Model('WithIndex', function() {})
})

it('are finished before ready() gets called', function() {
Expand Down Expand Up @@ -231,5 +233,23 @@ module.exports = function(title, beforeFn, afterFn, storeConf) {
})
})

it('reference is in place', function() {
return store.ready(function() {
var WithReference = store.Model('WithReference')
// no post with id 9999 in the db!
return WithReference.create({post_id: 9999})
})
.should.be.rejectedWith(store.SQLError)
})


it('uniqe index workds', function() {
return store.ready(function() {
var WithIndex = store.Model('WithIndex')
// no duplicate keys
return WithIndex.create([{foo: 1}, {foo: 1}])
})
.should.be.rejectedWith(Error)
})
})
}

0 comments on commit d288cc3

Please sign in to comment.