Skip to content

Commit 47d3521

Browse files
authored
Merge pull request #176 from imcaizheng/init-db-with-existing-migration
Update MigrationMeta table when running init-db
2 parents de8b164 + 4113b0b commit 47d3521

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/init-db.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,39 @@
22
* Sync the database models to db tables.
33
*/
44
const config = require('config')
5+
const fs = require('fs')
56
const models = require('./models')
67
const 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+
822
const 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

1640
if (!module.parent) {

0 commit comments

Comments
 (0)