Commit 8eddc479 authored by NGPixel's avatar NGPixel

fix: stable db migration + beta migration to stable

parent e9afcfcd
...@@ -6,6 +6,7 @@ const Knex = require('knex') ...@@ -6,6 +6,7 @@ const Knex = require('knex')
const Objection = require('objection') const Objection = require('objection')
const migrationSource = require('../db/migrator-source') const migrationSource = require('../db/migrator-source')
const migrateFromBeta = require('../db/beta')
/* global WIKI */ /* global WIKI */
...@@ -110,15 +111,8 @@ module.exports = { ...@@ -110,15 +111,8 @@ module.exports = {
// Set init tasks // Set init tasks
let conAttempts = 0 let conAttempts = 0
let initTasks = { let initTasks = {
// -> Migrate DB Schemas
async syncSchemas() {
return self.knex.migrate.latest({
tableName: 'migrations',
migrationSource
})
},
// -> Attempt initial connection // -> Attempt initial connection
async connect() { async connect () {
try { try {
WIKI.logger.info('Connecting to database...') WIKI.logger.info('Connecting to database...')
await self.knex.raw('SELECT 1 + 1;') await self.knex.raw('SELECT 1 + 1;')
...@@ -133,11 +127,23 @@ module.exports = { ...@@ -133,11 +127,23 @@ module.exports = {
throw err throw err
} }
} }
},
// -> Migrate DB Schemas
async syncSchemas () {
return self.knex.migrate.latest({
tableName: 'migrations',
migrationSource
})
},
// -> Migrate DB Schemas from beta
async migrateFromBeta () {
return migrateFromBeta.migrate(self.knex)
} }
} }
let initTasksQueue = (WIKI.IS_MASTER) ? [ let initTasksQueue = (WIKI.IS_MASTER) ? [
initTasks.connect, initTasks.connect,
initTasks.migrateFromBeta,
initTasks.syncSchemas initTasks.syncSchemas
] : [ ] : [
() => { return Promise.resolve() } () => { return Promise.resolve() }
......
const _ = require('lodash')
const path = require('path')
const fs = require('fs-extra')
const semver = require('semver')
/* global WIKI */
module.exports = {
async migrate (knex) {
const migrationsTableExists = await knex.schema.hasTable('migrations')
if (!migrationsTableExists) {
return
}
const dbCompat = {
charset: (WIKI.config.db.type === `mysql` || WIKI.config.db.type === `mariadb`)
}
const migrations = await knex('migrations')
if (_.some(migrations, m => m.name.indexOf('2.0.0-beta') >= 0)) {
// -> Pre-beta.241 locale field length fix
const localeColnInfo = await knex('pages').columnInfo('localeCode')
if (WIKI.config.db.type !== 'sqlite' && localeColnInfo.maxLength === 2) {
// -> Load locales
const locales = await knex('locales')
await knex.schema
// -> Remove constraints
.table('users', table => {
table.dropForeign('localeCode')
})
.table('pages', table => {
table.dropForeign('localeCode')
})
.table('pageHistory', table => {
table.dropForeign('localeCode')
})
.table('pageTree', table => {
table.dropForeign('localeCode')
})
// -> Recreate locales table
.dropTable('locales')
.createTable('locales', table => {
if (dbCompat.charset) { table.charset('utf8mb4') }
table.string('code', 5).notNullable().primary()
table.json('strings')
table.boolean('isRTL').notNullable().defaultTo(false)
table.string('name').notNullable()
table.string('nativeName').notNullable()
table.integer('availability').notNullable().defaultTo(0)
table.string('createdAt').notNullable()
table.string('updatedAt').notNullable()
})
await knex('locales').insert(locales)
// -> Alter columns length
await knex.schema
.table('users', table => {
table.string('localeCode', 5).notNullable().defaultTo('en').alter()
})
.table('pages', table => {
table.string('localeCode', 5).alter()
})
.table('pageHistory', table => {
table.string('localeCode', 5).alter()
})
.table('pageTree', table => {
table.string('localeCode', 5).alter()
})
// -> Restore restraints
.table('users', table => {
table.foreign('localeCode').references('code').inTable('locales')
})
.table('pages', table => {
table.foreign('localeCode').references('code').inTable('locales')
})
.table('pageHistory', table => {
table.foreign('localeCode').references('code').inTable('locales')
})
.table('pageTree', table => {
table.foreign('localeCode').references('code').inTable('locales')
})
}
// -> Advance to latest beta/rc migration state
const baseMigrationPath = path.join(WIKI.SERVERPATH, (WIKI.config.db.type !== 'sqlite') ? 'db/beta/migrations' : 'db/beta/migrations-sqlite')
await knex.migrate.latest({
tableName: 'migrations',
migrationSource: {
async getMigrations() {
const migrationFiles = await fs.readdir(baseMigrationPath)
return migrationFiles.sort(semver.compare).map(m => ({
file: m,
directory: baseMigrationPath
}))
},
getMigrationName(migration) {
return migration.file
},
getMigration(migration) {
return require(path.join(baseMigrationPath, migration.file))
}
}
})
// -> Cleanup migration table
await knex('migrations').truncate()
// -> Advance to stable 2.0 migration state
await knex('migrations').insert({
name: '2.0.0.js',
batch: 1,
migration_time: knex.fn.now()
})
}
}
}
...@@ -69,7 +69,7 @@ exports.up = knex => { ...@@ -69,7 +69,7 @@ exports.up = knex => {
// LOCALES ----------------------------- // LOCALES -----------------------------
.createTable('locales', table => { .createTable('locales', table => {
if (dbCompat.charset) { table.charset('utf8mb4') } if (dbCompat.charset) { table.charset('utf8mb4') }
table.string('code', 5).notNullable().primary() table.string('code', 2).notNullable().primary()
table.json('strings') table.json('strings')
table.boolean('isRTL').notNullable().defaultTo(false) table.boolean('isRTL').notNullable().defaultTo(false)
table.string('name').notNullable() table.string('name').notNullable()
...@@ -243,26 +243,26 @@ exports.up = knex => { ...@@ -243,26 +243,26 @@ exports.up = knex => {
.table('pageHistory', table => { .table('pageHistory', table => {
table.integer('pageId').unsigned().references('id').inTable('pages') table.integer('pageId').unsigned().references('id').inTable('pages')
table.string('editorKey').references('key').inTable('editors') table.string('editorKey').references('key').inTable('editors')
table.string('localeCode', 5).references('code').inTable('locales') table.string('localeCode', 2).references('code').inTable('locales')
table.integer('authorId').unsigned().references('id').inTable('users') table.integer('authorId').unsigned().references('id').inTable('users')
}) })
.table('pages', table => { .table('pages', table => {
table.string('editorKey').references('key').inTable('editors') table.string('editorKey').references('key').inTable('editors')
table.string('localeCode', 5).references('code').inTable('locales') table.string('localeCode', 2).references('code').inTable('locales')
table.integer('authorId').unsigned().references('id').inTable('users') table.integer('authorId').unsigned().references('id').inTable('users')
table.integer('creatorId').unsigned().references('id').inTable('users') table.integer('creatorId').unsigned().references('id').inTable('users')
}) })
.table('pageTree', table => { .table('pageTree', table => {
table.integer('parent').unsigned().references('id').inTable('pageTree') table.integer('parent').unsigned().references('id').inTable('pageTree')
table.integer('pageId').unsigned().references('id').inTable('pages') table.integer('pageId').unsigned().references('id').inTable('pages')
table.string('localeCode', 5).references('code').inTable('locales') table.string('localeCode', 2).references('code').inTable('locales')
}) })
.table('userKeys', table => { .table('userKeys', table => {
table.integer('userId').unsigned().references('id').inTable('users') table.integer('userId').unsigned().references('id').inTable('users')
}) })
.table('users', table => { .table('users', table => {
table.string('providerKey').references('key').inTable('authentication').notNullable().defaultTo('local') table.string('providerKey').references('key').inTable('authentication').notNullable().defaultTo('local')
table.string('localeCode', 5).references('code').inTable('locales').notNullable().defaultTo('en') table.string('localeCode', 2).references('code').inTable('locales').notNullable().defaultTo('en')
table.string('defaultEditor').references('key').inTable('editors').notNullable().defaultTo('markdown') table.string('defaultEditor').references('key').inTable('editors').notNullable().defaultTo('markdown')
table.unique(['providerKey', 'email']) table.unique(['providerKey', 'email'])
......
/* global WIKI */ /* global WIKI */
exports.up = knex => { exports.up = async knex => {
const dbCompat = { const dbCompat = {
charset: (WIKI.config.db.type === `mysql` || WIKI.config.db.type === `mariadb`), charset: (WIKI.config.db.type === `mysql` || WIKI.config.db.type === `mariadb`),
selfCascadeDelete: WIKI.config.db.type !== 'mssql' selfCascadeDelete: WIKI.config.db.type !== 'mssql'
} }
return knex.schema return knex.schema
.dropTable('pageTree') .dropTable('pageTree')
.createTable('pageTree', table => { .createTable('pageTree', table => {
......
/* global WIKI */
exports.up = knex => {
return knex.schema
.table('pages', table => {
switch (WIKI.config.db.type) {
case 'mariadb':
case 'mysql':
table.specificType('content', 'LONGTEXT').alter()
table.specificType('render', 'LONGTEXT').alter()
break
case 'mssql':
table.specificType('content', 'VARCHAR(max)').alter()
table.specificType('render', 'VARCHAR(max)').alter()
break
}
})
}
exports.down = knex => { }
...@@ -13,7 +13,7 @@ const he = require('he') ...@@ -13,7 +13,7 @@ const he = require('he')
const frontmatterRegex = { const frontmatterRegex = {
html: /^(<!-{2}(?:\n|\r)([\w\W]+?)(?:\n|\r)-{2}>)?(?:\n|\r)*([\w\W]*)*/, html: /^(<!-{2}(?:\n|\r)([\w\W]+?)(?:\n|\r)-{2}>)?(?:\n|\r)*([\w\W]*)*/,
legacy: /^(<!-- TITLE: ?([\w\W]+?) -{2}>)?(?:\n|\r)?(<!-- SUBTITLE: ?([\w\W]+?) -{2}>)?(?:\n|\r)*([\w\W]*)*/i, legacy: /^(<!-- TITLE: ?([\w\W]+?) ?-{2}>)?(?:\n|\r)?(<!-- SUBTITLE: ?([\w\W]+?) ?-{2}>)?(?:\n|\r)*([\w\W]*)*/i,
markdown: /^(-{3}(?:\n|\r)([\w\W]+?)(?:\n|\r)-{3})?(?:\n|\r)*([\w\W]*)*/ markdown: /^(-{3}(?:\n|\r)([\w\W]+?)(?:\n|\r)-{3})?(?:\n|\r)*([\w\W]*)*/
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment