Files
cloudron-box/migrations/schema.sql
T

212 lines
7.9 KiB
SQL
Raw Normal View History

#### WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
#### This file is not used by any code and is here to document the latest schema
#### General ideas
#### Default char set is utf8 and DEFAULT COLLATE is utf8_bin. Collate affects comparisons in WHERE and ORDER
#### Strict mode is enabled
#### VARCHAR - stored as part of table row (use for strings)
#### TEXT - stored offline from table row (use for strings)
#### BLOB - stored offline from table row (use for binary data)
#### https://dev.mysql.com/doc/refman/5.0/en/storage-requirements.html
2017-11-19 12:36:05 -08:00
# The code uses zero dates. Make sure sql_mode does NOT have NO_ZERO_DATE
# http://johnemb.blogspot.com/2014/09/adding-or-removing-individual-sql-modes.html
# SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'NO_ZERO_DATE',''));
CREATE TABLE IF NOT EXISTS users(
id VARCHAR(128) NOT NULL UNIQUE,
2016-04-05 10:54:09 +02:00
username VARCHAR(254) UNIQUE,
email VARCHAR(254) NOT NULL UNIQUE,
password VARCHAR(1024) NOT NULL,
salt VARCHAR(512) NOT NULL,
createdAt VARCHAR(512) NOT NULL,
modifiedAt VARCHAR(512) NOT NULL,
2018-04-25 16:33:18 +02:00
displayName VARCHAR(512) DEFAULT "",
fallbackEmail VARCHAR(512) DEFAULT "",
twoFactorAuthenticationSecret VARCHAR(128) DEFAULT "",
twoFactorAuthenticationEnabled BOOLEAN DEFAULT false,
admin BOOLEAN DEFAULT false,
2018-01-21 14:25:39 +01:00
PRIMARY KEY(id));
2018-12-01 00:38:00 +01:00
CREATE TABLE IF NOT EXISTS userGroups(
2016-02-07 19:24:07 -08:00
id VARCHAR(128) NOT NULL UNIQUE,
2016-09-27 21:11:41 +02:00
name VARCHAR(254) NOT NULL UNIQUE,
2016-02-07 19:24:07 -08:00
PRIMARY KEY(id));
2016-02-08 08:55:37 -08:00
CREATE TABLE IF NOT EXISTS groupMembers(
groupId VARCHAR(128) NOT NULL,
userId VARCHAR(128) NOT NULL,
2018-12-01 00:38:00 +01:00
FOREIGN KEY(groupId) REFERENCES userGroups(id),
2016-02-08 08:55:37 -08:00
FOREIGN KEY(userId) REFERENCES users(id));
CREATE TABLE IF NOT EXISTS tokens(
2018-08-27 14:50:41 -07:00
name VARCHAR(64) DEFAULT "", // description
accessToken VARCHAR(128) NOT NULL UNIQUE,
identifier VARCHAR(128) NOT NULL,
clientId VARCHAR(128),
scope VARCHAR(512) NOT NULL,
expires BIGINT NOT NULL, // FIXME: make this a timestamp
PRIMARY KEY(accessToken));
CREATE TABLE IF NOT EXISTS clients(
2015-10-15 16:31:45 -07:00
id VARCHAR(128) NOT NULL UNIQUE, // prefixed with cid- to identify token easily in auth routes
2018-08-27 14:30:39 -07:00
appId VARCHAR(128) NOT NULL, // name of the client (for external apps) or id of app (for built-in apps)
2015-10-15 16:31:45 -07:00
type VARCHAR(16) NOT NULL,
clientSecret VARCHAR(512) NOT NULL,
redirectURI VARCHAR(512) NOT NULL,
scope VARCHAR(512) NOT NULL,
PRIMARY KEY(id));
CREATE TABLE IF NOT EXISTS apps(
id VARCHAR(128) NOT NULL UNIQUE,
appStoreId VARCHAR(128) NOT NULL,
installationState VARCHAR(512) NOT NULL,
2016-06-20 22:30:17 -05:00
installationProgress TEXT,
runState VARCHAR(512),
health VARCHAR(128),
containerId VARCHAR(128),
manifestJson TEXT,
httpPort INTEGER, // this is the nginx proxy port and not manifest.httpPort
2017-10-30 00:16:33 +01:00
location VARCHAR(128) NOT NULL,
domain VARCHAR(128) NOT NULL,
2016-05-27 11:10:36 -07:00
accessRestrictionJson TEXT, // { users: [ ], groups: [ ] }
2018-06-26 18:09:04 -07:00
creationTime TIMESTAMP(2) NOT NULL DEFAULT CURRENT_TIMESTAMP, // when the app was installed
updateTime TIMESTAMP(2) NOT NULL DEFAULT CURRENT_TIMESTAMP, // when the last app update was done
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, // when this db record was updated (useful for UI caching)
2016-02-05 15:03:45 +01:00
memoryLimit BIGINT DEFAULT 0,
2016-07-14 15:04:52 +02:00
xFrameOptions VARCHAR(512),
2016-11-11 10:48:33 +05:30
sso BOOLEAN DEFAULT 1, // whether user chose to enable SSO
debugModeJson TEXT, // options for development mode
2017-07-18 12:03:45 -07:00
robotsTxt TEXT,
2017-11-21 18:09:44 -08:00
enableBackup BOOLEAN DEFAULT 1, // misnomer: controls automatic daily backups
2017-04-23 22:00:17 -07:00
// the following fields do not belong here, they can be removed when we use a queue for apptask
2017-11-16 14:47:05 -08:00
restoreConfigJson VARCHAR(256), // used to pass backupId to restore from to apptask
oldConfigJson TEXT, // used to pass old config to apptask (configure, restore)
updateConfigJson TEXT, // used to pass new config to apptask (update)
2015-10-09 11:08:16 -07:00
2018-05-13 21:02:57 -07:00
ownerId VARCHAR(128),
FOREIGN KEY(ownerId) REFERENCES users(id),
PRIMARY KEY(id));
CREATE TABLE IF NOT EXISTS appPortBindings(
hostPort INTEGER NOT NULL UNIQUE,
2018-08-12 22:08:19 -07:00
type VARCHAR(8) NOT NULL DEFAULT "tcp",
environmentVariable VARCHAR(128) NOT NULL,
appId VARCHAR(128) NOT NULL,
FOREIGN KEY(appId) REFERENCES apps(id),
PRIMARY KEY(hostPort));
CREATE TABLE IF NOT EXISTS authcodes(
authCode VARCHAR(128) NOT NULL UNIQUE,
userId VARCHAR(128) NOT NULL,
clientId VARCHAR(128) NOT NULL,
expiresAt BIGINT NOT NULL, // ## FIXME: make this a timestamp
PRIMARY KEY(authCode));
CREATE TABLE IF NOT EXISTS settings(
name VARCHAR(128) NOT NULL UNIQUE,
2017-09-11 15:35:55 +02:00
value TEXT,
PRIMARY KEY(name));
CREATE TABLE IF NOT EXISTS appAddonConfigs(
appId VARCHAR(128) NOT NULL,
addonId VARCHAR(32) NOT NULL,
2017-03-25 13:35:28 -07:00
name VARCHAR(128) NOT NULL,
value VARCHAR(512) NOT NULL,
FOREIGN KEY(appId) REFERENCES apps(id));
2018-10-11 14:07:43 -07:00
CREATE TABLE IF NOT EXISTS appEnvVars(
appId VARCHAR(128) NOT NULL,
name TEXT NOT NULL,
value TEXT NOT NULL,
FOREIGN KEY(appId) REFERENCES apps(id));
2016-03-07 09:26:26 -08:00
CREATE TABLE IF NOT EXISTS backups(
2017-05-26 22:23:24 -07:00
id VARCHAR(128) NOT NULL,
2016-03-07 09:26:26 -08:00
creationTime TIMESTAMP,
version VARCHAR(128) NOT NULL, /* app version or box version */
type VARCHAR(16) NOT NULL, /* 'box' or 'app' */
dependsOn TEXT, /* comma separate list of objects this backup depends on */
2016-03-07 09:26:26 -08:00
state VARCHAR(16) NOT NULL,
manifestJson TEXT, /* to validate if the app can be installed in this version of box */
format VARCHAR(16) DEFAULT "tgz",
2016-03-07 09:26:26 -08:00
2017-05-26 22:23:24 -07:00
PRIMARY KEY (id));
2016-04-29 23:28:54 -07:00
CREATE TABLE IF NOT EXISTS eventlog(
id VARCHAR(128) NOT NULL,
action VARCHAR(128) NOT NULL,
source TEXT, /* { userId, username, ip }. userId can be null for cron,sysadmin */
data TEXT, /* free flowing json based on action */
2018-03-22 18:40:57 -07:00
createdAt TIMESTAMP(2) NOT NULL,
2016-04-29 23:28:54 -07:00
PRIMARY KEY (id));
2016-05-26 18:02:22 -07:00
2017-10-27 23:39:53 +02:00
CREATE TABLE IF NOT EXISTS domains(
2017-10-30 00:16:33 +01:00
domain VARCHAR(128) NOT NULL UNIQUE, /* if this needs to be larger, InnoDB has a limit of 767 bytes for PRIMARY KEY values! */
2017-10-27 23:39:53 +02:00
zoneName VARCHAR(128) NOT NULL, /* this mostly contains the domain itself again */
2018-01-09 14:46:38 -08:00
provider VARCHAR(16) NOT NULL,
2017-10-27 23:39:53 +02:00
configJson TEXT, /* JSON containing the dns backend provider config */
tlsConfigJson TEXT, /* JSON containing the tls provider config */
2017-10-27 23:39:53 +02:00
2017-10-30 00:16:33 +01:00
PRIMARY KEY (domain))
/* the default db collation is utf8mb4_unicode_ci but for the app table domain constraint we have to use the old one */
CHARACTER SET utf8 COLLATE utf8_bin;
2018-01-20 22:17:53 -08:00
2018-01-23 15:45:30 +01:00
CREATE TABLE IF NOT EXISTS mail(
2018-01-20 22:17:53 -08:00
domain VARCHAR(128) NOT NULL UNIQUE,
enabled BOOLEAN DEFAULT 0, /* MDA enabled */
mailFromValidation BOOLEAN DEFAULT 1,
catchAllJson TEXT,
relayJson TEXT,
FOREIGN KEY(domain) REFERENCES domains(domain),
PRIMARY KEY(domain))
CHARACTER SET utf8 COLLATE utf8_bin;
/* Future fields:
* accessRestriction - to determine who can access it. So this has foreign keys
* quota - per mailbox quota
*/
CREATE TABLE IF NOT EXISTS mailboxes(
name VARCHAR(128) NOT NULL,
2018-04-07 19:12:07 -07:00
type VARCHAR(16) NOT NULL, /* 'mailbox', 'alias', 'list' */
ownerId VARCHAR(128) NOT NULL, /* app id or user id or group id */
ownerType VARCHAR(16) NOT NULL, /* 'app' or 'user' or 'group' */
aliasTarget VARCHAR(128), /* the target name type is an alias */
2018-04-05 15:46:13 -07:00
membersJson TEXT, /* members of a group */
creationTime TIMESTAMP,
domain VARCHAR(128),
FOREIGN KEY(domain) REFERENCES mail(domain),
UNIQUE (name, domain));
2018-01-20 22:17:53 -08:00
2018-06-28 19:39:15 +02:00
CREATE TABLE IF NOT EXISTS subdomains(
appId VARCHAR(128) NOT NULL,
domain VARCHAR(128) NOT NULL,
subdomain VARCHAR(128) NOT NULL,
type VARCHAR(128) NOT NULL,
2018-06-29 11:29:30 +02:00
2018-06-28 19:39:15 +02:00
FOREIGN KEY(domain) REFERENCES domains(domain),
FOREIGN KEY(appId) REFERENCES apps(id),
2018-11-16 11:13:03 -08:00
UNIQUE (subdomain, domain));
CREATE TABLE IF NOT EXISTS tasks(
id VARCHAR(32) NOT NULL UNIQUE,
percent INTEGER DEFAULT 0,
message TEXT,
2018-11-29 14:28:56 -08:00
errorMessage TEXT,
2018-11-16 11:13:03 -08:00
result TEXT,
creationTime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id));
2018-06-29 11:04:14 +02:00
2018-07-26 10:20:19 -07:00
CHARACTER SET utf8 COLLATE utf8_bin;
2018-11-16 11:13:03 -08:00