From db1e765d6c3fc72731571c72c98740e316a5c379 Mon Sep 17 00:00:00 2001
From: laceysanderson <laceysanderson@781094.no-reply.drupal.org>
Date: Wed, 27 Mar 2013 11:30:58 -0600
Subject: [PATCH 1/7] Issue 847214: Added drush help to describe new option db-backend which defaults to mysql

---
 drush/quickstart.drush.inc |    7 ++++---
 1 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/drush/quickstart.drush.inc b/drush/quickstart.drush.inc
index 2293593..58ccddc 100644
--- a/drush/quickstart.drush.inc
+++ b/drush/quickstart.drush.inc
@@ -31,12 +31,13 @@ function quickstart_drush_command() {
           ),
           'options' => array(
                   'domain'       => 'Required (all).  Domain name of new site.  Use this to reference site with other commands.  Becomes dns, dbname, dbuser, dbpass, site-name, and kitchen-sink',
-                  'dbuser'       => 'Optional (db).  Root mysql user.  Defaults to "root"',
-                  'dbpassword'   => 'Optional (db).  Root mysql password.  Defaults to "quickstart"',
+                  'db-backend'       => 'Optional (db).  The type of backend: either "mysql" or "pgsql". Defaults to "mysql"',
+                  'dbuser'       => 'Optional (db).  Root database user.  Defaults to "root" for mysql and "quickstart" for pgsql',
+                  'dbpassword'   => 'Optional (db).  Root database password.  Defaults to "quickstart"',
                   'codepath'     => 'Optional (apache,code,install).  Path to drupal code.  Defaults to "~/websites/[domain]"',
                   'makefile'     => 'Optional (code).  Path to drupal make file.  Defaults to D7 "~/websites/default.make"',
                   'profile'      => 'Optional (install).  Name of profile to install.  Defaults to "default" in D6, "standard" in D7',
-                  'db-url'       => 'Optional (install).  DB url.  Defaults to "mysql://[domain]]:[domain]@localhost/[domain]" (replace . with _ in [domian])',
+                  'db-url'       => 'Optional (install).  DB url.  Defaults to "mysql://[domain]]:[domain]@localhost/[domain]" (replace . with _ in [domian]). If db backend is pgsql then defaults to pgsql://[domain]]:[domain]@localhost/[domain]',
                   'db-prefix'    => 'Optional (install).  DB table prefix.  Defaults to ""',
                   'account-name' => 'Optional (install).  Name of user 1.  Defaults to "admin"',
                   'account-pass' => 'Optional (install).  Password user 1.  Defaults to "admin"',
-- 
1.7.4.1


From 6b2db7aa318eee3482eeeabfc743b1c3c35a2c44 Mon Sep 17 00:00:00 2001
From: laceysanderson <laceysanderson@781094.no-reply.drupal.org>
Date: Wed, 27 Mar 2013 11:35:51 -0600
Subject: [PATCH 2/7] Issue 847214: Updated _quickstart_params() to handle the new db-backend parameter

---
 drush/quickstart.drush.inc |   17 +++++++++++++++--
 1 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/drush/quickstart.drush.inc b/drush/quickstart.drush.inc
index 58ccddc..1324581 100644
--- a/drush/quickstart.drush.inc
+++ b/drush/quickstart.drush.inc
@@ -155,7 +155,15 @@ function _quickstart_params($required) {
   // defaults
   $defaults['domain_'] = $domain_ = str_replace('.','_',$options['domain']);
   // database
-  $defaults['dbuser'] = 'root';
+  if (empty($options['db-backend'])) {
+    $defaults['db-backend'] = 'mysql';
+  }
+  if ($options['db-backend'] == 'pgsql') {
+    $defaults['dbuser'] = 'quickstart';
+  }
+  else {
+    $defaults['dbuser'] = 'root';
+  }
   $defaults['dbpassword'] = 'quickstart';
   // apache, code, install
   $defaults['codepath'] = quickstart_fixpath("~/websites/" . $options['domain']);
@@ -163,7 +171,12 @@ function _quickstart_params($required) {
   $defaults['makefile'] = quickstart_fixpath("~/websites/default.make");
   // install
   $defaults['profile'] = 'standard';
-  $defaults['db-url'] = "mysql://$domain_:$domain_@localhost/$domain_";
+  if ($options['db-backend'] == 'pgsql') {
+    $defaults['db-url'] = "pgsql://$domain_:$domain_@localhost/$domain_";
+  }
+  else {
+    $defaults['db-url'] = "mysql://$domain_:$domain_@localhost/$domain_";
+  }
   $defaults['db-prefix'] = '';
   $defaults['account-name'] = 'admin';
   $defaults['account-pass'] = 'admin';
-- 
1.7.4.1


From 8136cca955a1811451c8f1cf78489a61f52435ed Mon Sep 17 00:00:00 2001
From: laceysanderson <laceysanderson@781094.no-reply.drupal.org>
Date: Wed, 27 Mar 2013 11:47:10 -0600
Subject: [PATCH 3/7] Issue 847214: Added to drush help to explain use of db-backend in destroy and also provide examples.

---
 drush/quickstart.drush.inc |   11 +++++++----
 1 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/drush/quickstart.drush.inc b/drush/quickstart.drush.inc
index 1324581..971e0e6 100644
--- a/drush/quickstart.drush.inc
+++ b/drush/quickstart.drush.inc
@@ -50,6 +50,7 @@ function quickstart_drush_command() {
           ),
           'examples' => array(
                   'Create D7 site in one command: setup dns, apache, create db, download d6, run installer, open in firefox' => 'drush quickstart-create --domain=example1.dev',
+                  'Create D7 site with PostgreSQL backend in one command' => 'drush quickstart-create --domain=example1.dev --db-backend=pgsql',
                   'Quick D6 site' => 'drush qc --domain=example2.dev --makefile=d6.make',
                   'Just DNS (/etc/hosts)' => 'drush quickstart-create dns --domain=example.dev',
                   'Just apache (/etc/apache2/sites-enabled/)' => 'drush quickstart-create apache --domain=example.dev --codepath=/path/to/drupal',
@@ -67,17 +68,19 @@ function quickstart_drush_command() {
                   'what' => 'Optional. One or more items to destroy: [' . implode('|', _quickstart_arg_list()) . '|all] Defaults to "all"',
           ),
           'options' => array(
-                  'domain' => 'Domain name to destroy.',
-                  'codepath' => 'Path to drupal code.  Defaults to ~/websites/[domain]',
+                  'domain'     => 'Domain name to destroy.',
+                  'db-backend' => 'The type of backend: either "mysql" or "pgsql". Defaults to "mysql"',
+                  'codepath'   => 'Path to drupal code.  Defaults to ~/websites/[domain]',
           ),
           'examples' => array(
                   'Simplest format' => 'drush qd --domain=example.dev',
                   'Complete format' => 'drush quickstart-destroy all --domain=example.dev --codepath=/path/to/drupal',
+                  'Complete format with PostgreSQL backend' => 'drush quickstart-destroy all --domain=example.dev --codepath=/path/to/drupal --db-backend=pgsql',
                   'Remove DNS (/etc/hosts)' => 'drush quickstart-destroy dns --domain=example.dev',
                   'Remove Apache (/etc/apache2/sites-enabled/)' => 'drush quickstart-destroy apache --domain=example.dev',
-                  'Drop Database (mysql db and user)' => 'drush quickstart-destroy database --domain=example.dev',
+                  'Drop Database (db and user)' => 'drush quickstart-destroy database --domain=example.dev',
                   'Delete code and files (~/webistes/<domain>)' => 'drush quickstart-destroy code --domain=example.dev --codepath=/path/to/drupal',
-                  'Reset for reinstall (~/webistes/<domain>)' => 'drush quickstart-destroy install --domain=example.dev --codepath=/path/to/drupal',
+                  'Reset for reinstall (~/webistes/<domain>)' => 'drush quickstart-destroy install --domain=example.dev --codepath=/path/to/drupal'
           ),
           'aliases' => array('qd'),
           'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap at all.
-- 
1.7.4.1


From c875a3912c0205b609333cb3e85f9da872c8b5e8 Mon Sep 17 00:00:00 2001
From: laceysanderson <laceysanderson@781094.no-reply.drupal.org>
Date: Wed, 27 Mar 2013 11:51:03 -0600
Subject: [PATCH 4/7] Issue 847214: Added quickstart_pgsql_exec() helper function to execute pgsql commands using psql.

---
 drush/quickstart.inc |   23 +++++++++++++++++++++++
 1 files changed, 23 insertions(+), 0 deletions(-)

diff --git a/drush/quickstart.inc b/drush/quickstart.inc
index 0bf38d1..0e6c291 100644
--- a/drush/quickstart.inc
+++ b/drush/quickstart.inc
@@ -40,6 +40,29 @@ function quickstart_mysql_exec($sql, $dbuser, $dbpassword) {
   quickstart_shell_exec("echo \"$sql\" | mysql -u$dbuser -p$dbpassword");
 }
 
+ /**
+ * Helper function for running SQL on a local PostgreSQL database.
+ *
+ * Note: The SQL is separated into separate statements based on the ;
+ *  because CREATE doesn't like to run in a mult-command environment.
+ *  This lets each command be executed separately.
+ *
+ * @param <type> $sql
+ */
+function quickstart_pgsql_exec($sql, $dbuser) {
+
+  // Break each SQL command into an array
+  $sql_stms = explode(';',$sql);
+  foreach($sql_stms as $k => $s) {
+    //remove any trailing whitespace from the command
+    $s = trim($s);
+    if (!empty($s)) {
+      // as long as there is a command, execute it using psql
+      quickstart_shell_exec("psql --user $dbuser --command \"$s;\"");
+    }
+  }
+}
+
 /**
  * Helper function for dereferencing paths.
  *
-- 
1.7.4.1


From 57413db5145d6e60d4799600e7147628bd3c5060 Mon Sep 17 00:00:00 2001
From: laceysanderson <laceysanderson@781094.no-reply.drupal.org>
Date: Wed, 27 Mar 2013 11:53:51 -0600
Subject: [PATCH 5/7] Issue 847214: Updated quickstart_create_database() to handle the pgsql backend

---
 drush/quickstart.inc |   25 +++++++++++++++++++++++--
 1 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/drush/quickstart.inc b/drush/quickstart.inc
index 0e6c291..8f2467e 100644
--- a/drush/quickstart.inc
+++ b/drush/quickstart.inc
@@ -195,6 +195,21 @@ DROP DATABASE IF EXISTS #DOMAIN#;
 FLUSH PRIVILEGES;");
 
 /**
+ * These define SQL commands to create a postgresql database and user
+ * User needs createdb permission in order for site-install to work. 
+ * Site-install drops and then re-creates the database using this user.
+ */
+define('PGSQL_DATABASE_CREATE', "
+CREATE USER #DOMAIN# WITH CREATEDB PASSWORD '#DOMAIN#';
+CREATE DATABASE #DOMAIN# WITH OWNER #DOMAIN#;");
+/**
+ * DROP BOTH PostgreSQL USER AND DATABASE
+ */
+define('PGSQL_DATABASE_DESTROY', "
+DROP DATABASE IF EXISTS #DOMAIN#;
+DROP USER IF EXISTS #DOMAIN#;");
+
+/**
  * Create the database.  Runs SQL.  Replaces .'s with _'s in domain name
  * to get the database name, database username, and password.
  *
@@ -206,8 +221,14 @@ function quickstart_create_database($domain, $params) {
   $dbpassword = $params['dbpassword'];
   $domain_ = $params['domain_'];
 
-  $sql = str_replace("#DOMAIN#", $domain_, DATABASE_CREATE);
-  quickstart_mysql_exec($sql, $dbuser, $dbpassword);
+  if ($params['db-backend'] == 'pgsql') {
+    $sql = str_replace("#DOMAIN#", $domain_, PGSQL_DATABASE_CREATE);
+    quickstart_pgsql_exec($sql, 'quickstart');
+  }
+  else {
+    $sql = str_replace("#DOMAIN#", $domain_, DATABASE_CREATE);
+    quickstart_mysql_exec($sql, $dbuser, $dbpassword);
+  }
 }
 /**
  * Destroys the database using SQL.
-- 
1.7.4.1


From 10289f9b3d15a2fed6c29551fd430786effe29c2 Mon Sep 17 00:00:00 2001
From: laceysanderson <laceysanderson@781094.no-reply.drupal.org>
Date: Wed, 27 Mar 2013 11:54:49 -0600
Subject: [PATCH 6/7] Issue 847214: Updated quickstart_destroy_database() to handle the pgsql backend

---
 drush/quickstart.inc |   10 ++++++++--
 1 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/drush/quickstart.inc b/drush/quickstart.inc
index 8f2467e..37ed28c 100644
--- a/drush/quickstart.inc
+++ b/drush/quickstart.inc
@@ -241,8 +241,14 @@ function quickstart_destroy_database($domain, $params) {
   $dbpassword = $params['dbpassword'];
   $domain_ = $params['domain_'];
 
-  $sql = str_replace("#DOMAIN#", $domain_, DATABASE_DESTROY);
-  quickstart_mysql_exec($sql, $dbuser, $dbpassword);
+  if ($params['db-backend'] == 'pgsql') {
+    $sql = str_replace("#DOMAIN#", $domain_, PGSQL_DATABASE_DESTROY);
+    quickstart_pgsql_exec($sql, 'quickstart');
+  }
+  else {
+    $sql = str_replace("#DOMAIN#", $domain_, DATABASE_DESTROY);
+    quickstart_mysql_exec($sql, $dbuser, $dbpassword);
+  }
 }
 
 /**
-- 
1.7.4.1


From a98da22e8bd002b7117adac43a40431e4fe6f079 Mon Sep 17 00:00:00 2001
From: laceysanderson <laceysanderson@781094.no-reply.drupal.org>
Date: Wed, 27 Mar 2013 11:55:51 -0600
Subject: [PATCH 7/7] Issue 847214: Added a warning to the site-install mentioning that psql won't take a password thus you need to supply it. It also tells the user what the password is so they don't have to look further up or guess.

---
 drush/quickstart.inc |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)

diff --git a/drush/quickstart.inc b/drush/quickstart.inc
index 37ed28c..d0ca947 100644
--- a/drush/quickstart.inc
+++ b/drush/quickstart.inc
@@ -329,6 +329,14 @@ function quickstart_create_install($domain, $params) {
           $str);
     file_put_contents("$codepath/sites/".$params['sites-subdir']."/settings.php",$str);
   }
+
+  if ($params['db-backend'] == 'pgsql') {
+    drush_print("\tDue to the PostgreSQL backend you will need to supply the \n"
+      ."\tuser password in order for the site-install to proceed. This is due \n"
+      ."\tto a bug in PostgreSQL's command-line utility psql and cannot be \n"
+      ."\tfixed at this time.\n\tPassword to Supply (user: " . $params['domain_']
+      ."):" . $params['domain_']);
+  }
   
   // Nuff foreplay, let's install!
   drush_backend_invoke("site-install"
-- 
1.7.4.1

