Skip to content
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions scrolls/postgresql.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
gem "pg"

gsub_file "config/database.yml", /username: .*/, "username: #{config['pg_username']}"
gsub_file "config/database.yml", /password: .*/, "password: #{config['pg_password']}"

after_bundler do
rake "db:create:all"

rakefile("sample.rake") do
require 'pg'

pg_username = ask_wizard "Local development PostgreSQL username:"
pg_password = ask_wizard "Local development PostgreSQL password:"

# attempt to connect to PostgreSQL using username and password
params = { :dbname => 'template1', :user => pg_username }
params[:password] = pg_password unless pg_password.blank?

begin
dbh = PG::Connection.new params
dbh.finish

gsub_file "config/database.yml", /username:\s?.*/, "username: #{pg_username}"
gsub_file "config/database.yml", /password:\s?.*/, "password: #{pg_password}"

rake "db:create:all"

rakefile("sample.rake") do
<<-RUBY
namespace :db do
desc "Populate the database with sample data"
Expand All @@ -16,6 +29,11 @@
task :populate => :sample
end
RUBY
end
rescue PG::Error => err
# surely there's a better way to report errors than this....
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps put the ask_wizard in a while loop until they give valid credentials?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, maybe. It doesn’t help when there are no valid credentials, but making the user ctrl-C or switch to another command prompt to create the user isn’t any worse than this.

puts err.to_s
exit 1
end
end

Expand All @@ -31,11 +49,3 @@
run_before: [eycloud]

args: -d postgresql

config:
- pg_username:
type: string
prompt: "Local development PostgreSQL username:"
- pg_password:
type: string
prompt: "Local development PostgreSQL password:"