Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserving the 'id' attribute in fhirbase_update #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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.

None yet

1 participant