Skip to content

Commit

Permalink
#2035: PG data model and functions for Dataset
Browse files Browse the repository at this point in the history
* First commit
  • Loading branch information
benedeki committed Apr 19, 2022
1 parent f088de7 commit 2d8a091
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
19 changes: 19 additions & 0 deletions database/src/main/dataset/_.ddl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2018 ABSA Group Limited
*
* 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 SCHEMA IF NOT EXISTS dataset;
ALTER SCHEMA dataset OWNER TO enceladus;

GRANT USAGE ON SCHEMA dataset TO menas;
29 changes: 29 additions & 0 deletions database/src/main/dataset/entities.ddl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2018 ABSA Group Limited
*
* 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.
*/

-- DROP TABLE IF EXISTS dataset.entities;

CREATE TABLE dataset.entities
(
entity_type CHAR NOT NULL DEFAULT 'D',
CONSTRAINT entities_pk PRIMARY KEY (entity_name)
)
INHERITS (entity_base.entities);

ALTER TABLE IF EXISTS dataset.entities
ADD CONSTRAINT check_dataset_entity_type CHECK (entity_type = 'D')
NOT VALID;

ALTER TABLE dataset.entities OWNER to enceladus;
51 changes: 51 additions & 0 deletions database/src/main/dataset/list.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2018 ABSA Group Limited
*
* 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 OR REPLACE FUNCTION dataset.list(
IN i_include_disabled BOOLEAN DEFAULT FALSE,
OUT entity_name TEXT,
OUT entity_latest_version INTEGER,
OUT locked BOOLEAN,
OUT disabled BOOLEAN
) RETURNS SETOF record AS
$$
-------------------------------------------------------------------------------
--
-- Function: dataset.list(1)
-- Returns a list of schemas with their latest versions
--
-- Parameters:
-- i_include_disabled - flag indicating if to include disabled schemas too
--
-- Returns:
-- entity_name - name of the schema
-- entity_latest_version - the latest version of the schema
-- locked - signals if the schema is locked or not
-- disabled - signals if the schema is disabled or not
--
-------------------------------------------------------------------------------
DECLARE
BEGIN
RETURN QUERY
SELECT E.entity_name, E.entity_latest_version, E.disabled_at IS NOT NULL, E.locked_at IS NOT NULL
FROM dataset.entities E
WHERE i_include_disabled OR E.disabled_at IS NULL
ORDER BY entity_name; --TODO Include order by?
END;
$$
LANGUAGE plpgsql VOLATILE SECURITY DEFINER;

ALTER FUNCTION dataset.list(BOOLEAN) OWNER TO enceladus;
GRANT EXECUTE ON FUNCTION dataset.list(BOOLEAN) TO menas;
29 changes: 29 additions & 0 deletions database/src/main/dataset/versions.ddl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2018 ABSA Group Limited
*
* 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.
*/

-- DROP TABLE IF EXISTS dataset.versions;

CREATE TABLE dataset.versions
(
source_path TEXT,
publish_path TEXT,
CONSTRAINT versions_pk PRIMARY KEY (id_entity_version)
)
INHERITS (entity_base.versions);

ALTER TABLE dataset.versions
ADD CONSTRAINT versions_unq UNIQUE (entity_name, entity_version);

ALTER TABLE dataset.versions OWNER to enceladus;

0 comments on commit 2d8a091

Please sign in to comment.