File tree 1 file changed +24
-0
lines changed
1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change 2
2
* Sync the database models to db tables.
3
3
*/
4
4
const config = require ( 'config' )
5
+ const fs = require ( 'fs' )
5
6
const models = require ( './models' )
6
7
const logger = require ( './common/logger' )
7
8
9
+ // the directory at which migration scripts are located
10
+ const MigrationsDirPath = './migrations'
11
+
12
+ /**
13
+ * List the filenames of the migration files.
14
+ *
15
+ * @returns {Array } the list of filenames
16
+ */
17
+ function listMigrationFiles ( ) {
18
+ const filenames = fs . readdirSync ( MigrationsDirPath )
19
+ return filenames
20
+ }
21
+
8
22
const initDB = async ( ) => {
9
23
if ( process . argv [ 2 ] === 'force' ) {
10
24
await models . sequelize . dropSchema ( config . DB_SCHEMA_NAME )
11
25
}
12
26
await models . sequelize . createSchema ( config . DB_SCHEMA_NAME )
27
+ // define SequelizeMeta table
28
+ const SequelizeMeta = await models . sequelize . define ( 'SequelizeMeta' , {
29
+ name : {
30
+ type : models . Sequelize . STRING ( 255 ) ,
31
+ allowNull : false
32
+ }
33
+ } , { timestamps : false } )
34
+ // re-init all tables including the SequelizeMeta table
13
35
await models . sequelize . sync ( { force : true } )
36
+ // add filenames of existing migration scripts to the SequelizeMeta table
37
+ await SequelizeMeta . bulkCreate ( listMigrationFiles ( ) . map ( filename => ( { name : filename } ) ) )
14
38
}
15
39
16
40
if ( ! module . parent ) {
You can’t perform that action at this time.
0 commit comments