Skip to content

Commit 912abbb

Browse files
Refactor db:export rake task for readability (#4023)
- more/better comments - smaller lines - remove unnecessary parens - added `==>` to final output - output better download URL at the end
1 parent f88af00 commit 912abbb

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

lib/tasks/db/export.rake

+22-28
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ namespace :db do
4747

4848
# rubocop:disable Rails/SkipsModelValidations
4949
puts '==> Scrubbing article page view counts…'
50-
Article.update_all(page_views: 0)
50+
Article.update_all page_views: 0
5151
# rubocop:enable Rails/SkipsModelValidations
5252
end
5353

@@ -63,17 +63,20 @@ namespace :db do
6363

6464
desc 'Upload DB dump to S3'
6565
task upload: :environment do
66-
puts '==> Uploading local development DB dump to S3…'
66+
# Set Bucket URL and file name for use later
67+
bucket_url = 'https://cdn.crimethinc.com'
68+
file_name = 'database-dumps/crimethinc_production_db_dump.sql'
6769

68-
# Check for required env vars
70+
puts '==> Uploading local development DB dump to S3…'
71+
# Check for required ENV vars
6972
env_vars = [
7073
aws_access_key_id = ENV.fetch('AWS_ACCESS_KEY_ID_FOR_DB_EXPORT') { 'TODO' },
7174
aws_secret_access_key = ENV.fetch('AWS_SECRET_ACCESS_KEY_FOR_DB_EXPORT') { 'TODO' },
72-
aws_bucket = ENV.fetch('AWS_BUCKET_FOR_DB_EXPORT') { 'TODO' },
75+
aws_bucket_name = ENV.fetch('AWS_BUCKET_FOR_DB_EXPORT') { 'TODO' },
7376
aws_region = ENV.fetch('AWS_REGION_FOR_DB_EXPORT') { 'TODO' }
7477
]
7578

76-
# Exit if any env vars aren't set
79+
# Exit if any env vars arent set
7780
if env_vars.include? 'TODO'
7881
puts 'You need to set these as environment variables or in a .env file:'
7982
puts ' AWS_ACCESS_KEY_ID_FOR_DB_EXPORT'
@@ -83,40 +86,31 @@ namespace :db do
8386
exit
8487
end
8588

86-
# Configure AWS
87-
Aws.config.update(
88-
region: aws_region,
89-
credentials: Aws::Credentials.new(aws_access_key_id, aws_secret_access_key)
90-
)
89+
# Auth the AWS client
90+
aws_credentials = Aws::Credentials.new(aws_access_key_id, aws_secret_access_key)
91+
Aws.config.update region: aws_region, credentials: aws_credentials
9192

92-
# For making file public
93+
# Make S3 client to make a public file
9394
client = Aws::S3::Client.new
94-
# for file upload
95+
# Make S3 resource to upload a file
9596
aws_s_3 = Aws::S3::Resource.new
9697

9798
# Reference an existing bucket by name
98-
bucket_name = aws_bucket
99-
bucket = aws_s_3.bucket(bucket_name)
100-
bucket_url = bucket.url
101-
102-
# Get just the file name
103-
file_name = 'database-dumps/crimethinc_production_db_dump.sql'
99+
aws_bucket = aws_s_3.bucket aws_bucket_name
104100

105101
# Create the object to upload
106-
obj = aws_s_3.bucket(bucket_name).object(file_name)
102+
aws_object = aws_bucket.object file_name
107103

108104
# Upload it
109-
obj.upload_file file_name
105+
aws_object.upload_file file_name
110106

111-
# Setting the object to public-read
112-
client.put_object_acl(
113-
acl: 'public-read',
114-
bucket: bucket_name,
115-
key: file_name
116-
)
107+
# Set the object to public-read
108+
client.put_object_acl acl: 'public-read', bucket: aws_bucket_name, key: file_name
117109

118-
download_url = [bucket_url, file_name].join('/')
119-
puts "SUCCESS! File written to S3: #{download_url}"
110+
# Finish
111+
download_url = [bucket_url, file_name].join '/'
112+
puts '==> SUCCESS! File written to S3:'
113+
puts "==> #{download_url}"
120114
end
121115
end
122116
end

0 commit comments

Comments
 (0)