Convert db backend to use Gorm, with basis for support

for Mysql and Postgres in addition to existing Sqlite
This commit is contained in:
Jamie Curnow
2023-05-26 11:04:43 +10:00
parent b4e5b8b6db
commit 29990110b1
93 changed files with 1215 additions and 3075 deletions

View File

@ -3,40 +3,39 @@
CREATE TABLE IF NOT EXISTS `user`
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_on INTEGER NOT NULL DEFAULT 0,
modified_on INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL DEFAULT 0,
updated_at INTEGER NOT NULL DEFAULT 0,
is_deleted INTEGER NOT NULL DEFAULT 0,
name TEXT NOT NULL,
nickname TEXT NOT NULL,
email TEXT NOT NULL,
is_system INTEGER NOT NULL DEFAULT 0,
is_disabled INTEGER NOT NULL DEFAULT 0,
is_deleted INTEGER NOT NULL DEFAULT 0
is_disabled INTEGER NOT NULL DEFAULT 0
);
CREATE TABLE IF NOT EXISTS `capability`
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
name TEXT PRIMARY KEY,
UNIQUE (name)
);
CREATE TABLE IF NOT EXISTS `user_has_capability`
(
user_id INTEGER NOT NULL,
capability_id INTEGER NOT NULL,
UNIQUE (user_id, capability_id),
FOREIGN KEY (capability_id) REFERENCES capability (id)
capability_name TEXT NOT NULL,
UNIQUE (user_id, capability_name),
FOREIGN KEY (capability_name) REFERENCES capability (name)
);
CREATE TABLE IF NOT EXISTS `auth`
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_on INTEGER NOT NULL DEFAULT 0,
modified_on INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL DEFAULT 0,
updated_at INTEGER NOT NULL DEFAULT 0,
is_deleted INTEGER NOT NULL DEFAULT 0,
user_id INTEGER NOT NULL,
type TEXT NOT NULL,
secret TEXT NOT NULL,
is_deleted INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (user_id) REFERENCES user (id),
UNIQUE (user_id, type)
);
@ -44,8 +43,9 @@ CREATE TABLE IF NOT EXISTS `auth`
CREATE TABLE IF NOT EXISTS `setting`
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_on INTEGER NOT NULL DEFAULT 0,
modified_on INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL DEFAULT 0,
updated_at INTEGER NOT NULL DEFAULT 0,
is_deleted INTEGER NOT NULL DEFAULT 0,
name TEXT NOT NULL,
description TEXT NOT NULL DEFAULT "",
value TEXT NOT NULL,
@ -55,8 +55,9 @@ CREATE TABLE IF NOT EXISTS `setting`
CREATE TABLE IF NOT EXISTS `audit_log`
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_on INTEGER NOT NULL DEFAULT 0,
modified_on INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL DEFAULT 0,
updated_at INTEGER NOT NULL DEFAULT 0,
is_deleted INTEGER NOT NULL DEFAULT 0,
user_id INTEGER NOT NULL,
object_type TEXT NOT NULL,
object_id INTEGER NOT NULL,
@ -68,36 +69,37 @@ CREATE TABLE IF NOT EXISTS `audit_log`
CREATE TABLE IF NOT EXISTS `certificate_authority`
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_on INTEGER NOT NULL DEFAULT 0,
modified_on INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL DEFAULT 0,
updated_at INTEGER NOT NULL DEFAULT 0,
is_deleted INTEGER NOT NULL DEFAULT 0,
name TEXT NOT NULL,
acmesh_server TEXT NOT NULL DEFAULT "",
ca_bundle TEXT NOT NULL DEFAULT "",
is_wildcard_supported INTEGER NOT NULL DEFAULT 0, -- specific to each CA, acme v1 doesn't usually have wildcards
max_domains INTEGER NOT NULL DEFAULT 5, -- per request
is_readonly INTEGER NOT NULL DEFAULT 0,
is_deleted INTEGER NOT NULL DEFAULT 0
is_readonly INTEGER NOT NULL DEFAULT 0
);
CREATE TABLE IF NOT EXISTS `dns_provider`
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_on INTEGER NOT NULL DEFAULT 0,
modified_on INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL DEFAULT 0,
updated_at INTEGER NOT NULL DEFAULT 0,
is_deleted INTEGER NOT NULL DEFAULT 0,
user_id INTEGER NOT NULL,
name TEXT NOT NULL,
acmesh_name TEXT NOT NULL,
dns_sleep INTEGER NOT NULL DEFAULT 0,
meta TEXT NOT NULL,
is_deleted INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (user_id) REFERENCES user (id)
);
CREATE TABLE IF NOT EXISTS `certificate`
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_on INTEGER NOT NULL DEFAULT 0,
modified_on INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL DEFAULT 0,
updated_at INTEGER NOT NULL DEFAULT 0,
is_deleted INTEGER NOT NULL DEFAULT 0,
type TEXT NOT NULL, -- custom,dns,http
user_id INTEGER NOT NULL,
certificate_authority_id INTEGER, -- 0 for a custom cert
@ -109,7 +111,6 @@ CREATE TABLE IF NOT EXISTS `certificate`
error_message text NOT NULL DEFAULT "",
meta TEXT NOT NULL,
is_ecc INTEGER NOT NULL DEFAULT 0,
is_deleted INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (user_id) REFERENCES user (id),
FOREIGN KEY (certificate_authority_id) REFERENCES certificate_authority (id),
FOREIGN KEY (dns_provider_id) REFERENCES dns_provider (id)
@ -118,8 +119,9 @@ CREATE TABLE IF NOT EXISTS `certificate`
CREATE TABLE IF NOT EXISTS `stream`
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_on INTEGER NOT NULL DEFAULT 0,
modified_on INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL DEFAULT 0,
updated_at INTEGER NOT NULL DEFAULT 0,
is_deleted INTEGER NOT NULL DEFAULT 0,
user_id INTEGER NOT NULL,
listen_interface TEXT NOT NULL,
incoming_port INTEGER NOT NULL,
@ -127,15 +129,15 @@ CREATE TABLE IF NOT EXISTS `stream`
udp_forwarding INTEGER NOT NULL DEFAULT 0,
advanced_config TEXT NOT NULL,
is_disabled INTEGER NOT NULL DEFAULT 0,
is_deleted INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (user_id) REFERENCES user (id)
);
CREATE TABLE IF NOT EXISTS `upstream`
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_on INTEGER NOT NULL DEFAULT 0,
modified_on INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL DEFAULT 0,
updated_at INTEGER NOT NULL DEFAULT 0,
is_deleted INTEGER NOT NULL DEFAULT 0,
user_id INTEGER NOT NULL,
name TEXT NOT NULL,
nginx_template_id INTEGER NOT NULL,
@ -148,7 +150,6 @@ CREATE TABLE IF NOT EXISTS `upstream`
advanced_config TEXT NOT NULL,
status TEXT NOT NULL DEFAULT "",
error_message TEXT NOT NULL DEFAULT "",
is_deleted INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (user_id) REFERENCES user (id),
FOREIGN KEY (nginx_template_id) REFERENCES nginx_template (id)
);
@ -156,49 +157,50 @@ CREATE TABLE IF NOT EXISTS `upstream`
CREATE TABLE IF NOT EXISTS `upstream_server`
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_on INTEGER NOT NULL DEFAULT 0,
modified_on INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL DEFAULT 0,
updated_at INTEGER NOT NULL DEFAULT 0,
is_deleted INTEGER NOT NULL DEFAULT 0,
upstream_id INTEGER NOT NULL,
server TEXT NOT NULL,
weight INTEGER NOT NULL DEFAULT 0,
max_conns INTEGER NOT NULL DEFAULT 0,
max_fails INTEGER NOT NULL DEFAULT 0,
fail_timeout INTEGER NOT NULL DEFAULT 0,
backup INTEGER NOT NULL DEFAULT 0,
is_deleted INTEGER NOT NULL DEFAULT 0,
is_backup INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (upstream_id) REFERENCES upstream (id)
);
CREATE TABLE IF NOT EXISTS `access_list`
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_on INTEGER NOT NULL DEFAULT 0,
modified_on INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL DEFAULT 0,
updated_at INTEGER NOT NULL DEFAULT 0,
is_deleted INTEGER NOT NULL DEFAULT 0,
user_id INTEGER NOT NULL,
name TEXT NOT NULL,
meta TEXT NOT NULL,
is_deleted INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (user_id) REFERENCES user (id)
);
CREATE TABLE IF NOT EXISTS `nginx_template`
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_on INTEGER NOT NULL DEFAULT 0,
modified_on INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL DEFAULT 0,
updated_at INTEGER NOT NULL DEFAULT 0,
is_deleted INTEGER NOT NULL DEFAULT 0,
user_id INTEGER NOT NULL,
name TEXT NOT NULL,
type TEXT NOT NULL,
template TEXT NOT NULL,
is_deleted INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (user_id) REFERENCES user (id)
);
CREATE TABLE IF NOT EXISTS `host`
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_on INTEGER NOT NULL DEFAULT 0,
modified_on INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL DEFAULT 0,
updated_at INTEGER NOT NULL DEFAULT 0,
is_deleted INTEGER NOT NULL DEFAULT 0,
user_id INTEGER NOT NULL,
type TEXT NOT NULL,
nginx_template_id INTEGER NOT NULL,
@ -222,7 +224,6 @@ CREATE TABLE IF NOT EXISTS `host`
status TEXT NOT NULL DEFAULT "",
error_message TEXT NOT NULL DEFAULT "",
is_disabled INTEGER NOT NULL DEFAULT 0,
is_deleted INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (user_id) REFERENCES user (id),
FOREIGN KEY (nginx_template_id) REFERENCES nginx_template (id),
FOREIGN KEY (upstream_id) REFERENCES upstream (id),

View File

@ -25,14 +25,14 @@ INSERT INTO `capability` (
-- Default error reporting setting
INSERT INTO `setting` (
created_on,
modified_on,
created_at,
updated_at,
name,
description,
value
) VALUES (
strftime('%s', 'now'),
strftime('%s', 'now'),
unixepoch() * 1000,
unixepoch() * 1000,
"error-reporting",
"If enabled, any application errors are reported to Sentry. Sensitive information is not sent.",
"true" -- remember this is json
@ -40,14 +40,14 @@ INSERT INTO `setting` (
-- Default site
INSERT INTO `setting` (
created_on,
modified_on,
created_at,
updated_at,
name,
description,
value
) VALUES (
strftime('%s', 'now'),
strftime('%s', 'now'),
unixepoch() * 1000,
unixepoch() * 1000,
"default-site",
"What to show users who hit your Nginx server by default",
'"welcome"' -- remember this is json
@ -56,56 +56,56 @@ INSERT INTO `setting` (
-- Default Certificate Authorities
INSERT INTO `certificate_authority` (
created_on,
modified_on,
created_at,
updated_at,
name,
acmesh_server,
is_wildcard_supported,
max_domains,
is_readonly
) VALUES (
strftime('%s', 'now'),
strftime('%s', 'now'),
unixepoch() * 1000,
unixepoch() * 1000,
"ZeroSSL",
"zerossl",
1,
10,
1
), (
strftime('%s', 'now'),
strftime('%s', 'now'),
unixepoch() * 1000,
unixepoch() * 1000,
"Let's Encrypt",
"https://acme-v02.api.letsencrypt.org/directory",
1,
10,
1
), (
strftime('%s', 'now'),
strftime('%s', 'now'),
unixepoch() * 1000,
unixepoch() * 1000,
"Buypass Go SSL",
"https://api.buypass.com/acme/directory",
0,
5,
1
), (
strftime('%s', 'now'),
strftime('%s', 'now'),
unixepoch() * 1000,
unixepoch() * 1000,
"SSL.com",
"ssl.com",
0,
10,
1
), (
strftime('%s', 'now'),
strftime('%s', 'now'),
unixepoch() * 1000,
unixepoch() * 1000,
"Let's Encrypt (Testing)",
"https://acme-staging-v02.api.letsencrypt.org/directory",
1,
10,
1
), (
strftime('%s', 'now'),
strftime('%s', 'now'),
unixepoch() * 1000,
unixepoch() * 1000,
"Buypass Go SSL (Testing)",
"https://api.test4.buypass.no/acme/directory",
0,
@ -115,15 +115,15 @@ INSERT INTO `certificate_authority` (
-- System User
INSERT INTO `user` (
created_on,
modified_on,
created_at,
updated_at,
name,
nickname,
email,
is_system
) VALUES (
strftime('%s', 'now'),
strftime('%s', 'now'),
unixepoch() * 1000,
unixepoch() * 1000,
"System",
"System",
"system@localhost",
@ -132,15 +132,15 @@ INSERT INTO `user` (
-- Host Templates
INSERT INTO `nginx_template` (
created_on,
modified_on,
created_at,
updated_at,
user_id,
name,
type,
template
) VALUES (
strftime('%s', 'now'),
strftime('%s', 'now'),
unixepoch() * 1000,
unixepoch() * 1000,
(SELECT id FROM user WHERE is_system = 1 LIMIT 1),
"Default Proxy Template",
"proxy",
@ -262,29 +262,29 @@ server {
}
"
), (
strftime('%s', 'now'),
strftime('%s', 'now'),
unixepoch() * 1000,
unixepoch() * 1000,
(SELECT id FROM user WHERE is_system = 1 LIMIT 1),
"Default Redirect Template",
"redirect",
"# this is a redirect template"
), (
strftime('%s', 'now'),
strftime('%s', 'now'),
unixepoch() * 1000,
unixepoch() * 1000,
(SELECT id FROM user WHERE is_system = 1 LIMIT 1),
"Default Dead Template",
"dead",
"# this is a dead template"
), (
strftime('%s', 'now'),
strftime('%s', 'now'),
unixepoch() * 1000,
unixepoch() * 1000,
(SELECT id FROM user WHERE is_system = 1 LIMIT 1),
"Default Stream Template",
"stream",
"# this is a stream template"
), (
strftime('%s', 'now'),
strftime('%s', 'now'),
unixepoch() * 1000,
unixepoch() * 1000,
(SELECT id FROM user WHERE is_system = 1 LIMIT 1),
"Default Upstream Template",
"upstream",