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.

115 lines
4.0 KiB

  1. const _ = require('lodash')
  2. const path = require('path')
  3. const fs = require('fs-extra')
  4. const semver = require('semver')
  5. /* global WIKI */
  6. module.exports = {
  7. async migrate (knex) {
  8. const migrationsTableExists = await knex.schema.hasTable('migrations')
  9. if (!migrationsTableExists) {
  10. return
  11. }
  12. const dbCompat = {
  13. charset: (WIKI.config.db.type === `mysql` || WIKI.config.db.type === `mariadb`)
  14. }
  15. const migrations = await knex('migrations')
  16. if (_.some(migrations, m => m.name.indexOf('2.0.0-beta') >= 0)) {
  17. // -> Pre-beta.241 locale field length fix
  18. const localeColnInfo = await knex('pages').columnInfo('localeCode')
  19. if (WIKI.config.db.type !== 'sqlite' && localeColnInfo.maxLength === 2) {
  20. // -> Load locales
  21. const locales = await knex('locales')
  22. await knex.schema
  23. // -> Remove constraints
  24. .table('users', table => {
  25. table.dropForeign('localeCode')
  26. })
  27. .table('pages', table => {
  28. table.dropForeign('localeCode')
  29. })
  30. .table('pageHistory', table => {
  31. table.dropForeign('localeCode')
  32. })
  33. .table('pageTree', table => {
  34. table.dropForeign('localeCode')
  35. })
  36. // -> Recreate locales table
  37. .dropTable('locales')
  38. .createTable('locales', table => {
  39. if (dbCompat.charset) { table.charset('utf8mb4') }
  40. table.string('code', 5).notNullable().primary()
  41. table.json('strings')
  42. table.boolean('isRTL').notNullable().defaultTo(false)
  43. table.string('name').notNullable()
  44. table.string('nativeName').notNullable()
  45. table.integer('availability').notNullable().defaultTo(0)
  46. table.string('createdAt').notNullable()
  47. table.string('updatedAt').notNullable()
  48. })
  49. await knex('locales').insert(locales)
  50. // -> Alter columns length
  51. await knex.schema
  52. .table('users', table => {
  53. table.string('localeCode', 5).notNullable().defaultTo('en').alter()
  54. })
  55. .table('pages', table => {
  56. table.string('localeCode', 5).alter()
  57. })
  58. .table('pageHistory', table => {
  59. table.string('localeCode', 5).alter()
  60. })
  61. .table('pageTree', table => {
  62. table.string('localeCode', 5).alter()
  63. })
  64. // -> Restore restraints
  65. .table('users', table => {
  66. table.foreign('localeCode').references('code').inTable('locales')
  67. })
  68. .table('pages', table => {
  69. table.foreign('localeCode').references('code').inTable('locales')
  70. })
  71. .table('pageHistory', table => {
  72. table.foreign('localeCode').references('code').inTable('locales')
  73. })
  74. .table('pageTree', table => {
  75. table.foreign('localeCode').references('code').inTable('locales')
  76. })
  77. }
  78. // -> Advance to latest beta/rc migration state
  79. const baseMigrationPath = path.join(WIKI.SERVERPATH, (WIKI.config.db.type !== 'sqlite') ? 'db/beta/migrations' : 'db/beta/migrations-sqlite')
  80. await knex.migrate.latest({
  81. tableName: 'migrations',
  82. migrationSource: {
  83. async getMigrations() {
  84. const migrationFiles = await fs.readdir(baseMigrationPath)
  85. return migrationFiles.sort(semver.compare).map(m => ({
  86. file: m,
  87. directory: baseMigrationPath
  88. }))
  89. },
  90. getMigrationName(migration) {
  91. return migration.file
  92. },
  93. getMigration(migration) {
  94. return require(path.join(baseMigrationPath, migration.file))
  95. }
  96. }
  97. })
  98. // -> Cleanup migration table
  99. await knex('migrations').truncate()
  100. // -> Advance to stable 2.0 migration state
  101. await knex('migrations').insert({
  102. name: '2.0.0.js',
  103. batch: 1,
  104. migration_time: knex.fn.now()
  105. })
  106. }
  107. }
  108. }