Commit 83d521927f8c2aa9684c38f0e2ccc81cbbbd6aa4
1 parent
a6178cf3
Exists in
master
and in
21 other branches
restore:remove_emails: removes actual emails from database
This is useful when you are loading a database dump from a production database for local tests, and you don't want people's emails there in order to avoid accidental outgoing email.
Showing
1 changed file
with
29 additions
and
0 deletions
Show diff stats
lib/tasks/backup.rake
| @@ -111,3 +111,32 @@ task :restore => :check_backup_support do | @@ -111,3 +111,32 @@ task :restore => :check_backup_support do | ||
| 111 | puts "Backup restored!" | 111 | puts "Backup restored!" |
| 112 | puts "****************************************************" | 112 | puts "****************************************************" |
| 113 | end | 113 | end |
| 114 | + | ||
| 115 | +desc 'Removes emails from database' | ||
| 116 | +task 'restore:remove_emails' => :environment do | ||
| 117 | + connection = ActiveRecord::Base.connection | ||
| 118 | + [ | ||
| 119 | + "UPDATE users SET email = concat('user', id, '@localhost.localdomain')", | ||
| 120 | + "UPDATE environments SET contact_email = concat('environment', id, '@localhost.localdomain')", | ||
| 121 | + ].each do |update| | ||
| 122 | + puts update | ||
| 123 | + connection.execute(update) | ||
| 124 | + end | ||
| 125 | + | ||
| 126 | + profiles = connection.execute("select id, data from profiles") | ||
| 127 | + profiles.each do |profile| | ||
| 128 | + if profile['data'] | ||
| 129 | + data = YAML.load(profile['data']) | ||
| 130 | + if data[:contact_email] && data[:contact_email] !~ /@localhost.localdomain$/ | ||
| 131 | + data[:contact_email] = ['profile', profile['id'], '@localhost.localdomain'].join | ||
| 132 | + sql = Environment.send(:sanitize_sql, [ | ||
| 133 | + "UPDATE profiles SET data = ? WHERE id = ?", | ||
| 134 | + YAML.dump(data), | ||
| 135 | + profile['id'], | ||
| 136 | + ]) | ||
| 137 | + puts sql | ||
| 138 | + connection.execute(sql) | ||
| 139 | + end | ||
| 140 | + end | ||
| 141 | + end | ||
| 142 | +end |