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

5986 Add migration to drop user_account fk constraint #6134

Open
wants to merge 1 commit into
base: V2.5.0-RC
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions server/repository/src/migrations/v2_05_00/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod new_store_preferences;
mod remove_contact_form_site_id;
mod remove_contact_form_user_account_fk;
mod remove_unique_description_on_tmp_breach;
mod remove_vaccination_user_account_fk;

use crate::StorageConnection;

Expand Down Expand Up @@ -44,6 +45,7 @@ impl Migration for V2_05_00 {
Box::new(add_email_retry_at::Migrate),
Box::new(remove_contact_form_user_account_fk::Migrate),
Box::new(add_contact_form_processor_pg_enum_type::Migrate),
Box::new(remove_vaccination_user_account_fk::Migrate),
]
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::migrations::*;

pub(crate) struct Migrate;

impl MigrationFragment for Migrate {
fn identifier(&self) -> &'static str {
"remove_vaccination_user_account_fk"
}

fn migrate(&self, connection: &StorageConnection) -> anyhow::Result<()> {
// Temp fix - user_account record not always available on central server!

if cfg!(feature = "postgres") {
let result = sql!(
connection,
r#"
ALTER TABLE vaccination DROP CONSTRAINT vaccination_user_id_fkey;
"#
);
if result.is_err() {
log::warn!("Failed to drop FK constraint on user_id column of vaccination table, please check name of constraint");
}
} else {
sql!(
connection,
r#"
CREATE TABLE tmp_vaccination (
id TEXT NOT NULL PRIMARY KEY,
program_enrolment_id TEXT NOT NULL,
encounter_id TEXT NOT NULL,
created_datetime TIMESTAMP NOT NULL,
user_id TEXT NOT NULL,
vaccine_course_dose_id TEXT NOT NULL REFERENCES vaccine_course_dose(id),
store_id TEXT NOT NULL,
clinician_link_id TEXT,
invoice_id TEXT,
stock_line_id TEXT,
vaccination_date {DATE} NOT NULL,
given BOOLEAN NOT NULL,
not_given_reason TEXT,
comment TEXT,
facility_name_link_id TEXT,
facility_free_text TEXT
);
INSERT INTO tmp_vaccination SELECT * FROM vaccination;

PRAGMA foreign_keys = OFF;
DROP TABLE vaccination;
ALTER TABLE tmp_vaccination RENAME TO vaccination;
PRAGMA foreign_keys = ON;
"#
)?;
}

Ok(())
}
}
Loading