Skip to content

Commit

Permalink
🔥 remove postgres support
Browse files Browse the repository at this point in the history
refs #1333, refs #5878, refs #5321
  • Loading branch information
kirrg001 committed Sep 19, 2016
1 parent 7e659af commit 9848190
Show file tree
Hide file tree
Showing 15 changed files with 15 additions and 182 deletions.
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Expand Up @@ -100,7 +100,7 @@ Any other info e.g. Why do you consider this to be a bug? What did you expect to
* Ghost Version: master (latest commit: a761de2079dca4df49567b1bddac492f25033985)
* Node Version: 4.4.7
* Browser: Chrome 48.0.2564.109 on Mac OS X 10.10.4
* Database: SQLite / MySQL / postgres
* Database: SQLite / MySQL
```

<a name="features"></a>
Expand Down
4 changes: 0 additions & 4 deletions .travis.yml
Expand Up @@ -10,8 +10,6 @@ cache:
- node_modules
- core/client/node_modules
- core/client/bower_components
addons:
postgresql: "9.3"
env:
global:
- GITHUB_OAUTH_KEY=003a44d58f12089d0c0261338298af3813330949
Expand All @@ -20,7 +18,6 @@ env:
matrix:
- DB=sqlite3 NODE_ENV=testing
- DB=mysql NODE_ENV=testing-mysql
- DB=pg NODE_ENV=testing-pg
matrix:
include:
- node_js: "4"
Expand All @@ -31,7 +28,6 @@ branches:
- /^greenkeeper-.+$/
before_install:
- if [ $DB == "mysql" ]; then mysql -e 'create database ghost_testing'; fi
- if [ $DB == "pg" ]; then psql -c 'create database ghost_testing;' -U postgres; fi
after_success:
- |
if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then
Expand Down
4 changes: 2 additions & 2 deletions Gruntfile.js
Expand Up @@ -561,8 +561,8 @@ var overrides = require('./core/server/overrides'),
// `grunt test:integration/api/api_tags_spec.js`
//
// Their purpose is to test that both the api and models behave as expected when the database layer is involved.
// These tests are run against sqlite3, mysql and pg on travis and ensure that differences between the databases
// don't cause bugs. At present, pg often fails and is not officially supported.
// These tests are run against sqlite3 and mysql on travis and ensure that differences between the databases
// don't cause bugs.
//
// A coverage report can be generated for these tests using the `grunt test-coverage` task.
grunt.registerTask('test-integration', 'Run integration tests (mocha + db access)',
Expand Down
2 changes: 1 addition & 1 deletion core/server/apps/sandbox.js
Expand Up @@ -91,7 +91,7 @@ AppSandbox.prototype.loadModule = function loadModuleSandboxed(modulePath) {
};

AppSandbox.defaults = {
blacklist: ['knex', 'fs', 'http', 'sqlite3', 'pg', 'mysql', 'ghost']
blacklist: ['knex', 'fs', 'http', 'sqlite3', 'mysql', 'ghost']
};

module.exports = AppSandbox;
17 changes: 0 additions & 17 deletions core/server/config/env/config.testing-pg.json

This file was deleted.

37 changes: 1 addition & 36 deletions core/server/data/db/connection.js
Expand Up @@ -2,46 +2,12 @@ var knex = require('knex'),
config = require('../../config'),
knexInstance;

function isPostgreSQL(client) {
if (!client) {
return false;
}

return client === 'pg' || client === 'postgres' || client === 'postgresql';
}

// @TODO:
// - if you require this file before config file was loaded,
// - then this file is cached and you have no chance to connect to the db anymore
// - bring dynamic into this file (db.connect())
function configure(dbConfig) {
var client = dbConfig.client,
pg;

if (isPostgreSQL(client)) {
try {
pg = require('pg');
} catch (e) {
pg = require('pg.js');
}

// By default PostgreSQL returns data as strings along with an OID that identifies
// its type. We're setting the parser to convert OID 20 (int8) into a javascript
// integer.
pg.types.setTypeParser(20, function (val) {
return val === null ? null : parseInt(val, 10);
});

// https://github.com/tgriesser/knex/issues/97
// this sets the timezone to UTC only for the connection!
dbConfig.pool = {
afterCreate: function (connection, callback) {
connection.query('set timezone=\'UTC\'', function (err) {
callback(err, connection);
});
}
};
}
var client = dbConfig.client;

if (client === 'sqlite3') {
dbConfig.useNullAsDefault = dbConfig.useNullAsDefault || false;
Expand All @@ -59,4 +25,3 @@ if (!knexInstance && config.get('database') && config.get('database').client) {
}

module.exports = knexInstance;
module.exports.isPostgreSQL = isPostgreSQL;
@@ -1,7 +1,6 @@
var config = require('../../../../config'),
models = require(config.get('paths').corePath + '/server/models'),
api = require(config.get('paths').corePath + '/server/api'),
db = require(config.get('paths').corePath + '/server/data/db/connection'),
sequence = require(config.get('paths').corePath + '/server/utils/sequence'),
moment = require('moment'),
_ = require('lodash'),
Expand All @@ -27,7 +26,6 @@ _private.addOffset = function addOffset(date) {
};

/**
* postgres: stores dates with offset, so it's enough to force timezone UTC in the db connection (see data/db/connection.js)
* sqlite: stores UTC timestamps, but we will normalize the format to YYYY-MM-DD HH:mm:ss
*/
module.exports = function transformDatesIntoUTC(options, logger) {
Expand All @@ -44,9 +42,7 @@ module.exports = function transformDatesIntoUTC(options, logger) {
return Promise.reject(new Error('skip'));
}

if (db.isPostgreSQL()) {
_private.noOffset = true;
} else if (config.get('database').client === 'mysql') {
if (config.get('database').client === 'mysql') {
_private.noOffset = false;
} else if (config.get('database').client === 'sqlite3') {
_private.noOffset = true;
Expand Down
13 changes: 3 additions & 10 deletions core/server/data/migration/fixtures/008/01-fix-sqlite-pg-format.js
@@ -1,10 +1,9 @@
var config = require('../../../../config'),
_ = require('lodash'),
models = require(config.get('paths').corePath + '/server/models'),
db = require(config.get('paths').corePath + '/server/data/db/connection'),
transfomDatesIntoUTC = require(config.get('paths').corePath + '/server/data/migration/fixtures/006/01-transform-dates-into-utc'),
Promise = require('bluebird'),
messagePrefix = 'Fix sqlite/pg format: ',
messagePrefix = 'Fix sqlite format: ',
_private = {};

_private.rerunDateMigration = function rerunDateMigration(options, logger) {
Expand Down Expand Up @@ -34,22 +33,16 @@ _private.rerunDateMigration = function rerunDateMigration(options, logger) {
};

/**
* this migration script is a very special one for people who run their server in UTC and use sqlite3 or run their server in any TZ and use postgres
* this migration script is a very special one for people who run their server in UTC and use sqlite3
* 006/01-transform-dates-into-utc had a bug for this case, see what happen because of this bug https://github.com/TryGhost/Ghost/issues/7192
*/
module.exports = function fixSqliteFormat(options, logger) {
// CASE: skip this script when using mysql
if (config.get('database').client === 'mysql') {
logger.warn(messagePrefix + 'This script only runs, when using sqlite/postgres as database.');
logger.warn(messagePrefix + 'This script only runs, when using sqlite as database.');
return Promise.resolve();
}

// CASE: database is postgres, server is in ANY TZ, run 006/001 again
// we can't check the format for PG somehow, so we just run the migration again
if (db.isPostgreSQL()) {
return _private.rerunDateMigration(options, logger);
}

// CASE: sqlite3 and server is UTC, we check if the date migration was already running
return options.transacting.raw('select created_at from users')
.then(function (users) {
Expand Down
8 changes: 2 additions & 6 deletions core/server/data/schema/clients/index.js
@@ -1,11 +1,7 @@
var sqlite3 = require('./sqlite3'),
mysql = require('./mysql'),
pg = require('./pg');
mysql = require('./mysql');

module.exports = {
sqlite3: sqlite3,
mysql: mysql,
pg: pg,
postgres: pg,
postgresql: pg
mysql: mysql
};
2 changes: 1 addition & 1 deletion core/server/update-check.js
Expand Up @@ -12,7 +12,7 @@
// - node version
// - npm version
// - env - production or development
// - database type - SQLite, MySQL, PostgreSQL
// - database type - SQLite, MySQL
// - email transport - mail.options.service, or otherwise mail.transport
// - created date - database creation date
// - post count - total number of posts
Expand Down
2 changes: 0 additions & 2 deletions core/test/functional/routes/api/users_spec.js
Expand Up @@ -38,8 +38,6 @@ describe('User API', function () {
describe('As Owner', function () {
describe('Browse', function () {
it('returns dates in ISO 8601 format', function (done) {
// @TODO: postgres returns for default oder (last_login DESC) something else then sqlite
// @TODO: maybe related to https://github.com/TryGhost/Ghost/issues/6104
request.get(testUtils.API.getApiQuery('users/?order=id%20ASC'))
.set('Authorization', 'Bearer ' + ownerAccessToken)
.expect('Content-Type', /json/)
Expand Down
2 changes: 1 addition & 1 deletion core/test/integration/update_check_spec.js
Expand Up @@ -34,7 +34,7 @@ describe('Update Check', function () {
data.ghost_version.should.equal(packageInfo.version);
data.node_version.should.equal(process.versions.node);
data.env.should.equal(process.env.NODE_ENV);
data.database_type.should.match(/sqlite3|pg|mysql/);
data.database_type.should.match(/sqlite3|mysql/);
data.blog_id.should.be.a.String();
data.blog_id.should.not.be.empty();
data.theme.should.be.equal('casper');
Expand Down
92 changes: 1 addition & 91 deletions core/test/unit/migration_fixture_spec.js
Expand Up @@ -9,7 +9,6 @@ var should = require('should'),
configUtils = require('../utils/configUtils'),
models = require('../../server/models'),
api = require('../../server/api'),
db = require('../../server/data/db/connection'),
permissions = require('../../server/permissions'),
notifications = require('../../server/api/notifications'),
versioning = require('../../server/data/schema/versioning'),
Expand Down Expand Up @@ -955,8 +954,6 @@ describe('Fixtures', function () {
});

describe('Tasks:', function () {
var isPostgres = false;

it('should have tasks for 006', function () {
should.exist(fixtures006);
fixtures006.should.be.an.Array().with.lengthOf(1);
Expand All @@ -968,19 +965,11 @@ describe('Fixtures', function () {
migrationsSettingsValue;

beforeEach(function () {
sandbox.stub(db, 'isPostgreSQL', function isPostgreSQL() {
return isPostgres;
});

sandbox.stub(Date.prototype, 'getTimezoneOffset', function () {
return serverTimezoneOffset;
});
});

afterEach(function () {
isPostgres = false;
});

describe('error cases', function () {
before(function () {
serverTimezoneOffset = 0;
Expand Down Expand Up @@ -1065,46 +1054,6 @@ describe('Fixtures', function () {
sandbox.stub(api.settings, 'updateSettingsCache').returns(Promise.resolve({}));
});

it('pg: server TZ is UTC, only format is changing', function (done) {
createdAt = moment(1464798678537).toDate();
configUtils.set('database:client', 'pg');

isPostgres = true;
serverTimezoneOffset = 0;

moment(createdAt).format('YYYY-MM-DD HH:mm:ss').should.eql('2016-06-01 16:31:18');

updateClient({}, loggerStub)
.then(function () {
_.each(newModels, function (model) {
moment(model.get('created_at')).format('YYYY-MM-DD HH:mm:ss').should.eql('2016-06-01 16:31:18');
});

migrationsSettingsWasUpdated.should.eql(true);
done();
})
.catch(done);
});

it('pg: server TZ is non UTC, only format is changing', function (done) {
createdAt = moment(1464798678537).toDate();
configUtils.set('database:client', 'pg');
isPostgres = true;

moment(createdAt).format('YYYY-MM-DD HH:mm:ss').should.eql('2016-06-01 16:31:18');

updateClient({}, loggerStub)
.then(function () {
_.each(newModels, function (model) {
moment(model.get('created_at')).format('YYYY-MM-DD HH:mm:ss').should.eql('2016-06-01 16:31:18');
});

migrationsSettingsWasUpdated.should.eql(true);
done();
})
.catch(done);
});

it('server offset is 0 and sqlite', function (done) {
serverTimezoneOffset = 0;
createdAt = moment(1464798678537).toDate();
Expand Down Expand Up @@ -1276,23 +1225,16 @@ describe('Fixtures', function () {
describe('01-fix-sqlite-pg-format', function () {
var updateClient = rewire('../../server/data/migration/fixtures/008/01-fix-sqlite-pg-format'),
serverTimezoneOffset = 60,
transfomDatesIntoUTCStub, rawStub, isPostgres = false, isPostgreSQLWasCalled = false;
transfomDatesIntoUTCStub, rawStub;

beforeEach(function () {
sandbox.stub(db, 'isPostgreSQL', function isPostgreSQL() {
isPostgreSQLWasCalled = true;
return isPostgres;
});

sandbox.stub(Date.prototype, 'getTimezoneOffset', function () {
return serverTimezoneOffset;
});
});

afterEach(function () {
serverTimezoneOffset = 60;
isPostgres = false;
isPostgreSQLWasCalled = false;
});

describe('success', function () {
Expand Down Expand Up @@ -1330,38 +1272,6 @@ describe('Fixtures', function () {
})
.catch(done);
});

it('postgres and server TZ is UTC', function (done) {
serverTimezoneOffset = 0;
configUtils.set('database:client', 'pg');

isPostgres = true;

updateClient({}, loggerStub)
.then(function () {
isPostgreSQLWasCalled.should.eql(true);
models.Settings.edit.callCount.should.eql(1);
models.Settings.findOne.callCount.should.eql(1);
transfomDatesIntoUTCStub.callCount.should.eql(1);
done();
})
.catch(done);
});

it('postgres and server TZ is not UTC', function (done) {
configUtils.set('database:client', 'pg');
isPostgres = true;

updateClient({}, loggerStub)
.then(function () {
isPostgreSQLWasCalled.should.eql(true);
models.Settings.edit.callCount.should.eql(1);
models.Settings.findOne.callCount.should.eql(1);
transfomDatesIntoUTCStub.callCount.should.eql(1);
done();
})
.catch(done);
});
});

describe('error', function () {
Expand Down
2 changes: 0 additions & 2 deletions core/test/utils/index.js
Expand Up @@ -84,7 +84,6 @@ fixtures = {
}));
}).then(function () {
return Promise.all([
// PostgreSQL can return results in any order
db.knex('posts').orderBy('id', 'asc').select('id'),
db.knex('tags').select('id')
]);
Expand Down Expand Up @@ -162,7 +161,6 @@ fixtures = {
max = max || 50;

return Promise.all([
// PostgreSQL can return results in any order
db.knex('posts').orderBy('id', 'asc').select('id'),
db.knex('tags').select('id', 'name')
]).then(function (results) {
Expand Down

0 comments on commit 9848190

Please sign in to comment.