Skip to content

Commit 3469d55

Browse files
committed
Add SQL scheme
1 parent aa17ae3 commit 3469d55

File tree

2 files changed

+324
-0
lines changed

2 files changed

+324
-0
lines changed

src/sql/scheme_mysql.sql

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# DEPRECATED. Now PostgreSQL is used.
2+
CREATE TABLE repos (
3+
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
4+
name VARCHAR(100),
5+
title VARCHAR(100),
6+
templates BOOLEAN,
7+
owner_id INTEGER REFERENCES users(id),
8+
create_date DATETIME DEFAULT CURRENT_TIMESTAMP
9+
) CHARACTER SET=utf8;
10+
11+
CREATE TABLE versions (
12+
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
13+
name varchar(255) not NULL,
14+
repo_id integer references repos(id),
15+
commit varchar(40) not NULL,
16+
hidden BOOLEAN DEFAULT FALSE,
17+
config JSON,
18+
samples_count INTEGER,
19+
create_date DATETIME DEFAULT CURRENT_TIMESTAMP
20+
) CHARACTER SET=utf8;
21+
22+
CREATE TABLE samples (
23+
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
24+
-- repo
25+
version_id INTEGER references versions(id),
26+
-- save/update
27+
version INTEGER DEFAULT 0,
28+
-- hash CHAR(8),
29+
30+
-- main info
31+
name VARCHAR(255),
32+
description TEXT,
33+
short_description VARCHAR(1024),
34+
url VARCHAR(255),
35+
36+
-- meta
37+
tags JSON,
38+
deleted_tags JSON, -- to not auto-add already deleted tags generated from code when save/fork
39+
create_date DATETIME DEFAULT CURRENT_TIMESTAMP,
40+
exports VARCHAR(255),
41+
views BIGINT DEFAULT 0,
42+
likes INTEGER DEFAULT 0,
43+
owner_id INTEGER REFERENCES users(id),
44+
preview BOOLEAN DEFAULT FALSE,
45+
latest BOOLEAN DEFAULT FALSE,
46+
47+
-- html, js, css
48+
scripts TEXT,
49+
local_scripts TEXT,
50+
styles TEXT,
51+
52+
code_type VARCHAR(16),
53+
code MEDIUMTEXT,
54+
55+
style_type VARCHAR(16),
56+
style TEXT,
57+
58+
markup_type VARCHAR(16),
59+
markup TEXT
60+
) CHARACTER SET=utf8;
61+
62+
CREATE TABLE templates (
63+
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
64+
sample_id BIGINT REFERENCES samples(id),
65+
create_date DATETIME DEFAULT CURRENT_TIMESTAMP
66+
) CHARACTER SET=utf8;
67+
68+
CREATE TABLE users (
69+
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
70+
fullname VARCHAR(64),
71+
username VARCHAR(64),
72+
email VARCHAR(64),
73+
password VARCHAR(64),
74+
salt VARCHAR(64),
75+
permissions BIGINT,
76+
create_date DATETIME DEFAULT CURRENT_TIMESTAMP
77+
) CHARACTER SET=utf8;
78+
79+
CREATE TABLE sessions (
80+
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
81+
session CHAR (36) NOT NULL,
82+
user_id INT REFERENCES users(id),
83+
create_date DATETIME DEFAULT CURRENT_TIMESTAMP
84+
) CHARACTER SET=utf8;
85+
86+
CREATE TABLE data_sources (
87+
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
88+
name CHAR (64) NOT NULL,
89+
title CHAR (64) NOT NULL,
90+
url VARCHAR(255) NOT NULL,
91+
type VARCHAR(64) NOT NULL,
92+
sets JSON,
93+
create_date DATETIME DEFAULT CURRENT_TIMESTAMP
94+
) CHARACTER SET=utf8;
95+
96+
CREATE TABLE data_sets (
97+
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
98+
data_source_id INTEGER REFERENCES data_sources(id),
99+
logo VARCHAR(64),
100+
name VARCHAR (64),
101+
title VARCHAR (64),
102+
description VARCHAR(256),
103+
tags JSON,
104+
source VARCHAR(256),
105+
sample MEDIUMTEXT,
106+
url VARCHAR(255) NOT NULL,
107+
data LONGTEXT,
108+
create_date DATETIME DEFAULT CURRENT_TIMESTAMP
109+
) CHARACTER SET=utf8;
110+
111+
CREATE TABLE banned_tags (
112+
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
113+
tag VARCHAR(64) NOT NULL UNIQUE,
114+
create_date DATETIME DEFAULT CURRENT_TIMESTAMP
115+
) CHARACTER SET=utf8;
116+
117+
CREATE TABLE visits (
118+
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
119+
user_id INTEGER NOT NULL REFERENCES users(id),
120+
sample_id BIGINT NOT NULL REFERENCES samples(id),
121+
create_date DATETIME DEFAULT CURRENT_TIMESTAMP
122+
) CHARACTER SET=utf8;
123+
124+
CREATE TABLE tags_mw (
125+
name VARCHAR(64),
126+
count BIGINT UNSIGNED
127+
) CHARACTER SET=utf8;
128+
129+
130+
CREATE INDEX sample_id_index ON visits (sample_id);
131+
132+
CREATE INDEX session_index ON sessions (session);
133+
CREATE INDEX user_id_index ON sessions (user_id);
134+
135+
CREATE INDEX version_id_index ON samples (version_id);
136+
CREATE INDEX latest_index ON samples (latest);
137+
CREATE INDEX url_index ON samples (url);
138+
CREATE INDEX owner_id ON samples (owner_id);
139+
CREATE INDEX views_id ON samples (views);

src/sql/scheme_postgre.sql

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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

Comments
 (0)