Created
July 8, 2020 05:59
-
-
Save hackimov/3e3a6fafc555a058b72475ccfcba247c to your computer and use it in GitHub Desktop.
update sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
START TRANSACTION; | |
-- дропаем рестикты | |
ALTER TABLE transactions DROP CONSTRAINT fk_transact_reference_document; | |
ALTER TABLE value_params DROP CONSTRAINT fk_value_pa_reference_document; | |
-- создаём каскадный update для value params | |
ALTER TABLE value_params ADD CONSTRAINT fk_value_pa_reference_document_upd | |
FOREIGN KEY (document_id) REFERENCES documents(document_id) ON UPDATE CASCADE; | |
-- создаём каскадный update для transactions | |
ALTER TABLE transactions ADD CONSTRAINT fk_transact_reference_document_upd | |
FOREIGN KEY (document_id) REFERENCES documents(document_id) ON UPDATE CASCADE; | |
DO $$ | |
BEGIN | |
-- само тело процедуры, которая селектит и обновляет документ id | |
UPDATE documents SET document_id = CAST(document_data::json->>'EmpowermentId' AS char(24)) where | |
doctype_id = '945dde7f-b55e-4666-990c-749751a3c726' | |
and document_id <> CAST(document_data::json->>'EmpowermentId' AS char(24)) | |
and CAST(document_data::json->>'EmpowermentId' AS char(24)) <> ' ' | |
and CAST(document_data::json->>'EmpowermentId' AS char(24)) IS NOT NULL; | |
END $$; | |
-- дропаем констрейны для апдейта, и обновляем запись | |
ALTER TABLE transactions DROP CONSTRAINT fk_transact_reference_document_upd; | |
ALTER TABLE value_params DROP CONSTRAINT fk_value_pa_reference_document_upd; | |
-- создаём обратно констрейны рестрикт которые мы удалили в самом начале для документов | |
ALTER TABLE value_params ADD CONSTRAINT fk_value_pa_reference_document | |
FOREIGN KEY (document_id) REFERENCES documents(document_id) ON UPDATE RESTRICT ON DELETE RESTRICT; | |
-- создаём обратно констрейны рестрикт которые мы удалили в самом начале value params | |
ALTER TABLE transactions ADD CONSTRAINT fk_transact_reference_document | |
FOREIGN KEY (document_id) REFERENCES documents(document_id) ON UPDATE RESTRICT ON DELETE RESTRICT; | |
COMMIT; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment