Skip to content

Instantly share code, notes, and snippets.

@hackimov
Last active July 26, 2020 13:43
Show Gist options
  • Save hackimov/52adbcb9d92c43dc97b6054a4ae8f662 to your computer and use it in GitHub Desktop.
Save hackimov/52adbcb9d92c43dc97b6054a4ae8f662 to your computer and use it in GitHub Desktop.
SQL TRANSACTION TO MOVE SIGNATURES TO SIGNATURE TABLE
BEGIN TRANSACTION;
INSERT INTO signatures (document_id, signature_pkcs7, signature_data, signature_role)
(
SELECT document_id, document_owner_pkcs7 as sign, document_sign_from_json as json, 'owner' as role FROM documents where document_owner_pkcs7 IS NOT NULL
UNION ALL
SELECT document_id, document_contragent_pkcs7 as sign, document_sign_from_json as json, 'contragent' as role FROM documents where document_contragent_pkcs7 IS NOT NULL
UNION ALL
SELECT document_id, document_confidant_pkcs7 as sign, document_confidant_sign_json as json, 'confidant' as role FROM documents where document_confidant_pkcs7 IS NOT NULL
);
UPDATE documents
SET document_owner_pkcs7 = signature_id
FROM signatures
WHERE document_owner_pkcs7 = signature_pkcs7
AND signature_role = 'owner'
AND document_owner_pkcs7 NOTNULL
AND document_owner_pkcs7 <> '';
UPDATE documents
SET document_contragent_pkcs7 = signature_id
FROM signatures
WHERE document_contragent_pkcs7 = signature_pkcs7
AND signature_role = 'contragent'
AND document_contragent_pkcs7 NOTNULL
AND document_contragent_pkcs7 <> '';
UPDATE documents
SET document_confidant_pkcs7 = signature_id
FROM signatures
WHERE document_confidant_pkcs7 = signature_pkcs7
AND signature_role = 'confidant'
AND document_confidant_pkcs7 NOTNULL
AND document_confidant_pkcs7 <> '';
DELETE FROM signatures WHERE signature_pkcs7 ISNULL OR signature_pkcs7 = '';
COMMIT;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment