@@ -8,8 +8,10 @@ const fs = require('fs');
8
8
const path = require ( 'path' ) ;
9
9
const cp = require ( 'child_process' ) ;
10
10
const dbmUtil = require ( 'db-migrate-shared' ) . util ;
11
+ const mkdirp = Promise . promisify ( require ( 'mkdirp' ) ) ;
11
12
12
13
const rmdir = Promise . promisify ( require ( 'rimraf' ) ) ;
14
+ const writeFile = Promise . promisify ( fs . writeFile ) ;
13
15
14
16
function wipeMigrations ( ) {
15
17
const dir = path . join ( __dirname , 'migrations' ) ;
@@ -49,9 +51,48 @@ lab.experiment('create', function () {
49
51
50
52
lab . test ( 'will create a new migration' , ( ) => {
51
53
const files = fs . readdirSync ( path . join ( __dirname , 'migrations' ) ) ;
52
- Code . expect ( files . length ) . to . equal ( 1 ) ;
53
- const file = files [ 0 ] ;
54
- Code . expect ( file ) . to . match ( / f i r s t - m i g r a t i o n \. j s $ / ) ;
54
+ const file = files . find ( file => / f i r s t - m i g r a t i o n \. j s $ / . test ( file ) ) ;
55
+ Code . expect ( file ) . to . exist ( ) ;
56
+ } ) ;
57
+
58
+ lab . test ( 'will create migrations/package.json' , ( ) => {
59
+ const files = fs . readdirSync ( path . join ( __dirname , 'migrations' ) ) ;
60
+ const packageJson = files . find ( file => / p a c k a g e \. j s o n $ / . test ( file ) ) ;
61
+ Code . expect ( packageJson ) . to . exist ( ) ;
62
+ } )
63
+ } ) ;
64
+
65
+ lab . experiment (
66
+ 'with existing migrations/package.json file' , ( ) => {
67
+ let exitCode ;
68
+
69
+ lab . before ( async ( ) => {
70
+ await wipeMigrations ( ) ;
71
+
72
+ await mkdirp ( path . join ( __dirname , 'migrations' ) ) ;
73
+
74
+ await writeFile ( path . join ( __dirname , 'migrations' , 'package.json' ) , '{"name": "test", "type": "module"}' ) ;
75
+
76
+ const db = dbMigrate ( 'create' , 'first migration' ) ;
77
+ // db.stderr.on('data', data => console.log(data.toString()));
78
+ // db.stdout.on('data', data => console.log(data.toString()));
79
+
80
+ const exitCodePromise = new Promise ( ( resolve ) => db . on ( 'exit' , resolve ) ) ;
81
+ exitCode = await exitCodePromise ;
82
+ } ) ;
83
+
84
+ lab . test ( 'does not cause an error' , ( ) => {
85
+ Code . expect ( exitCode ) . to . equal ( 0 ) ;
86
+ } ) ;
87
+
88
+ lab . test ( 'will modify an existing migrations/package.json\'s type field' , ( ) => {
89
+ const packageJson = JSON . parse ( fs . readFileSync ( path . join ( __dirname , 'migrations' , 'package.json' ) ) )
90
+ Code . expect ( packageJson . type ) . to . equal ( "commonjs" ) ;
91
+ } ) ;
92
+
93
+ lab . test ( 'will preserve other properties in an existing package.json' , ( ) => {
94
+ const packageJson = JSON . parse ( fs . readFileSync ( path . join ( __dirname , 'migrations' , 'package.json' ) ) )
95
+ Code . expect ( packageJson . name ) . to . equal ( "test" ) ;
55
96
} ) ;
56
97
} ) ;
57
98
@@ -84,7 +125,7 @@ lab.experiment('create', function () {
84
125
for ( let i = 0 ; i < files . length ; i ++ ) {
85
126
const file = files [ i ] ;
86
127
const stats = fs . statSync ( path . join ( __dirname , 'migrations' , file ) ) ;
87
- if ( stats . isFile ( ) ) {
128
+ if ( stats . isFile ( ) && ! / p a c k a g e \. j s o n $ / . test ( file ) ) {
88
129
Code . expect ( file ) . to . match ( / s e c o n d - m i g r a t i o n \. j s $ / ) ;
89
130
}
90
131
}
@@ -97,9 +138,8 @@ lab.experiment('create', function () {
97
138
98
139
lab . test ( 'will create a new migration sql up file' , ( ) => {
99
140
const files = fs . readdirSync ( path . join ( __dirname , 'migrations/sqls' ) ) ;
100
- Code . expect ( files . length ) . to . equal ( 2 ) ;
101
- const file = files [ 1 ] ;
102
- Code . expect ( file ) . to . match ( / s e c o n d - m i g r a t i o n - u p \. s q l $ / ) ;
141
+ const file = files . find ( file => / s e c o n d - m i g r a t i o n - u p \. s q l $ / . test ( file ) ) ;
142
+ Code . expect ( file ) . to . exist ( ) ;
103
143
} ) ;
104
144
}
105
145
) ;
@@ -129,7 +169,7 @@ lab.experiment('create', function () {
129
169
for ( let i = 0 ; i < files . length ; i ++ ) {
130
170
const file = files [ i ] ;
131
171
const stats = fs . statSync ( path . join ( __dirname , 'migrations' , file ) ) ;
132
- if ( stats . isFile ( ) ) {
172
+ if ( stats . isFile ( ) && ! / p a c k a g e \. j s o n $ / . test ( file ) ) {
133
173
Code . expect ( file ) . to . match ( / t h i r d - m i g r a t i o n \. j s $ / ) ;
134
174
}
135
175
}
@@ -142,9 +182,8 @@ lab.experiment('create', function () {
142
182
143
183
lab . test ( 'will create a new migration sql up file' , ( ) => {
144
184
const files = fs . readdirSync ( path . join ( __dirname , 'migrations/sqls' ) ) ;
145
- Code . expect ( files . length ) . to . equal ( 2 ) ;
146
- const file = files [ 1 ] ;
147
- Code . expect ( file ) . to . match ( / t h i r d - m i g r a t i o n - u p \. s q l $ / ) ;
185
+ const file = files . find ( file => / t h i r d - m i g r a t i o n - u p \. s q l $ / . test ( file ) ) ;
186
+ Code . expect ( file ) . to . exist ( ) ;
148
187
} ) ;
149
188
}
150
189
) ;
@@ -177,7 +216,7 @@ lab.experiment('create', function () {
177
216
for ( let i = 0 ; i < files . length ; i ++ ) {
178
217
const file = files [ i ] ;
179
218
const stats = fs . statSync ( path . join ( __dirname , 'migrations' , file ) ) ;
180
- if ( stats . isFile ( ) ) {
219
+ if ( stats . isFile ( ) && ! / p a c k a g e \. j s o n $ / . test ( file ) ) {
181
220
Code . expect ( file ) . to . match ( / f o u r t h - m i g r a t i o n \. c o f f e e $ / ) ;
182
221
}
183
222
}
@@ -211,19 +250,17 @@ lab.experiment('create', function () {
211
250
212
251
lab . test ( 'will create a new migration' , ( ) => {
213
252
const files = fs . readdirSync ( path . join ( __dirname , 'migrations/test' ) ) ;
214
- Code . expect ( files . length ) . to . equal ( 2 ) ;
215
- const file = files [ 0 ] ;
216
- Code . expect ( file ) . to . match ( / f i r s t - m i g r a t i o n \. j s $ / ) ;
253
+ const file = files . find ( file => / f i r s t - m i g r a t i o n \. j s $ / . test ( file ) ) ;
254
+ Code . expect ( file ) . to . exist ( ) ;
217
255
} ) ;
218
256
lab . test ( 'will create a new migration/test/sqls directory' , ( ) => {
219
257
const stats = fs . statSync ( path . join ( __dirname , 'migrations/test/sqls' ) ) ;
220
258
Code . expect ( stats . isDirectory ( ) ) . to . be . true ( ) ;
221
259
} ) ;
222
260
lab . test ( 'will create a new migration sql up file' , ( ) => {
223
261
const files = fs . readdirSync ( path . join ( __dirname , 'migrations/test/sqls' ) ) ;
224
- Code . expect ( files . length ) . to . equal ( 2 ) ;
225
- const file = files [ 1 ] ;
226
- Code . expect ( file ) . to . match ( / f i r s t - m i g r a t i o n - u p \. s q l $ / ) ;
262
+ const file = files . find ( file => / f i r s t - m i g r a t i o n - u p \. s q l $ / . test ( file ) ) ;
263
+ Code . expect ( file ) . to . exist ( ) ;
227
264
} ) ;
228
265
} ) ;
229
266
}
@@ -254,7 +291,7 @@ lab.experiment('create', function () {
254
291
for ( let i = 0 ; i < files . length ; i ++ ) {
255
292
const file = files [ i ] ;
256
293
const stats = fs . statSync ( path . join ( __dirname , 'migrations' , file ) ) ;
257
- if ( stats . isFile ( ) ) {
294
+ if ( stats . isFile ( ) && ! / p a c k a g e \. j s o n $ / . test ( file ) ) {
258
295
Code . expect ( file ) . to . match ( / f i f t h - m i g r a t i o n \. c o f f e e $ / ) ;
259
296
}
260
297
}
@@ -306,7 +343,7 @@ lab.experiment('create', function () {
306
343
for ( let i = 0 ; i < files . length ; i ++ ) {
307
344
const file = files [ i ] ;
308
345
const stats = fs . statSync ( path . join ( __dirname , 'migrations' , file ) ) ;
309
- if ( stats . isFile ( ) ) {
346
+ if ( stats . isFile ( ) && ! / p a c k a g e \. j s o n $ / . test ( file ) ) {
310
347
Code . expect ( file ) . to . match ( / s i x t h - m i g r a t i o n \. j s $ / ) ;
311
348
}
312
349
}
0 commit comments