Skip to content

Commit 3cbbb3f

Browse files
committed
🔥 Switched to node-sqlite dialect of knex
These bindings also rely on a forked version of knex, as well as updates to @tryghost/database-info and knex-migrator
1 parent e23636a commit 3cbbb3f

File tree

9 files changed

+31
-18
lines changed

9 files changed

+31
-18
lines changed

ghost/core/core/server/data/db/connection.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,33 @@ let knexInstance;
1717
function configure(dbConfig) {
1818
const client = dbConfig.client;
1919

20-
if (client === 'sqlite3') {
20+
if (client === 'sqlite3' || client === 'node-sqlite') {
2121
// Backwards compatibility with old knex behaviour
2222
dbConfig.useNullAsDefault = Object.prototype.hasOwnProperty.call(dbConfig, 'useNullAsDefault') ? dbConfig.useNullAsDefault : true;
2323

2424
// Enables foreign key checks and delete on cascade
2525
dbConfig.pool = {
2626
afterCreate(conn, cb) {
27-
conn.run('PRAGMA foreign_keys = ON', cb);
27+
if (client === 'sqlite3') {
28+
conn.run('PRAGMA foreign_keys = ON', cb);
2829

29-
// These two are meant to improve performance at the cost of reliability
30-
// Should be safe for tests. We add them here and leave them on
31-
if (config.get('env').startsWith('testing')) {
32-
conn.run('PRAGMA synchronous = OFF;');
33-
conn.run('PRAGMA journal_mode = TRUNCATE;');
30+
// These two are meant to improve performance at the cost of reliability
31+
// Should be safe for tests. We add them here and leave them on
32+
if (config.get('env').startsWith('testing')) {
33+
conn.run('PRAGMA synchronous = OFF;');
34+
conn.run('PRAGMA journal_mode = TRUNCATE;');
35+
}
36+
} else if (client === 'node-sqlite') {
37+
// node-sqlite uses exec for PRAGMA statements
38+
conn.exec('PRAGMA foreign_keys = ON');
39+
40+
// These two are meant to improve performance at the cost of reliability
41+
// Should be safe for tests. We add them here and leave them on
42+
if (config.get('env').startsWith('testing')) {
43+
conn.exec('PRAGMA synchronous = OFF');
44+
conn.exec('PRAGMA journal_mode = TRUNCATE');
45+
}
46+
cb();
3447
}
3548
}
3649
};

ghost/core/core/server/data/schema/commands.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ function deleteTable(table, transaction = db.knex) {
506506
async function getTables(transaction = db.knex) {
507507
const client = transaction.client.config.client;
508508

509-
if (client === 'sqlite3') {
509+
if (client === 'sqlite3' || client === 'node-sqlite') {
510510
const response = await transaction.raw('select * from sqlite_master where type = "table"');
511511
return _.reject(_.map(response, 'tbl_name'), name => name === 'sqlite_sequence');
512512
} else if (client === 'mysql2') {
@@ -524,7 +524,7 @@ async function getTables(transaction = db.knex) {
524524
async function getIndexes(table, transaction = db.knex) {
525525
const client = transaction.client.config.client;
526526

527-
if (client === 'sqlite3') {
527+
if (client === 'sqlite3' || client === 'node-sqlite') {
528528
const response = await transaction.raw(`pragma index_list("${table}")`);
529529
return _.flatten(_.map(response, 'name'));
530530
} else if (client === 'mysql2') {
@@ -542,7 +542,7 @@ async function getIndexes(table, transaction = db.knex) {
542542
async function getColumns(table, transaction = db.knex) {
543543
const client = transaction.client.config.client;
544544

545-
if (client === 'sqlite3') {
545+
if (client === 'sqlite3' || client === 'node-sqlite') {
546546
const response = await transaction.raw(`pragma table_info("${table}")`);
547547
return _.flatten(_.map(response, 'name'));
548548
} else if (client === 'mysql2') {

ghost/core/core/server/services/url/Resources.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class Resources {
122122
debug('_fetch', resourceConfig.type, resourceConfig.modelOptions);
123123

124124
let modelOptions = _.cloneDeep(resourceConfig.modelOptions);
125-
const isSQLite = config.get('database:client') === 'sqlite3';
125+
const isSQLite = ['sqlite3', 'node-sqlite'].includes(config.get('database:client'));
126126

127127
// CASE: prevent "too many SQL variables" error on SQLite3 (https://github.com/TryGhost/Ghost/issues/5810)
128128
if (isSQLite) {

ghost/core/core/shared/config/env/config.testing-browser.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"url": "http://127.0.0.1:2369",
33
"database": {
4-
"client": "sqlite3",
4+
"client": "node-sqlite",
55
"connection": {
66
"filename": "/tmp/ghost-test.db"
77
},

ghost/core/core/shared/config/env/config.testing.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"url": "http://127.0.0.1:2369",
33
"database": {
4-
"client": "sqlite3",
4+
"client": "node-sqlite",
55
"connection": {
66
"filename": "/tmp/ghost-test.db"
77
},

ghost/core/core/shared/config/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const sanitizeDatabaseProperties = function sanitizeDatabaseProperties(nconf) {
7070

7171
nconf.set('database', database);
7272

73-
if (nconf.get('database:client') === 'sqlite3') {
73+
if (['sqlite3', 'node-sqlite'].includes(nconf.get('database:client'))) {
7474
makePathsAbsolute(nconf, nconf.get('database:connection'), 'database:connection');
7575
}
7676
};

ghost/core/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
"license": "MIT",
2222
"scripts": {
2323
"archive": "npm pack",
24-
"dev": "node --watch --import=tsx index.js",
24+
"dev": "node --experimental-sqlite --watch --import=tsx index.js",
2525
"build:assets": "yarn build:assets:css && yarn build:assets:js",
2626
"build:assets:js": "node bin/minify-assets.js",
2727
"build:assets:css": "postcss core/frontend/public/ghost.css --no-map --use cssnano -o core/frontend/public/ghost.min.css",
2828
"build:tsc": "tsc",
2929
"pretest": "yarn build:assets",
3030
"test": "yarn test:unit",
31-
"test:base": "mocha --reporter dot --require tsx --require=./test/utils/overrides.js --exit --trace-warnings --recursive --extension=test.js,test.ts",
31+
"test:base": "mocha --experimental-sqlite --reporter dot --require tsx --require=./test/utils/overrides.js --exit --trace-warnings --recursive --extension=test.js,test.ts",
3232
"test:single": "yarn test:base --timeout=60000",
3333
"test:all": "yarn test:unit && yarn test:integration && yarn test:e2e && yarn lint",
3434
"test:debug": "DEBUG=ghost:test* yarn test",

ghost/core/test/e2e-api/admin/__snapshots__/config.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`Config API As Owner Can retrieve config and all expected properties 1:
44
Object {
55
"config": Object {
66
"clientExtensions": Object {},
7-
"database": StringMatching /sqlite3\\|mysql\\|mysql2/,
7+
"database": StringMatching /sqlite3\\|node-sqlite\\|mysql\\|mysql2/,
88
"emailAnalytics": true,
99
"enableDeveloperExperiments": false,
1010
"environment": StringMatching /\\^testing/,

ghost/core/test/e2e-api/admin/config.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('Config API', function () {
3737
.expectStatus(200)
3838
.matchBodySnapshot({
3939
config: {
40-
database: stringMatching(/sqlite3|mysql|mysql2/),
40+
database: stringMatching(/sqlite3|node-sqlite|mysql|mysql2/),
4141
environment: stringMatching(/^testing/),
4242
version: stringMatching(/\d+\.\d+\.\d+/)
4343
}

0 commit comments

Comments
 (0)