Skip to content

Commit

Permalink
Add migration for data sources. (#5008)
Browse files Browse the repository at this point in the history
This migration adds support for three additional tables,
`data_sources` and `data_sources_functions` containing the definition
of a single data source in a many-to-one relationship, and a
`rule_type_data_sources` table representing the many-to-many
relationship between rule types and data sources.
  • Loading branch information
blkt authored Nov 21, 2024
1 parent 3cc4ae8 commit 64aec6a
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
10 changes: 10 additions & 0 deletions database/migrations/000108_data_sources.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- SPDX-FileCopyrightText: Copyright 2024 The Minder Authors
-- SPDX-License-Identifier: Apache-2.0

BEGIN;

DROP TABLE rule_type_data_sources;
DROP TABLE data_sources_functions;
DROP TABLE data_sources;

COMMIT;
60 changes: 60 additions & 0 deletions database/migrations/000108_data_sources.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
-- SPDX-FileCopyrightText: Copyright 2024 The Minder Authors
-- SPDX-License-Identifier: Apache-2.0

BEGIN;

-- This migration adds storage support for data sources. The only
-- constraints we enforce at the database layer are
--
-- * functions can only reference one data source, and must be deleted
-- if the data source is deleted
-- * data sources are tied to a project, and must be deleted if the
-- project is deleted
-- * rule types can reference one or more data source, and we want to
-- prevent deletion of a data source if there's a rule type
-- referencing it
--
-- The first two are simple foreign keys, while the third one is
-- enforced by the lack of `ON DELETE ...` clause in the
-- `rule_type_data_sources` table.
--
-- We also want to prevent the creation of a data source with a given
-- name if another data source with the same name exists in the
-- project hierarchy. I'm not sure how to express this as a database
-- constraint, nor I believe this would be efficient, so we decided to
-- let the application layer enforce that as we do with profiles.

CREATE TABLE data_sources(
id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
name TEXT NOT NULL,
display_name TEXT NOT NULL,
project_id UUID NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE
);

CREATE UNIQUE INDEX data_sources_name_lower_idx ON data_sources (project_id, lower(name));

CREATE TABLE data_sources_functions(
id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
name TEXT NOT NULL,
type TEXT NOT NULL,
data_source_id UUID NOT NULL,
definition JSONB NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
FOREIGN KEY (data_source_id) REFERENCES data_sources(id) ON DELETE CASCADE
);

CREATE UNIQUE INDEX data_sources_functions_name_lower_idx ON data_sources_functions (data_source_id, lower(name));

CREATE TABLE rule_type_data_sources(
rule_type_id UUID NOT NULL,
data_sources_id UUID NOT NULL,
FOREIGN KEY (rule_type_id) REFERENCES rule_type(id),
FOREIGN KEY (data_sources_id) REFERENCES data_sources(id),
UNIQUE (rule_type_id, data_sources_id)
);

COMMIT;
24 changes: 24 additions & 0 deletions internal/db/models.go

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

0 comments on commit 64aec6a

Please sign in to comment.