copydb 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/config/sampleconfig.yml +1 -0
  2. data/lib/copydb.rb +100 -12
  3. metadata +21 -4
@@ -0,0 +1 @@
1
+ helloworld:
data/lib/copydb.rb CHANGED
@@ -3,25 +3,37 @@ require 'yaml'
3
3
  require 'active_record'
4
4
  require 'active_support/core_ext/kernel/reporting'
5
5
  require 'rails/railtie'
6
+ require 'fileutils'
7
+ require 'faker'
6
8
 
7
9
  module CopyDb
8
10
 
9
11
  class DumpDb
10
12
  def dump
11
- o = File.new(File.expand_path('tmp/copydb_dumped_data.yml'), "w+")
13
+
14
+ anonymizer = CopyDb::Config.read_anonymize_config
15
+
16
+ output = File.new(File.expand_path('db/copydb_dumped_data.yml'), "w+")
12
17
  yml = [self.schema_version]
13
18
  self.tables.each do |table|
14
- puts "Dumping table: #{table}"
15
- yml << self.table_dump(table)
19
+ if anonymizer.has_key?(table)
20
+ yml << self.table_dump_anonymous(table,anonymizer[table])
21
+ else
22
+ yml << self.table_dump(table)
23
+ end
16
24
  end
17
- o.write(yml.to_yaml)
18
- o.close
25
+ output.write(yml.to_yaml)
26
+ output.close
19
27
  end
20
28
 
21
29
  def tables
22
30
  ActiveRecord::Base.connection.tables.reject { |table| ['schema_info', 'schema_migrations'].include?(table) }
23
31
  end
24
32
 
33
+ def table_column_names(table)
34
+ ActiveRecord::Base.connection.columns(table).map { |c| c.name }
35
+ end
36
+
25
37
  def table_dump(table)
26
38
  rs = ActiveRecord::Base.connection.execute("SELECT * FROM #{table}")
27
39
  yml = Array.new
@@ -32,6 +44,36 @@ module CopyDb
32
44
  yml
33
45
  end
34
46
 
47
+ def table_dump_anonymous(table,anonymize_column_configurations)
48
+
49
+ column_names = Array.new
50
+ anonymizing_types = Array.new
51
+
52
+ anonymize_column_configurations.each do |anonymize_column_configuration|
53
+ column_names << anonymize_column_configuration.keys[0]
54
+ anonymizing_types << anonymize_column_configuration.values[0]
55
+ end
56
+
57
+ rs = ActiveRecord::Base.connection.execute("SELECT * FROM #{table}")
58
+ yml = Array.new
59
+ yml << table
60
+ rs.each do |result|
61
+ resultHash = Hash.new
62
+ result.each do |result_column,result_value|
63
+
64
+ if column_names.include?(result_column)
65
+ anonymize_type = anonymizing_types[(column_names.index(result_column))]
66
+ resultHash[result_column] = CopyDb::Anonymizer.anonymize(anonymize_type)
67
+ else
68
+ resultHash[result_column] = result_value
69
+ end
70
+
71
+ end
72
+ yml << resultHash
73
+ end
74
+ yml
75
+ end
76
+
35
77
  def schema_version
36
78
  ActiveRecord::Migrator.current_version
37
79
  end
@@ -39,23 +81,28 @@ module CopyDb
39
81
 
40
82
  class LoadDb
41
83
  def load
42
- if FileTest.exists?(File.expand_path('tmp/copydb_dumped_data.yml'))
84
+ if FileTest.exists?(File.expand_path('db/copydb_dumped_data.yml'))
43
85
 
44
- yml = YAML.load_file(File.expand_path('tmp/copydb_dumped_data.yml'))
45
-
86
+ yml = YAML.load_file(File.expand_path('db/copydb_dumped_data.yml'))
46
87
 
47
88
  yml.each_with_index do |entry,i|
48
89
  if i == 0
49
- puts entry
90
+ unless entry.to_s == self.schema_version.to_s
91
+ puts "ERROR: schema version mismatch"
92
+ end
93
+ next
50
94
  end
51
95
  unless entry[1].nil?
52
- quoted_column_names = entry[1].each_key.to_a.map { |column| ActiveRecord::Base.connection.quote_column_name(column) }.join(',')
96
+ columns = entry[1].each_key.to_a
97
+ quoted_column_names = columns.map { |column| ActiveRecord::Base.connection.quote_column_name(column) }.join(',')
53
98
 
54
99
  for i in 1..(entry.length-1)
55
- quoted_column_values = entry[i].each_value.to_a.map { |record| ActiveRecord::Base.connection.quote(record) }.join(',')
100
+ entries = entry[i].each_value.to_a
101
+ quoted_column_values = entries.map { |record| ActiveRecord::Base.connection.quote(record) }.join(',')
56
102
 
57
103
  sql_string = "INSERT INTO #{entry[0]} (#{quoted_column_names}) VALUES (#{quoted_column_values});"
58
- #ActiveRecord::Base.connection.execute(sql_string)
104
+
105
+ ActiveRecord::Base.connection.execute(sql_string)
59
106
  end
60
107
  end
61
108
  end
@@ -70,6 +117,47 @@ module CopyDb
70
117
  end
71
118
  end
72
119
 
120
+ class Config
121
+ def self.read_anonymize_config
122
+ if FileTest.exists?(File.expand_path('config/copydb_anonymize.yml'))
123
+ YAML.load_file(File.expand_path('config/copydb_anonymize.yml'))
124
+ else
125
+ Hash.new
126
+ end
127
+ end
128
+ end
129
+
130
+ class Anonymizer
131
+
132
+ Faker::Config.locale = "en"
133
+
134
+ def self.anonymize(type)
135
+ if type == "name"
136
+ Faker::Name.name
137
+ elsif type == "first_name"
138
+ Faker::Name.first_name
139
+ elsif type == "last_name"
140
+ Faker::Name.last_name
141
+ elsif type == "street_address"
142
+ Faker::Address.street_address
143
+ elsif type == "city"
144
+ Faker::Address.city
145
+ elsif type == "zip"
146
+ Faker::Address.zip_code
147
+ elsif type == "phone"
148
+ Faker::PhoneNumber.phone_number
149
+ elsif type == "email"
150
+ Faker::Internet.free_email
151
+ elsif type == "company"
152
+ Faker::Company.name
153
+ elsif type == "date"
154
+ "2011-11-11"
155
+ else
156
+ Faker::Company.bs
157
+ end
158
+ end
159
+ end
160
+
73
161
  class Railtie < Rails::Railtie
74
162
  rake_tasks do
75
163
  load File.expand_path('../tasks/copydb.rake',__FILE__)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: copydb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,9 +11,24 @@ bindir: bin
11
11
  cert_chain: []
12
12
  date: 2011-09-02 00:00:00.000000000 +02:00
13
13
  default_executable:
14
- dependencies: []
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: faker
17
+ requirement: &21538480 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 0.9.5
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *21538480
15
26
  description: Copydb is a gem that helps you to copy databases e.g. from a production
16
- database to your development database.
27
+ database to your development database. During the process it is possible to define
28
+ columns in tables in which the data should be exchanged for fake data. This can
29
+ be interesting in a scenario where you want to run your development machine with
30
+ production data, but privacy laws (such as in Germany) prohibit you to use real
31
+ data.
17
32
  email: eicke@yfu.de
18
33
  executables: []
19
34
  extensions: []
@@ -21,6 +36,7 @@ extra_rdoc_files: []
21
36
  files:
22
37
  - lib/copydb.rb
23
38
  - lib/tasks/copydb.rake
39
+ - lib/config/sampleconfig.yml
24
40
  has_rdoc: true
25
41
  homepage: https://github.com/ceicke/copydb
26
42
  licenses: []
@@ -45,5 +61,6 @@ rubyforge_project:
45
61
  rubygems_version: 1.6.2
46
62
  signing_key:
47
63
  specification_version: 3
48
- summary: Copydb helps you synchronize your databases
64
+ summary: Copydb helps you synchronize your databases and can be configured to anonymize
65
+ certain parts of a table
49
66
  test_files: []