Skip to content

Multidb - sqlite support #510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions backend/config/sqlite-test-db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"database": {
"engine": "knex-native",
"knex": {
"client": "sqlite3",
"connection": {
"filename": "/app/backend/config/mydb.sqlite"
},
"pool": {
"min": 0,
"max": 1,
"createTimeoutMillis": 3000,
"acquireTimeoutMillis": 30000,
"idleTimeoutMillis": 30000,
"reapIntervalMillis": 1000,
"createRetryIntervalMillis": 100,
"propagateCreateError": false
},
"migrations": {
"tableName": "migrations",
"stub": "src/backend/lib/migrate_template.js",
"directory": "src/backend/migrations"
}
}
}
}
34 changes: 21 additions & 13 deletions backend/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,27 @@ if (!config.has('database')) {
throw new Error('Database config does not exist! Please read the instructions: https://github.com/jc21/nginx-proxy-manager/blob/master/doc/INSTALL.md');
}

let data = {
client: config.database.engine,
connection: {
host: config.database.host,
user: config.database.user,
password: config.database.password,
database: config.database.name,
port: config.database.port
},
migrations: {
tableName: 'migrations'
}
};
function generateDbConfig() {
if (config.database.engine === 'knex-native') {
return config.database.knex;
} else
return {
client: config.database.engine,
connection: {
host: config.database.host,
user: config.database.user,
password: config.database.password,
database: config.database.name,
port: config.database.port
},
migrations: {
tableName: 'migrations'
}
};
}


let data = generateDbConfig();

if (typeof config.database.version !== 'undefined') {
data.version = config.database.version;
Expand Down
16 changes: 0 additions & 16 deletions backend/migrations/20190227065017_settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,6 @@ exports.up = function (knex/*, Promise*/) {
})
.then(() => {
logger.info('[' + migrate_name + '] setting Table created');

// TODO: add settings
let settingModel = require('../models/setting');

return settingModel
.query()
.insert({
id: 'default-site',
name: 'Default Site',
description: 'What to show when Nginx is hit with an unknown Host',
value: 'congratulations',
meta: {}
});
})
.then(() => {
logger.info('[' + migrate_name + '] Default settings added');
});
};

Expand Down
7 changes: 4 additions & 3 deletions backend/models/access_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ const Model = require('objection').Model;
const User = require('./user');
const AccessListAuth = require('./access_list_auth');
const AccessListClient = require('./access_list_client');
const now = require('./now_helper');

Model.knex(db);

class AccessList extends Model {
$beforeInsert () {
this.created_on = Model.raw('NOW()');
this.modified_on = Model.raw('NOW()');
this.created_on = now();
this.modified_on = now();

// Default for meta
if (typeof this.meta === 'undefined') {
Expand All @@ -21,7 +22,7 @@ class AccessList extends Model {
}

$beforeUpdate () {
this.modified_on = Model.raw('NOW()');
this.modified_on = now();
}

static get name () {
Expand Down
7 changes: 4 additions & 3 deletions backend/models/access_list_auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

const db = require('../db');
const Model = require('objection').Model;
const now = require('./now_helper');

Model.knex(db);

class AccessListAuth extends Model {
$beforeInsert () {
this.created_on = Model.raw('NOW()');
this.modified_on = Model.raw('NOW()');
this.created_on = now();
this.modified_on = now();

// Default for meta
if (typeof this.meta === 'undefined') {
Expand All @@ -18,7 +19,7 @@ class AccessListAuth extends Model {
}

$beforeUpdate () {
this.modified_on = Model.raw('NOW()');
this.modified_on = now();
}

static get name () {
Expand Down
7 changes: 4 additions & 3 deletions backend/models/access_list_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

const db = require('../db');
const Model = require('objection').Model;
const now = require('./now_helper');

Model.knex(db);

class AccessListClient extends Model {
$beforeInsert () {
this.created_on = Model.raw('NOW()');
this.modified_on = Model.raw('NOW()');
this.created_on = now();
this.modified_on = now();

// Default for meta
if (typeof this.meta === 'undefined') {
Expand All @@ -18,7 +19,7 @@ class AccessListClient extends Model {
}

$beforeUpdate () {
this.modified_on = Model.raw('NOW()');
this.modified_on = now();
}

static get name () {
Expand Down
7 changes: 4 additions & 3 deletions backend/models/audit-log.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
const db = require('../db');
const Model = require('objection').Model;
const User = require('./user');
const now = require('./now_helper');

Model.knex(db);

class AuditLog extends Model {
$beforeInsert () {
this.created_on = Model.raw('NOW()');
this.modified_on = Model.raw('NOW()');
this.created_on = now();
this.modified_on = now();

// Default for meta
if (typeof this.meta === 'undefined') {
Expand All @@ -19,7 +20,7 @@ class AuditLog extends Model {
}

$beforeUpdate () {
this.modified_on = Model.raw('NOW()');
this.modified_on = now();
}

static get name () {
Expand Down
7 changes: 4 additions & 3 deletions backend/models/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const bcrypt = require('bcrypt');
const db = require('../db');
const Model = require('objection').Model;
const User = require('./user');
const now = require('./now_helper');

Model.knex(db);

Expand All @@ -24,8 +25,8 @@ function encryptPassword () {

class Auth extends Model {
$beforeInsert (queryContext) {
this.created_on = Model.raw('NOW()');
this.modified_on = Model.raw('NOW()');
this.created_on = now();
this.modified_on = now();

// Default for meta
if (typeof this.meta === 'undefined') {
Expand All @@ -36,7 +37,7 @@ class Auth extends Model {
}

$beforeUpdate (queryContext) {
this.modified_on = Model.raw('NOW()');
this.modified_on = now();
return encryptPassword.apply(this, queryContext);
}

Expand Down
9 changes: 5 additions & 4 deletions backend/models/certificate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
const db = require('../db');
const Model = require('objection').Model;
const User = require('./user');
const now = require('./now_helper');

Model.knex(db);

class Certificate extends Model {
$beforeInsert () {
this.created_on = Model.raw('NOW()');
this.modified_on = Model.raw('NOW()');
this.created_on = now();
this.modified_on = now();

// Default for expires_on
if (typeof this.expires_on === 'undefined') {
this.expires_on = Model.raw('NOW()');
this.expires_on = now();
}

// Default for domain_names
Expand All @@ -31,7 +32,7 @@ class Certificate extends Model {
}

$beforeUpdate () {
this.modified_on = Model.raw('NOW()');
this.modified_on = now();

// Sort domain_names
if (typeof this.domain_names !== 'undefined') {
Expand Down
7 changes: 4 additions & 3 deletions backend/models/dead_host.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ const db = require('../db');
const Model = require('objection').Model;
const User = require('./user');
const Certificate = require('./certificate');
const now = require('./now_helper');

Model.knex(db);

class DeadHost extends Model {
$beforeInsert () {
this.created_on = Model.raw('NOW()');
this.modified_on = Model.raw('NOW()');
this.created_on = now();
this.modified_on = now();

// Default for domain_names
if (typeof this.domain_names === 'undefined') {
Expand All @@ -27,7 +28,7 @@ class DeadHost extends Model {
}

$beforeUpdate () {
this.modified_on = Model.raw('NOW()');
this.modified_on = now();

// Sort domain_names
if (typeof this.domain_names !== 'undefined') {
Expand Down
13 changes: 13 additions & 0 deletions backend/models/now_helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const db = require('../db');
const config = require('config');
const Model = require('objection').Model;

Model.knex(db);

module.exports = function () {
if (config.database.knex && config.database.knex.client === 'sqlite3') {
return Model.raw('date(\'now\')');
} else {
return Model.raw('NOW()');
}
};
7 changes: 4 additions & 3 deletions backend/models/proxy_host.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ const Model = require('objection').Model;
const User = require('./user');
const AccessList = require('./access_list');
const Certificate = require('./certificate');
const now = require('./now_helper');

Model.knex(db);

class ProxyHost extends Model {
$beforeInsert () {
this.created_on = Model.raw('NOW()');
this.modified_on = Model.raw('NOW()');
this.created_on = now();
this.modified_on = now();

// Default for domain_names
if (typeof this.domain_names === 'undefined') {
Expand All @@ -28,7 +29,7 @@ class ProxyHost extends Model {
}

$beforeUpdate () {
this.modified_on = Model.raw('NOW()');
this.modified_on = now();

// Sort domain_names
if (typeof this.domain_names !== 'undefined') {
Expand Down
7 changes: 4 additions & 3 deletions backend/models/redirection_host.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ const db = require('../db');
const Model = require('objection').Model;
const User = require('./user');
const Certificate = require('./certificate');
const now = require('./now_helper');

Model.knex(db);

class RedirectionHost extends Model {
$beforeInsert () {
this.created_on = Model.raw('NOW()');
this.modified_on = Model.raw('NOW()');
this.created_on = now();
this.modified_on = now();

// Default for domain_names
if (typeof this.domain_names === 'undefined') {
Expand All @@ -27,7 +28,7 @@ class RedirectionHost extends Model {
}

$beforeUpdate () {
this.modified_on = Model.raw('NOW()');
this.modified_on = now();

// Sort domain_names
if (typeof this.domain_names !== 'undefined') {
Expand Down
7 changes: 4 additions & 3 deletions backend/models/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
const db = require('../db');
const Model = require('objection').Model;
const User = require('./user');
const now = require('./now_helper');

Model.knex(db);

class Stream extends Model {
$beforeInsert () {
this.created_on = Model.raw('NOW()');
this.modified_on = Model.raw('NOW()');
this.created_on = now();
this.modified_on = now();

// Default for meta
if (typeof this.meta === 'undefined') {
Expand All @@ -19,7 +20,7 @@ class Stream extends Model {
}

$beforeUpdate () {
this.modified_on = Model.raw('NOW()');
this.modified_on = now();
}

static get name () {
Expand Down
7 changes: 4 additions & 3 deletions backend/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
const db = require('../db');
const Model = require('objection').Model;
const UserPermission = require('./user_permission');
const now = require('./now_helper');

Model.knex(db);

class User extends Model {
$beforeInsert () {
this.created_on = Model.raw('NOW()');
this.modified_on = Model.raw('NOW()');
this.created_on = now();
this.modified_on = now();

// Default for roles
if (typeof this.roles === 'undefined') {
Expand All @@ -19,7 +20,7 @@ class User extends Model {
}

$beforeUpdate () {
this.modified_on = Model.raw('NOW()');
this.modified_on = now();
}

static get name () {
Expand Down
Loading