Skip to content

Commit

Permalink
chore(zbugs): Add some data length limits for publically mutable data.
Browse files Browse the repository at this point in the history
  • Loading branch information
aboodman committed Dec 16, 2024
1 parent 901b904 commit 2e9c198
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 33 deletions.
102 changes: 72 additions & 30 deletions apps/zbugs/docker/init_upstream/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ DROP TABLE IF EXISTS "user",
"userPref",
"zero.schemaVersions" CASCADE;

-- user

CREATE TABLE "user" (
"id" VARCHAR PRIMARY KEY,
"login" VARCHAR NOT NULL,
Expand All @@ -19,26 +21,60 @@ CREATE TABLE "user" (
CREATE UNIQUE INDEX user_login_idx ON "user" (login);
CREATE UNIQUE INDEX user_githubid_idx ON "user" ("githubID");

-- issue

CREATE TABLE issue (
"id" VARCHAR PRIMARY KEY,
"shortID" INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 3000),
"title" VARCHAR NOT NULL,
"title" VARCHAR(128) NOT NULL,
"open" BOOLEAN NOT NULL,
"modified" double precision DEFAULT (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) * 1000),
"created" double precision DEFAULT (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) * 1000),
"creatorID" VARCHAR REFERENCES "user"(id) NOT NULL,
"assigneeID" VARCHAR REFERENCES "user"(id),
"description" TEXT DEFAULT '',
-- Size chosen because max we currently have in legacy data is ~9KB.
"description" VARCHAR(10240) DEFAULT '',
"visibility" VARCHAR DEFAULT 'public' NOT NULL
);


CREATE OR REPLACE FUNCTION update_modified_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.modified = (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) * 1000);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER issue_set_last_modified
BEFORE INSERT OR UPDATE ON issue
FOR EACH ROW
EXECUTE FUNCTION update_modified_column();

CREATE OR REPLACE FUNCTION issue_set_created_on_insert()
RETURNS TRIGGER AS $$
BEGIN
NEW.created = (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) * 1000);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER issue_set_created_on_insert_trigger
BEFORE INSERT ON issue
FOR EACH ROW
EXECUTE FUNCTION issue_set_created_on_insert();

-- viewState

CREATE TABLE "viewState" (
"userID" VARCHAR REFERENCES "user"(id) ON DELETE CASCADE,
"issueID" VARCHAR REFERENCES issue(id) ON DELETE CASCADE,
"viewed" double precision,
PRIMARY KEY ("userID", "issueID")
);

-- comment

CREATE TABLE comment (
id VARCHAR PRIMARY KEY,
"issueID" VARCHAR REFERENCES issue(id) ON DELETE CASCADE,
Expand Down Expand Up @@ -75,17 +111,46 @@ BEFORE INSERT ON comment
FOR EACH ROW
EXECUTE FUNCTION comment_set_created_on_insert();

CREATE OR REPLACE FUNCTION validate_comment_body_length()
RETURNS TRIGGER AS $$
BEGIN
IF NEW.body IS NOT NULL THEN
-- The launch post has a special case maxlength of 1024 because trolls
IF NEW."issueID" = 'duuW9Nyj5cTNLlimp9Qje' AND LENGTH(NEW.body) > 1024 THEN
RAISE EXCEPTION 'Column value exceeds maximum allowed length of %', 1024;
END IF;
-- Length chosen because we have some old comments that are ~44KB.
IF LENGTH(NEW.body) > 64*1024 THEN
RAISE EXCEPTION 'Column value exceeds maximum allowed length of %', 64*1024;
END IF;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER check_comment_body_length
BEFORE INSERT OR UPDATE ON comment
FOR EACH ROW
EXECUTE FUNCTION validate_comment_body_length();


-- label

CREATE TABLE label (
"id" VARCHAR PRIMARY KEY,
"name" VARCHAR NOT NULL
);

-- issueLabel

CREATE TABLE "issueLabel" (
"labelID" VARCHAR REFERENCES label(id),
"issueID" VARCHAR REFERENCES issue(id) ON DELETE CASCADE,
PRIMARY KEY ("labelID", "issueID")
);

-- emoji

CREATE TABLE emoji (
"id" VARCHAR PRIMARY KEY,
"value" VARCHAR NOT NULL,
Expand Down Expand Up @@ -181,6 +246,8 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

-- userPref

CREATE TABLE "userPref" (
"key" VARCHAR NOT NULL,
"value" VARCHAR NOT NULL,
Expand All @@ -189,6 +256,8 @@ CREATE TABLE "userPref" (
PRIMARY KEY ("userID", "key")
);

-- zero.schemaVersions

CREATE SCHEMA IF NOT EXISTS zero;

CREATE TABLE IF NOT EXISTS zero."schemaVersions" (
Expand All @@ -206,34 +275,6 @@ INSERT INTO zero."schemaVersions" ("lock", "minSupportedVersion", "maxSupportedV
VALUES (true, 3, 5) ON CONFLICT DO NOTHING;


-- last modified function and trigger
CREATE OR REPLACE FUNCTION update_modified_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.modified = (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) * 1000);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER issue_set_last_modified
BEFORE INSERT OR UPDATE ON issue
FOR EACH ROW
EXECUTE FUNCTION update_modified_column();

CREATE OR REPLACE FUNCTION issue_set_created_on_insert()
RETURNS TRIGGER AS $$
BEGIN
NEW.created = (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) * 1000);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER issue_set_created_on_insert_trigger
BEFORE INSERT ON issue
FOR EACH ROW
EXECUTE FUNCTION issue_set_created_on_insert();


COPY "user"
FROM
'/docker-entrypoint-initdb.d/users.csv' WITH CSV HEADER;
Expand All @@ -254,6 +295,7 @@ COPY "comment"
FROM
'/docker-entrypoint-initdb.d/comments.csv' WITH CSV HEADER;


-- Create the indices on upstream so we can copy to downstream on replication.
-- We have discussed that, in the future, the indices of the Zero replica
-- can / should diverge from the indices of the upstream. This is because
Expand Down
6 changes: 3 additions & 3 deletions apps/zbugs/docker/init_upstream/issues.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3055,7 +3055,7 @@ Profile attached. More information when less tired :).

[Profile-live.json.zip](https://github.com/rocicorp/reflect-server/files/9663620/Profile-live.json.zip)
"
N983dAQkVWxJwlWi8nb_N,75,We can get rid of the whole complicated updateClients (which was needed due to async hashing and indexeddb). Callers can use setClients (and they need to put their own chunks).,False,1677091437000.0,1664205709000.0,nqYkxAGMnzk7Y5STjZryV,," We can get rid of the whole complicated updateClients (which was needed due to async hashing and indexeddb). Callers can use setClients (and they need to put their own chunks).
N983dAQkVWxJwlWi8nb_N,75,We can get rid of the whole complicated updateClients (which was needed due to async hashing and indexeddb)..,False,1677091437000.0,1664205709000.0,nqYkxAGMnzk7Y5STjZryV,,"Callers can use setClients (and they need to put their own chunks).

Fine to do as a follow up as this is already quite a big pr.

Expand Down Expand Up @@ -3365,10 +3365,10 @@ zrpBifgcCO7g1DjYQ1v9A,90,"Rails ""done done""",False,1677091445000.0,16570873140
- [ ] Update ReplidrawDO to use
- [ ] Update Repliear to use
- [ ] Rename to something else... replicache-crud?"
jC2ZSB7TCGAhDeK_CTNuj,147,Maybe call this test `crossClientGetMutationID` or something to make more clear why it only runs under DD31. A comment would also help.,False,1677091452000.0,1656407388000.0,nqYkxAGMnzk7Y5STjZryV,,"Maybe call this test `crossClientGetMutationID` or something to make more clear why it only runs under DD31. A comment would also help.
jC2ZSB7TCGAhDeK_CTNuj,147,Maybe call this test `crossClientGetMutationID` or something to make more clear why it only runs under DD31.,False,1677091452000.0,1656407388000.0,nqYkxAGMnzk7Y5STjZryV,,"A comment would also help.

_Originally posted by @aboodman in https://github.com/rocicorp/replicache-internal/pull/177#discussion_r906419411_"
bbBCilPYtx_FfumPLQRqG,148,"Why not adjust these tests to have a dag.Read. Even if most of the commit types / tests don't need it, some do. Would be easier to read the test if you didn't have to understand why it's OK to pass null here.",False,1677091453000.0,1656407353000.0,nqYkxAGMnzk7Y5STjZryV,,"Why not adjust these tests to have a dag.Read. Even if most of the commit types / tests don't need it, some do. Would be easier to read the test if you didn't have to understand why it's OK to pass null here.
bbBCilPYtx_FfumPLQRqG,148,"Why not adjust these tests to have a dag.Read. Even if most of the commit types / tests don't need it, some do.",False,1677091453000.0,1656407353000.0,nqYkxAGMnzk7Y5STjZryV,,"Would be easier to read the test if you didn't have to understand why it's OK to pass null here.

_Originally posted by @aboodman in https://github.com/rocicorp/replicache-internal/pull/177#discussion_r906418791_"
0aANjS4hkfgYM6jyk0bnz,111,DD 3.1,False,1709536712000.0,1655804122000.0,nqYkxAGMnzk7Y5STjZryV,,"Tracking bug for https://www.notion.so/replicache/DD-3-1-e42489fc2e6b4340a01c7fa0de353a30
Expand Down

0 comments on commit 2e9c198

Please sign in to comment.