diff --git a/packages/db/migrations/0172.do.service_usage.sql b/packages/db/migrations/0172.do.service_usage.sql new file mode 100755 index 000000000..f0beeb316 --- /dev/null +++ b/packages/db/migrations/0172.do.service_usage.sql @@ -0,0 +1,37 @@ +-- Type: DO +-- Name: service_usage +-- Description: Create table for tracking service usage and enforce limit + +BEGIN; + +ALTER TABLE omnivore.received_emails + ADD COLUMN reply_to TEXT, + ADD COLUMN reply TEXT; + +CREATE TABLE omnivore.subscription_plan ( + id SERIAL PRIMARY KEY, + name VARCHAR(255) NOT NULL, + description TEXT, + max_emails_sent_per_day INT NOT NULL, + created_at timestamptz NOT NULL default current_timestamp +); + +INSERT INTO omnivore.subscription_plan (id, name, description, max_emails_sent_per_day) +VALUES (1, 'Basic', 'Basic plan', 3); + +CREATE TABLE omnivore.service_usage ( + id uuid PRIMARY KEY DEFAULT uuid_generate_v1mc(), + user_id uuid NOT NULL REFERENCES omnivore.user, + action VARCHAR(255) NOT NULL, + created_at timestamptz NOT NULL default current_timestamp +); + +CREATE INDEX ON omnivore.service_usage (user_id); + +CREATE POLICY create_service_usage on omnivore.service_usage + FOR INSERT TO omnivore_user + WITH CHECK (true); + +GRANT SELECT, INSERT ON omnivore.service_usage TO omnivore_user; + +COMMIT; diff --git a/packages/db/migrations/0172.undo.service_usage.sql b/packages/db/migrations/0172.undo.service_usage.sql new file mode 100755 index 000000000..99d324323 --- /dev/null +++ b/packages/db/migrations/0172.undo.service_usage.sql @@ -0,0 +1,15 @@ +-- Type: UNDO +-- Name: service_usage +-- Description: Create table for tracking service usage and enforce limit + +BEGIN; + +DROP TABLE IF EXISTS omnivore.service_usage; + +DROP TABLE IF EXISTS omnivore.subscription_plan; + +ATLER TABLE omnivore.received_emails + DROP COLUMN IF EXISTS reply_to, + DROP COLUMN IF EXISTS reply; + +COMMIT;