Skip to content

Commit

Permalink
feat(gateway): Event DB respec WIP. Registration tables work.
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenj committed Nov 23, 2023
1 parent 5bafe1c commit 0120e03
Show file tree
Hide file tree
Showing 5 changed files with 344 additions and 289 deletions.
45 changes: 23 additions & 22 deletions catalyst-gateway/event-db/migrations/V1__config_tables.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Catalyst Event Database
-- Catalyst Voices Database - Configuration Data
-- sqlfluff:dialect:postgres

-- Configuration Tables
Expand All @@ -17,30 +17,29 @@ COMMENT ON TABLE refinery_schema_history IS
'History of Schema Updates to the Database.
Managed by the `refinery` cli tool.';

-- -------------------------------------------------------------------------------------------------

-- All known Json Schema Types
CREATE TABLE json_schema_type_names (
id TEXT PRIMARY KEY
);

COMMENT ON TABLE json_schema_type_names IS 'All known Json Schema Type Names.';

-- Known Schema Type Names are inserted when the Table which uses that type is created.


-- -------------------------------------------------------------------------------------------------

-- Json Schema Library
-- Json Schemas used to validate the contents of JSONB fields in this database.
-- * `id,type,name` matches the $id URI in the schema itself.
-- * The URI format is customized and is of the form `catalyst_schema://<id>/<type>/<name>`
-- * Schemas will be added here automatically during migration of the database, or interactively
-- * during operation of the system.
-- * They should match the schema they hold, and on read they should be validated.
-- * The code should refuse to serve or use any schema that does not match.
-- *
-- * id - This is unique and can uniquely identify any schema.
-- * type - This allows us to find all schemas of a known type.
-- * name - This is the unique name of the schema. `id` always equals the same `type/name`.
-- * for convention `type` and `name` string should only used a-z,0-9 and underscore.
-- * Dashes, symbols or upper case should not be used.
-- Catalyst Event Database
CREATE TABLE json_schema_type (
id UUID PRIMARY KEY,
type TEXT NOT NULL,
name TEXT NOT NULL,
schema JSONB NOT NULL,

FOREIGN KEY ("type") REFERENCES json_schema_type_names (id) ON DELETE CASCADE
schema JSONB NOT NULL
);

CREATE INDEX json_schema_type_idx ON json_schema_type ("type");
Expand All @@ -51,15 +50,15 @@ COMMENT ON TABLE json_schema_type IS

COMMENT ON COLUMN json_schema_type.id IS
'Synthetic Unique ID for each json_schema_type (UUIDv4).
Must match $id URI inside the schema.';
Must match the `UUID` component of the $id URI inside the schema.';
COMMENT ON COLUMN json_schema_type.type IS
'The type of the json schema type.
eg. "event"
Must match $id URI inside the schema.';
Must match the `type` component of the $id URI inside the schema.';
COMMENT ON COLUMN json_schema_type.name IS
'The name of the json schema type.
eg. "catalyst_v1"
Must match $id URI inside the schema.';
Must match the `name` component of the $id URI inside the schema.';

-- Known Schema Types are inserted when the Table which uses that type is created.
-- Or can be added by migrations as the database evolves.
Expand Down Expand Up @@ -111,10 +110,12 @@ COMMENT ON INDEX config_idx IS
'We use three keys combined uniquely rather than forcing string concatenation
at the app level to allow for querying groups of data.';

-- Add Config Schemas to the known schema types.
INSERT INTO json_schema_type_names (id)
VALUES
('config');-- Configuration Data Schemas

-- -------------------------------------------------------------------------------------------------

-- * Temporary.
-- * Insert known json schema manually until automated json schema migration scripting is added.
-- * This will be removed in the future.

-- Add the Initial Schemas for configuration.
INSERT INTO json_schema_type (id, type, name, schema)
Expand Down
35 changes: 34 additions & 1 deletion catalyst-gateway/event-db/migrations/V2__event_tables.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
-- Catalyst Event Database
-- Catalyst Voices Database - Event Data
-- sqlfluff:dialect:postgres

-- `id` mapping.
-- * `id` is a UUID, in the past it was an auto incrementing value.
-- * it is changed to a UUID so that the data can be generated independently and it is more friendly
-- * and simpler for a decentralized or distributed system to safely create UUID than incremental number.
-- * However we need compatibility with the rpe-existing incremental numbers.
-- * Accordingly we will abuse the UUID format.
-- * A V8 UUID will be defined where the low 32 bits are 0s.
-- * If the ID is an incremental ID it will be mapped to this special uuid, by or-ing the incremental ID
-- * with the mapping UUID, creating a compatible UUID.
-- * As ALL autogenerated UUID's will be type 4, there is no possibility of collision.
--
-- The Mapping UUID is defined as: `164fba58-31ff-8084-96cb-eb9d00000000`


-- Event Tables

-- -------------------------------------------------------------------------------------------------

CREATE TABLE event_type (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
Expand Down Expand Up @@ -50,10 +69,24 @@ VALUES
(SELECT jsonb FROM pg_read_file('../json_schemas/event/description/catalyst_v1.json'))
);

-- Define a Catalyst V1 Event.

INSERT INTO event_type (name, description_schema, data_schema)
VALUES
(
'Catalyst V1',
'd899cd44-3513-487b-ab46-fdca662a724d',
'9c5df318-fa9a-4310-80fa-490f46d1cc43'
);

-- -------------------------------------------------------------------------------------------------

-- Event Table - Defines each voting or decision event
CREATE TABLE "event" (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
-- The Organizer/Administrator of this event.
-- Update once RBAC is defined, as Organizer is an RBAC Role.
organizer TEXT NOT NULL,
type UUID REFERENCES event_type (id),
name TEXT NOT NULL,
description JSONB NOT NULL,
Expand Down
141 changes: 0 additions & 141 deletions catalyst-gateway/event-db/migrations/V3__objective_tables.sql

This file was deleted.

Loading

0 comments on commit 0120e03

Please sign in to comment.