]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
DRYD-1372: Add numberofobjects to objectcountgroup upgrade script. (#403)
authorRay Lee <ray.lee@lyrasis.org>
Sat, 16 Mar 2024 01:41:19 +0000 (21:41 -0400)
committerGitHub <noreply@github.com>
Sat, 16 Mar 2024 01:41:19 +0000 (21:41 -0400)
src/main/resources/db/postgresql/upgrade/8.0.0/post-init/01_collectionobject_fieldcollectionplace.sql [moved from src/main/resources/db/postgresql/upgrade/8.0.0/post-init/01_collectionobject.sql with 100% similarity]
src/main/resources/db/postgresql/upgrade/8.0.0/post-init/02_collectionobject_numberofobjects.sql [new file with mode: 0644]

diff --git a/src/main/resources/db/postgresql/upgrade/8.0.0/post-init/02_collectionobject_numberofobjects.sql b/src/main/resources/db/postgresql/upgrade/8.0.0/post-init/02_collectionobject_numberofobjects.sql
new file mode 100644 (file)
index 0000000..8240957
--- /dev/null
@@ -0,0 +1,76 @@
+-- Upgrade collectionobject. Move number of objects into the new repeating object count group (DRYD-1372).
+
+DO $$
+DECLARE
+    trow record;
+    maxpos int;
+    uuid varchar(36);
+BEGIN
+
+    -- For new install, if collectionobjects_common.numberofobjects does not exist, there is nothing to migrate.
+
+    IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='collectionobjects_common' AND column_name='numberofobjects') THEN
+        FOR trow IN
+            -- Get record in collectionobjects_common that does not have an existing/matching record in objectcountgroup:
+
+            SELECT id AS parentid, numberofobjects
+            FROM public.collectionobjects_common
+            WHERE id NOT IN (
+                SELECT cc.id
+                FROM public.collectionobjects_common cc
+                JOIN public.hierarchy h ON (cc.id = h.parentid)
+                JOIN public.objectcountgroup ocg ON (
+                    h.id = ocg.id
+                    AND cc.numberofobjects IS NOT DISTINCT FROM ocg.objectcount
+                )
+            )
+            AND (numberofobjects IS NOT NULL)
+
+            LOOP
+                -- Get max pos value for the collectionobject record's object count group, and generate a new uuid:
+
+                SELECT
+                    coalesce(max(pos), -1),
+                    uuid_generate_v4()::varchar
+                INTO
+                    maxpos,
+                    uuid
+                FROM public.hierarchy
+                WHERE parentid = trow.parentid
+                    AND name = 'collectionobjects_common:objectCountGroupList'
+                    AND primarytype = 'objectCountGroup';
+
+                -- Insert new record into hierarchy table first, due to foreign key on objectcountgroup table:
+
+                INSERT INTO public.hierarchy (
+                    id,
+                    parentid,
+                    pos,
+                    name,
+                    isproperty,
+                    primarytype)
+                VALUES (
+                    uuid,
+                    trow.parentid,
+                    maxpos + 1,
+                    'collectionobjects_common:objectCountGroupList',
+                    TRUE,
+                    'objectCountGroup');
+
+                -- Migrate collectionobject number of objects data into objectcountgroup table:
+
+                INSERT INTO public.objectcountgroup (
+                    id,
+                    objectcount)
+                VALUES (
+                    uuid,
+                    trow.numberofobjects);
+
+            END LOOP;
+
+    ELSE
+        RAISE NOTICE 'No v7.2 collectionobject number of objects data to migrate: collectionobjects_common.numberofobjects does not exist';
+
+    END IF;
+END
+$$;
\ No newline at end of file