|
| 1 | +DROP TABLE samples; |
| 2 | +DROP TABLE versions; |
| 3 | +DROP TABLE repos; |
| 4 | +DROP TABLE users; |
| 5 | + |
| 6 | +DROP SEQUENCE user_id_seq; |
| 7 | +DROP SEQUENCE repo_id_seq; |
| 8 | +DROP SEQUENCE version_id_seq; |
| 9 | +DROP SEQUENCE sample_id_seq; |
| 10 | + |
| 11 | + |
| 12 | +CREATE SEQUENCE user_id_seq; |
| 13 | +CREATE TABLE users ( |
| 14 | + id BIGINT NOT NULL PRIMARY KEY DEFAULT nextval('user_id_seq'), |
| 15 | + fullname VARCHAR(64), |
| 16 | + username VARCHAR(64), |
| 17 | + email VARCHAR(64), |
| 18 | + password VARCHAR(64), |
| 19 | + salt VARCHAR(64), |
| 20 | + permissions BIGINT, |
| 21 | + create_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP |
| 22 | +); |
| 23 | + |
| 24 | + |
| 25 | +CREATE SEQUENCE repo_id_seq; |
| 26 | +CREATE TABLE repos ( |
| 27 | + id BIGINT NOT NULL PRIMARY KEY DEFAULT nextval('repo_id_seq'), |
| 28 | + name VARCHAR(100), |
| 29 | + title VARCHAR(100), |
| 30 | + templates BOOLEAN, |
| 31 | + owner_id BIGINT REFERENCES users(id), |
| 32 | + create_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP |
| 33 | +); |
| 34 | + |
| 35 | + |
| 36 | +CREATE SEQUENCE version_id_seq; |
| 37 | +CREATE TABLE versions ( |
| 38 | + id BIGINT NOT NULL PRIMARY KEY DEFAULT nextval('version_id_seq'), |
| 39 | + name VARCHAR (255) NOT NULL, |
| 40 | + repo_id BIGINT REFERENCES repos(id), |
| 41 | + commit VARCHAR (40) NOT NULL, |
| 42 | + hidden BOOLEAN DEFAULT FALSE, |
| 43 | + config JSONB, |
| 44 | + samples_count INTEGER, |
| 45 | + create_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP |
| 46 | +); |
| 47 | + |
| 48 | + |
| 49 | +CREATE SEQUENCE sample_id_seq; |
| 50 | +CREATE TABLE samples ( |
| 51 | + id BIGINT NOT NULL PRIMARY KEY DEFAULT nextval('sample_id_seq'), |
| 52 | + -- repo |
| 53 | + version_id BIGINT REFERENCES versions(id), |
| 54 | + -- save/update |
| 55 | + version INTEGER DEFAULT 0, |
| 56 | + |
| 57 | + -- main info |
| 58 | + name VARCHAR(255), |
| 59 | + description TEXT, |
| 60 | + short_description VARCHAR(1024), |
| 61 | + url VARCHAR(255), |
| 62 | + |
| 63 | + -- meta |
| 64 | + tags VARCHAR(128)[], |
| 65 | + deleted_tags VARCHAR(128)[], -- to not auto-add already deleted tags generated from code when save/fork |
| 66 | + create_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
| 67 | + views BIGINT DEFAULT 0, |
| 68 | + likes INTEGER DEFAULT 0, |
| 69 | + owner_id BIGINT REFERENCES users(id), |
| 70 | + preview BOOLEAN DEFAULT FALSE, |
| 71 | + latest BOOLEAN DEFAULT FALSE, |
| 72 | + |
| 73 | + -- html, js, css |
| 74 | + scripts VARCHAR(256)[], |
| 75 | + styles VARCHAR(256)[], |
| 76 | + |
| 77 | + code_type VARCHAR(16), |
| 78 | + code TEXT, |
| 79 | + |
| 80 | + style_type VARCHAR(16), |
| 81 | + style TEXT, |
| 82 | + |
| 83 | + markup_type VARCHAR(16), |
| 84 | + markup TEXT |
| 85 | +); |
| 86 | +CREATE INDEX tags_index ON samples USING gin (tags); |
| 87 | +CREATE INDEX version_id_index ON samples (version_id); |
| 88 | +CREATE INDEX latest_index ON samples (latest); |
| 89 | +CREATE INDEX url_index ON samples (url); |
| 90 | +CREATE INDEX owner_id ON samples (owner_id); |
| 91 | +CREATE INDEX views_id ON samples (views); |
| 92 | + |
| 93 | + |
| 94 | +CREATE SEQUENCE template_id_seq; |
| 95 | +CREATE TABLE templates ( |
| 96 | + id BIGINT NOT NULL PRIMARY KEY DEFAULT nextval('template_id_seq'), |
| 97 | + sample_id BIGINT REFERENCES samples(id), |
| 98 | + create_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP |
| 99 | +); |
| 100 | + |
| 101 | + |
| 102 | +CREATE SEQUENCE session_id_seq; |
| 103 | +CREATE TABLE sessions ( |
| 104 | + id BIGINT NOT NULL PRIMARY KEY DEFAULT nextval('session_id_seq'), |
| 105 | + session CHAR (36) NOT NULL, |
| 106 | + user_id BIGINT REFERENCES users(id), |
| 107 | + create_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP |
| 108 | +); |
| 109 | +CREATE INDEX session_index ON sessions (session); |
| 110 | +CREATE INDEX user_id_index ON sessions (user_id); |
| 111 | + |
| 112 | + |
| 113 | +CREATE SEQUENCE data_source_id_seq; |
| 114 | +CREATE TABLE data_sources ( |
| 115 | + id BIGINT NOT NULL PRIMARY KEY DEFAULT nextval('data_source_id_seq'), |
| 116 | + name VARCHAR (64) NOT NULL, |
| 117 | + title VARCHAR (64) NOT NULL, |
| 118 | + url VARCHAR(255) NOT NULL, |
| 119 | + type VARCHAR(64) NOT NULL, |
| 120 | + create_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP |
| 121 | +); |
| 122 | + |
| 123 | + |
| 124 | +CREATE SEQUENCE data_set_id_seq; |
| 125 | +CREATE TABLE data_sets ( |
| 126 | + id BIGINT NOT NULL PRIMARY KEY DEFAULT nextval('data_set_id_seq'), |
| 127 | + data_source_id BIGINT REFERENCES data_sources(id), |
| 128 | + logo VARCHAR(64), |
| 129 | + name VARCHAR (64), |
| 130 | + title VARCHAR (64), |
| 131 | + description VARCHAR(256), |
| 132 | + tags VARCHAR(128), |
| 133 | + source VARCHAR(256), |
| 134 | + sample TEXT, |
| 135 | + url VARCHAR(255) NOT NULL, |
| 136 | + data TEXT, |
| 137 | + create_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP |
| 138 | +); |
| 139 | + |
| 140 | + |
| 141 | +CREATE SEQUENCE banned_tag_id_seq; |
| 142 | +CREATE TABLE banned_tags ( |
| 143 | + id BIGINT NOT NULL PRIMARY KEY DEFAULT nextval('banned_tag_id_seq'), |
| 144 | + tag VARCHAR(128) NOT NULL UNIQUE, |
| 145 | + create_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP |
| 146 | +); |
| 147 | + |
| 148 | + |
| 149 | +CREATE SEQUENCE visit_id_seq; |
| 150 | +CREATE TABLE visits ( |
| 151 | + id BIGINT NOT NULL PRIMARY KEY DEFAULT nextval('visit_id_seq'), |
| 152 | + user_id BIGINT NOT NULL REFERENCES users(id), |
| 153 | + sample_id BIGINT NOT NULL REFERENCES samples(id), |
| 154 | + create_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP |
| 155 | +); |
| 156 | +CREATE INDEX sample_id_index ON visits (sample_id); |
| 157 | + |
| 158 | + |
| 159 | +CREATE SEQUENCE canonical_visit_id_seq; |
| 160 | +CREATE TABLE canonical_visits ( |
| 161 | + id BIGINT NOT NULL PRIMARY KEY DEFAULT nextval('canonical_visit_id_seq'), |
| 162 | + user_id BIGINT NOT NULL REFERENCES users(id), |
| 163 | + url VARCHAR(255) NOT NULL, |
| 164 | + repo_id BIGINT REFERENCES repos(id), |
| 165 | + create_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 166 | + UNIQUE (user_id, url, repo_id) |
| 167 | +); |
| 168 | +CREATE INDEX url_canonical_visits_index ON canonical_visits (url); |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | +CREATE MATERIALIZED VIEW tags AS |
| 173 | + SELECT tags.nm AS name, |
| 174 | + regexp_replace(regexp_replace(regexp_replace(lower(tags.nm), '[^a-z0-9]', '-', 'g'), '-[-]+', '-', 'g'), '(-$|^-)', '', 'g') AS id, |
| 175 | + count |
| 176 | + FROM |
| 177 | + (SELECT unnest(tags) AS nm, |
| 178 | + count(*) AS count |
| 179 | + FROM samples |
| 180 | + WHERE samples.latest |
| 181 | + AND samples.id NOT IN (SELECT sample_id FROM templates) |
| 182 | + GROUP BY nm |
| 183 | + ORDER BY COUNT DESC) tags |
| 184 | + WHERE nm NOT IN (SELECT tag FROM banned_tags) |
| 185 | +WITH DATA; |
0 commit comments