From e49066676072c2ba0d77813d1b6cae78cdfdeaa2 Mon Sep 17 00:00:00 2001 From: "Craig S. Cottingham" Date: Wed, 11 Apr 2012 23:52:00 -0500 Subject: [PATCH 1/3] Prompt for username and password in after_bundler callback; move gsub_file to callback Signed-off-by: Craig S. Cottingham --- scrolls/postgresql.rb | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/scrolls/postgresql.rb b/scrolls/postgresql.rb index 42d19b3..d25373a 100644 --- a/scrolls/postgresql.rb +++ b/scrolls/postgresql.rb @@ -1,11 +1,14 @@ 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 + pg_username = ask_wizard "Local development PostgreSQL username:" + pg_password = ask_wizard "Local development PostgreSQL password:" + + 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 @@ -31,11 +34,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:" From 36cd79550a62dc6fa1f94762939972bc867dc41b Mon Sep 17 00:00:00 2001 From: "Craig S. Cottingham" Date: Thu, 12 Apr 2012 00:20:53 -0500 Subject: [PATCH 2/3] Add check that username already exists Signed-off-by: Craig S. Cottingham --- scrolls/postgresql.rb | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/scrolls/postgresql.rb b/scrolls/postgresql.rb index d25373a..da058d2 100644 --- a/scrolls/postgresql.rb +++ b/scrolls/postgresql.rb @@ -1,15 +1,25 @@ gem "pg" after_bundler do + require 'pg' + pg_username = ask_wizard "Local development PostgreSQL username:" pg_password = ask_wizard "Local development PostgreSQL password:" - gsub_file "config/database.yml", /username:\s?.*/, "username: #{pg_username}" - gsub_file "config/database.yml", /password:\s?.*/, "password: #{pg_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" + rake "db:create:all" - rakefile("sample.rake") do + rakefile("sample.rake") do <<-RUBY namespace :db do desc "Populate the database with sample data" @@ -19,6 +29,11 @@ task :populate => :sample end RUBY + end + rescue PG::Error => err + # surely there's a better way to report errors than this.... + puts err.to_s + exit 1 end end From c8bee991fecfee4082d5e59b70eaeae82d5742bb Mon Sep 17 00:00:00 2001 From: "Craig S. Cottingham" Date: Thu, 12 Apr 2012 09:18:11 -0500 Subject: [PATCH 3/3] Move prompt for username and password to inside loop Signed-off-by: Craig S. Cottingham --- scrolls/postgresql.rb | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/scrolls/postgresql.rb b/scrolls/postgresql.rb index da058d2..29f4b5b 100644 --- a/scrolls/postgresql.rb +++ b/scrolls/postgresql.rb @@ -3,23 +3,33 @@ after_bundler 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 + pg_username = nil + pg_password = nil + + while pg_username.nil? + 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 + rescue PG::Error => err + say_wizard err.to_s + pg_username = nil + pg_password = nil + end + end - gsub_file "config/database.yml", /username:\s?.*/, "username: #{pg_username}" - gsub_file "config/database.yml", /password:\s?.*/, "password: #{pg_password}" + gsub_file "config/database.yml", /username:\s?.*/, "username: #{pg_username}" + gsub_file "config/database.yml", /password:\s?.*/, "password: #{pg_password}" - rake "db:create:all" + rake "db:create:all" - rakefile("sample.rake") do + rakefile("sample.rake") do <<-RUBY namespace :db do desc "Populate the database with sample data" @@ -29,11 +39,6 @@ task :populate => :sample end RUBY - end - rescue PG::Error => err - # surely there's a better way to report errors than this.... - puts err.to_s - exit 1 end end