Skip to content

Update species migration to work with new uuids#462

Merged
nickpalladino merged 3 commits into
developfrom
feature/BI-2622
May 22, 2025
Merged

Update species migration to work with new uuids#462
nickpalladino merged 3 commits into
developfrom
feature/BI-2622

Conversation

@nickpalladino
Copy link
Copy Markdown
Member

@nickpalladino nickpalladino commented May 6, 2025

Description

Story: B-2622

This migration change assumes you're starting with a database that has no entries in the crop table or data has been migrated using this:

-- cleanup duplicate Strawberry crop record on qa test
delete from crop where id = '30';

-- define utility function to update all user references
CREATE OR REPLACE FUNCTION public.replace_anonymous_user()
    RETURNS void LANGUAGE plpgsql AS $$
DECLARE
    r  record;
    tgt uuid := 'AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA'::uuid;
    src text := 'anonymousUser';
BEGIN
    FOR r IN
        SELECT table_schema, table_name, column_name
        FROM   information_schema.columns
        WHERE  table_schema = 'public'
          AND  column_name  = 'auth_user_id'
          AND  data_type IN ('uuid','text','character varying')
        LOOP
            RAISE NOTICE 'Updating %.% -> %', r.table_schema, r.table_name, r.column_name;
            EXECUTE format('UPDATE %I.%I SET %I = $1 WHERE %I = $2',
                           r.table_schema, r.table_name, r.column_name, r.column_name)
                USING tgt, src;
        END LOOP;
END $$;

-- run function
SELECT public.replace_anonymous_user();

-- create table of crop foreign keys
CREATE TEMP TABLE crop_fk_columns AS
SELECT
    tc.table_schema,
    tc.table_name,
    kcu.column_name,
    tc.constraint_name
FROM   information_schema.table_constraints tc
           JOIN   information_schema.key_column_usage  kcu
                  ON tc.constraint_name = kcu.constraint_name
WHERE  tc.constraint_type = 'FOREIGN KEY'
  AND  tc.table_schema = 'public'
  AND  tc.constraint_schema = 'public'
  AND  tc.constraint_name IN (
    SELECT conname
    FROM   pg_constraint
    WHERE  confrelid = 'public.crop'::regclass
);

-- verify this matches migrate_crops.sql
select * from crop_fk_columns;


CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

-- migrate old crop id references to new uuids
BEGIN;

-- 2.1  Add a uuid column to public.crop
ALTER TABLE public.crop ADD COLUMN new_id uuid;

-- 2.2  Fill new_id with a generated UUID for each row
-- The namespace in the seeding script
DO $$
DECLARE
    ns  constant uuid := '9a4deca9-4068-46a3-9efe-db0c181f491a';
BEGIN
    /* -----------------------------------------------------------
       Normalise names exactly the same way the seeder does:
       1. lower‑case                → lower()
       2. trim leading/trailing     → trim()
       3. remove ALL whitespace     → regexp_replace(...,'\s','','g')
    ----------------------------------------------------------- */
    UPDATE public.crop c
    SET    new_id =
               uuid_generate_v5(
                       ns,
                       regexp_replace(lower(trim(c.crop_name)), '\s', '', 'g')
               )
    WHERE  c.new_id IS NULL;        -- just in case you run it twice
END $$;

-- 2.3  Build a mapping table for easy joins (optional to keep)
CREATE TEMP TABLE crop_id_map AS
SELECT id         AS old_id,
       new_id     AS new_id
FROM   public.crop;

-- 2.4  Drop FK constraints so we can rewrite ids in child tables
DO $drop_fks$
    DECLARE
        r record;
    BEGIN
        FOR r IN SELECT * FROM crop_fk_columns LOOP
                EXECUTE format(
                        'ALTER TABLE %I.%I DROP CONSTRAINT %I;',
                        r.table_schema, r.table_name, r.constraint_name);
            END LOOP;
    END $drop_fks$;

-- 2.5  Rewrite crop_id in every child table
DO $update_children$
    DECLARE
        r record;
    BEGIN
        FOR r IN SELECT * FROM crop_fk_columns LOOP
                RAISE NOTICE 'Updating %.% → %', r.table_schema, r.table_name, r.column_name;
                EXECUTE format(
                        'UPDATE %I.%I AS t
                           SET %I = m.new_id
                          FROM crop_id_map m
                         WHERE t.%I = m.old_id;',
                        r.table_schema, r.table_name, r.column_name, r.column_name);
                -- Change column type to uuid
                EXECUTE format(
                        'ALTER TABLE %I.%I
                           ALTER COLUMN %I TYPE uuid USING %I::uuid;',
                        r.table_schema, r.table_name, r.column_name, r.column_name);
            END LOOP;
    END $update_children$;

-- 2.6  Swap primary key in public.crop
ALTER TABLE public.crop
    DROP CONSTRAINT crop_pkey,
    ALTER COLUMN id DROP DEFAULT,
    ALTER COLUMN id TYPE uuid USING new_id,
    ADD PRIMARY KEY (id);

ALTER TABLE public.crop DROP COLUMN new_id;

-- 2.7  Re‑create FK constraints
DO $add_fks$
DECLARE
    r record;
BEGIN
    FOR r IN SELECT * FROM crop_fk_columns LOOP
            EXECUTE format(
                    'ALTER TABLE %I.%I
                       ADD CONSTRAINT %I
                       FOREIGN KEY (%I) REFERENCES public.crop(id);',
                    r.table_schema, r.table_name, r.constraint_name,
                    r.column_name);
        END LOOP;
END $add_fks$;

-- if looks good
COMMIT;

-- if problems
ROLLBACK;

select * from program;

-- verify there are no duplicates
WITH norm AS (
    SELECT id,
           crop_name,
           regexp_replace(lower(trim(crop_name)), '\s', '', 'g') AS normalised
    FROM   public.crop
)
SELECT normalised, array_agg(id) AS dupe_ids
FROM   norm
GROUP  BY normalised
HAVING count(*) > 1;

Dependencies

  • brapi server latest develop

Testing

  • Can create and update species list

Checklist:

  • I have performed a self-review of my own code
  • I have tested my code and ensured it meets the acceptance criteria of the story
  • I have tested that my code works with both the brapi-java-server and BreedBase
  • I have create/modified unit tests to cover this change
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to documentation
  • I have run TAF: <please include a link to TAF run>

@nickpalladino nickpalladino marked this pull request as ready for review May 22, 2025 01:25
@nickpalladino nickpalladino merged commit 2226854 into develop May 22, 2025
1 check failed
@nickpalladino nickpalladino deleted the feature/BI-2622 branch May 22, 2025 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant