You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.9 KiB

  1. const _ = require('lodash')
  2. /* global WIKI */
  3. exports.up = async knex => {
  4. let sqlVersionDate = ''
  5. switch (WIKI.config.db.type) {
  6. case 'postgres':
  7. 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")'
  8. break
  9. case 'mssql':
  10. 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'
  11. break
  12. case 'mysql':
  13. case 'mariadb':
  14. // -> Fix for 2.2.50 failed migration
  15. const pageHistoryColumns = await knex.schema.raw('SHOW COLUMNS FROM pageHistory')
  16. if (_.some(pageHistoryColumns[0], ['Field', 'versionDate'])) {
  17. console.info('MySQL 2.2.50 Migration Fix - Dropping failed versionDate column...')
  18. await knex.schema.raw('ALTER TABLE pageHistory DROP COLUMN versionDate')
  19. console.info('versionDate column dropped successfully.')
  20. }
  21. 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`
  22. break
  23. // case 'mariadb':
  24. // 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`
  25. // break
  26. }
  27. await knex.schema
  28. .alterTable('pageHistory', table => {
  29. table.string('versionDate').notNullable().defaultTo('')
  30. })
  31. .raw(sqlVersionDate)
  32. }
  33. exports.down = knex => { }