pgsql: update the query format

This commit is contained in:
Sida Chen 2018-09-12 14:09:17 -04:00
parent d0a3fe9206
commit 53433090a3

View File

@ -22,170 +22,170 @@ func init() {
Up: migrate.Queries([]string{ Up: migrate.Queries([]string{
// namespaces // namespaces
`CREATE TABLE IF NOT EXISTS namespace ( `CREATE TABLE IF NOT EXISTS namespace (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
name TEXT NULL, name TEXT NULL,
version_format TEXT, version_format TEXT,
UNIQUE (name, version_format));`, UNIQUE (name, version_format));`,
`CREATE INDEX ON namespace(name);`, `CREATE INDEX ON namespace(name);`,
// features // features
`CREATE TABLE IF NOT EXISTS feature ( `CREATE TABLE IF NOT EXISTS feature (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
name TEXT NOT NULL, name TEXT NOT NULL,
version TEXT NOT NULL, version TEXT NOT NULL,
version_format TEXT NOT NULL, version_format TEXT NOT NULL,
UNIQUE (name, version, version_format));`, UNIQUE (name, version, version_format));`,
`CREATE INDEX ON feature(name);`, `CREATE INDEX ON feature(name);`,
`CREATE TABLE IF NOT EXISTS namespaced_feature ( `CREATE TABLE IF NOT EXISTS namespaced_feature (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
namespace_id INT REFERENCES namespace, namespace_id INT REFERENCES namespace,
feature_id INT REFERENCES feature, feature_id INT REFERENCES feature,
UNIQUE (namespace_id, feature_id));`, UNIQUE (namespace_id, feature_id));`,
// layers // layers
`CREATE TABLE IF NOT EXISTS layer( `CREATE TABLE IF NOT EXISTS layer(
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
hash TEXT NOT NULL UNIQUE);`, hash TEXT NOT NULL UNIQUE);`,
`CREATE TABLE IF NOT EXISTS layer_feature ( `CREATE TABLE IF NOT EXISTS layer_feature (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
layer_id INT REFERENCES layer ON DELETE CASCADE, layer_id INT REFERENCES layer ON DELETE CASCADE,
feature_id INT REFERENCES feature ON DELETE CASCADE, feature_id INT REFERENCES feature ON DELETE CASCADE,
UNIQUE (layer_id, feature_id));`, UNIQUE (layer_id, feature_id));`,
`CREATE INDEX ON layer_feature(layer_id);`, `CREATE INDEX ON layer_feature(layer_id);`,
`CREATE TABLE IF NOT EXISTS layer_lister ( `CREATE TABLE IF NOT EXISTS layer_lister (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
layer_id INT REFERENCES layer ON DELETE CASCADE, layer_id INT REFERENCES layer ON DELETE CASCADE,
lister TEXT NOT NULL, lister TEXT NOT NULL,
UNIQUE (layer_id, lister));`, UNIQUE (layer_id, lister));`,
`CREATE INDEX ON layer_lister(layer_id);`, `CREATE INDEX ON layer_lister(layer_id);`,
`CREATE TABLE IF NOT EXISTS layer_detector ( `CREATE TABLE IF NOT EXISTS layer_detector (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
layer_id INT REFERENCES layer ON DELETE CASCADE, layer_id INT REFERENCES layer ON DELETE CASCADE,
detector TEXT, detector TEXT,
UNIQUE (layer_id, detector));`, UNIQUE (layer_id, detector));`,
`CREATE INDEX ON layer_detector(layer_id);`, `CREATE INDEX ON layer_detector(layer_id);`,
`CREATE TABLE IF NOT EXISTS layer_namespace ( `CREATE TABLE IF NOT EXISTS layer_namespace (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
layer_id INT REFERENCES layer ON DELETE CASCADE, layer_id INT REFERENCES layer ON DELETE CASCADE,
namespace_id INT REFERENCES namespace ON DELETE CASCADE, namespace_id INT REFERENCES namespace ON DELETE CASCADE,
UNIQUE (layer_id, namespace_id));`, UNIQUE (layer_id, namespace_id));`,
`CREATE INDEX ON layer_namespace(layer_id);`, `CREATE INDEX ON layer_namespace(layer_id);`,
// ancestry // ancestry
`CREATE TABLE IF NOT EXISTS ancestry ( `CREATE TABLE IF NOT EXISTS ancestry (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
name TEXT NOT NULL UNIQUE);`, name TEXT NOT NULL UNIQUE);`,
`CREATE TABLE IF NOT EXISTS ancestry_layer ( `CREATE TABLE IF NOT EXISTS ancestry_layer (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
ancestry_id INT REFERENCES ancestry ON DELETE CASCADE, ancestry_id INT REFERENCES ancestry ON DELETE CASCADE,
ancestry_index INT NOT NULL, ancestry_index INT NOT NULL,
layer_id INT REFERENCES layer ON DELETE RESTRICT, layer_id INT REFERENCES layer ON DELETE RESTRICT,
UNIQUE (ancestry_id, ancestry_index));`, UNIQUE (ancestry_id, ancestry_index));`,
`CREATE INDEX ON ancestry_layer(ancestry_id);`, `CREATE INDEX ON ancestry_layer(ancestry_id);`,
`CREATE TABLE IF NOT EXISTS ancestry_feature( `CREATE TABLE IF NOT EXISTS ancestry_feature(
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
ancestry_layer_id INT REFERENCES ancestry_layer ON DELETE CASCADE, ancestry_layer_id INT REFERENCES ancestry_layer ON DELETE CASCADE,
namespaced_feature_id INT REFERENCES namespaced_feature ON DELETE CASCADE, namespaced_feature_id INT REFERENCES namespaced_feature ON DELETE CASCADE,
UNIQUE (ancestry_layer_id, namespaced_feature_id));`, UNIQUE (ancestry_layer_id, namespaced_feature_id));`,
`CREATE TABLE IF NOT EXISTS ancestry_lister ( `CREATE TABLE IF NOT EXISTS ancestry_lister (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
ancestry_id INT REFERENCES ancestry ON DELETE CASCADE, ancestry_id INT REFERENCES ancestry ON DELETE CASCADE,
lister TEXT, lister TEXT,
UNIQUE (ancestry_id, lister));`, UNIQUE (ancestry_id, lister));`,
`CREATE INDEX ON ancestry_lister(ancestry_id);`, `CREATE INDEX ON ancestry_lister(ancestry_id);`,
`CREATE TABLE IF NOT EXISTS ancestry_detector ( `CREATE TABLE IF NOT EXISTS ancestry_detector (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
ancestry_id INT REFERENCES ancestry ON DELETE CASCADE, ancestry_id INT REFERENCES ancestry ON DELETE CASCADE,
detector TEXT, detector TEXT,
UNIQUE (ancestry_id, detector));`, UNIQUE (ancestry_id, detector));`,
`CREATE INDEX ON ancestry_detector(ancestry_id);`, `CREATE INDEX ON ancestry_detector(ancestry_id);`,
`CREATE TYPE severity AS ENUM ('Unknown', 'Negligible', 'Low', 'Medium', 'High', 'Critical', 'Defcon1');`, `CREATE TYPE severity AS ENUM ('Unknown', 'Negligible', 'Low', 'Medium', 'High', 'Critical', 'Defcon1');`,
// vulnerability // vulnerability
`CREATE TABLE IF NOT EXISTS vulnerability ( `CREATE TABLE IF NOT EXISTS vulnerability (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
namespace_id INT NOT NULL REFERENCES Namespace, namespace_id INT NOT NULL REFERENCES Namespace,
name TEXT NOT NULL, name TEXT NOT NULL,
description TEXT NULL, description TEXT NULL,
link TEXT NULL, link TEXT NULL,
severity severity NOT NULL, severity severity NOT NULL,
metadata TEXT NULL, metadata TEXT NULL,
created_at TIMESTAMP WITH TIME ZONE, created_at TIMESTAMP WITH TIME ZONE,
deleted_at TIMESTAMP WITH TIME ZONE NULL);`, deleted_at TIMESTAMP WITH TIME ZONE NULL);`,
`CREATE INDEX ON vulnerability(namespace_id, name);`, `CREATE INDEX ON vulnerability(namespace_id, name);`,
`CREATE INDEX ON vulnerability(namespace_id);`, `CREATE INDEX ON vulnerability(namespace_id);`,
`CREATE TABLE IF NOT EXISTS vulnerability_affected_feature ( `CREATE TABLE IF NOT EXISTS vulnerability_affected_feature (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
vulnerability_id INT NOT NULL REFERENCES vulnerability ON DELETE CASCADE, vulnerability_id INT NOT NULL REFERENCES vulnerability ON DELETE CASCADE,
feature_name TEXT NOT NULL, feature_name TEXT NOT NULL,
affected_version TEXT, affected_version TEXT,
fixedin TEXT);`, fixedin TEXT);`,
`CREATE INDEX ON vulnerability_affected_feature(vulnerability_id, feature_name);`, `CREATE INDEX ON vulnerability_affected_feature(vulnerability_id, feature_name);`,
`CREATE TABLE IF NOT EXISTS vulnerability_affected_namespaced_feature( `CREATE TABLE IF NOT EXISTS vulnerability_affected_namespaced_feature(
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
vulnerability_id INT NOT NULL REFERENCES vulnerability ON DELETE CASCADE, vulnerability_id INT NOT NULL REFERENCES vulnerability ON DELETE CASCADE,
namespaced_feature_id INT NOT NULL REFERENCES namespaced_feature ON DELETE CASCADE, namespaced_feature_id INT NOT NULL REFERENCES namespaced_feature ON DELETE CASCADE,
added_by INT NOT NULL REFERENCES vulnerability_affected_feature ON DELETE CASCADE, added_by INT NOT NULL REFERENCES vulnerability_affected_feature ON DELETE CASCADE,
UNIQUE (vulnerability_id, namespaced_feature_id));`, UNIQUE (vulnerability_id, namespaced_feature_id));`,
`CREATE INDEX ON vulnerability_affected_namespaced_feature(namespaced_feature_id);`, `CREATE INDEX ON vulnerability_affected_namespaced_feature(namespaced_feature_id);`,
`CREATE TABLE IF NOT EXISTS KeyValue ( `CREATE TABLE IF NOT EXISTS KeyValue (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
key TEXT NOT NULL UNIQUE, key TEXT NOT NULL UNIQUE,
value TEXT);`, value TEXT);`,
`CREATE TABLE IF NOT EXISTS Lock ( `CREATE TABLE IF NOT EXISTS Lock (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
name VARCHAR(64) NOT NULL UNIQUE, name VARCHAR(64) NOT NULL UNIQUE,
owner VARCHAR(64) NOT NULL, owner VARCHAR(64) NOT NULL,
until TIMESTAMP WITH TIME ZONE);`, until TIMESTAMP WITH TIME ZONE);`,
`CREATE INDEX ON Lock (owner);`, `CREATE INDEX ON Lock (owner);`,
// Notification // Notification
`CREATE TABLE IF NOT EXISTS Vulnerability_Notification ( `CREATE TABLE IF NOT EXISTS Vulnerability_Notification (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
name VARCHAR(64) NOT NULL UNIQUE, name VARCHAR(64) NOT NULL UNIQUE,
created_at TIMESTAMP WITH TIME ZONE, created_at TIMESTAMP WITH TIME ZONE,
notified_at TIMESTAMP WITH TIME ZONE NULL, notified_at TIMESTAMP WITH TIME ZONE NULL,
deleted_at TIMESTAMP WITH TIME ZONE NULL, deleted_at TIMESTAMP WITH TIME ZONE NULL,
old_vulnerability_id INT NULL REFERENCES Vulnerability ON DELETE CASCADE, old_vulnerability_id INT NULL REFERENCES Vulnerability ON DELETE CASCADE,
new_vulnerability_id INT NULL REFERENCES Vulnerability ON DELETE CASCADE);`, new_vulnerability_id INT NULL REFERENCES Vulnerability ON DELETE CASCADE);`,
`CREATE INDEX ON Vulnerability_Notification (notified_at);`, `CREATE INDEX ON Vulnerability_Notification (notified_at);`,
}), }),
Down: migrate.Queries([]string{ Down: migrate.Queries([]string{
`DROP TABLE IF EXISTS `DROP TABLE IF EXISTS
ancestry, ancestry,
ancestry_layer, ancestry_layer,
ancestry_detector, ancestry_detector,
ancestry_lister, ancestry_lister,
ancestry_feature, ancestry_feature,
feature, feature,
namespaced_feature, namespaced_feature,
keyvalue, keyvalue,
layer, layer,
layer_detector, layer_detector,
layer_feature, layer_feature,
layer_lister, layer_lister,
layer_namespace, layer_namespace,
lock, lock,
namespace, namespace,
vulnerability, vulnerability,
vulnerability_affected_feature, vulnerability_affected_feature,
vulnerability_affected_namespaced_feature, vulnerability_affected_namespaced_feature,
vulnerability_notification vulnerability_notification
CASCADE;`, CASCADE;`,
`DROP TYPE IF EXISTS severity;`, `DROP TYPE IF EXISTS severity;`,
}), }),
}) })