-- TO EXECUTE ON GAZELLE MASTER MODEL INSTANCE YOU WANT TO TAKE VALUES FROM
--This command will create a list of insert lines corresponding to ws_transaction_usages. The actual transaction link will be done based on transaction keyword.
--Warning : a small unwanted header may be generated by the select command.

SELECT 'INSERT INTO tf_ws_transaction_usage (id, last_changed, last_modifier_id, usage, transaction_id) VALUES ('
|| id
|| ', '''
|| last_changed
|| ''', '''
|| last_modifier_id
|| ''', '''
|| usage
|| ''', '
|| ' get_transaction_id('''
|| (SELECT keyword from tf_transaction where id = transaction_id)
|| '''));'
  from tf_ws_transaction_usage order by id ASC;

--Second step is to insert all datas (with file generated with first command) into your target database.
--The get_transaction_id function is to import in target database from file create_functions in order to retrieve
--transaction id's with their keyword.
--Once all datas are imported, run the following delete command. It will remove all rows with a null value in transaction_id
--which basically means that the transaction does not exist in the target database, and the ws_transaction_usage is useless.
-- EDIT : as there is a not null constraint on transaction_id collumn, the command should delete no line !

DELETE FROM tf_ws_transaction_usage where transaction_id IS NULL;

-- Finally, the sequence has to be set to the last inserted id so there is no collision :

ALTER SEQUENCE tf_ws_transaction_usage_id_seq RESTART 141 ; -- WARNING MODIFY VALUE !!!! Chose last id from list of inserts