-- +goose Up
-- Remove deprecated places table and patient_data.place_id relation.

ALTER TABLE patient_data
  DROP FOREIGN KEY fk_patient_data_place;

ALTER TABLE patient_data
  DROP COLUMN place_id;

DROP TABLE IF EXISTS places;

-- +goose Down
-- Recreate places table and patient_data.place_id column.

CREATE TABLE IF NOT EXISTS places (
  id BINARY(16) NOT NULL PRIMARY KEY,
  state VARCHAR(255) NULL,
  post_code VARCHAR(32) NULL,
  location VARCHAR(255) NOT NULL,
  street VARCHAR(255) NULL,
  no VARCHAR(50) NULL,
  country_code VARCHAR(10) NULL,
  country_name VARCHAR(255) NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  created_by BINARY(16) NULL,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  updated_by BINARY(16) NULL,
  deleted_at DATETIME NULL,
  deleted_by BINARY(16) NULL,

  KEY idx_places_location (location),
  KEY idx_places_state (state),
  KEY idx_places_post_code (post_code),
  KEY idx_places_country_code (country_code),
  KEY idx_places_deleted_at (deleted_at)
);

ALTER TABLE patient_data
  ADD COLUMN place_id BINARY(16) NULL AFTER co_insured,
  ADD KEY idx_patient_data_place_id (place_id),
  ADD CONSTRAINT fk_patient_data_place
    FOREIGN KEY (place_id) REFERENCES places (id)
    ON UPDATE CASCADE ON DELETE SET NULL;
