Skip to content

Update MigrationMeta table when running init-db #176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/init-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,39 @@
* Sync the database models to db tables.
*/
const config = require('config')
const fs = require('fs')
const models = require('./models')
const logger = require('./common/logger')

// the directory at which migration scripts are located
const MigrationsDirPath = './migrations'

/**
* List the filenames of the migration files.
*
* @returns {Array} the list of filenames
*/
function listMigrationFiles () {
const filenames = fs.readdirSync(MigrationsDirPath)
return filenames
}

const initDB = async () => {
if (process.argv[2] === 'force') {
await models.sequelize.dropSchema(config.DB_SCHEMA_NAME)
}
await models.sequelize.createSchema(config.DB_SCHEMA_NAME)
// define SequelizeMeta table
const SequelizeMeta = await models.sequelize.define('SequelizeMeta', {
name: {
type: models.Sequelize.STRING(255),
allowNull: false
}
}, { timestamps: false })
// re-init all tables including the SequelizeMeta table
await models.sequelize.sync({ force: true })
// add filenames of existing migration scripts to the SequelizeMeta table
await SequelizeMeta.bulkCreate(listMigrationFiles().map(filename => ({ name: filename })))
}

if (!module.parent) {
Expand Down