2011-12-09 15:23:29 +00:00
|
|
|
juggling = require('../index')
|
|
|
|
Schema = juggling.Schema
|
|
|
|
Text = Schema.Text
|
|
|
|
|
2012-01-30 19:34:30 +00:00
|
|
|
DBNAME = process.env.DBNAME || 'myapp_test'
|
2012-01-13 20:06:57 +00:00
|
|
|
DBUSER = process.env.DBUSER || 'root'
|
2011-12-09 15:23:29 +00:00
|
|
|
DBPASS = ''
|
2012-01-13 20:06:57 +00:00
|
|
|
DBENGINE = process.env.DBENGINE || 'mysql'
|
2011-12-09 15:23:29 +00:00
|
|
|
|
|
|
|
require('./spec_helper').init module.exports
|
|
|
|
|
2012-01-13 20:06:57 +00:00
|
|
|
schema = new Schema DBENGINE, database: '', username: DBUSER, password: DBPASS
|
2011-12-09 15:23:29 +00:00
|
|
|
schema.log = (q) -> console.log q
|
|
|
|
|
|
|
|
query = (sql, cb) ->
|
|
|
|
schema.adapter.query sql, cb
|
|
|
|
|
|
|
|
User = schema.define 'User',
|
|
|
|
email: { type: String, null: false, index: true }
|
|
|
|
name: String
|
|
|
|
bio: Text
|
|
|
|
password: String
|
|
|
|
birthDate: Date
|
|
|
|
pendingPeriod: Number
|
|
|
|
createdByAdmin: Boolean
|
2012-08-19 15:40:21 +00:00
|
|
|
, indexes:
|
|
|
|
index1:
|
|
|
|
columns: 'email, createdByAdmin'
|
2011-12-09 15:23:29 +00:00
|
|
|
|
|
|
|
withBlankDatabase = (cb) ->
|
|
|
|
db = schema.settings.database = DBNAME
|
|
|
|
query 'DROP DATABASE IF EXISTS ' + db, (err) ->
|
|
|
|
query 'CREATE DATABASE ' + db, (err) ->
|
|
|
|
query 'USE '+ db, cb
|
|
|
|
|
|
|
|
getFields = (model, cb) ->
|
|
|
|
query 'SHOW FIELDS FROM ' + model, (err, res) ->
|
|
|
|
if err
|
|
|
|
cb err
|
|
|
|
else
|
|
|
|
fields = {}
|
|
|
|
res.forEach (field) -> fields[field.Field] = field
|
|
|
|
cb err, fields
|
|
|
|
|
2012-08-17 19:39:22 +00:00
|
|
|
getIndexes = (model, cb) ->
|
|
|
|
query 'SHOW INDEXES FROM ' + model, (err, res) ->
|
|
|
|
if err
|
2012-09-26 04:08:27 +00:00
|
|
|
console.log err
|
2012-08-17 19:39:22 +00:00
|
|
|
cb err
|
|
|
|
else
|
|
|
|
indexes = {}
|
2012-09-26 04:08:27 +00:00
|
|
|
res.forEach (index) ->
|
|
|
|
indexes[index.Key_name] = index if index.Seq_in_index == '1'
|
2012-08-17 19:39:22 +00:00
|
|
|
cb err, indexes
|
|
|
|
|
2011-12-09 15:23:29 +00:00
|
|
|
it 'should run migration', (test) ->
|
|
|
|
withBlankDatabase (err) ->
|
|
|
|
schema.automigrate ->
|
|
|
|
getFields 'User', (err, fields) ->
|
|
|
|
test.deepEqual fields,
|
|
|
|
id:
|
|
|
|
Field: 'id'
|
|
|
|
Type: 'int(11)'
|
|
|
|
Null: 'NO'
|
|
|
|
Key: 'PRI'
|
|
|
|
Default: null
|
|
|
|
Extra: 'auto_increment'
|
|
|
|
email:
|
|
|
|
Field: 'email'
|
|
|
|
Type: 'varchar(255)'
|
|
|
|
Null: 'NO'
|
|
|
|
Key: ''
|
|
|
|
Default: null
|
|
|
|
Extra: ''
|
|
|
|
name:
|
|
|
|
Field: 'name'
|
|
|
|
Type: 'varchar(255)'
|
|
|
|
Null: 'YES'
|
|
|
|
Key: ''
|
|
|
|
Default: null
|
|
|
|
Extra: ''
|
|
|
|
bio:
|
|
|
|
Field: 'bio'
|
|
|
|
Type: 'text'
|
|
|
|
Null: 'YES'
|
|
|
|
Key: ''
|
|
|
|
Default: null
|
|
|
|
Extra: ''
|
|
|
|
password:
|
|
|
|
Field: 'password'
|
|
|
|
Type: 'varchar(255)'
|
|
|
|
Null: 'YES'
|
|
|
|
Key: ''
|
|
|
|
Default: null
|
|
|
|
Extra: ''
|
|
|
|
birthDate:
|
|
|
|
Field: 'birthDate'
|
|
|
|
Type: 'datetime'
|
|
|
|
Null: 'YES'
|
|
|
|
Key: ''
|
|
|
|
Default: null
|
|
|
|
Extra: ''
|
|
|
|
pendingPeriod:
|
|
|
|
Field: 'pendingPeriod'
|
|
|
|
Type: 'int(11)'
|
|
|
|
Null: 'YES'
|
|
|
|
Key: ''
|
|
|
|
Default: null
|
|
|
|
Extra: ''
|
|
|
|
createdByAdmin:
|
|
|
|
Field: 'createdByAdmin'
|
|
|
|
Type: 'tinyint(1)'
|
|
|
|
Null: 'YES'
|
|
|
|
Key: ''
|
|
|
|
Default: null
|
|
|
|
Extra: ''
|
|
|
|
|
|
|
|
test.done()
|
|
|
|
|
|
|
|
it 'should autoupgrade', (test) ->
|
|
|
|
userExists = (cb) ->
|
|
|
|
query 'SELECT * FROM User', (err, res) ->
|
|
|
|
cb(not err and res[0].email == 'test@example.com')
|
|
|
|
|
|
|
|
User.create email: 'test@example.com', (err, user) ->
|
|
|
|
test.ok not err
|
|
|
|
userExists (yep) ->
|
|
|
|
test.ok yep
|
|
|
|
User.defineProperty 'email', type: String
|
|
|
|
User.defineProperty 'name', type: String, limit: 50
|
|
|
|
User.defineProperty 'newProperty', type: Number
|
2011-12-11 07:51:57 +00:00
|
|
|
User.defineProperty 'pendingPeriod', false
|
2011-12-09 15:23:29 +00:00
|
|
|
schema.autoupdate (err) ->
|
|
|
|
getFields 'User', (err, fields) ->
|
2011-12-11 07:51:57 +00:00
|
|
|
# change nullable for email
|
2012-02-20 18:33:11 +00:00
|
|
|
test.equal fields.email.Null, 'YES', 'Email is not null'
|
2011-12-11 07:51:57 +00:00
|
|
|
# change type of name
|
2012-02-20 18:33:11 +00:00
|
|
|
test.equal fields.name.Type, 'varchar(50)', 'Name is not varchar(50)'
|
2011-12-11 07:51:57 +00:00
|
|
|
# add new column
|
2012-02-20 18:33:11 +00:00
|
|
|
test.ok fields.newProperty, 'New column was not added'
|
2011-12-09 15:23:29 +00:00
|
|
|
if fields.newProperty
|
2012-02-20 18:33:11 +00:00
|
|
|
test.equal fields.newProperty.Type, 'int(11)', 'New column type is not int(11)'
|
2011-12-11 07:51:57 +00:00
|
|
|
# drop column
|
2012-02-20 18:33:11 +00:00
|
|
|
test.ok not fields.pendingPeriod, 'drop column'
|
2011-12-11 07:51:57 +00:00
|
|
|
|
|
|
|
# user still exists
|
2011-12-09 15:23:29 +00:00
|
|
|
userExists (yep) ->
|
|
|
|
test.ok yep
|
|
|
|
test.done()
|
|
|
|
|
2012-02-20 18:33:11 +00:00
|
|
|
it 'should check actuality of schema', (test) ->
|
|
|
|
# drop column
|
|
|
|
User.schema.isActual (err, ok) ->
|
2012-08-19 15:40:21 +00:00
|
|
|
test.ok ok, 'schema is actual'
|
2012-02-20 18:33:11 +00:00
|
|
|
User.defineProperty 'email', false
|
|
|
|
User.schema.isActual (err, ok) ->
|
2012-08-19 15:40:21 +00:00
|
|
|
test.ok not ok, 'schema is not actual'
|
2012-02-20 18:33:11 +00:00
|
|
|
test.done()
|
|
|
|
|
2012-08-17 19:39:22 +00:00
|
|
|
it 'should add single-column index', (test) ->
|
2012-08-19 15:40:21 +00:00
|
|
|
User.defineProperty 'email', type: String, index: { kind: 'FULLTEXT', type: 'HASH'}
|
2012-08-17 19:39:22 +00:00
|
|
|
User.schema.autoupdate (err) ->
|
|
|
|
return console.log(err) if err
|
|
|
|
getIndexes 'User', (err, ixs) ->
|
|
|
|
test.ok ixs.email && ixs.email.Column_name == 'email'
|
2012-08-19 15:40:21 +00:00
|
|
|
console.log(ixs)
|
2012-09-26 04:08:27 +00:00
|
|
|
test.equal ixs.email.Index_type, 'BTREE', 'default index type'
|
2012-08-19 15:40:21 +00:00
|
|
|
test.done()
|
|
|
|
|
|
|
|
it 'should change type of single-column index', (test) ->
|
|
|
|
User.defineProperty 'email', type: String, index: { type: 'BTREE' }
|
|
|
|
User.schema.isActual (err, ok) ->
|
2012-08-19 20:44:15 +00:00
|
|
|
test.ok ok, 'schema is actual'
|
2012-08-19 15:40:21 +00:00
|
|
|
User.schema.autoupdate (err) ->
|
|
|
|
return console.log(err) if err
|
|
|
|
getIndexes 'User', (err, ixs) ->
|
|
|
|
test.ok ixs.email && ixs.email.Column_name == 'email'
|
|
|
|
test.equal ixs.email.Index_type, 'BTREE'
|
2012-08-17 19:39:22 +00:00
|
|
|
test.done()
|
|
|
|
|
|
|
|
it 'should remove single-column index', (test) ->
|
|
|
|
User.defineProperty 'email', type: String, index: false
|
|
|
|
User.schema.autoupdate (err) ->
|
|
|
|
return console.log(err) if err
|
|
|
|
getIndexes 'User', (err, ixs) ->
|
|
|
|
test.ok !ixs.email
|
|
|
|
test.done()
|
|
|
|
|
2012-08-19 15:40:21 +00:00
|
|
|
it 'should update multi-column index when order of columns changed', (test) ->
|
|
|
|
User.schema.adapter._models.User.settings.indexes.index1.columns = 'createdByAdmin, email'
|
|
|
|
User.schema.isActual (err, ok) ->
|
|
|
|
test.ok not ok, 'schema is not actual'
|
|
|
|
User.schema.autoupdate (err) ->
|
|
|
|
return console.log(err) if err
|
|
|
|
getIndexes 'User', (err, ixs) ->
|
|
|
|
test.equals ixs.index1.Column_name, 'createdByAdmin'
|
|
|
|
test.done()
|
|
|
|
|
2012-08-19 20:44:15 +00:00
|
|
|
|
|
|
|
it 'test', (test) ->
|
|
|
|
User.defineProperty 'email', type: String, index: true
|
|
|
|
User.schema.autoupdate (err) ->
|
|
|
|
User.schema.autoupdate (err) ->
|
|
|
|
User.schema.autoupdate (err) ->
|
|
|
|
test.done()
|
|
|
|
|
2011-12-09 15:23:29 +00:00
|
|
|
it 'should disconnect when done', (test) ->
|
|
|
|
schema.disconnect()
|
|
|
|
test.done()
|
2011-12-11 07:51:57 +00:00
|
|
|
|