From 4f2421fac754486f4aad443bfe0c4fe0d3cb8ed6 Mon Sep 17 00:00:00 2001 From: William Dibbern Date: Fri, 16 Aug 2013 09:11:36 -0500 Subject: [PATCH] MySQL Support Closes #364 - Confirmed integration with local mysql installation works. - Updated fixtures and migration with appropriate schema-conforming values. - Updated schema with appropriate defaults and nullable columns. - Updated fixDates function on model base to appropriately deserialize values coming from SQLite now that dates are stored as actual DateTime objects/ISO strings. - Updated default language to be 'en_US'. --- .gitignore | 1 + config.js | 2 +- core/server/data/fixtures/001.js | 4 +- core/server/data/fixtures/003.js | 13 +++++++ core/server/data/migration/001.js | 12 +++--- core/server/data/migration/003.js | 49 ++++++++++++++++++++++++ core/server/data/migration/index.js | 4 +- core/server/models/base.js | 3 +- core/server/models/post.js | 5 ++- core/shared/lang/{en.json => en_US.json} | 0 core/shared/lang/i18n.js | 8 ++-- package.json | 1 + 12 files changed, 84 insertions(+), 18 deletions(-) create mode 100644 core/server/data/fixtures/003.js create mode 100644 core/server/data/migration/003.js rename core/shared/lang/{en.json => en_US.json} (100%) diff --git a/.gitignore b/.gitignore index 434d54d874..cd668d60b2 100644 --- a/.gitignore +++ b/.gitignore @@ -37,6 +37,7 @@ projectFilesBackup /core/server/data/export/exported* /docs /_site +/core/test/functional/*_test.png # Changelog, which is autogenerated, not committed CHANGELOG.md \ No newline at end of file diff --git a/config.js b/config.js index 7453684cf3..e4a2b67ccb 100644 --- a/config.js +++ b/config.js @@ -6,7 +6,7 @@ var path = require('path'), // ## Admin settings // Default language -config.defaultLang = 'en'; +config.defaultLang = 'en_US'; // Force i18n to be on config.forceI18n = true; diff --git a/core/server/data/fixtures/001.js b/core/server/data/fixtures/001.js index 3452fad213..9c807b511f 100644 --- a/core/server/data/fixtures/001.js +++ b/core/server/data/fixtures/001.js @@ -11,10 +11,10 @@ module.exports = { "meta_title": null, "meta_description": null, "meta_keywords": null, - "featured": null, + "featured": true, "image": null, "status": "published", - "language": null, + "language": "en", "author_id": 1, "created_at": 1373578890610, "created_by": 1, diff --git a/core/server/data/fixtures/003.js b/core/server/data/fixtures/003.js new file mode 100644 index 0000000000..64ee1f721a --- /dev/null +++ b/core/server/data/fixtures/003.js @@ -0,0 +1,13 @@ +var uuid = require('node-uuid'); + +module.exports = { + posts: [], + + settings: [], + + roles: [], + + permissions: [], + + permissions_roles: [] +}; \ No newline at end of file diff --git a/core/server/data/migration/001.js b/core/server/data/migration/001.js index 82f2c7743c..45cda92bb9 100644 --- a/core/server/data/migration/001.js +++ b/core/server/data/migration/001.js @@ -15,13 +15,13 @@ up = function () { t.string('slug'); t.text('content_raw'); t.text('content'); - t.string('meta_title'); - t.string('meta_description'); - t.string('meta_keywords'); - t.bool('featured'); - t.string('image'); + t.string('meta_title').nullable(); + t.string('meta_description').nullable(); + t.string('meta_keywords').nullable(); + t.bool('featured').defaultTo(false); + t.string('image').nullable(); t.string('status'); - t.string('language'); + t.string('language').defaultTo('en'); t.integer('author_id'); t.dateTime('created_at'); t.integer('created_by'); diff --git a/core/server/data/migration/003.js b/core/server/data/migration/003.js new file mode 100644 index 0000000000..a603689cf8 --- /dev/null +++ b/core/server/data/migration/003.js @@ -0,0 +1,49 @@ +var when = require('when'), + _ = require('underscore'), + knex = require('../../models/base').Knex, + migrationVersion = '003', + fixtures = require('../fixtures/' + migrationVersion), + errors = require('../../errorHandling'), + up, + down; + +up = function up() { + + return when.all([ + + knex('posts') + .whereNull('language') + .orWhere('language', 'en') + .update({ + 'language': 'en_US' + }), + + knex('posts') + .whereNull('featured') + .update({ + 'featured': false + }) + + ]).then(function incrementVersion() { + + // Lastly, update the current version settings to reflect this version + return knex('settings') + .where('key', 'currentVersion') + .update({ 'value': migrationVersion }); + + }); +}; + +down = function down() { + + return when.all([ + + // No new tables as of yet, so just return a wrapped value + when(true) + + ]); + +}; + +exports.up = up; +exports.down = down; \ No newline at end of file diff --git a/core/server/data/migration/index.js b/core/server/data/migration/index.js index ee7450a6fa..b77e791929 100644 --- a/core/server/data/migration/index.js +++ b/core/server/data/migration/index.js @@ -4,10 +4,10 @@ var _ = require('underscore'), series = require('when/sequence'), errors = require('../../errorHandling'), knex = require('../../models/base').Knex, - initialVersion = "001", + initialVersion = '001', // This currentVersion string should always be the current version of Ghost, // we could probably load it from the config file. - currentVersion = "002"; + currentVersion = '003'; function getCurrentVersion() { return knex.Schema.hasTable('settings').then(function () { diff --git a/core/server/models/base.js b/core/server/models/base.js index 3a0fb51086..edc52490cb 100644 --- a/core/server/models/base.js +++ b/core/server/models/base.js @@ -1,5 +1,6 @@ var GhostBookshelf, Bookshelf = require('bookshelf'), + moment = require('moment'), _ = require('underscore'), config = require('../../../config'); @@ -16,7 +17,7 @@ GhostBookshelf.Model = GhostBookshelf.Model.extend({ fixDates: function (attrs) { _.each(attrs, function (value, key) { if (key.substr(-3) === '_at' && value !== null) { - attrs[key] = new Date(attrs[key]); + attrs[key] = moment(attrs[key]).toDate(); } }); diff --git a/core/server/models/post.js b/core/server/models/post.js index 4b590e71a3..bcfc4d068b 100644 --- a/core/server/models/post.js +++ b/core/server/models/post.js @@ -7,6 +7,7 @@ var Post, Showdown = require('showdown'), converter = new Showdown.converter(), User = require('./user').User, + config = require('../../../config'), GhostBookshelf = require('./base'); Post = GhostBookshelf.Model.extend({ @@ -18,8 +19,8 @@ Post = GhostBookshelf.Model.extend({ defaults: function () { return { uuid: uuid.v4(), - status: 'draft' - // TODO: language: ghost.config().defaultLang); + status: 'draft', + language: config.defaultLang }; }, diff --git a/core/shared/lang/en.json b/core/shared/lang/en_US.json similarity index 100% rename from core/shared/lang/en.json rename to core/shared/lang/en_US.json diff --git a/core/shared/lang/i18n.js b/core/shared/lang/i18n.js index b8a29fb21c..86719906a8 100644 --- a/core/shared/lang/i18n.js +++ b/core/shared/lang/i18n.js @@ -14,11 +14,11 @@ I18n = function (ghost) { return function (req, res, next) { - if (lang === 'en') { + if (lang === 'en_US') { // TODO: do stuff here to optimise for en // Make jslint empty block error go away - lang = 'en'; + lang = 'en_US'; } /** TODO: potentially use req.acceptedLanguages rather than the default @@ -26,8 +26,8 @@ I18n = function (ghost) { * TODO: switch this mess to be promise driven */ fs.stat(langFilePath, function (error) { if (error) { - console.log('No language file found for language ' + lang + '. Defaulting to en'); - lang = 'en'; + console.log('No language file found for language ' + lang + '. Defaulting to en_US'); + lang = 'en_US'; } fs.readFile(langFilePath, function (error, data) { diff --git a/package.json b/package.json index 1c62e4aeee..1508594a50 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "underscore": "1.5.1", "showdown": "0.3.1", "sqlite3": "2.1.14", + "mysql": "~2.0.0-alpha8", "bookshelf": "0.2.4", "knex": "0.1.8", "when": "2.2.1",