Sha256: fc4823be15770a9b7274133998a051a09a3f481aca4e2bfbbb930f1056a7ec50

Contents?: true

Size: 1.74 KB

Versions: 161

Compression:

Stored size: 1.74 KB

Contents

CREATE OR REPLACE FUNCTION renalware.import_practice_memberships_csv(file text) RETURNS void
  AS $$
  BEGIN

  DROP TABLE IF EXISTS memberships_via_copy;
  CREATE TEMP TABLE copied_memberships (
    gp_code text NOT NULL,
    practice_code text NOT NULL,
    unused3 text,
    unused4 text,
    unused5 text,
    unused7 text
  );

  -- Import the CSV file into copied_memberships - note there is no CSV header in this file
  EXECUTE format ('COPY copied_memberships FROM %L DELIMITER %L CSV ', file, ',');

  DROP TABLE IF EXISTS tmp_memberships;
  CREATE TEMP TABLE tmp_memberships AS
    SELECT
      gp_code,
      practice_code,
      patient_primary_care_physicians.id primary_care_physician_id,
      patient_practices.id as practice_id
      from copied_memberships
      INNER JOIN patient_practices on patient_practices.code = practice_code
      INNER JOIN patient_primary_care_physicians on patient_primary_care_physicians.code = gp_code;

  -- Insert any new memberships, ignoring any conflicts where the
  -- practice_id + primary_care_physician_id already exists
  INSERT INTO renalware.patient_practice_memberships
    (practice_id, primary_care_physician_id, created_at, updated_at)
  SELECT
    practice_id,
    primary_care_physician_id,
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
  FROM tmp_memberships
  ON CONFLICT (practice_id, primary_care_physician_id) DO NOTHING;

  -- Mark as deleted any memberships not in the latest uploaded data set - ie those gps have retired or moved on
  UPDATE patient_practice_memberships mem
    SET deleted_at = CURRENT_TIMESTAMP
    WHERE NOT EXISTS (select 1 FROM tmp_memberships tmem
    WHERE tmem.practice_id = mem.practice_id AND tmem.primary_care_physician_id = mem.primary_care_physician_id);

END;
$$ LANGUAGE plpgsql;

Version data entries

161 entries across 161 versions & 1 rubygems

Version Path
renalware-core-2.1.1 db/functions/import_practice_memberships_csv_v01.sql
renalware-core-2.1.0 db/functions/import_practice_memberships_csv_v01.sql
renalware-core-2.0.167 db/functions/import_practice_memberships_csv_v01.sql
renalware-core-2.0.166 db/functions/import_practice_memberships_csv_v01.sql
renalware-core-2.0.165 db/functions/import_practice_memberships_csv_v01.sql
renalware-core-2.0.164 db/functions/import_practice_memberships_csv_v01.sql
renalware-core-2.0.163 db/functions/import_practice_memberships_csv_v01.sql
renalware-core-2.0.162 db/functions/import_practice_memberships_csv_v01.sql
renalware-core-2.0.161 db/functions/import_practice_memberships_csv_v01.sql
renalware-core-2.0.160 db/functions/import_practice_memberships_csv_v01.sql
renalware-core-2.0.159 db/functions/import_practice_memberships_csv_v01.sql
renalware-core-2.0.158 db/functions/import_practice_memberships_csv_v01.sql
renalware-core-2.0.157 db/functions/import_practice_memberships_csv_v01.sql
renalware-core-2.0.156 db/functions/import_practice_memberships_csv_v01.sql
renalware-core-2.0.155 db/functions/import_practice_memberships_csv_v01.sql
renalware-core-2.0.153 db/functions/import_practice_memberships_csv_v01.sql
renalware-core-2.0.152 db/functions/import_practice_memberships_csv_v01.sql
renalware-core-2.0.151 db/functions/import_practice_memberships_csv_v01.sql
renalware-core-2.0.149 db/functions/import_practice_memberships_csv_v01.sql
renalware-core-2.0.148 db/functions/import_practice_memberships_csv_v01.sql