Skip to content

Commit

Permalink
Use nicer table names, establish better events, and resolve #27
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkula committed Jun 1, 2022
1 parent cc29b48 commit deac3df
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 123 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
table:
schema: public
name: play_status
name: deck
object_relationships:
- name: workspace
using:
Expand All @@ -9,10 +9,10 @@ array_relationships:
- name: queue
using:
foreign_key_constraint_on:
column: status_id
column: deck_id
table:
schema: public
name: play_queue_entry
name: track
insert_permissions:
- role: anonymous
permission:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
table:
schema: public
name: play_status_type_enum
name: deck_type_enum
is_enum: true
18 changes: 17 additions & 1 deletion hasura/metadata/databases/default/tables/public_event.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
table:
schema: public
name: event
object_relationships:
- name: deck
using:
foreign_key_constraint_on: deck_id
- name: file
using:
foreign_key_constraint_on: file_id
- name: track
using:
foreign_key_constraint_on: track_id
- name: workspace
using:
foreign_key_constraint_on: workspace_id
select_permissions:
- role: anonymous
permission:
columns:
- deck_id
- file_id
- id
- invalidate
- time
- track_id
- type
- workspace_id
filter: {}
allow_aggregations: true

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
table:
schema: public
name: play_queue_entry
name: track
object_relationships:
- name: deck
using:
foreign_key_constraint_on: deck_id
- name: file
using:
foreign_key_constraint_on: file_id
- name: status
using:
foreign_key_constraint_on: status_id
insert_permissions:
- role: anonymous
permission:
check: {}
columns:
- file_id
- status_id
- deck_id
backend_only: false
select_permissions:
- role: anonymous
Expand Down
21 changes: 6 additions & 15 deletions hasura/metadata/databases/default/tables/public_workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,27 @@ table:
schema: public
name: workspace
array_relationships:
- name: files
- name: decks
using:
foreign_key_constraint_on:
column: workspace_id
table:
schema: public
name: file
- name: jobs
name: deck
- name: files
using:
foreign_key_constraint_on:
column: workspace_id
table:
schema: public
name: job
- name: play_statuses
name: file
- name: jobs
using:
foreign_key_constraint_on:
column: workspace_id
table:
schema: public
name: play_status
computed_fields:
- name: main_play_id
definition:
function:
schema: public
name: get_workspace_main_id
comment: Gets the main play id of this workspace (if it exists)
name: job
insert_permissions:
- role: anonymous
permission:
Expand All @@ -45,8 +38,6 @@ select_permissions:
- id
- name
- updated_at
computed_fields:
- main_play_id
filter: {}
update_permissions:
- role: anonymous
Expand Down
6 changes: 3 additions & 3 deletions hasura/metadata/databases/default/tables/tables.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
- "!include public_deck.yaml"
- "!include public_deck_type_enum.yaml"
- "!include public_delete_job.yaml"
- "!include public_event.yaml"
- "!include public_file.yaml"
- "!include public_file_type_enum.yaml"
- "!include public_job.yaml"
- "!include public_play_queue_entry.yaml"
- "!include public_play_status.yaml"
- "!include public_play_status_type_enum.yaml"
- "!include public_track.yaml"
- "!include public_workspace.yaml"
23 changes: 23 additions & 0 deletions hasura/migrations/default/0_init/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
DROP TABLE public.delete_job;

DROP TABLE public.job;

DROP TABLE public.track;

DROP TRIGGER ensure_main_deck_trigger ON public.deck;
DROP FUNCTION public.ensure_main_deck();

DROP TABLE public.deck;

DROP TABLE public.deck_type_enum;

DROP TRIGGER reconcile_ordering ON public.file;
DROP FUNCTION public.order_is_reconciled();
DROP FUNCTION public.reconcile_ordering();

DROP TABLE public.file;

DROP TABLE public.file_type_enum;

DROP TABLE public.workspace;

Original file line number Diff line number Diff line change
Expand Up @@ -101,26 +101,26 @@ EXECUTE PROCEDURE public.reconcile_ordering();
COMMENT ON TRIGGER reconcile_ordering ON public.file
IS 'trigger to set value of column updated_at to current timestamp on row update';

CREATE TABLE public.play_status_type_enum
CREATE TABLE public.deck_type_enum
(
value text NOT NULL,
description text NOT NULL,
PRIMARY KEY (value),
UNIQUE (value)
);

INSERT INTO public.play_status_type_enum(value, description)
INSERT INTO public.deck_type_enum(value, description)
VALUES (E'main', E'the main player');

INSERT INTO public.play_status_type_enum(value, description)
INSERT INTO public.deck_type_enum(value, description)
VALUES (E'ambience', E'any ambience');

INSERT INTO public.play_status_type_enum(value, description)
INSERT INTO public.deck_type_enum(value, description)
VALUES (E'sfx', E'any ambience');



CREATE TABLE public.play_status
CREATE TABLE public.deck
(
id uuid NOT NULL DEFAULT gen_random_uuid(),
workspace_id uuid NOT NULL,
Expand All @@ -132,52 +132,63 @@ CREATE TABLE public.play_status
type text NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (workspace_id) REFERENCES public.workspace (id) ON UPDATE cascade ON DELETE cascade,
FOREIGN KEY (type) REFERENCES public.play_status_type_enum (value) ON UPDATE cascade ON DELETE restrict,
FOREIGN KEY (type) REFERENCES public.deck_type_enum (value) ON UPDATE cascade ON DELETE restrict,
UNIQUE (id),
CONSTRAINT volume_in_range CHECK (volume >= 0 AND volume <= 1),
CONSTRAINT speed_in_range CHECK (speed > 0)
);
COMMENT ON TABLE public.play_status IS E'workspace status';
COMMENT ON TABLE public.deck IS E'one set of tracks playing';
CREATE EXTENSION IF NOT EXISTS pgcrypto;

CREATE OR REPLACE FUNCTION public.ensure_main_entry()
CREATE OR REPLACE FUNCTION public.ensure_main_deck()
RETURNS TRIGGER AS
$$
BEGIN
DELETE FROM play_status WHERE type = 'main' AND workspace_id = NEW.workspace_id;
DELETE FROM deck WHERE type = 'main' AND workspace_id = NEW.workspace_id;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER ensure_main_entry_trigger
CREATE TRIGGER ensure_main_deck_trigger
BEFORE INSERT
ON play_status
ON deck
FOR EACH ROW
WHEN (NEW.type = 'main')
EXECUTE FUNCTION public.ensure_main_entry();
EXECUTE FUNCTION public.ensure_main_deck();

CREATE TABLE public.play_queue_entry
CREATE OR REPLACE FUNCTION public.update_pause()
RETURNS TRIGGER AS
$$
DECLARE
_new deck;
BEGIN
_new = NEW;
IF OLD.pause_timestamp IS NOT NULL AND NEW.pause_timestamp IS NULL AND
OLD.start_timestamp = NEW.start_timestamp THEN
_new.start_timestamp = NEW.start_timestamp + (now() - NEW.start_timestamp);
END IF;
RETURN _new;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER update_paused_track
BEFORE UPDATE OF pause_timestamp
ON deck
FOR EACH ROW
EXECUTE FUNCTION public.update_pause();

CREATE TABLE public.track
(
id uuid NOT NULL DEFAULT gen_random_uuid(),
file_id uuid NOT NULL,
status_id uuid NOT NULL,
deck_id uuid NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (id),
FOREIGN KEY (file_id) REFERENCES public.file (id) ON UPDATE cascade ON DELETE cascade,
FOREIGN KEY (status_id) REFERENCES public.play_status (id) ON UPDATE cascade ON DELETE cascade
FOREIGN KEY (deck_id) REFERENCES public.deck (id) ON UPDATE cascade ON DELETE cascade
);
CREATE EXTENSION IF NOT EXISTS pgcrypto;

CREATE FUNCTION public.get_workspace_main_id(ws_row workspace)
RETURNS uuid AS
$$
SELECT id
FROM public.play_status
WHERE workspace_id = ws_row.id
AND type = 'main'
ORDER BY ws_row.created_at DESC
$$ LANGUAGE sql STABLE;

CREATE TABLE public.job
(
id uuid NOT NULL DEFAULT gen_random_uuid(),
Expand All @@ -203,40 +214,4 @@ CREATE TABLE public.delete_job
assigned_worker uuid DEFAULT NULL,
PRIMARY KEY (id),
FOREIGN KEY (file_id) REFERENCES public.file (id) ON UPDATE restrict ON DELETE cascade
);

CREATE TABLE public.event
(
id uuid NOT NULL DEFAULT gen_random_uuid(),
time timestamptz NOT NULL DEFAULT now(),
workspace_id uuid NOT NULL,
invalidate text NOT NULL,
FOREIGN KEY (workspace_id) REFERENCES public.workspace (id) ON UPDATE restrict ON DELETE cascade
);

CREATE OR REPLACE FUNCTION public.create_event()
RETURNS TRIGGER AS
$$
BEGIN
INSERT INTO event (workspace_id, invalidate) VALUES (NEW.workspace_id, tg_argv[0]);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER status_update_event
AFTER UPDATE OR INSERT
ON public.play_status
FOR EACH ROW
EXECUTE FUNCTION public.create_event('play_status');

CREATE TRIGGER file_update_event
AFTER UPDATE OR INSERT
ON public.file
FOR EACH ROW
EXECUTE FUNCTION public.create_event('file');

CREATE TRIGGER job_update_event
AFTER UPDATE OR INSERT
ON public.job
FOR EACH ROW
EXECUTE FUNCTION public.create_event('job');
);
30 changes: 0 additions & 30 deletions hasura/migrations/default/1653299124642_squashed/down.sql

This file was deleted.

17 changes: 17 additions & 0 deletions hasura/migrations/default/1_events/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
DROP TRIGGER file_update_trigger_delete ON public.file;
DROP TRIGGER file_update_trigger_update ON public.file;
DROP TRIGGER file_update_trigger_insert ON public.file;

DROP TRIGGER track_update_trigger_delete ON public.track;
DROP TRIGGER track_update_trigger_update ON public.track;
DROP TRIGGER track_update_trigger_insert ON public.track;

DROP TRIGGER deck_update_trigger_delete ON public.deck;
DROP TRIGGER deck_update_trigger_update ON public.deck;
DROP TRIGGER deck_update_trigger_insert ON public.deck;

DROP FUNCTION public.create_file_update();
DROP FUNCTION public.create_track_update();
DROP FUNCTION public.create_deck_update();

DROP TABLE public.event;
Loading

0 comments on commit deac3df

Please sign in to comment.