File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change 22 * Sync the database models to db tables.
33 */
44const config = require ( 'config' )
5+ const fs = require ( 'fs' )
56const models = require ( './models' )
67const logger = require ( './common/logger' )
78
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+
822const initDB = async ( ) => {
923 if ( process . argv [ 2 ] === 'force' ) {
1024 await models . sequelize . dropSchema ( config . DB_SCHEMA_NAME )
1125 }
1226 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
1335 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 } ) ) )
1438}
1539
1640if ( ! module . parent ) {
You can’t perform that action at this time.
0 commit comments