fix: MSSQL - setup + pageTree + page delete

This commit is contained in:
NGPixel 2019-10-25 23:02:33 -04:00
parent ea5c4ea33d
commit 820cc77a83
5 changed files with 53 additions and 36 deletions

View File

@ -2,7 +2,7 @@ exports.up = knex => {
return knex.schema return knex.schema
.dropTable('pageTree') .dropTable('pageTree')
.createTable('pageTree', table => { .createTable('pageTree', table => {
table.increments('id').primary() table.integer('id').primary()
table.string('path').notNullable() table.string('path').notNullable()
table.integer('depth').unsigned().notNullable() table.integer('depth').unsigned().notNullable()
table.string('title').notNullable() table.string('title').notNullable()
@ -20,7 +20,7 @@ exports.down = knex => {
return knex.schema return knex.schema
.dropTable('pageTree') .dropTable('pageTree')
.createTable('pageTree', table => { .createTable('pageTree', table => {
table.increments('id').primary() table.integer('id').primary()
table.string('path').notNullable() table.string('path').notNullable()
table.integer('depth').unsigned().notNullable() table.integer('depth').unsigned().notNullable()
table.string('title').notNullable() table.string('title').notNullable()

View File

@ -1,23 +1,53 @@
/* global WIKI */
exports.up = knex => { exports.up = knex => {
const dbCompat = {
charset: (WIKI.config.db.type === `mysql` || WIKI.config.db.type === `mariadb`),
selfCascadeDelete: WIKI.config.db.type !== 'mssql'
}
return knex.schema return knex.schema
.table('pageTree', table => { .dropTable('pageTree')
table.dropColumn('parent') .createTable('pageTree', table => {
table.dropColumn('pageId') if (dbCompat.charset) { table.charset('utf8mb4') }
table.integer('id').unsigned().primary()
table.string('path').notNullable()
table.integer('depth').unsigned().notNullable()
table.string('title').notNullable()
table.boolean('isPrivate').notNullable().defaultTo(false)
table.boolean('isFolder').notNullable().defaultTo(false)
table.string('privateNS')
}) })
.table('pageTree', table => { .table('pageTree', table => {
if (dbCompat.selfCascadeDelete) {
table.integer('parent').unsigned().references('id').inTable('pageTree').onDelete('CASCADE') table.integer('parent').unsigned().references('id').inTable('pageTree').onDelete('CASCADE')
} else {
table.integer('parent').unsigned()
}
table.integer('pageId').unsigned().references('id').inTable('pages').onDelete('CASCADE') table.integer('pageId').unsigned().references('id').inTable('pages').onDelete('CASCADE')
table.string('localeCode', 5).references('code').inTable('locales')
}) })
} }
exports.down = knex => { exports.down = knex => {
const dbCompat = {
charset: (WIKI.config.db.type === `mysql` || WIKI.config.db.type === `mariadb`),
selfCascadeDelete: WIKI.config.db.type !== 'mssql'
}
return knex.schema return knex.schema
.table('pageTree', table => { .dropTable('pageTree')
table.dropColumn('parent') .createTable('pageTree', table => {
table.dropColumn('pageId') if (dbCompat.charset) { table.charset('utf8mb4') }
table.integer('id').primary()
table.string('path').notNullable()
table.integer('depth').unsigned().notNullable()
table.string('title').notNullable()
table.boolean('isPrivate').notNullable().defaultTo(false)
table.boolean('isFolder').notNullable().defaultTo(false)
table.string('privateNS')
}) })
.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')
}) })
} }

View File

@ -53,16 +53,7 @@ module.exports = async (pageId) => {
await WIKI.models.knex.table('pageTree').truncate() await WIKI.models.knex.table('pageTree').truncate()
if (tree.length > 0) { if (tree.length > 0) {
const { bindings, sql } = WIKI.models.knex.table('pageTree').insert(tree).toSQL(); await WIKI.models.knex.table('pageTree').insert(tree)
if (WIKI.config.db.type === 'mssql') {
await WIKI.models.knex.raw(sql, bindings).wrap(
'SET IDENTITY_INSERT pageTree ON;',
'SET IDENTITY_INSERT pageTree OFF;',
)
} else {
await WIKI.models.knex.raw(sql, bindings)
}
// await WIKI.models.knex.table('pageTree').insert(tree)
} }
await WIKI.models.knex.destroy() await WIKI.models.knex.destroy()

View File

@ -568,8 +568,8 @@ module.exports = class Page extends Model {
} }
let affectedHashes = [] let affectedHashes = []
// -> Perform replace and return affected page hashes (POSTGRES, MSSQL only) // -> Perform replace and return affected page hashes (POSTGRES only)
if (WIKI.config.db.type === 'postgres' || WIKI.config.db.type === 'mssql') { if (WIKI.config.db.type === 'postgres') {
affectedHashes = await WIKI.models.pages.query() affectedHashes = await WIKI.models.pages.query()
.returning('hash') .returning('hash')
.patch({ .patch({
@ -583,7 +583,7 @@ module.exports = class Page extends Model {
}) })
.pluck('hash') .pluck('hash')
} else { } else {
// -> Perform replace, then query affected page hashes (MYSQL, MARIADB, SQLITE only) // -> Perform replace, then query affected page hashes (MYSQL, MARIADB, MSSQL, SQLITE only)
await WIKI.models.pages.query() await WIKI.models.pages.query()
.patch({ .patch({
render: WIKI.models.knex.raw('REPLACE(??, ?, ?)', ['render', replaceArgs.from, replaceArgs.to]) render: WIKI.models.knex.raw('REPLACE(??, ?, ?)', ['render', replaceArgs.from, replaceArgs.to])

View File

@ -9,7 +9,6 @@ const https = require('https')
const Promise = require('bluebird') const Promise = require('bluebird')
const fs = require('fs-extra') const fs = require('fs-extra')
const _ = require('lodash') const _ = require('lodash')
const cfgHelper = require('./helpers/config')
const crypto = Promise.promisifyAll(require('crypto')) const crypto = Promise.promisifyAll(require('crypto'))
const pem2jwk = require('pem-jwk').pem2jwk const pem2jwk = require('pem-jwk').pem2jwk
const semver = require('semver') const semver = require('semver')
@ -203,14 +202,14 @@ module.exports = () => {
WIKI.logger.info('Creating default groups...') WIKI.logger.info('Creating default groups...')
const adminGroup = await WIKI.models.groups.query().insert({ const adminGroup = await WIKI.models.groups.query().insert({
id: 1, ...(WIKI.config.db.type !== `mssql` && { id: 1 }),
name: 'Administrators', name: 'Administrators',
permissions: JSON.stringify(['manage:system']), permissions: JSON.stringify(['manage:system']),
pageRules: JSON.stringify([]), pageRules: JSON.stringify([]),
isSystem: true isSystem: true
}) })
const guestGroup = await WIKI.models.groups.query().insert({ const guestGroup = await WIKI.models.groups.query().insert({
id: 2, ...(WIKI.config.db.type !== `mssql` && { id: 2 }),
name: 'Guests', name: 'Guests',
permissions: JSON.stringify(['read:pages', 'read:assets', 'read:comments']), permissions: JSON.stringify(['read:pages', 'read:assets', 'read:comments']),
pageRules: JSON.stringify([ pageRules: JSON.stringify([
@ -218,6 +217,9 @@ module.exports = () => {
]), ]),
isSystem: true isSystem: true
}) })
if (adminGroup.id !== 1 || guestGroup.id !== 2) {
throw new Error('Incorrect groups auto-increment configuration! Should start at 0 and increment by 1. Contact your database administrator.')
}
// Load authentication strategies + enable local // Load authentication strategies + enable local
await WIKI.models.authentication.refreshStrategiesFromDisk() await WIKI.models.authentication.refreshStrategiesFromDisk()
@ -244,12 +246,8 @@ module.exports = () => {
// Create root administrator // Create root administrator
WIKI.logger.info('Creating root administrator...') WIKI.logger.info('Creating root administrator...')
await WIKI.models.users.query().delete().where({
providerKey: 'local',
email: req.body.adminEmail
}).orWhere('id', 1)
const adminUser = await WIKI.models.users.query().insert({ const adminUser = await WIKI.models.users.query().insert({
id: 1, ...(WIKI.config.db.type !== `mssql` && { id: 1 }),
email: req.body.adminEmail, email: req.body.adminEmail,
provider: 'local', provider: 'local',
password: req.body.adminPassword, password: req.body.adminPassword,
@ -264,12 +262,8 @@ module.exports = () => {
// Create Guest account // Create Guest account
WIKI.logger.info('Creating guest account...') WIKI.logger.info('Creating guest account...')
await WIKI.models.users.query().delete().where({
providerKey: 'local',
email: 'guest@example.com'
}).orWhere('id', 2)
const guestUser = await WIKI.models.users.query().insert({ const guestUser = await WIKI.models.users.query().insert({
id: 2, ...(WIKI.config.db.type !== `mssql` && { id: 2 }),
provider: 'local', provider: 'local',
email: 'guest@example.com', email: 'guest@example.com',
name: 'Guest', name: 'Guest',
@ -282,11 +276,13 @@ module.exports = () => {
isVerified: true isVerified: true
}) })
await guestUser.$relatedQuery('groups').relate(guestGroup.id) await guestUser.$relatedQuery('groups').relate(guestGroup.id)
if (adminUser.id !== 1 || guestUser.id !== 2) {
throw new Error('Incorrect groups auto-increment configuration! Should start at 0 and increment by 1. Contact your database administrator.')
}
// Create site nav // Create site nav
WIKI.logger.info('Creating default site navigation') WIKI.logger.info('Creating default site navigation')
await WIKI.models.navigation.query().delete().where({ key: 'site' })
await WIKI.models.navigation.query().insert({ await WIKI.models.navigation.query().insert({
key: 'site', key: 'site',
config: [ config: [