From 1ad7a91f4ddbe83bb95e26a3d3bf1d980f8a4bcf Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Mon, 25 Jan 2016 17:50:04 +0000 Subject: [PATCH] Reorganise & rename server/data/ folder internals refs #6301 - In the migration folder, commands.js changed to builder.js to resolve conflict with the 'commands' inside data/utils/clients/. - a new data/schema/ folder has been added to hold all the code related to the database schema - data/utils/clients have been moved to data/schema/clients - data/utils/index.js has become data/schema/commands.js - data/schema.js has been split, the definition of the DB schema stays put, the additional checks have moved to data/schema/checks.js - data/validation/index.js has become data/schema/versioning.js - data/fixtures has moved to data/migration/fixtures - data/default-settings.json has moved to data/schema/default-settings.json --- core/server/data/export/index.js | 6 +- .../migration/{commands.js => builder.js} | 14 +- .../{ => migration}/fixtures/fixtures.json | 0 .../data/{ => migration}/fixtures/index.js | 24 +- .../fixtures/permissions/index.js | 8 +- .../fixtures/permissions/permissions.json | 0 core/server/data/migration/index.js | 34 +-- core/server/data/schema.js | 225 ------------------ core/server/data/schema/checks.js | 26 ++ .../data/{utils => schema}/clients/index.js | 0 .../data/{utils => schema}/clients/mysql.js | 0 .../data/{utils => schema}/clients/pg.js | 0 .../data/{utils => schema}/clients/sqlite3.js | 0 .../{utils/index.js => schema/commands.js} | 4 +- .../data/{ => schema}/default-settings.json | 0 core/server/data/schema/index.js | 11 + core/server/data/schema/schema.js | 197 +++++++++++++++ .../index.js => schema/versioning.js} | 3 +- core/server/models/settings.js | 2 +- core/test/integration/export_spec.js | 2 +- core/test/unit/migration_spec.js | 4 +- core/test/utils/index.js | 4 +- 22 files changed, 286 insertions(+), 278 deletions(-) rename core/server/data/migration/{commands.js => builder.js} (86%) rename core/server/data/{ => migration}/fixtures/fixtures.json (100%) rename core/server/data/{ => migration}/fixtures/index.js (95%) rename core/server/data/{ => migration}/fixtures/permissions/index.js (94%) rename core/server/data/{ => migration}/fixtures/permissions/permissions.json (100%) delete mode 100644 core/server/data/schema.js create mode 100644 core/server/data/schema/checks.js rename core/server/data/{utils => schema}/clients/index.js (100%) rename core/server/data/{utils => schema}/clients/mysql.js (100%) rename core/server/data/{utils => schema}/clients/pg.js (100%) rename core/server/data/{utils => schema}/clients/sqlite3.js (100%) rename core/server/data/{utils/index.js => schema/commands.js} (98%) rename core/server/data/{ => schema}/default-settings.json (100%) create mode 100644 core/server/data/schema/index.js create mode 100644 core/server/data/schema/schema.js rename core/server/data/{versioning/index.js => schema/versioning.js} (97%) diff --git a/core/server/data/export/index.js b/core/server/data/export/index.js index 1245908528..94aad9530f 100644 --- a/core/server/data/export/index.js +++ b/core/server/data/export/index.js @@ -1,8 +1,8 @@ var _ = require('lodash'), Promise = require('bluebird'), - versioning = require('../versioning'), config = require('../../config'), - utils = require('../utils'), + commands = require('../schema').commands, + versioning = require('../schema').versioning, serverUtils = require('../../utils'), errors = require('../../errors'), settings = require('../../api/settings'), @@ -28,7 +28,7 @@ exportFileName = function () { }; exporter = function () { - return Promise.join(versioning.getDatabaseVersion(), utils.getTables()).then(function (results) { + return Promise.join(versioning.getDatabaseVersion(), commands.getTables()).then(function (results) { var version = results[0], tables = results[1], selectOps = _.map(tables, function (name) { diff --git a/core/server/data/migration/commands.js b/core/server/data/migration/builder.js similarity index 86% rename from core/server/data/migration/commands.js rename to core/server/data/migration/builder.js index 2649bd9cff..a4bbd15769 100644 --- a/core/server/data/migration/commands.js +++ b/core/server/data/migration/builder.js @@ -1,6 +1,6 @@ -var _ = require('lodash'), +var _ = require('lodash'), errors = require('../../errors'), - utils = require('../utils'), + commands = require('../schema').commands, schema = require('../schema').tables, i18n = require('../../i18n'), @@ -22,7 +22,7 @@ getDeleteCommands = function getDeleteCommands(oldTables, newTables) { return _.map(deleteTables, function (table) { return function () { logInfo(i18n.t('notices.data.migration.commands.deletingTable', {table: table})); - return utils.deleteTable(table); + return commands.deleteTable(table); }; }); }; @@ -31,7 +31,7 @@ getAddCommands = function getAddCommands(oldTables, newTables) { return _.map(addTables, function (table) { return function () { logInfo(i18n.t('notices.data.migration.commands.creatingTable', {table: table})); - return utils.createTable(table); + return commands.createTable(table); }; }); }; @@ -42,7 +42,7 @@ addColumnCommands = function addColumnCommands(table, columns) { return _.map(addColumns, function (column) { return function () { logInfo(i18n.t('notices.data.migration.commands.addingColumn', {table: table, column: column})); - return utils.addColumn(table, column); + return commands.addColumn(table, column); }; }); }; @@ -53,14 +53,14 @@ modifyUniqueCommands = function modifyUniqueCommands(table, indexes) { if (!_.contains(indexes, table + '_' + column + '_unique')) { return function () { logInfo(i18n.t('notices.data.migration.commands.addingUnique', {table: table, column: column})); - return utils.addUnique(table, column); + return commands.addUnique(table, column); }; } } else if (!schema[table][column].unique) { if (_.contains(indexes, table + '_' + column + '_unique')) { return function () { logInfo(i18n.t('notices.data.migration.commands.droppingUnique', {table: table, column: column})); - return utils.dropUnique(table, column); + return commands.dropUnique(table, column); }; } } diff --git a/core/server/data/fixtures/fixtures.json b/core/server/data/migration/fixtures/fixtures.json similarity index 100% rename from core/server/data/fixtures/fixtures.json rename to core/server/data/migration/fixtures/fixtures.json diff --git a/core/server/data/fixtures/index.js b/core/server/data/migration/fixtures/index.js similarity index 95% rename from core/server/data/fixtures/index.js rename to core/server/data/migration/fixtures/index.js index e40379021f..d845ba609c 100644 --- a/core/server/data/fixtures/index.js +++ b/core/server/data/migration/fixtures/index.js @@ -5,18 +5,18 @@ // rather than abstracted into a migration system. The upgrade function checks that its changes are safe before // making them. -var Promise = require('bluebird'), - crypto = require('crypto'), - sequence = require('../../utils/sequence'), - _ = require('lodash'), - errors = require('../../errors'), - config = require('../../config'), - utils = require('../../utils'), - models = require('../../models'), - fixtures = require('./fixtures'), - permissions = require('./permissions'), - notifications = require('../../api/notifications'), - i18n = require('../../i18n'), +var Promise = require('bluebird'), + crypto = require('crypto'), + _ = require('lodash'), + fixtures = require('./fixtures'), + permissions = require('./permissions/index'), + notifications = require('../../../api/notifications'), + config = require('../../../config'), + errors = require('../../../errors'), + i18n = require('../../../i18n'), + models = require('../../../models'), + utils = require('../../../utils'), + sequence = require('../../../utils/sequence'), // Private logInfo, diff --git a/core/server/data/fixtures/permissions/index.js b/core/server/data/migration/fixtures/permissions/index.js similarity index 94% rename from core/server/data/fixtures/permissions/index.js rename to core/server/data/migration/fixtures/permissions/index.js index e9e525cd82..ccd7cf5910 100644 --- a/core/server/data/fixtures/permissions/index.js +++ b/core/server/data/migration/fixtures/permissions/index.js @@ -1,12 +1,12 @@ // # Permissions Fixtures // Sets up the permissions, and the default permissions_roles relationships var Promise = require('bluebird'), - sequence = require('../../../utils/sequence'), _ = require('lodash'), - errors = require('../../../errors'), - models = require('../../../models'), + errors = require('../../../../errors'), + i18n = require('../../../../i18n'), + models = require('../../../../models'), + sequence = require('../../../../utils/sequence'), fixtures = require('./permissions'), - i18n = require('../../../i18n'), // private logInfo, diff --git a/core/server/data/fixtures/permissions/permissions.json b/core/server/data/migration/fixtures/permissions/permissions.json similarity index 100% rename from core/server/data/fixtures/permissions/permissions.json rename to core/server/data/migration/fixtures/permissions/permissions.json diff --git a/core/server/data/migration/index.js b/core/server/data/migration/index.js index f39e5a1473..3997470786 100644 --- a/core/server/data/migration/index.js +++ b/core/server/data/migration/index.js @@ -1,19 +1,19 @@ var _ = require('lodash'), Promise = require('bluebird'), crypto = require('crypto'), - sequence = require('../../utils/sequence'), path = require('path'), fs = require('fs'), - errors = require('../../errors'), - commands = require('./commands'), - versioning = require('../versioning'), - models = require('../../models'), - fixtures = require('../fixtures'), + builder = require('./builder'), + fixtures = require('./fixtures'), schema = require('../schema').tables, + commands = require('../schema').commands, + versioning = require('../schema').versioning, dataExport = require('../export'), - utils = require('../utils'), config = require('../../config'), + errors = require('../../errors'), i18n = require('../../i18n'), + models = require('../../models'), + sequence = require('../../utils/sequence'), schemaTables = _.keys(schema), @@ -125,7 +125,7 @@ init = function (tablesOnly) { reset = function () { var tables = _.map(schemaTables, function (table) { return function () { - return utils.deleteTable(table); + return commands.deleteTable(table); }; }).reverse(); @@ -138,7 +138,7 @@ migrateUpFreshDb = function (tablesOnly) { tables = _.map(schemaTables, function (table) { return function () { logInfo(i18n.t('notices.data.migration.index.creatingTable', {table: table})); - return utils.createTable(table); + return commands.createTable(table); }; }); logInfo(i18n.t('notices.data.migration.index.creatingTables')); @@ -162,27 +162,27 @@ migrateUp = function (fromVersion, toVersion) { migrateOps = []; return backupDatabase().then(function () { - return utils.getTables(); + return commands.getTables(); }).then(function (tables) { oldTables = tables; if (!_.isEmpty(oldTables)) { - return utils.checkTables(); + return commands.checkTables(); } }).then(function () { - migrateOps = migrateOps.concat(commands.getDeleteCommands(oldTables, schemaTables)); - migrateOps = migrateOps.concat(commands.getAddCommands(oldTables, schemaTables)); + migrateOps = migrateOps.concat(builder.getDeleteCommands(oldTables, schemaTables)); + migrateOps = migrateOps.concat(builder.getAddCommands(oldTables, schemaTables)); return Promise.all( _.map(oldTables, function (table) { - return utils.getIndexes(table).then(function (indexes) { - modifyUniCommands = modifyUniCommands.concat(commands.modifyUniqueCommands(table, indexes)); + return commands.getIndexes(table).then(function (indexes) { + modifyUniCommands = modifyUniCommands.concat(builder.modifyUniqueCommands(table, indexes)); }); }) ); }).then(function () { return Promise.all( _.map(oldTables, function (table) { - return utils.getColumns(table).then(function (columns) { - migrateOps = migrateOps.concat(commands.addColumnCommands(table, columns)); + return commands.getColumns(table).then(function (columns) { + migrateOps = migrateOps.concat(builder.addColumnCommands(table, columns)); }); }) ); diff --git a/core/server/data/schema.js b/core/server/data/schema.js deleted file mode 100644 index 1ce4bbfa4c..0000000000 --- a/core/server/data/schema.js +++ /dev/null @@ -1,225 +0,0 @@ -var db = { - posts: { - id: {type: 'increments', nullable: false, primary: true}, - uuid: {type: 'string', maxlength: 36, nullable: false, validations: {isUUID: true}}, - title: {type: 'string', maxlength: 150, nullable: false}, - slug: {type: 'string', maxlength: 150, nullable: false, unique: true}, - markdown: {type: 'text', maxlength: 16777215, fieldtype: 'medium', nullable: true}, - html: {type: 'text', maxlength: 16777215, fieldtype: 'medium', nullable: true}, - image: {type: 'text', maxlength: 2000, nullable: true}, - featured: {type: 'bool', nullable: false, defaultTo: false, validations: {isIn: [[0, 1, false, true]]}}, - page: {type: 'bool', nullable: false, defaultTo: false, validations: {isIn: [[0, 1, false, true]]}}, - status: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'draft'}, - language: {type: 'string', maxlength: 6, nullable: false, defaultTo: 'en_US'}, - meta_title: {type: 'string', maxlength: 150, nullable: true}, - meta_description: {type: 'string', maxlength: 200, nullable: true}, - author_id: {type: 'integer', nullable: false}, - created_at: {type: 'dateTime', nullable: false}, - created_by: {type: 'integer', nullable: false}, - updated_at: {type: 'dateTime', nullable: true}, - updated_by: {type: 'integer', nullable: true}, - published_at: {type: 'dateTime', nullable: true}, - published_by: {type: 'integer', nullable: true} - }, - users: { - id: {type: 'increments', nullable: false, primary: true}, - uuid: {type: 'string', maxlength: 36, nullable: false, validations: {isUUID: true}}, - name: {type: 'string', maxlength: 150, nullable: false}, - slug: {type: 'string', maxlength: 150, nullable: false, unique: true}, - password: {type: 'string', maxlength: 60, nullable: false}, - email: {type: 'string', maxlength: 254, nullable: false, unique: true, validations: {isEmail: true}}, - image: {type: 'text', maxlength: 2000, nullable: true}, - cover: {type: 'text', maxlength: 2000, nullable: true}, - bio: {type: 'string', maxlength: 200, nullable: true}, - website: {type: 'text', maxlength: 2000, nullable: true, validations: {isEmptyOrURL: true}}, - location: {type: 'text', maxlength: 65535, nullable: true}, - accessibility: {type: 'text', maxlength: 65535, nullable: true}, - status: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'active'}, - language: {type: 'string', maxlength: 6, nullable: false, defaultTo: 'en_US'}, - meta_title: {type: 'string', maxlength: 150, nullable: true}, - meta_description: {type: 'string', maxlength: 200, nullable: true}, - tour: {type: 'text', maxlength: 65535, nullable: true}, - last_login: {type: 'dateTime', nullable: true}, - created_at: {type: 'dateTime', nullable: false}, - created_by: {type: 'integer', nullable: false}, - updated_at: {type: 'dateTime', nullable: true}, - updated_by: {type: 'integer', nullable: true} - }, - roles: { - id: {type: 'increments', nullable: false, primary: true}, - uuid: {type: 'string', maxlength: 36, nullable: false, validations: {isUUID: true}}, - name: {type: 'string', maxlength: 150, nullable: false}, - description: {type: 'string', maxlength: 200, nullable: true}, - created_at: {type: 'dateTime', nullable: false}, - created_by: {type: 'integer', nullable: false}, - updated_at: {type: 'dateTime', nullable: true}, - updated_by: {type: 'integer', nullable: true} - }, - roles_users: { - id: {type: 'increments', nullable: false, primary: true}, - role_id: {type: 'integer', nullable: false}, - user_id: {type: 'integer', nullable: false} - }, - permissions: { - id: {type: 'increments', nullable: false, primary: true}, - uuid: {type: 'string', maxlength: 36, nullable: false, validations: {isUUID: true}}, - name: {type: 'string', maxlength: 150, nullable: false}, - object_type: {type: 'string', maxlength: 150, nullable: false}, - action_type: {type: 'string', maxlength: 150, nullable: false}, - object_id: {type: 'integer', nullable: true}, - created_at: {type: 'dateTime', nullable: false}, - created_by: {type: 'integer', nullable: false}, - updated_at: {type: 'dateTime', nullable: true}, - updated_by: {type: 'integer', nullable: true} - }, - permissions_users: { - id: {type: 'increments', nullable: false, primary: true}, - user_id: {type: 'integer', nullable: false}, - permission_id: {type: 'integer', nullable: false} - }, - permissions_roles: { - id: {type: 'increments', nullable: false, primary: true}, - role_id: {type: 'integer', nullable: false}, - permission_id: {type: 'integer', nullable: false} - }, - permissions_apps: { - id: {type: 'increments', nullable: false, primary: true}, - app_id: {type: 'integer', nullable: false}, - permission_id: {type: 'integer', nullable: false} - }, - settings: { - id: {type: 'increments', nullable: false, primary: true}, - uuid: {type: 'string', maxlength: 36, nullable: false, validations: {isUUID: true}}, - key: {type: 'string', maxlength: 150, nullable: false, unique: true}, - value: {type: 'text', maxlength: 65535, nullable: true}, - type: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'core', validations: {isIn: [['core', 'blog', 'theme', 'app', 'plugin', 'private']]}}, - created_at: {type: 'dateTime', nullable: false}, - created_by: {type: 'integer', nullable: false}, - updated_at: {type: 'dateTime', nullable: true}, - updated_by: {type: 'integer', nullable: true} - }, - tags: { - id: {type: 'increments', nullable: false, primary: true}, - uuid: {type: 'string', maxlength: 36, nullable: false, validations: {isUUID: true}}, - name: {type: 'string', maxlength: 150, nullable: false, validations: {matches: /^([^,]|$)/}}, - slug: {type: 'string', maxlength: 150, nullable: false, unique: true}, - description: {type: 'string', maxlength: 200, nullable: true}, - image: {type: 'text', maxlength: 2000, nullable: true}, - hidden: {type: 'bool', nullable: false, defaultTo: false, validations: {isIn: [[0, 1, false, true]]}}, - parent_id: {type: 'integer', nullable: true}, - meta_title: {type: 'string', maxlength: 150, nullable: true}, - meta_description: {type: 'string', maxlength: 200, nullable: true}, - created_at: {type: 'dateTime', nullable: false}, - created_by: {type: 'integer', nullable: false}, - updated_at: {type: 'dateTime', nullable: true}, - updated_by: {type: 'integer', nullable: true} - }, - posts_tags: { - id: {type: 'increments', nullable: false, primary: true}, - post_id: {type: 'integer', nullable: false, unsigned: true, references: 'posts.id'}, - tag_id: {type: 'integer', nullable: false, unsigned: true, references: 'tags.id'}, - sort_order: {type: 'integer', nullable: false, unsigned: true, defaultTo: 0} - }, - apps: { - id: {type: 'increments', nullable: false, primary: true}, - uuid: {type: 'string', maxlength: 36, nullable: false, validations: {isUUID: true}}, - name: {type: 'string', maxlength: 150, nullable: false, unique: true}, - slug: {type: 'string', maxlength: 150, nullable: false, unique: true}, - version: {type: 'string', maxlength: 150, nullable: false}, - status: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'inactive'}, - created_at: {type: 'dateTime', nullable: false}, - created_by: {type: 'integer', nullable: false}, - updated_at: {type: 'dateTime', nullable: true}, - updated_by: {type: 'integer', nullable: true} - }, - app_settings: { - id: {type: 'increments', nullable: false, primary: true}, - uuid: {type: 'string', maxlength: 36, nullable: false, validations: {isUUID: true}}, - key: {type: 'string', maxlength: 150, nullable: false, unique: true}, - value: {type: 'text', maxlength: 65535, nullable: true}, - app_id: {type: 'integer', nullable: false, unsigned: true, references: 'apps.id'}, - created_at: {type: 'dateTime', nullable: false}, - created_by: {type: 'integer', nullable: false}, - updated_at: {type: 'dateTime', nullable: true}, - updated_by: {type: 'integer', nullable: true} - }, - app_fields: { - id: {type: 'increments', nullable: false, primary: true}, - uuid: {type: 'string', maxlength: 36, nullable: false, validations: {isUUID: true}}, - key: {type: 'string', maxlength: 150, nullable: false}, - value: {type: 'text', maxlength: 65535, nullable: true}, - type: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'html'}, - app_id: {type: 'integer', nullable: false, unsigned: true, references: 'apps.id'}, - relatable_id: {type: 'integer', nullable: false, unsigned: true}, - relatable_type: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'posts'}, - active: {type: 'bool', nullable: false, defaultTo: true, validations: {isIn: [[0, 1, false, true]]}}, - created_at: {type: 'dateTime', nullable: false}, - created_by: {type: 'integer', nullable: false}, - updated_at: {type: 'dateTime', nullable: true}, - updated_by: {type: 'integer', nullable: true} - }, - clients: { - id: {type: 'increments', nullable: false, primary: true}, - uuid: {type: 'string', maxlength: 36, nullable: false}, - name: {type: 'string', maxlength: 150, nullable: false, unique: true}, - slug: {type: 'string', maxlength: 150, nullable: false, unique: true}, - secret: {type: 'string', maxlength: 150, nullable: false}, - redirection_uri: {type: 'string', maxlength: 2000, nullable: true}, - logo: {type: 'string', maxlength: 2000, nullable: true}, - status: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'development'}, - type: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'ua'}, - description: {type: 'string', maxlength: 200, nullable: true}, - created_at: {type: 'dateTime', nullable: false}, - created_by: {type: 'integer', nullable: false}, - updated_at: {type: 'dateTime', nullable: true}, - updated_by: {type: 'integer', nullable: true} - }, - client_trusted_domains: { - id: {type: 'increments', nullable: false, primary: true}, - uuid: {type: 'string', maxlength: 36, nullable: false}, - client_id: {type: 'integer', nullable: false, unsigned: true, references: 'clients.id'}, - trusted_domain: {type: 'string', maxlength: 2000, nullable: true} - }, - accesstokens: { - id: {type: 'increments', nullable: false, primary: true}, - token: {type: 'string', nullable: false, unique: true}, - user_id: {type: 'integer', nullable: false, unsigned: true, references: 'users.id'}, - client_id: {type: 'integer', nullable: false, unsigned: true, references: 'clients.id'}, - expires: {type: 'bigInteger', nullable: false} - }, - refreshtokens: { - id: {type: 'increments', nullable: false, primary: true}, - token: {type: 'string', nullable: false, unique: true}, - user_id: {type: 'integer', nullable: false, unsigned: true, references: 'users.id'}, - client_id: {type: 'integer', nullable: false, unsigned: true, references: 'clients.id'}, - expires: {type: 'bigInteger', nullable: false} - } - }; - -function isPost(jsonData) { - return jsonData.hasOwnProperty('html') && jsonData.hasOwnProperty('markdown') && - jsonData.hasOwnProperty('title') && jsonData.hasOwnProperty('slug'); -} - -function isTag(jsonData) { - return jsonData.hasOwnProperty('name') && jsonData.hasOwnProperty('slug') && - jsonData.hasOwnProperty('description') && jsonData.hasOwnProperty('parent'); -} - -function isUser(jsonData) { - return jsonData.hasOwnProperty('bio') && jsonData.hasOwnProperty('website') && - jsonData.hasOwnProperty('status') && jsonData.hasOwnProperty('location'); -} - -function isNav(jsonData) { - return jsonData.hasOwnProperty('label') && jsonData.hasOwnProperty('url') && - jsonData.hasOwnProperty('slug') && jsonData.hasOwnProperty('current'); -} - -module.exports.tables = db; -module.exports.checks = { - isPost: isPost, - isTag: isTag, - isUser: isUser, - isNav: isNav -}; diff --git a/core/server/data/schema/checks.js b/core/server/data/schema/checks.js new file mode 100644 index 0000000000..59eaba8b6a --- /dev/null +++ b/core/server/data/schema/checks.js @@ -0,0 +1,26 @@ +function isPost(jsonData) { + return jsonData.hasOwnProperty('html') && jsonData.hasOwnProperty('markdown') && + jsonData.hasOwnProperty('title') && jsonData.hasOwnProperty('slug'); +} + +function isTag(jsonData) { + return jsonData.hasOwnProperty('name') && jsonData.hasOwnProperty('slug') && + jsonData.hasOwnProperty('description') && jsonData.hasOwnProperty('parent'); +} + +function isUser(jsonData) { + return jsonData.hasOwnProperty('bio') && jsonData.hasOwnProperty('website') && + jsonData.hasOwnProperty('status') && jsonData.hasOwnProperty('location'); +} + +function isNav(jsonData) { + return jsonData.hasOwnProperty('label') && jsonData.hasOwnProperty('url') && + jsonData.hasOwnProperty('slug') && jsonData.hasOwnProperty('current'); +} + +module.exports = { + isPost: isPost, + isTag: isTag, + isUser: isUser, + isNav: isNav +}; diff --git a/core/server/data/utils/clients/index.js b/core/server/data/schema/clients/index.js similarity index 100% rename from core/server/data/utils/clients/index.js rename to core/server/data/schema/clients/index.js diff --git a/core/server/data/utils/clients/mysql.js b/core/server/data/schema/clients/mysql.js similarity index 100% rename from core/server/data/utils/clients/mysql.js rename to core/server/data/schema/clients/mysql.js diff --git a/core/server/data/utils/clients/pg.js b/core/server/data/schema/clients/pg.js similarity index 100% rename from core/server/data/utils/clients/pg.js rename to core/server/data/schema/clients/pg.js diff --git a/core/server/data/utils/clients/sqlite3.js b/core/server/data/schema/clients/sqlite3.js similarity index 100% rename from core/server/data/utils/clients/sqlite3.js rename to core/server/data/schema/clients/sqlite3.js diff --git a/core/server/data/utils/index.js b/core/server/data/schema/commands.js similarity index 98% rename from core/server/data/utils/index.js rename to core/server/data/schema/commands.js index 64d03e0d74..4078a9f986 100644 --- a/core/server/data/utils/index.js +++ b/core/server/data/schema/commands.js @@ -1,9 +1,9 @@ var _ = require('lodash'), Promise = require('bluebird'), config = require('../../config'), - schema = require('../schema').tables, - clients = require('./clients'), i18n = require('../../i18n'), + schema = require('./schema'), + clients = require('./clients'), dbConfig; diff --git a/core/server/data/default-settings.json b/core/server/data/schema/default-settings.json similarity index 100% rename from core/server/data/default-settings.json rename to core/server/data/schema/default-settings.json diff --git a/core/server/data/schema/index.js b/core/server/data/schema/index.js new file mode 100644 index 0000000000..f641d958f3 --- /dev/null +++ b/core/server/data/schema/index.js @@ -0,0 +1,11 @@ +var schema = require('./schema'), + checks = require('./checks'), + commands = require('./commands'), + versioning = require('./versioning'), + defaultSettings = require('./default-settings'); + +module.exports.tables = schema; +module.exports.checks = checks; +module.exports.commands = commands; +module.exports.versioning = versioning; +module.exports.defaultSettings = defaultSettings; diff --git a/core/server/data/schema/schema.js b/core/server/data/schema/schema.js new file mode 100644 index 0000000000..312601c1cc --- /dev/null +++ b/core/server/data/schema/schema.js @@ -0,0 +1,197 @@ +module.exports = { + posts: { + id: {type: 'increments', nullable: false, primary: true}, + uuid: {type: 'string', maxlength: 36, nullable: false, validations: {isUUID: true}}, + title: {type: 'string', maxlength: 150, nullable: false}, + slug: {type: 'string', maxlength: 150, nullable: false, unique: true}, + markdown: {type: 'text', maxlength: 16777215, fieldtype: 'medium', nullable: true}, + html: {type: 'text', maxlength: 16777215, fieldtype: 'medium', nullable: true}, + image: {type: 'text', maxlength: 2000, nullable: true}, + featured: {type: 'bool', nullable: false, defaultTo: false, validations: {isIn: [[0, 1, false, true]]}}, + page: {type: 'bool', nullable: false, defaultTo: false, validations: {isIn: [[0, 1, false, true]]}}, + status: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'draft'}, + language: {type: 'string', maxlength: 6, nullable: false, defaultTo: 'en_US'}, + meta_title: {type: 'string', maxlength: 150, nullable: true}, + meta_description: {type: 'string', maxlength: 200, nullable: true}, + author_id: {type: 'integer', nullable: false}, + created_at: {type: 'dateTime', nullable: false}, + created_by: {type: 'integer', nullable: false}, + updated_at: {type: 'dateTime', nullable: true}, + updated_by: {type: 'integer', nullable: true}, + published_at: {type: 'dateTime', nullable: true}, + published_by: {type: 'integer', nullable: true} + }, + users: { + id: {type: 'increments', nullable: false, primary: true}, + uuid: {type: 'string', maxlength: 36, nullable: false, validations: {isUUID: true}}, + name: {type: 'string', maxlength: 150, nullable: false}, + slug: {type: 'string', maxlength: 150, nullable: false, unique: true}, + password: {type: 'string', maxlength: 60, nullable: false}, + email: {type: 'string', maxlength: 254, nullable: false, unique: true, validations: {isEmail: true}}, + image: {type: 'text', maxlength: 2000, nullable: true}, + cover: {type: 'text', maxlength: 2000, nullable: true}, + bio: {type: 'string', maxlength: 200, nullable: true}, + website: {type: 'text', maxlength: 2000, nullable: true, validations: {isEmptyOrURL: true}}, + location: {type: 'text', maxlength: 65535, nullable: true}, + accessibility: {type: 'text', maxlength: 65535, nullable: true}, + status: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'active'}, + language: {type: 'string', maxlength: 6, nullable: false, defaultTo: 'en_US'}, + meta_title: {type: 'string', maxlength: 150, nullable: true}, + meta_description: {type: 'string', maxlength: 200, nullable: true}, + tour: {type: 'text', maxlength: 65535, nullable: true}, + last_login: {type: 'dateTime', nullable: true}, + created_at: {type: 'dateTime', nullable: false}, + created_by: {type: 'integer', nullable: false}, + updated_at: {type: 'dateTime', nullable: true}, + updated_by: {type: 'integer', nullable: true} + }, + roles: { + id: {type: 'increments', nullable: false, primary: true}, + uuid: {type: 'string', maxlength: 36, nullable: false, validations: {isUUID: true}}, + name: {type: 'string', maxlength: 150, nullable: false}, + description: {type: 'string', maxlength: 200, nullable: true}, + created_at: {type: 'dateTime', nullable: false}, + created_by: {type: 'integer', nullable: false}, + updated_at: {type: 'dateTime', nullable: true}, + updated_by: {type: 'integer', nullable: true} + }, + roles_users: { + id: {type: 'increments', nullable: false, primary: true}, + role_id: {type: 'integer', nullable: false}, + user_id: {type: 'integer', nullable: false} + }, + permissions: { + id: {type: 'increments', nullable: false, primary: true}, + uuid: {type: 'string', maxlength: 36, nullable: false, validations: {isUUID: true}}, + name: {type: 'string', maxlength: 150, nullable: false}, + object_type: {type: 'string', maxlength: 150, nullable: false}, + action_type: {type: 'string', maxlength: 150, nullable: false}, + object_id: {type: 'integer', nullable: true}, + created_at: {type: 'dateTime', nullable: false}, + created_by: {type: 'integer', nullable: false}, + updated_at: {type: 'dateTime', nullable: true}, + updated_by: {type: 'integer', nullable: true} + }, + permissions_users: { + id: {type: 'increments', nullable: false, primary: true}, + user_id: {type: 'integer', nullable: false}, + permission_id: {type: 'integer', nullable: false} + }, + permissions_roles: { + id: {type: 'increments', nullable: false, primary: true}, + role_id: {type: 'integer', nullable: false}, + permission_id: {type: 'integer', nullable: false} + }, + permissions_apps: { + id: {type: 'increments', nullable: false, primary: true}, + app_id: {type: 'integer', nullable: false}, + permission_id: {type: 'integer', nullable: false} + }, + settings: { + id: {type: 'increments', nullable: false, primary: true}, + uuid: {type: 'string', maxlength: 36, nullable: false, validations: {isUUID: true}}, + key: {type: 'string', maxlength: 150, nullable: false, unique: true}, + value: {type: 'text', maxlength: 65535, nullable: true}, + type: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'core', validations: {isIn: [['core', 'blog', 'theme', 'app', 'plugin', 'private']]}}, + created_at: {type: 'dateTime', nullable: false}, + created_by: {type: 'integer', nullable: false}, + updated_at: {type: 'dateTime', nullable: true}, + updated_by: {type: 'integer', nullable: true} + }, + tags: { + id: {type: 'increments', nullable: false, primary: true}, + uuid: {type: 'string', maxlength: 36, nullable: false, validations: {isUUID: true}}, + name: {type: 'string', maxlength: 150, nullable: false, validations: {matches: /^([^,]|$)/}}, + slug: {type: 'string', maxlength: 150, nullable: false, unique: true}, + description: {type: 'string', maxlength: 200, nullable: true}, + image: {type: 'text', maxlength: 2000, nullable: true}, + hidden: {type: 'bool', nullable: false, defaultTo: false, validations: {isIn: [[0, 1, false, true]]}}, + parent_id: {type: 'integer', nullable: true}, + meta_title: {type: 'string', maxlength: 150, nullable: true}, + meta_description: {type: 'string', maxlength: 200, nullable: true}, + created_at: {type: 'dateTime', nullable: false}, + created_by: {type: 'integer', nullable: false}, + updated_at: {type: 'dateTime', nullable: true}, + updated_by: {type: 'integer', nullable: true} + }, + posts_tags: { + id: {type: 'increments', nullable: false, primary: true}, + post_id: {type: 'integer', nullable: false, unsigned: true, references: 'posts.id'}, + tag_id: {type: 'integer', nullable: false, unsigned: true, references: 'tags.id'}, + sort_order: {type: 'integer', nullable: false, unsigned: true, defaultTo: 0} + }, + apps: { + id: {type: 'increments', nullable: false, primary: true}, + uuid: {type: 'string', maxlength: 36, nullable: false, validations: {isUUID: true}}, + name: {type: 'string', maxlength: 150, nullable: false, unique: true}, + slug: {type: 'string', maxlength: 150, nullable: false, unique: true}, + version: {type: 'string', maxlength: 150, nullable: false}, + status: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'inactive'}, + created_at: {type: 'dateTime', nullable: false}, + created_by: {type: 'integer', nullable: false}, + updated_at: {type: 'dateTime', nullable: true}, + updated_by: {type: 'integer', nullable: true} + }, + app_settings: { + id: {type: 'increments', nullable: false, primary: true}, + uuid: {type: 'string', maxlength: 36, nullable: false, validations: {isUUID: true}}, + key: {type: 'string', maxlength: 150, nullable: false, unique: true}, + value: {type: 'text', maxlength: 65535, nullable: true}, + app_id: {type: 'integer', nullable: false, unsigned: true, references: 'apps.id'}, + created_at: {type: 'dateTime', nullable: false}, + created_by: {type: 'integer', nullable: false}, + updated_at: {type: 'dateTime', nullable: true}, + updated_by: {type: 'integer', nullable: true} + }, + app_fields: { + id: {type: 'increments', nullable: false, primary: true}, + uuid: {type: 'string', maxlength: 36, nullable: false, validations: {isUUID: true}}, + key: {type: 'string', maxlength: 150, nullable: false}, + value: {type: 'text', maxlength: 65535, nullable: true}, + type: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'html'}, + app_id: {type: 'integer', nullable: false, unsigned: true, references: 'apps.id'}, + relatable_id: {type: 'integer', nullable: false, unsigned: true}, + relatable_type: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'posts'}, + active: {type: 'bool', nullable: false, defaultTo: true, validations: {isIn: [[0, 1, false, true]]}}, + created_at: {type: 'dateTime', nullable: false}, + created_by: {type: 'integer', nullable: false}, + updated_at: {type: 'dateTime', nullable: true}, + updated_by: {type: 'integer', nullable: true} + }, + clients: { + id: {type: 'increments', nullable: false, primary: true}, + uuid: {type: 'string', maxlength: 36, nullable: false}, + name: {type: 'string', maxlength: 150, nullable: false, unique: true}, + slug: {type: 'string', maxlength: 150, nullable: false, unique: true}, + secret: {type: 'string', maxlength: 150, nullable: false}, + redirection_uri: {type: 'string', maxlength: 2000, nullable: true}, + logo: {type: 'string', maxlength: 2000, nullable: true}, + status: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'development'}, + type: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'ua'}, + description: {type: 'string', maxlength: 200, nullable: true}, + created_at: {type: 'dateTime', nullable: false}, + created_by: {type: 'integer', nullable: false}, + updated_at: {type: 'dateTime', nullable: true}, + updated_by: {type: 'integer', nullable: true} + }, + client_trusted_domains: { + id: {type: 'increments', nullable: false, primary: true}, + uuid: {type: 'string', maxlength: 36, nullable: false}, + client_id: {type: 'integer', nullable: false, unsigned: true, references: 'clients.id'}, + trusted_domain: {type: 'string', maxlength: 2000, nullable: true} + }, + accesstokens: { + id: {type: 'increments', nullable: false, primary: true}, + token: {type: 'string', nullable: false, unique: true}, + user_id: {type: 'integer', nullable: false, unsigned: true, references: 'users.id'}, + client_id: {type: 'integer', nullable: false, unsigned: true, references: 'clients.id'}, + expires: {type: 'bigInteger', nullable: false} + }, + refreshtokens: { + id: {type: 'increments', nullable: false, primary: true}, + token: {type: 'string', nullable: false, unique: true}, + user_id: {type: 'integer', nullable: false, unsigned: true, references: 'users.id'}, + client_id: {type: 'integer', nullable: false, unsigned: true, references: 'clients.id'}, + expires: {type: 'bigInteger', nullable: false} + } +}; diff --git a/core/server/data/versioning/index.js b/core/server/data/schema/versioning.js similarity index 97% rename from core/server/data/versioning/index.js rename to core/server/data/schema/versioning.js index 309489d191..9152b55660 100644 --- a/core/server/data/versioning/index.js +++ b/core/server/data/schema/versioning.js @@ -2,8 +2,7 @@ var _ = require('lodash'), errors = require('../../errors'), config = require('../../config'), i18n = require('../../i18n'), - - defaultSettings = require('../default-settings'), + defaultSettings = require('./default-settings'), initialVersion = '000', defaultDatabaseVersion; diff --git a/core/server/models/settings.js b/core/server/models/settings.js index d017a6ccee..dc80c3969f 100644 --- a/core/server/models/settings.js +++ b/core/server/models/settings.js @@ -15,7 +15,7 @@ var Settings, // It's much easier for us to work with it as a single level // instead of iterating those categories every time function parseDefaultSettings() { - var defaultSettingsInCategories = require('../data/default-settings.json'), + var defaultSettingsInCategories = require('../data/schema/').defaultSettings, defaultSettingsFlattened = {}; _.each(defaultSettingsInCategories, function each(settings, categoryName) { diff --git a/core/test/integration/export_spec.js b/core/test/integration/export_spec.js index 0455d99318..baf3b8cbfe 100644 --- a/core/test/integration/export_spec.js +++ b/core/test/integration/export_spec.js @@ -7,7 +7,7 @@ var testUtils = require('../utils/index'), _ = require('lodash'), // Stuff we are testing - versioning = require('../../server/data/versioning/index'), + versioning = require('../../server/data/schema').versioning, exporter = require('../../server/data/export/index'), sandbox = sinon.sandbox.create(); diff --git a/core/test/unit/migration_spec.js b/core/test/unit/migration_spec.js index 7d07ddae31..bb9fb788ac 100644 --- a/core/test/unit/migration_spec.js +++ b/core/test/unit/migration_spec.js @@ -5,9 +5,9 @@ var should = require('should'), crypto = require('crypto'), // Stuff we are testing - defaultSettings = require('../../server/data/default-settings'), schema = require('../../server/data/schema'), - permissions = require('../../server/data/fixtures/permissions/permissions'); + permissions = require('../../server/data/migration/fixtures/permissions/permissions'), + defaultSettings = schema.defaultSettings; // To stop jshint complaining should.equal(true, true); diff --git a/core/test/utils/index.js b/core/test/utils/index.js index 0aee00aa1f..2e0e5a5495 100644 --- a/core/test/utils/index.js +++ b/core/test/utils/index.js @@ -1,5 +1,4 @@ var Promise = require('bluebird'), - sequence = require('../../server/utils/sequence'), _ = require('lodash'), fs = require('fs-extra'), path = require('path'), @@ -8,7 +7,8 @@ var Promise = require('bluebird'), Models = require('../../server/models'), SettingsAPI = require('../../server/api/settings'), permissions = require('../../server/permissions'), - permsFixtures = require('../../server/data/fixtures/permissions/permissions.json'), + permsFixtures = require('../../server/data/migration/fixtures/permissions/permissions.json'), + sequence = require('../../server/utils/sequence'), DataGenerator = require('./fixtures/data-generator'), filterData = require('./fixtures/filter-param'), API = require('./api'),