Skip to content

Commit

Permalink
Merge pull request #18 from planetscale/add-ca-path
Browse files Browse the repository at this point in the history
Disable schema dump after running migrations + improve SSL cert
  • Loading branch information
mscoutermarsh authored Jan 12, 2024
2 parents 06cbb39 + f3691a8 commit ec9ec0d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
AllCops:
TargetRubyVersion: 3

Metrics/CyclomaticComplexity:
Enabled: false

Metrics/PerceivedComplexity:
Enabled: false

Style/StringLiterals:
Enabled: true
EnforcedStyle: double_quotes
Expand Down
22 changes: 16 additions & 6 deletions lib/planetscale_rails/tasks/psdb.rake
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,17 @@ namespace :psdb do
adapter = "trilogy"
end

"#{adapter}://#{username}:#{password}@#{host}:3306/#{database}?ssl_mode=VERIFY_IDENTITY"
# Setting schema_dump to nil is intentional. It prevents Rails from creating a dump after running migrations.
url = "#{adapter}://#{username}:#{password}@#{host}:3306/#{database}?ssl_mode=VERIFY_IDENTITY&schema_dump="

# Check common CA paths for certs.
ssl_ca_path = %w[/etc/ssl/certs/ca-certificates.crt /etc/pki/tls/certs/ca-bundle.crt /etc/ssl/ca-bundle.pem /etc/ssl/cert.pem].find { |f| File.exist?(f) }

if ssl_ca_path
url += "&sslca=#{ssl_ca_path}"
end

url
else
puts "Failed to create credentials for PlanetScale #{db_branch_colorized(database, branch)}"
puts "Command: #{command}"
Expand All @@ -110,7 +120,7 @@ namespace :psdb do

puts "Running migrations..."

command = "DATABASE_URL=#{ENV["PSCALE_DATABASE_URL"]} bundle exec rails db:migrate"
command = "DATABASE_URL=\"#{ENV["PSCALE_DATABASE_URL"]}\" bundle exec rails db:migrate"
IO.popen(command) do |io|
io.each_line do |line|
puts line
Expand All @@ -133,7 +143,7 @@ namespace :psdb do
puts "Running migrations..."

name_env_key = "#{name.upcase}_DATABASE_URL"
command = "#{name_env_key}=#{ENV["PSCALE_DATABASE_URL"]} bundle exec rails db:migrate:#{name}"
command = "#{name_env_key}=\"#{ENV["PSCALE_DATABASE_URL"]}\" bundle exec rails db:migrate:#{name}"

IO.popen(command) do |io|
io.each_line do |line|
Expand All @@ -160,7 +170,7 @@ namespace :psdb do
puts "Loading schema..."

name_env_key = "#{name.upcase}_DATABASE_URL"
command = "#{name_env_key}=#{ENV["PSCALE_DATABASE_URL"]} bundle exec rake db:schema:load:#{name}"
command = "#{name_env_key}=\"#{ENV["PSCALE_DATABASE_URL"]}\" bundle exec rake db:schema:load:#{name}"

IO.popen(command) do |io|
io.each_line do |line|
Expand All @@ -186,7 +196,7 @@ namespace :psdb do
raise "Found multiple database configurations, please specify which database you want to rollback using `psdb:rollback:<database_name>`".colorize(:red)
end

command = "DATABASE_URL=#{ENV["PSCALE_DATABASE_URL"]} bundle exec rails db:rollback"
command = "DATABASE_URL=\"#{ENV["PSCALE_DATABASE_URL"]}\" bundle exec rails db:rollback"

IO.popen(command) do |io|
io.each_line do |line|
Expand Down Expand Up @@ -215,7 +225,7 @@ namespace :psdb do
puts "Rolling back migrations..."

name_env_key = "#{name.upcase}_DATABASE_URL"
command = "#{name_env_key}=#{ENV["PSCALE_DATABASE_URL"]} bundle exec rake db:rollback:#{name}"
command = "#{name_env_key}=\"#{ENV["PSCALE_DATABASE_URL"]}\" bundle exec rake db:rollback:#{name}"

IO.popen(command) do |io|
io.each_line do |line|
Expand Down

0 comments on commit ec9ec0d

Please sign in to comment.