Skip to content

Commit

Permalink
Define DB schema for bundles and subscriptions (#2670)
Browse files Browse the repository at this point in the history
Relates to #2543

Define the subscriptions and bundles table. Add a foreign key reference
to subscriptions in both the profiles and rule type tables.
  • Loading branch information
dmjb authored Mar 15, 2024
1 parent fafc696 commit be6e703
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 24 deletions.
19 changes: 19 additions & 0 deletions database/migrations/000029_bundle_subscription.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- Copyright 2024 Stacklok, Inc
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

ALTER TABLE projects DROP COLUMN subscription_id;
ALTER TABLE rule_type DROP COLUMN subscription_id;

DROP TABLE IF EXISTS subscriptions;
DROP TABLE IF EXISTS bundles;
43 changes: 43 additions & 0 deletions database/migrations/000029_bundle_subscription.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
-- Copyright 2024 Stacklok, Inc
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

CREATE TABLE bundles(
-- I have given this a separate PK to simplify some of the queries
-- the combination of the remaining columns are unique
id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
namespace TEXT NOT NULL,
name TEXT NOT NULL,
UNIQUE (namespace, name)
);

CREATE TABLE subscriptions(
-- same comment as for PK of `bundles`
id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
project_id UUID NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
-- this FK is not ON DELETE CASCADE
-- this prevents deletion of bundles which are still in use
bundle_id UUID NOT NULL REFERENCES bundles(id),
current_version TEXT NOT NULL,
UNIQUE (bundle_id, project_id)
);

-- none of these FKs are ON DELETE CASCADE
-- prevents deletion of an in-use subscription
ALTER TABLE projects
ADD COLUMN subscription_id UUID DEFAULT NULL
REFERENCES subscriptions(id);

ALTER TABLE rule_type
ADD COLUMN subscription_id UUID DEFAULT NULL
REFERENCES subscriptions(id);
37 changes: 26 additions & 11 deletions internal/db/models.go

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

24 changes: 16 additions & 8 deletions internal/db/projects.sql.go

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

15 changes: 10 additions & 5 deletions internal/db/rule_types.sql.go

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

0 comments on commit be6e703

Please sign in to comment.