Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotate sample application schemas #169

Merged
merged 2 commits into from
May 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,857 changes: 0 additions & 2,857 deletions experiments/schema-annot/PrestaShop.sql

This file was deleted.

427 changes: 427 additions & 0 deletions experiments/schema-annot/README.md

Large diffs are not rendered by default.

175 changes: 175 additions & 0 deletions experiments/schema-annot/annotated/commento-annotated.sql
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;
14 changes: 14 additions & 0 deletions experiments/schema-annot/annotated/count.sh
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
79 changes: 79 additions & 0 deletions experiments/schema-annot/annotated/ghchat-annotated.sql
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;
Loading