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

Respond to tony's updates #3

Merged
merged 7 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
42 changes: 21 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ edition = "2021"
[dependencies]
anyhow = "1.0"
axum = { version = "0.6.16", features = ["headers"] }
base64 = "0.13.1"
chrono = { version = "0.4.26", features = ["serde"] }
diesel = { version = "2.1", features = ["postgres", "r2d2", "chrono", "numeric"] }
diesel_migrations = "2.1.0"
dotenv = "0.15.0"
futures = "0.3.28"
hex = "0.4.3"
Expand All @@ -23,3 +23,6 @@ tokio = { version = "1.12.0", features = ["full"] }
tower-http = { version = "0.4.0", features = ["cors"] }

ureq = { version = "2.5.0", features = ["json"] }

[dev-dependencies]
diesel_migrations = "2.1.0"
27 changes: 27 additions & 0 deletions drill.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
base: 'https://vss-staging.fly.dev'
concurrency: 250
iterations: 2000

plan:
- name: Health Check
request:
method: GET
url: /health-check

- name: Put Objects
request:
method: PUT
url: /putObjects
body: '{"transaction_items": [{"key": "key", "value": [0, 1, 2], "version": 0}]}'
headers:
Content-Type: 'application/json'
Authorization: Bearer eyJhbGciOiJFUzI1NksiLCJraWQiOiIwMzU0N2Q5MmI2MTg4NTZmNGVkYTg0YTY0ZWMzMmYxNjk0Yzk2MDhhM2Y5ZGM3M2U5MWYwOGI1ZGFhMDg3MjYwMTYifQ.eyJleHAiOjE2OTU0NTc4MjgsIm5iZiI6MTY5NTQ1MDYyOCwiaWF0IjoxNjk1NDU0MjI4LCJzdWIiOiIwMjlhOGYwY2MxZWNmZDU5NWNjNzQyYWU5OGU4NDZlNDRjODdmMWJjYWVjNzcxOTZhOTc3MzFjNzllMmJmZDI1ODUifQ.llJlMub-FWU9tgmQRMchziyg6jLGOgKYPq5DOm4dOGUqMCtDyRQX--ILBLhgkHTVhTy0EFyYu0x4clVcb7kV0A

- name: Get Object
request:
method: POST
url: /getObject
body: '{"key": "key"}'
headers:
Content-Type: 'application/json'
Authorization: Bearer eyJhbGciOiJFUzI1NksiLCJraWQiOiIwMzU0N2Q5MmI2MTg4NTZmNGVkYTg0YTY0ZWMzMmYxNjk0Yzk2MDhhM2Y5ZGM3M2U5MWYwOGI1ZGFhMDg3MjYwMTYifQ.eyJleHAiOjE2OTU0NTc4MjgsIm5iZiI6MTY5NTQ1MDYyOCwiaWF0IjoxNjk1NDU0MjI4LCJzdWIiOiIwMjlhOGYwY2MxZWNmZDU5NWNjNzQyYWU5OGU4NDZlNDRjODdmMWJjYWVjNzcxOTZhOTc3MzFjNzllMmJmZDI1ODUifQ.llJlMub-FWU9tgmQRMchziyg6jLGOgKYPq5DOm4dOGUqMCtDyRQX--ILBLhgkHTVhTy0EFyYu0x4clVcb7kV0A
43 changes: 0 additions & 43 deletions migrations/2023-09-18-225828_baseline/up.sql

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions migrations/2023-09-20-043550_change-default-timestamp/up.sql

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ DROP TRIGGER IF EXISTS tr_set_dates_after_update ON vss_db;
DROP FUNCTION IF EXISTS set_created_date();
DROP FUNCTION IF EXISTS set_updated_date();

DROP TABLE IF EXISTS vss_db;
DROP TABLE IF EXISTS vss_db;
79 changes: 79 additions & 0 deletions migrations/2023-09-23-030518_baseline/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
CREATE TABLE vss_db
(
store_id TEXT NOT NULL CHECK (store_id != ''),
key TEXT NOT NULL,
value bytea,
version BIGINT NOT NULL,
created_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (store_id, key)
);

-- triggers to set dates automatically, generated by ChatGPT

-- Function to set created_date and updated_date during INSERT
CREATE OR REPLACE FUNCTION set_created_date()
RETURNS TRIGGER AS
$$
BEGIN
NEW.created_date := CURRENT_TIMESTAMP;
NEW.updated_date := CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

-- Function to set updated_date during UPDATE
CREATE OR REPLACE FUNCTION set_updated_date()
RETURNS TRIGGER AS
$$
BEGIN
NEW.updated_date := CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

-- Trigger for INSERT operation on vss_db
CREATE TRIGGER tr_set_dates_after_insert
BEFORE INSERT
ON vss_db
FOR EACH ROW
EXECUTE FUNCTION set_created_date();

-- Trigger for UPDATE operation on vss_db
CREATE TRIGGER tr_set_dates_after_update
BEFORE UPDATE
ON vss_db
FOR EACH ROW
EXECUTE FUNCTION set_updated_date();

CREATE OR REPLACE FUNCTION upsert_vss_db(
p_store_id TEXT,
p_key TEXT,
p_value bytea,
p_version BIGINT
) RETURNS VOID AS
$$
BEGIN

WITH new_values (store_id, key, value, version) AS (VALUES (p_store_id, p_key, p_value, p_version))
INSERT
INTO vss_db
(store_id, key, value, version)
SELECT new_values.store_id,
new_values.key,
new_values.value,
new_values.version
FROM new_values
LEFT JOIN vss_db AS existing
ON new_values.store_id = existing.store_id
AND new_values.key = existing.key
WHERE CASE
WHEN new_values.version >= 4294967295 THEN new_values.version >= COALESCE(existing.version, -1)
ELSE new_values.version > COALESCE(existing.version, -1)
END
ON CONFLICT (store_id, key)
DO UPDATE SET value = excluded.value,
version = excluded.version;

END;
$$ LANGUAGE plpgsql;
Loading
Loading