sunitparekh/data-anonymization

Clear destination database before each run to prevent duplicate primary key errors?

justinwiley opened this issue · 2 comments

I generated a new whitelist via the command line utility, created a new destination database 'snapshot', and executed using the following script:

require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
require 'data-anonymization'

DataAnon::Utils::Logging.logger.level = Logger::INFO

database 'Example' do
  strategy DataAnon::Strategy::Whitelist
  source_db ActiveRecord::Base.configurations['development']
  destination_db ActiveRecord::Base.configurations['snapshot']

  table 'answers' do
    primary_key 'id'
    whitelist 'question_id'
    whitelist 'name'
    whitelist 'position'
    whitelist 'value'
    whitelist 'created_at'
    whitelist 'updated_at'
  end
end

This failed, since there were no tables in the source database. I copied the table structure, and re-ran the script.

This succeeded the first time, but fails the second time, with duplicate ID errors:

Mysql2::Error: Duplicate entry '3355' for key 'PRIMARY': INSERT INTO `answers` (`created_at`, `id`, `name`, `position`, `question_id`, `updated_at`, `value`) VALUES ('2012-10-04 17:35:03', 3355, NULL, NULL, NULL, '2012-10-04 17:35:03', NULL)

Is there some way to have clear the destination database before each run?

Best way is to take the dump of the empty destination database and keep restoring it at the beginning of the script. example line # 1

Apologies for year late reply. I missed your message, really sorry. Might be helpful for others.

Yes, for Whitelist Destination tables have to be created.