-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add annotated schema for 7 other smaller applications:
* Commento * ghchat * hotcrp * instagram * schnack * socify * mouthful This includes adding missing FKs for many of the schemas that did not have any FKs, extracting schema from non-sql code, and normalizing some schemas that were not in normal form. Add a README with a description of interesting scenarios and output of EXPLAIN COMPLIANCE.
- Loading branch information
Showing
27 changed files
with
1,667 additions
and
5,099 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
175 changes: 175 additions & 0 deletions
175
experiments/schema-annot/annotated/commento-annotated.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
CREATE TABLE config ( \ | ||
allowNewOwners int NOT NULL, \ | ||
PRIMARY KEY(allowNewOwners) \ | ||
); | ||
|
||
-- INSERT INTO | ||
-- config (allowNewOwners) | ||
-- VALUES (true); | ||
|
||
CREATE DATA_SUBJECT TABLE owners ( \ | ||
ownerHex TEXT NOT NULL UNIQUE PRIMARY KEY, \ | ||
email TEXT NOT NULL, \ | ||
name TEXT NOT NULL, \ | ||
passwordHash TEXT NOT NULL, \ | ||
confirmedEmail TEXT NOT NULL, | ||
joinDate datetime NOT NULL \ | ||
); | ||
|
||
CREATE TABLE ownerSessions ( \ | ||
session TEXT NOT NULL UNIQUE PRIMARY KEY, \ | ||
ownerHex TEXT NOT NULL, \ | ||
loginDate datetime NOT NULL, \ | ||
FOREIGN KEY (ownerHex) OWNED_BY owners(ownerHex) \ | ||
); | ||
|
||
CREATE TABLE ownerConfirmHexes ( | ||
confirmHex TEXT NOT NULL UNIQUE PRIMARY KEY, \ | ||
ownerHex TEXT NOT NULL, \ | ||
sendDate TEXT NOT NULL, \ | ||
FOREIGN KEY (ownerHex) OWNED_BY owners(ownerHex) \ | ||
); | ||
|
||
CREATE TABLE ownerResetHexes ( \ | ||
resetHex TEXT NOT NULL UNIQUE PRIMARY KEY, \ | ||
ownerHex TEXT NOT NULL, \ | ||
sendDate TEXT NOT NULL, \ | ||
FOREIGN KEY (ownerHex) OWNED_BY owners(ownerHex) \ | ||
); | ||
|
||
CREATE TABLE domains ( \ | ||
domain TEXT NOT NULL UNIQUE PRIMARY KEY, \ | ||
ownerHex TEXT NOT NULL, \ | ||
name TEXT NOT NULL, \ | ||
creationDate datetime NOT NULL, \ | ||
state TEXT NOT NULL, \ | ||
importedComments TEXT NOT NULL, \ | ||
autoSpamFilter int NOT NULL, \ | ||
requireModeration int NOT NULL, \ | ||
requireIdentification int NOT NULL, \ | ||
viewsThisMonth INTEGER NOT NULL, \ | ||
FOREIGN KEY (ownerHex) OWNED_BY owners(ownerHex) \ | ||
); | ||
|
||
CREATE DATA_SUBJECT TABLE moderators ( \ | ||
id int NOT NULL, | ||
domain TEXT NOT NULL, \ | ||
email TEXT NOT NULL, \ | ||
addDate datetime NOT NULL, \ | ||
PRIMARY KEY(id), \ | ||
FOREIGN KEY (domain) REFERENCES ONLY domains(domain) \ | ||
); | ||
|
||
CREATE DATA_SUBJECT TABLE commenters ( \ | ||
commenterHex TEXT NOT NULL UNIQUE PRIMARY KEY, \ | ||
email TEXT NOT NULL, \ | ||
name TEXT NOT NULL, \ | ||
link TEXT NOT NULL, \ | ||
photo TEXT NOT NULL, \ | ||
provider TEXT NOT NULL, \ | ||
joinDate datetime NOT NULL, \ | ||
state TEXT NOT NULL \ | ||
); | ||
|
||
CREATE TABLE commenterSessions ( \ | ||
session TEXT NOT NULL UNIQUE PRIMARY KEY, \ | ||
commenterHex TEXT NOT NULL, \ | ||
creationDate datetime NOT NULL, \ | ||
FOREIGN KEY (commenterHex) OWNED_BY commenters(commenterHex) \ | ||
); | ||
|
||
CREATE TABLE comments ( \ | ||
commentHex TEXT NOT NULL UNIQUE PRIMARY KEY, \ | ||
domain TEXT NOT NULL, \ | ||
path TEXT NOT NULL, \ | ||
commenterHex TEXT NOT NULL, \ | ||
markdown TEXT NOT NULL, \ | ||
html TEXT NOT NULL, \ | ||
parentHex TEXT NOT NULL, \ | ||
score INTEGER NOT NULL, \ | ||
state TEXT NOT NULL, \ | ||
creationDate datetime NOT NULL, \ | ||
FOREIGN KEY (commenterHex) OWNED_BY commenters(commenterHex), \ | ||
FOREIGN KEY (domain) ACCESSED_BY domains(domain), \ | ||
ON DEL parentHex DELETE_ROW, \ | ||
FOREIGN KEY (parentHex) ACCESSED_BY comments(commentHex), \ | ||
ON GET parentHex ANON (commenterHex, score, state), \ | ||
ON GET domain ANON (commenterHex, score, state) \ | ||
); | ||
|
||
-- DELETEing a comment should recursively delete all children | ||
-- CREATE OR REPLACE FUNCTION commentsDeleteTriggerFunction() RETURNS TRIGGER AS $trigger$ | ||
-- BEGIN | ||
-- DELETE FROM comments | ||
-- WHERE parentHex = old.commentHex; | ||
|
||
-- RETURN NULL; | ||
-- END; | ||
-- $trigger$ LANGUAGE plpgsql; | ||
|
||
-- CREATE TRIGGER commentsDeleteTrigger AFTER DELETE ON comments | ||
-- FOR EACH ROW EXECUTE PROCEDURE commentsDeleteTriggerFunction(); | ||
|
||
CREATE TABLE votes ( \ | ||
commentHex TEXT NOT NULL, \ | ||
commenterHex TEXT NOT NULL, \ | ||
direction INTEGER NOT NULL, \ | ||
voteDate datetime NOT NULL, \ | ||
PRIMARY KEY(commentHex), \ | ||
FOREIGN KEY (commenterHex) OWNED_BY commenters(commenterHex) \ | ||
); | ||
|
||
-- CREATE UNIQUE INDEX votesUniqueIndex ON votes(commentHex, commenterHex); | ||
|
||
-- CREATE OR REPLACE FUNCTION votesInsertTriggerFunction() RETURNS TRIGGER AS $trigger$ | ||
-- BEGIN | ||
-- UPDATE comments | ||
-- SET score = score + new.direction | ||
-- WHERE commentHex = new.commentHex; | ||
|
||
-- RETURN NEW; | ||
-- END; | ||
-- $trigger$ LANGUAGE plpgsql; | ||
|
||
-- CREATE TRIGGER votesInsertTrigger AFTER INSERT ON votes | ||
-- FOR EACH ROW EXECUTE PROCEDURE votesInsertTriggerFunction(); | ||
|
||
-- CREATE OR REPLACE FUNCTION votesUpdateTriggerFunction() RETURNS TRIGGER AS $trigger$ | ||
-- BEGIN | ||
-- UPDATE comments | ||
-- SET score = score - old.direction + new.direction | ||
-- WHERE commentHex = old.commentHex; | ||
|
||
-- RETURN NEW; | ||
-- END; | ||
-- $trigger$ LANGUAGE plpgsql; | ||
|
||
-- CREATE TRIGGER votesUpdateTrigger AFTER UPDATE ON votes | ||
-- FOR EACH ROW EXECUTE PROCEDURE votesUpdateTriggerFunction(); | ||
|
||
-- note: had to add PRIMARY KEY(domain) to avoid "invalid PK" error | ||
CREATE TABLE views ( \ | ||
id int NOT NULL UNIQUE PRIMARY KEY, \ | ||
domain text NOT NULL, \ | ||
commenterHex text NOT NULL, \ | ||
viewDate datetime NOT NULL, \ | ||
FOREIGN KEY (commenterHex) OWNED_BY commenters(commenterHex), \ | ||
FOREIGN KEY (domain) REFERENCES domains(domain) \ | ||
); | ||
|
||
-- CREATE INDEX IF NOT EXISTS domainIndex ON views(domain); | ||
|
||
-- CREATE OR REPLACE FUNCTION viewsInsertTriggerFunction() RETURNS TRIGGER AS $trigger$ | ||
-- BEGIN | ||
-- UPDATE domains | ||
-- SET viewsThisMonth = viewsThisMonth + 1 | ||
-- WHERE domain = new.domain; | ||
|
||
-- RETURN NULL; | ||
-- END; | ||
-- $trigger$ LANGUAGE plpgsql; | ||
|
||
-- CREATE TRIGGER viewsInsertTrigger AFTER INSERT ON views | ||
-- FOR EACH ROW EXECUTE PROCEDURE viewsInsertTriggerFunction(); | ||
|
||
EXPLAIN COMPLIANCE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/bin/bash | ||
for f in *.sql; do | ||
tbls=$(grep -i -R -E "CREATE (DATA_SUBJECT )?TABLE" $f | grep -E -v "^\s*--" | wc -l) | ||
ds=$(grep -i -R -E "CREATE DATA_SUBJECT TABLE" $f | grep -E -v "^\s*--" | wc -l) | ||
own=$(grep -i -R -E " (OWNED_BY|OWNS) " $f | grep -E -v "^\s*--" | wc -l) | ||
acc=$(grep -i -R -E " (ACCESSED_BY|ACCESSES) " $f | grep -E -v "^\s*--" | wc -l) | ||
anon=$(grep -i -R -E "ON (GET|DEL) .* (ANON|DELETE_ROW)" $f | grep -E -v "^\s*--" | wc -l) | ||
prefix=(${f//-/ }) | ||
prefix=${prefix[0]} | ||
prefix=(${prefix//_/ }) | ||
prefix=${prefix[0]} | ||
printf "\\\\%-20s & %-6s & %-9s & %-3s & %-6s & %-5s \\\\\\ \\hline" $prefix $tbls $ds $own $acc $anon | ||
echo "" | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
CREATE DATA_SUBJECT TABLE user_info ( | ||
id int NOT NULL, | ||
github_id int, | ||
name text NOT NULL, | ||
password text, | ||
avatar text, | ||
location text, | ||
socketid text, | ||
website text, | ||
github text, | ||
intro text, | ||
company text, | ||
PRIMARY KEY (id) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | ||
|
||
CREATE TABLE group_info ( | ||
id int NOT NULL, | ||
to_group_id int NOT NULL, | ||
name text NOT NULL, | ||
group_notice text NOT NULL, | ||
creator_id int NOT NULL, | ||
create_time int NOT NULL, | ||
PRIMARY KEY (id), | ||
FOREIGN KEY (creator_id) OWNED_BY user_info(id) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | ||
|
||
CREATE TABLE group_user_relation ( | ||
id text NOT NULL, | ||
to_group_id int NOT NULL, | ||
user_id int NOT NULL, | ||
PRIMARY KEY (id), | ||
FOREIGN KEY (user_id) OWNED_BY user_info(id), | ||
FOREIGN KEY (to_group_id) ACCESSES group_info(id) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | ||
|
||
CREATE TABLE group_msg ( | ||
id text NOT NULL, | ||
from_user int NOT NULL, | ||
to_group_id int NOT NULL, | ||
message text NOT NULL, | ||
time text NOT NULL, | ||
attachments text, | ||
PRIMARY KEY (id), | ||
-- KEY to_group (to_group_id), | ||
FOREIGN KEY (from_user) OWNED_BY user_info(id), | ||
FOREIGN KEY (to_group_id) ACCESSED_BY group_info(id) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | ||
|
||
CREATE TABLE private_msg ( | ||
id text NOT NULL, | ||
from_user text NOT NULL, | ||
to_user text NOT NULL, | ||
message text, | ||
time text NOT NULL, | ||
attachments text, | ||
PRIMARY KEY (id), | ||
-- KEY from_user (from_user), | ||
-- KEY to_user (to_user), | ||
FOREIGN KEY (from_user) OWNED_BY user_info(id), | ||
FOREIGN KEY (to_user) OWNED_BY user_info(id), | ||
ON DEL from_user ANON (from_user), | ||
ON DEL to_user ANON (to_user) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | ||
|
||
CREATE TABLE user_user_relation ( | ||
id text NOT NULL, | ||
user_id int NOT NULL, | ||
from_user text NOT NULL, | ||
remark text, | ||
shield int NOT NULL, | ||
time text NOT NULL, | ||
PRIMARY KEY (id), | ||
FOREIGN KEY (user_id) OWNED_BY user_info(id), | ||
FOREIGN KEY (from_user) OWNED_BY user_info(id), | ||
ON DEL user_id ANON (user_id), | ||
ON DEL from_user ANON (from_user) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | ||
|
||
EXPLAIN COMPLIANCE; |
Oops, something went wrong.