Skip to content

Instantly share code, notes, and snippets.

@olly
Last active March 6, 2018 17:38
Show Gist options
  • Save olly/6388e7db348d1023e340109ea9ce0362 to your computer and use it in GitHub Desktop.
Save olly/6388e7db348d1023e340109ea9ce0362 to your computer and use it in GitHub Desktop.
`data-anonymization` `SelectFromDatabase` patch

Prevents SQL error caused by primary_key not being set

Previously the SelectFromDatabase strategy connects to the database, and extracts the values, when it's initalized. However, at this stage,the table isn't fully configured and so when it creates the source table, it doesn't set a primary key.

By changing it to lazily fetch, and cache, the values; we ensure that the source table will be correctly configured.

diff --git a/lib/strategy/field/string/select_from_database.rb b/lib/strategy/field/string/select_from_database.rb
index 43bfc45..c7adef6 100644
--- a/lib/strategy/field/string/select_from_database.rb
+++ b/lib/strategy/field/string/select_from_database.rb
@@ -12,16 +12,23 @@ class SelectFromDatabase < SelectFromFile
include Utils::Logging
def initialize table_name, field_name, connection_spec
- DataAnon::Utils::SourceDatabase.establish_connection connection_spec
- source = Utils::SourceTable.create table_name, []
- @values = source.select(field_name).distinct.collect { |record| record[field_name]}
- logger.debug "For field strategy #{table_name}:#{field_name} using values #{@values} "
-
+ @table_name = table_name
+ @field_name = field_name
+ @connection_spec = connection_spec
end
- end
-
+ def anonymize field
+ @values ||= begin
+ DataAnon::Utils::SourceDatabase.establish_connection @connection_spec
+ source = Utils::SourceTable.create @table_name, []
+ values = source.select(@field_name).distinct.collect { |record| record[@field_name]}
+ logger.debug "For field strategy #{@table_name}:#{@field_name} using values #{values} "
+ values
+ end
+ super
+ end
+ end
end
end
end
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment