Skip to content

Conversation

@datadevopscloud
Copy link

@datadevopscloud datadevopscloud commented Aug 31, 2020

closes #issue-28

#20 solve the situation in fhirbase_create, but fhirbase_update has the same behavior.

With this PR, fhirbase_create and fhirbase_update keep 'id' attribute in jsonb resource, bringing consistency.

@datadevopscloud
Copy link
Author

datadevopscloud commented Aug 31, 2020

-- after update fhirbase_update function, you can run the code below
-- to re-introduze the 'id' attribute in all resources without that.

-- fix current resources to recreate the id attribute
DO
$$
    DECLARE
        _table text;
        _sql text;

    BEGIN
        FOR _table IN

            SELECT concat('"', t.table_schema, '"."', t.table_name, '"') _table
            FROM information_schema.tables t, information_schema.columns c
            WHERE
                t.table_catalog = c.table_catalog AND
                t.table_schema = c.table_schema AND
                t.table_name = c.table_name AND
                t.table_type = 'BASE TABLE' AND
                c.table_schema = 'public' AND
                c.column_name = 'resource' AND
                c.data_type = 'jsonb'

        LOOP

            _sql = format(
                    $SQL$
                        UPDATE %s r
                        SET resource = r.resource || ('{"id":"' || r.id || '"}')::jsonb
                        WHERE r.resource->>'id' is null

                    $SQL$,
                    _table);

            EXECUTE _sql;

        END LOOP;

    END
$$;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant