wikijs-fork/server/db/migrations/2.2.17.js

38 lines
1.9 KiB
JavaScript
Raw Normal View History

2020-03-29 17:42:55 +00:00
const _ = require('lodash')
2020-02-29 23:57:54 +00:00
/* global WIKI */
2020-03-29 17:42:55 +00:00
exports.up = async knex => {
2020-02-29 23:57:54 +00:00
let sqlVersionDate = ''
switch (WIKI.config.db.type) {
case 'postgres':
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")'
2020-02-29 23:57:54 +00:00
break
case 'mssql':
sqlVersionDate = 'UPDATE h1 SET "versionDate" = COALESCE((SELECT TOP 1 prev."createdAt" FROM "pageHistory" prev WHERE prev."pageId" = h1."pageId" AND prev.id < h1.id ORDER BY prev.id DESC), h1."createdAt") FROM "pageHistory" h1'
break
2020-02-29 23:57:54 +00:00
case 'mysql':
case 'mariadb':
2020-03-29 17:42:55 +00:00
// -> 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':
// 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
2020-02-29 23:57:54 +00:00
}
2020-03-29 17:42:55 +00:00
await knex.schema
2020-02-29 23:57:54 +00:00
.alterTable('pageHistory', table => {
table.string('versionDate').notNullable().defaultTo('')
})
.raw(sqlVersionDate)
}
exports.down = knex => { }