91e2606bb85397a350bdd6f4b027bf3bb1552c5c
[smo/teiv.git] /
1 --
2 -- ============LICENSE_START=======================================================
3 -- Copyright (C) 2024 Ericsson
4 -- Modifications Copyright (C) 2024 OpenInfra Foundation Europe
5 -- ================================================================================
6 -- Licensed under the Apache License, Version 2.0 (the "License");
7 -- you may not use this file except in compliance with the License.
8 -- You may obtain a copy of the License at
9 --
10 --       http://www.apache.org/licenses/LICENSE-2.0
11 --
12 -- Unless required by applicable law or agreed to in writing, software
13 -- distributed under the License is distributed on an "AS IS" BASIS,
14 -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 -- See the License for the specific language governing permissions and
16 -- limitations under the License.
17 --
18 -- SPDX-License-Identifier: Apache-2.0
19 -- ============LICENSE_END=========================================================
20 --
21
22
23 BEGIN;
24
25 CREATE EXTENSION IF NOT EXISTS postgis;
26 CREATE EXTENSION IF NOT EXISTS postgis_topology;
27
28 GRANT USAGE ON SCHEMA topology to :pguser;
29 GRANT SELECT ON ALL SEQUENCES IN SCHEMA topology TO :pguser;
30 GRANT SELECT ON ALL TABLES IN SCHEMA topology TO :pguser;
31
32 CREATE SCHEMA IF NOT EXISTS ties_data;
33 ALTER SCHEMA ties_data OWNER TO :pguser;
34 SET default_tablespace = '';
35 SET default_table_access_method = heap;
36
37 SET ROLE :'pguser';
38
39 -- Function to create CONSTRAINT only if it does not exists
40 CREATE OR REPLACE FUNCTION ties_data.create_constraint_if_not_exists (
41         t_name TEXT, c_name TEXT, constraint_sql TEXT
42 )
43 RETURNS void AS
44 $$
45 BEGIN
46         IF NOT EXISTS (SELECT constraint_name FROM information_schema.table_constraints WHERE table_name = t_name AND constraint_name = c_name) THEN
47                 EXECUTE constraint_sql;
48         END IF;
49 END;
50 $$ language 'plpgsql';
51
52 -- Update data schema exec status
53 INSERT INTO ties_model.entity_info("schema", "status") VALUES ('ties_data', 'success');
54
55 CREATE TABLE IF NOT EXISTS ties_data."Sector" (
56         "id"                     VARCHAR(511),
57         "azimuth"                       DECIMAL,
58         "sectorId"                       jsonb,
59         "geo-location"                  "geography"
60 );
61
62 CREATE TABLE IF NOT EXISTS ties_data."Namespace" (
63         "id"                     VARCHAR(511),
64         "name"                  TEXT
65 );
66
67 CREATE TABLE IF NOT EXISTS ties_data."REL_serviced-sector_serving-namespace" (
68         "id"                    VARCHAR(511),
69         "aSide_Sector"                  VARCHAR(511),
70         "bSide_Namespace"                       VARCHAR(511)
71 );
72
73 SELECT ties_data.create_constraint_if_not_exists(
74         'Sector',
75  'PK_Sector_id',
76  'ALTER TABLE ties_data."Sector" ADD CONSTRAINT "PK_Sector_id" PRIMARY KEY ("id");'
77 );
78
79 SELECT ties_data.create_constraint_if_not_exists(
80         'Namespace',
81  'PK_Namespace_id',
82  'ALTER TABLE ties_data."Namespace" ADD CONSTRAINT "PK_Sector_id" PRIMARY KEY ("id");'
83 );
84
85 SELECT ties_data.create_constraint_if_not_exists(
86         'REL_serviced-sector_serving-namespace',
87  'PK_REL_serviced-sector_serving-namespace_id',
88  'ALTER TABLE ties_data."REL_serviced-sector_serving-namespace" ADD CONSTRAINT "PK_REL_serviced-sector_serving-namespace_id" PRIMARY KEY ("id");'
89 );
90
91 SELECT ties_data.create_constraint_if_not_exists(
92         'REL_serviced-sector_serving-namespace',
93  'FK_REL_serviced-sector_serving-namespace_aSide_Sector',
94  'ALTER TABLE ties_data."REL_serviced-sector_serving-namespace" ADD CONSTRAINT FK_REL_serviced-sector_serving-namespace_aSide_Sector FOREIGN KEY ("aSide_Sector") REFERENCES ties_data."Sector" (id) ON DELETE CASCADE;')
95
96 SELECT ties_data.create_constraint_if_not_exists(
97         'REL_serviced-sector_serving-namespace',
98  'FK_REL_serviced-sector_serving-namespace_bSide_Namespace',
99  'ALTER TABLE ties_data."REL_serviced-sector_serving-namespace" ADD CONSTRAINT FK_REL_serviced-sector_serving-namespace_bSide_Namespace FOREIGN KEY ("bSide_Namespace") REFERENCES ties_data."Namespace" (id) ON DELETE CASCADE;')
100
101 COMMIT;