@@ -47,7 +47,7 @@ namespace :db do
47
47
48
48
# rubocop:disable Rails/SkipsModelValidations
49
49
puts '==> Scrubbing article page view counts…'
50
- Article . update_all ( page_views : 0 )
50
+ Article . update_all page_views : 0
51
51
# rubocop:enable Rails/SkipsModelValidations
52
52
end
53
53
@@ -63,17 +63,20 @@ namespace :db do
63
63
64
64
desc 'Upload DB dump to S3'
65
65
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'
67
69
68
- # Check for required env vars
70
+ puts '==> Uploading local development DB dump to S3…'
71
+ # Check for required ENV vars
69
72
env_vars = [
70
73
aws_access_key_id = ENV . fetch ( 'AWS_ACCESS_KEY_ID_FOR_DB_EXPORT' ) { 'TODO' } ,
71
74
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' } ,
73
76
aws_region = ENV . fetch ( 'AWS_REGION_FOR_DB_EXPORT' ) { 'TODO' }
74
77
]
75
78
76
- # Exit if any env vars aren' t set
79
+ # Exit if any env vars aren’ t set
77
80
if env_vars . include? 'TODO'
78
81
puts 'You need to set these as environment variables or in a .env file:'
79
82
puts ' AWS_ACCESS_KEY_ID_FOR_DB_EXPORT'
@@ -83,40 +86,31 @@ namespace :db do
83
86
exit
84
87
end
85
88
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
91
92
92
- # For making file public
93
+ # Make S3 client to make a public file
93
94
client = Aws ::S3 ::Client . new
94
- # for file upload
95
+ # Make S3 resource to upload a file
95
96
aws_s_3 = Aws ::S3 ::Resource . new
96
97
97
98
# 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
104
100
105
101
# Create the object to upload
106
- obj = aws_s_3 . bucket ( bucket_name ) . object ( file_name )
102
+ aws_object = aws_bucket . object file_name
107
103
108
104
# Upload it
109
- obj . upload_file file_name
105
+ aws_object . upload_file file_name
110
106
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
117
109
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 } "
120
114
end
121
115
end
122
116
end
0 commit comments