Skip to content

Commit

Permalink
add CONSTRAINT to volume_resource_usage table to separate two enum uses
Browse files Browse the repository at this point in the history
add comment to table as well
  • Loading branch information
jmpesp committed Oct 16, 2024
1 parent 89744ea commit bd2ef84
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
24 changes: 23 additions & 1 deletion schema/crdb/crucible-ref-count-records/up04.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,27 @@ CREATE TABLE IF NOT EXISTS omicron.public.volume_resource_usage (
region_snapshot_region_id UUID,
region_snapshot_snapshot_id UUID,

PRIMARY KEY (usage_id)
PRIMARY KEY (usage_id),

CONSTRAINT exactly_one_usage_source CHECK (
(
(usage_type = 'read_only_region') AND
(region_id IS NOT NULL) AND
(
region_snapshot_dataset_id IS NULL AND
region_snapshot_region_id IS NULL AND
region_snapshot_snapshot_id IS NULL
)
)
OR
(
(usage_type = 'region_snapshot') AND
(region_id IS NOT NULL) AND
(
region_snapshot_dataset_id IS NOT NULL AND
region_snapshot_region_id IS NOT NULL AND
region_snapshot_snapshot_id IS NOT NULL
)
)
)
);
37 changes: 36 additions & 1 deletion schema/crdb/dbinit.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4489,20 +4489,55 @@ CREATE TYPE IF NOT EXISTS omicron.public.volume_resource_usage_type AS ENUM (
'region_snapshot'
);

/*
* This table records when a Volume makes use of a read-only resource. When
* there are no more entries for a particular read-only resource, then that
* resource can be garbage collected.
*/
CREATE TABLE IF NOT EXISTS omicron.public.volume_resource_usage (
usage_id UUID NOT NULL,

volume_id UUID NOT NULL,

usage_type omicron.public.volume_resource_usage_type NOT NULL,

/*
* This column contains a non-NULL value when the usage type is read_only
* region
*/
region_id UUID,

/*
* These columns contain non-NULL values when the usage type is region
* snapshot
*/
region_snapshot_dataset_id UUID,
region_snapshot_region_id UUID,
region_snapshot_snapshot_id UUID,

PRIMARY KEY (usage_id)
PRIMARY KEY (usage_id),

CONSTRAINT exactly_one_usage_source CHECK (
(
(usage_type = 'read_only_region') AND
(region_id IS NOT NULL) AND
(
region_snapshot_dataset_id IS NULL AND
region_snapshot_region_id IS NULL AND
region_snapshot_snapshot_id IS NULL
)
)
OR
(
(usage_type = 'region_snapshot') AND
(region_id IS NOT NULL) AND
(
region_snapshot_dataset_id IS NOT NULL AND
region_snapshot_region_id IS NOT NULL AND
region_snapshot_snapshot_id IS NOT NULL
)
)
)
);

CREATE INDEX IF NOT EXISTS lookup_volume_resource_usage_by_region on omicron.public.volume_resource_usage (
Expand Down

0 comments on commit bd2ef84

Please sign in to comment.