Skip to content
Newer
Older
100644 211 lines (188 sloc) 7.26 KB
4cb7af1 @1602 Automigrade/update
authored Dec 9, 2011
1 juggling = require('../index')
2 Schema = juggling.Schema
3 Text = Schema.Text
4
643c287 @1602 Mongoose order/limit/offset and more
authored Jan 30, 2012
5 DBNAME = process.env.DBNAME || 'myapp_test'
a8db2ad @1602 Do not store null values
authored Jan 14, 2012
6 DBUSER = process.env.DBUSER || 'root'
4cb7af1 @1602 Automigrade/update
authored Dec 9, 2011
7 DBPASS = ''
a8db2ad @1602 Do not store null values
authored Jan 13, 2012
8 DBENGINE = process.env.DBENGINE || 'mysql'
4cb7af1 @1602 Automigrade/update
authored Dec 9, 2011
9
10 require('./spec_helper').init module.exports
11
a8db2ad @1602 Do not store null values
authored Jan 13, 2012
12 schema = new Schema DBENGINE, database: '', username: DBUSER, password: DBPASS
4cb7af1 @1602 Automigrade/update
authored Dec 9, 2011
13 schema.log = (q) -> console.log q
14
15 query = (sql, cb) ->
16 schema.adapter.query sql, cb
17
18 User = schema.define 'User',
19 email: { type: String, null: false, index: true }
20 name: String
21 bio: Text
22 password: String
23 birthDate: Date
24 pendingPeriod: Number
25 createdByAdmin: Boolean
12eadb8 @1602 Autoupdate multicolumn indexes
authored Aug 19, 2012
26 , indexes:
27 index1:
28 columns: 'email, createdByAdmin'
4cb7af1 @1602 Automigrade/update
authored Dec 9, 2011
29
30 withBlankDatabase = (cb) ->
31 db = schema.settings.database = DBNAME
32 query 'DROP DATABASE IF EXISTS ' + db, (err) ->
33 query 'CREATE DATABASE ' + db, (err) ->
34 query 'USE '+ db, cb
35
36 getFields = (model, cb) ->
37 query 'SHOW FIELDS FROM ' + model, (err, res) ->
38 if err
39 cb err
40 else
41 fields = {}
42 res.forEach (field) -> fields[field.Field] = field
43 cb err, fields
44
62d0e45 @1602 Single-column indexes in mysql (autoupdate)
authored Aug 17, 2012
45 getIndexes = (model, cb) ->
46 query 'SHOW INDEXES FROM ' + model, (err, res) ->
47 if err
46e65b7 @1602 Update mysql to 2.0
authored Sep 26, 2012
48 console.log err
62d0e45 @1602 Single-column indexes in mysql (autoupdate)
authored Aug 17, 2012
49 cb err
50 else
51 indexes = {}
46e65b7 @1602 Update mysql to 2.0
authored Sep 26, 2012
52 res.forEach (index) ->
53 indexes[index.Key_name] = index if index.Seq_in_index == '1'
62d0e45 @1602 Single-column indexes in mysql (autoupdate)
authored Aug 17, 2012
54 cb err, indexes
55
4cb7af1 @1602 Automigrade/update
authored Dec 9, 2011
56 it 'should run migration', (test) ->
57 withBlankDatabase (err) ->
58 schema.automigrate ->
59 getFields 'User', (err, fields) ->
60 test.deepEqual fields,
61 id:
62 Field: 'id'
63 Type: 'int(11)'
64 Null: 'NO'
65 Key: 'PRI'
66 Default: null
67 Extra: 'auto_increment'
68 email:
69 Field: 'email'
70 Type: 'varchar(255)'
71 Null: 'NO'
72 Key: ''
73 Default: null
74 Extra: ''
75 name:
76 Field: 'name'
77 Type: 'varchar(255)'
78 Null: 'YES'
79 Key: ''
80 Default: null
81 Extra: ''
82 bio:
83 Field: 'bio'
84 Type: 'text'
85 Null: 'YES'
86 Key: ''
87 Default: null
88 Extra: ''
89 password:
90 Field: 'password'
91 Type: 'varchar(255)'
92 Null: 'YES'
93 Key: ''
94 Default: null
95 Extra: ''
96 birthDate:
97 Field: 'birthDate'
98 Type: 'datetime'
99 Null: 'YES'
100 Key: ''
101 Default: null
102 Extra: ''
103 pendingPeriod:
104 Field: 'pendingPeriod'
105 Type: 'int(11)'
106 Null: 'YES'
107 Key: ''
108 Default: null
109 Extra: ''
110 createdByAdmin:
111 Field: 'createdByAdmin'
112 Type: 'tinyint(1)'
113 Null: 'YES'
114 Key: ''
115 Default: null
116 Extra: ''
117
118 test.done()
119
120 it 'should autoupgrade', (test) ->
121 userExists = (cb) ->
122 query 'SELECT * FROM User', (err, res) ->
123 cb(not err and res[0].email == 'test@example.com')
124
125 User.create email: 'test@example.com', (err, user) ->
126 test.ok not err
127 userExists (yep) ->
128 test.ok yep
129 User.defineProperty 'email', type: String
130 User.defineProperty 'name', type: String, limit: 50
131 User.defineProperty 'newProperty', type: Number
877e309 @1602 Drop and add columns
authored Dec 11, 2011
132 User.defineProperty 'pendingPeriod', false
4cb7af1 @1602 Automigrade/update
authored Dec 9, 2011
133 schema.autoupdate (err) ->
134 getFields 'User', (err, fields) ->
877e309 @1602 Drop and add columns
authored Dec 11, 2011
135 # change nullable for email
e8c6f68 @1602 Add schema.isActual and automigrate on sqlite start
authored Feb 20, 2012
136 test.equal fields.email.Null, 'YES', 'Email is not null'
877e309 @1602 Drop and add columns
authored Dec 11, 2011
137 # change type of name
e8c6f68 @1602 Add schema.isActual and automigrate on sqlite start
authored Feb 20, 2012
138 test.equal fields.name.Type, 'varchar(50)', 'Name is not varchar(50)'
877e309 @1602 Drop and add columns
authored Dec 11, 2011
139 # add new column
e8c6f68 @1602 Add schema.isActual and automigrate on sqlite start
authored Feb 20, 2012
140 test.ok fields.newProperty, 'New column was not added'
4cb7af1 @1602 Automigrade/update
authored Dec 9, 2011
141 if fields.newProperty
e8c6f68 @1602 Add schema.isActual and automigrate on sqlite start
authored Feb 20, 2012
142 test.equal fields.newProperty.Type, 'int(11)', 'New column type is not int(11)'
877e309 @1602 Drop and add columns
authored Dec 11, 2011
143 # drop column
e8c6f68 @1602 Add schema.isActual and automigrate on sqlite start
authored Feb 20, 2012
144 test.ok not fields.pendingPeriod, 'drop column'
877e309 @1602 Drop and add columns
authored Dec 11, 2011
145
146 # user still exists
4cb7af1 @1602 Automigrade/update
authored Dec 9, 2011
147 userExists (yep) ->
148 test.ok yep
149 test.done()
150
e8c6f68 @1602 Add schema.isActual and automigrate on sqlite start
authored Feb 20, 2012
151 it 'should check actuality of schema', (test) ->
152 # drop column
153 User.schema.isActual (err, ok) ->
12eadb8 @1602 Autoupdate multicolumn indexes
authored Aug 19, 2012
154 test.ok ok, 'schema is actual'
e8c6f68 @1602 Add schema.isActual and automigrate on sqlite start
authored Feb 20, 2012
155 User.defineProperty 'email', false
156 User.schema.isActual (err, ok) ->
12eadb8 @1602 Autoupdate multicolumn indexes
authored Aug 19, 2012
157 test.ok not ok, 'schema is not actual'
e8c6f68 @1602 Add schema.isActual and automigrate on sqlite start
authored Feb 20, 2012
158 test.done()
159
62d0e45 @1602 Single-column indexes in mysql (autoupdate)
authored Aug 17, 2012
160 it 'should add single-column index', (test) ->
12eadb8 @1602 Autoupdate multicolumn indexes
authored Aug 19, 2012
161 User.defineProperty 'email', type: String, index: { kind: 'FULLTEXT', type: 'HASH'}
62d0e45 @1602 Single-column indexes in mysql (autoupdate)
authored Aug 17, 2012
162 User.schema.autoupdate (err) ->
163 return console.log(err) if err
164 getIndexes 'User', (err, ixs) ->
165 test.ok ixs.email && ixs.email.Column_name == 'email'
12eadb8 @1602 Autoupdate multicolumn indexes
authored Aug 19, 2012
166 console.log(ixs)
46e65b7 @1602 Update mysql to 2.0
authored Sep 26, 2012
167 test.equal ixs.email.Index_type, 'BTREE', 'default index type'
12eadb8 @1602 Autoupdate multicolumn indexes
authored Aug 19, 2012
168 test.done()
169
170 it 'should change type of single-column index', (test) ->
171 User.defineProperty 'email', type: String, index: { type: 'BTREE' }
172 User.schema.isActual (err, ok) ->
cc40d15 @1602 Fix autoupdate for mysql
authored Aug 20, 2012
173 test.ok ok, 'schema is actual'
12eadb8 @1602 Autoupdate multicolumn indexes
authored Aug 19, 2012
174 User.schema.autoupdate (err) ->
175 return console.log(err) if err
176 getIndexes 'User', (err, ixs) ->
177 test.ok ixs.email && ixs.email.Column_name == 'email'
178 test.equal ixs.email.Index_type, 'BTREE'
62d0e45 @1602 Single-column indexes in mysql (autoupdate)
authored Aug 17, 2012
179 test.done()
180
181 it 'should remove single-column index', (test) ->
182 User.defineProperty 'email', type: String, index: false
183 User.schema.autoupdate (err) ->
184 return console.log(err) if err
185 getIndexes 'User', (err, ixs) ->
186 test.ok !ixs.email
187 test.done()
188
12eadb8 @1602 Autoupdate multicolumn indexes
authored Aug 19, 2012
189 it 'should update multi-column index when order of columns changed', (test) ->
190 User.schema.adapter._models.User.settings.indexes.index1.columns = 'createdByAdmin, email'
191 User.schema.isActual (err, ok) ->
192 test.ok not ok, 'schema is not actual'
193 User.schema.autoupdate (err) ->
194 return console.log(err) if err
195 getIndexes 'User', (err, ixs) ->
196 test.equals ixs.index1.Column_name, 'createdByAdmin'
197 test.done()
198
cc40d15 @1602 Fix autoupdate for mysql
authored Aug 19, 2012
199
200 it 'test', (test) ->
201 User.defineProperty 'email', type: String, index: true
202 User.schema.autoupdate (err) ->
203 User.schema.autoupdate (err) ->
204 User.schema.autoupdate (err) ->
205 test.done()
206
4cb7af1 @1602 Automigrade/update
authored Dec 9, 2011
207 it 'should disconnect when done', (test) ->
208 schema.disconnect()
209 test.done()
877e309 @1602 Drop and add columns
authored Dec 11, 2011
210
Something went wrong with that request. Please try again.