ALTER TABLE transaction DROP CONSTRAINT chk_transaction_sides;

ALTER TABLE transaction
    DROP COLUMN envelope_source_id,
    DROP COLUMN envelope_dest_id,
    ADD COLUMN envelope_id INTEGER REFERENCES envelope(id);

ALTER TABLE transaction ADD CONSTRAINT chk_transaction_sides CHECK (
    (account_source_id IS NOT NULL AND instrument_source_id IS NOT NULL AND quantite_source IS NOT NULL
     OR account_source_id IS NULL AND instrument_source_id IS NULL AND quantite_source IS NULL)
    AND
    (account_dest_id IS NOT NULL AND instrument_dest_id IS NOT NULL AND quantite_dest IS NOT NULL
     OR account_dest_id IS NULL AND instrument_dest_id IS NULL AND quantite_dest IS NULL)
    AND
    (account_source_id IS NOT NULL OR account_dest_id IS NOT NULL)
);

DROP TABLE IF EXISTS envelope_snapshot;
