From 49fe075361e4f0f3b2a1a561bc01f831983a3efb Mon Sep 17 00:00:00 2001
From: Ruslan Kashapov <ruslan.kashapov@pantheon.tech>
Date: Mon, 11 Jan 2021 16:44:32 +0200
Subject: Decouple anchor from fragment in persistence module

Issue-ID: CPS-161
Change-Id: Ia446b26ee4eca9281e86bd2be3dd6836aa201597
Signed-off-by: Ruslan Kashapov <ruslan.kashapov@pantheon.tech>
---
 cps-ri/src/main/resources/schema.sql | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

(limited to 'cps-ri/src/main/resources/schema.sql')

diff --git a/cps-ri/src/main/resources/schema.sql b/cps-ri/src/main/resources/schema.sql
index 58b3c63c8..d37d932aa 100755
--- a/cps-ri/src/main/resources/schema.sql
+++ b/cps-ri/src/main/resources/schema.sql
@@ -53,18 +53,25 @@ CREATE TABLE IF NOT EXISTS MODULE
     CONSTRAINT MODULE_DATASPACE FOREIGN KEY (DATASPACE_ID) REFERENCES DATASPACE (id) ON UPDATE CASCADE ON DELETE CASCADE
 );
 
+CREATE TABLE IF NOT EXISTS ANCHOR
+(
+    ID BIGSERIAL PRIMARY KEY,
+    NAME TEXT,
+    SCHEMA_SET_ID INTEGER REFERENCES SCHEMA_SET(ID),
+    DATASPACE_ID INTEGER NOT NULL REFERENCES DATASPACE(ID),
+    UNIQUE (DATASPACE_ID, NAME)
+);
+
 CREATE TABLE IF NOT EXISTS FRAGMENT
 (
     ID BIGSERIAL PRIMARY KEY,
     XPATH TEXT NOT NULL,
     ATTRIBUTES JSONB,
-    ANCHOR_NAME TEXT,
-    ANCHOR_ID BIGINT REFERENCES FRAGMENT(ID),
+    ANCHOR_ID BIGINT REFERENCES ANCHOR(ID),
     PARENT_ID BIGINT REFERENCES FRAGMENT(ID),
-    SCHEMA_SET_ID INTEGER REFERENCES SCHEMA_SET(ID),
     DATASPACE_ID INTEGER NOT NULL REFERENCES DATASPACE(ID),
     SCHEMA_NODE_ID INTEGER REFERENCES SCHEMA_NODE(ID),
-    UNIQUE (DATASPACE_ID, ANCHOR_NAME, XPATH)
+    UNIQUE (DATASPACE_ID, ANCHOR_ID, XPATH)
 );
 
 CREATE TABLE IF NOT EXISTS RELATION
@@ -78,9 +85,9 @@ CREATE TABLE IF NOT EXISTS RELATION
 );
 
 CREATE INDEX  IF NOT EXISTS "FKI_FRAGMENT_DATASPACE_ID_FK"     ON FRAGMENT USING BTREE(DATASPACE_ID) ;
-CREATE INDEX  IF NOT EXISTS "FKI_FRAGMENT_SCHEMA_SET_ID_FK"    ON FRAGMENT USING BTREE(SCHEMA_SET_ID) ;
 CREATE INDEX  IF NOT EXISTS "FKI_FRAGMENT_PARENT_ID_FK"        ON FRAGMENT USING BTREE(PARENT_ID) ;
 CREATE INDEX  IF NOT EXISTS "FKI_FRAGMENT_ANCHOR_ID_FK"        ON FRAGMENT USING BTREE(ANCHOR_ID) ;
+CREATE INDEX  IF NOT EXISTS "FKI_ANCHOR_SCHEMA_SET_ID_FK"      ON ANCHOR USING BTREE(SCHEMA_SET_ID) ;
 CREATE INDEX  IF NOT EXISTS "PERF_SCHEMA_NODE_SCHEMA_NODE_ID"  ON SCHEMA_NODE USING BTREE(SCHEMA_NODE_IDENTIFIER) ;
 CREATE INDEX  IF NOT EXISTS "FKI_SCHEMA_NODE_ID_TO_ID"         ON FRAGMENT USING BTREE(SCHEMA_NODE_ID) ;
 CREATE INDEX  IF NOT EXISTS "FKI_RELATION_TYPE_ID_FK"          ON RELATION USING BTREE(RELATION_TYPE_ID);
-- 
cgit 1.2.3-korg

