Add db migration management infratructure

This commit is contained in:
mikiher 2024-09-04 12:48:10 +03:00
parent 0344a63b48
commit 3f93b93d9e
13 changed files with 1385 additions and 172 deletions

View file

@ -0,0 +1,42 @@
const { DataTypes } = require('sequelize')
const Logger = require('../../../server/Logger')
/**
* This is an example of an upward migration script.
*
* @param {import { QueryInterface } from "sequelize";} options.context.queryInterface - a suquelize QueryInterface object.
* @returns {Promise<void>} - A promise that resolves when the migration is complete.
*/
async function up({ context: queryInterface }) {
Logger.info('Running migration_example up...')
Logger.info('Creating example_table...')
await queryInterface.createTable('example_table', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING,
allowNull: false
}
})
Logger.info('example_table created.')
Logger.info('migration_example up complete.')
}
/**
* This is an example of a downward migration script.
*
* @param {import { QueryInterface } from "sequelize";} options.context.queryInterface - a suquelize QueryInterface object.
* @returns {Promise<void>} - A promise that resolves when the migration is complete.
*/
async function down({ context: queryInterface }) {
Logger.info('Running migration_example down...')
Logger.info('Dropping example_table...')
await queryInterface.dropTable('example_table')
Logger.info('example_table dropped.')
Logger.info('migration_example down complete.')
}
module.exports = { up, down }

View file

@ -0,0 +1,53 @@
const { expect } = require('chai')
const sinon = require('sinon')
const { up, down } = require('./v0.0.1-migration_example')
const { Sequelize } = require('sequelize')
const Logger = require('../../../server/Logger')
describe('migration_example', () => {
let sequelize
let queryInterface
let loggerInfoStub
beforeEach(() => {
sequelize = new Sequelize({ dialect: 'sqlite', storage: ':memory:', logging: false })
queryInterface = sequelize.getQueryInterface()
loggerInfoStub = sinon.stub(Logger, 'info')
})
afterEach(() => {
sinon.restore()
})
describe('up', () => {
it('should create example_table', async () => {
await up({ context: queryInterface })
expect(loggerInfoStub.callCount).to.equal(4)
expect(loggerInfoStub.getCall(0).calledWith(sinon.match('Running migration_example up...'))).to.be.true
expect(loggerInfoStub.getCall(1).calledWith(sinon.match('Creating example_table...'))).to.be.true
expect(loggerInfoStub.getCall(2).calledWith(sinon.match('example_table created.'))).to.be.true
expect(loggerInfoStub.getCall(3).calledWith(sinon.match('migration_example up complete.'))).to.be.true
expect(await queryInterface.showAllTables()).to.include('example_table')
const tableDescription = await queryInterface.describeTable('example_table')
expect(tableDescription).to.deep.equal({
id: { type: 'INTEGER', allowNull: true, defaultValue: undefined, primaryKey: true, unique: false },
name: { type: 'VARCHAR(255)', allowNull: false, defaultValue: undefined, primaryKey: false, unique: false }
})
})
})
describe('down', () => {
it('should drop example_table', async () => {
await up({ context: queryInterface })
await down({ context: queryInterface })
expect(loggerInfoStub.callCount).to.equal(8)
expect(loggerInfoStub.getCall(4).calledWith(sinon.match('Running migration_example down...'))).to.be.true
expect(loggerInfoStub.getCall(5).calledWith(sinon.match('Dropping example_table...'))).to.be.true
expect(loggerInfoStub.getCall(6).calledWith(sinon.match('example_table dropped.'))).to.be.true
expect(loggerInfoStub.getCall(7).calledWith(sinon.match('migration_example down complete.'))).to.be.true
expect(await queryInterface.showAllTables()).not.to.include('example_table')
})
})
})