fix: mysql 2.2.50 failed migration

This commit is contained in:
NGPixel 2020-03-29 13:42:55 -04:00
parent dba4ad138b
commit 3613c73008
2 changed files with 18 additions and 6 deletions

View File

@ -27,10 +27,10 @@ module.exports = {
let dbClient = null let dbClient = null
let dbConfig = (!_.isEmpty(process.env.DATABASE_URL)) ? process.env.DATABASE_URL : { let dbConfig = (!_.isEmpty(process.env.DATABASE_URL)) ? process.env.DATABASE_URL : {
host: WIKI.config.db.host, host: WIKI.config.db.host.toString(),
user: WIKI.config.db.user, user: WIKI.config.db.user.toString(),
password: WIKI.config.db.pass, password: WIKI.config.db.pass.toString(),
database: WIKI.config.db.db, database: WIKI.config.db.db.toString(),
port: WIKI.config.db.port port: WIKI.config.db.port
} }

View File

@ -1,6 +1,8 @@
const _ = require('lodash')
/* global WIKI */ /* global WIKI */
exports.up = knex => { exports.up = async knex => {
let sqlVersionDate = '' let sqlVersionDate = ''
switch (WIKI.config.db.type) { switch (WIKI.config.db.type) {
case 'postgres': case 'postgres':
@ -8,11 +10,21 @@ exports.up = knex => {
sqlVersionDate = 'UPDATE "pageHistory" h1 SET "versionDate" = COALESCE((SELECT prev."createdAt" FROM "pageHistory" prev WHERE prev."pageId" = h1."pageId" AND prev.id < h1.id ORDER BY prev.id DESC LIMIT 1), h1."createdAt")' sqlVersionDate = 'UPDATE "pageHistory" h1 SET "versionDate" = COALESCE((SELECT prev."createdAt" FROM "pageHistory" prev WHERE prev."pageId" = h1."pageId" AND prev.id < h1.id ORDER BY prev.id DESC LIMIT 1), h1."createdAt")'
break break
case 'mysql': case 'mysql':
// -> Fix for 2.2.50 failed migration
const pageHistoryColumns = await knex.schema.raw('SHOW COLUMNS FROM pageHistory')
if (_.some(pageHistoryColumns[0], ['Field', 'versionDate'])) {
console.info('MySQL 2.2.50 Migration Fix - Dropping failed versionDate column...')
await knex.schema.raw('ALTER TABLE pageHistory DROP COLUMN versionDate')
console.info('versionDate column dropped successfully.')
}
sqlVersionDate = `UPDATE pageHistory AS h1 INNER JOIN pageHistory AS h2 ON h2.id = (SELECT prev.id FROM (SELECT * FROM pageHistory) AS prev WHERE prev.pageId = h1.pageId AND prev.id < h1.id ORDER BY prev.id DESC LIMIT 1) SET h1.versionDate = h2.createdAt`
break
case 'mariadb': case 'mariadb':
sqlVersionDate = `UPDATE pageHistory AS h1 INNER JOIN pageHistory AS h2 ON h2.id = (SELECT prev.id FROM pageHistory AS prev WHERE prev.pageId = h1.pageId AND prev.id < h1.id ORDER BY prev.id DESC LIMIT 1) SET h1.versionDate = h2.createdAt` sqlVersionDate = `UPDATE pageHistory AS h1 INNER JOIN pageHistory AS h2 ON h2.id = (SELECT prev.id FROM pageHistory AS prev WHERE prev.pageId = h1.pageId AND prev.id < h1.id ORDER BY prev.id DESC LIMIT 1) SET h1.versionDate = h2.createdAt`
break break
} }
return knex.schema await knex.schema
.alterTable('pageHistory', table => { .alterTable('pageHistory', table => {
table.string('versionDate').notNullable().defaultTo('') table.string('versionDate').notNullable().defaultTo('')
}) })