Commit 3743777b authored by Nick's avatar Nick

fix: sqlite migrations

parent fefd60a3
......@@ -2,7 +2,7 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).
## [2.0.0-beta.XX] - 2018-XX-XX
## [2.0.0-beta.42] - 2018-02-17
### Added
- Added Patreon link in Contribute admin page
- Added Theme Code Injection functionality
......@@ -27,5 +27,5 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## [2.0.0-beta.11] - 2018-01-20
- First beta release
[2.0.0-beta.12]: https://github.com/Requarks/wiki/releases/tag/2.0.0-beta.12
[2.0.0-beta.42]: https://github.com/Requarks/wiki/releases/tag/2.0.0-beta.42
[2.0.0-beta.11]: https://github.com/Requarks/wiki/releases/tag/2.0.0-beta.11
......@@ -11,7 +11,6 @@ RUN apk update && \
WORKDIR /wiki
COPY package.json .
RUN yarn --silent
COPY ./dev/docker-sqlite/init.sh ./init.sh
ENV dockerdev 1
ENV DEVDB sqlite
......
......@@ -11,8 +11,16 @@ module.exports = {
WIKI.models = require('./db').init()
await WIKI.models.onReady
await WIKI.configSvc.loadFromDb()
try {
await WIKI.models.onReady
await WIKI.configSvc.loadFromDb()
} catch (err) {
WIKI.logger.error('Database Initialization Error: ' + err.message)
if (WIKI.IS_DEBUG) {
console.error(err)
}
process.exit(1)
}
this.bootMaster()
},
......
exports.up = knex => {
return knex.schema
.renameTable('pageHistory', 'pageHistory_old')
.createTable('pageHistory', table => {
table.increments('id').primary()
table.string('path').notNullable()
table.string('hash').notNullable()
table.string('title').notNullable()
table.string('description')
table.boolean('isPrivate').notNullable().defaultTo(false)
table.boolean('isPublished').notNullable().defaultTo(false)
table.string('publishStartDate')
table.string('publishEndDate')
table.text('content')
table.string('contentType').notNullable()
table.string('createdAt').notNullable()
table.string('action').defaultTo('updated')
table.string('editorKey').references('key').inTable('editors')
table.string('localeCode', 2).references('code').inTable('locales')
table.integer('authorId').unsigned().references('id').inTable('users')
})
.raw(`INSERT INTO pageHistory SELECT id,path,hash,title,description,isPrivate,isPublished,publishStartDate,publishEndDate,content,contentType,createdAt,'updated' AS action,editorKey,localeCode,authorId FROM pageHistory_old;`)
.dropTable('pageHistory_old')
}
exports.down = knex => {
return knex.schema
.renameTable('pageHistory', 'pageHistory_old')
.createTable('pageHistory', table => {
table.increments('id').primary()
table.string('path').notNullable()
table.string('hash').notNullable()
table.string('title').notNullable()
table.string('description')
table.boolean('isPrivate').notNullable().defaultTo(false)
table.boolean('isPublished').notNullable().defaultTo(false)
table.string('publishStartDate')
table.string('publishEndDate')
table.text('content')
table.string('contentType').notNullable()
table.string('createdAt').notNullable()
table.integer('pageId').unsigned().references('id').inTable('pages')
table.string('editorKey').references('key').inTable('editors')
table.string('localeCode', 2).references('code').inTable('locales')
table.integer('authorId').unsigned().references('id').inTable('users')
})
.raw('INSERT INTO pageHistory SELECT id,path,hash,title,description,isPrivate,isPublished,publishStartDate,publishEndDate,content,contentType,createdAt,NULL as pageId,editorKey,localeCode,authorId FROM pageHistory_old;')
.dropTable('pageHistory_old')
}
exports.up = knex => {
return knex.schema
.table('storage', table => {
table.string('syncInterval')
table.json('state')
})
}
exports.down = knex => {
return knex.schema
.table('storage', table => {
table.dropColumn('syncInterval')
table.dropColumn('state')
})
}
......@@ -2,6 +2,8 @@ const path = require('path')
const fs = require('fs-extra')
const semver = require('semver')
const baseMigrationPath = path.join(WIKI.SERVERPATH, (WIKI.config.db.type !== 'sqlite') ? 'db/migrations' : 'db/migrations-sqlite')
/* global WIKI */
module.exports = {
......@@ -10,11 +12,10 @@ module.exports = {
* @returns Promise<string[]>
*/
async getMigrations() {
const absoluteDir = path.join(WIKI.SERVERPATH, 'db/migrations')
const migrationFiles = await fs.readdir(absoluteDir)
const migrationFiles = await fs.readdir(baseMigrationPath)
return migrationFiles.sort(semver.compare).map(m => ({
file: m,
directory: absoluteDir
directory: baseMigrationPath
}))
},
......@@ -23,6 +24,6 @@ module.exports = {
},
getMigration(migration) {
return require(path.join(WIKI.SERVERPATH, 'db/migrations', migration.file));
return require(path.join(baseMigrationPath, migration.file));
}
}
......@@ -174,6 +174,7 @@ module.exports = class Page extends Model {
}
await WIKI.models.pageHistory.addVersion({
...ogPage,
isPublished: ogPage.isPublished === true || ogPage.isPublished === 1,
action: 'updated'
})
await WIKI.models.pages.query().patch({
......
......@@ -12,30 +12,28 @@ module.exports = {
new LocalStrategy({
usernameField: 'email',
passwordField: 'password'
}, (uEmail, uPassword, done) => {
WIKI.models.users.query().findOne({
email: uEmail,
providerKey: 'local'
}).then((user) => {
}, async (uEmail, uPassword, done) => {
try {
const user = await WIKI.models.users.query().findOne({
email: uEmail,
providerKey: 'local'
})
if (user) {
return user.verifyPassword(uPassword).then(() => {
if (!user.isActive) {
done(new WIKI.Error.AuthAccountBanned(), null)
} else if (!user.isVerified) {
done(new WIKI.Error.AuthAccountNotVerified(), null)
} else {
done(null, user)
}
}).catch((err) => {
done(err, null)
})
await user.verifyPassword(uPassword)
if (!user.isActive) {
done(new WIKI.Error.AuthAccountBanned(), null)
} else if (!user.isVerified) {
done(new WIKI.Error.AuthAccountNotVerified(), null)
} else {
done(null, user)
}
} else {
done(new WIKI.Error.AuthLoginFailed(), null)
}
}).catch((err) => {
} catch (err) {
done(err, null)
})
}
))
}
})
)
}
}
......@@ -124,28 +124,29 @@ const init = {
console.warn(chalk.yellow('--- Closing DB connections...'))
await global.WIKI.models.knex.destroy()
console.warn(chalk.yellow('--- Closing Server connections...'))
global.WIKI.server.destroy(() => {
console.warn(chalk.yellow('--- Purging node modules cache...'))
if (global.WIKI.server) {
await new Promise((resolve, reject) => global.WIKI.server.destroy(resolve))
}
console.warn(chalk.yellow('--- Purging node modules cache...'))
global.WIKI = {}
Object.keys(require.cache).forEach(id => {
if (/[/\\]server[/\\]/.test(id)) {
delete require.cache[id]
}
})
Object.keys(module.constructor._pathCache).forEach(cacheKey => {
if (/[/\\]server[/\\]/.test(cacheKey)) {
delete module.constructor._pathCache[cacheKey]
}
})
global.WIKI = {}
Object.keys(require.cache).forEach(id => {
if (/[/\\]server[/\\]/.test(id)) {
delete require.cache[id]
}
})
Object.keys(module.constructor._pathCache).forEach(cacheKey => {
if (/[/\\]server[/\\]/.test(cacheKey)) {
delete module.constructor._pathCache[cacheKey]
}
})
console.warn(chalk.yellow('--- Unregistering process listeners...'))
console.warn(chalk.yellow('--- Unregistering process listeners...'))
process.removeAllListeners('unhandledRejection')
process.removeAllListeners('uncaughtException')
process.removeAllListeners('unhandledRejection')
process.removeAllListeners('uncaughtException')
require('./server')
})
require('./server')
}
}
......
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