private function common_mismatch($a, $b, $expected)
    {
        $docxml = <<<XML
<dbsteward>
  <database>
    <host>db-host</host>
    <name>dbsteward</name>
    <role>
      <application>dbsteward_phpunit_app</application>
      <owner>deployment</owner>
      <replication/>
      <readonly/>
    </role>
    <slony clusterName="duplicate_slony_ids_testsuite">
      <slonyNode id="1" comment="DSI - Local Primary"  dbName="test" dbHost="db-dev1" dbUser="******" dbPassword="******"/>
      <slonyNode id="2" comment="DSI - Local Backup"   dbName="test" dbHost="db-dev1" dbUser="******" dbPassword="******"/>
      <slonyNode id="3" comment="DSI - Local Backup"   dbName="test" dbHost="db-dev1" dbUser="******" dbPassword="******"/>
      <slonyReplicaSet id="100" originNodeId="1" upgradeSetId="101" comment="common duplicate testing database definition">
        <slonyReplicaSetNode id="2" providerNodeId="1"/>
        <slonyReplicaSetNode id="3" providerNodeId="2"/>
      </slonyReplicaSet>
    </slony>
    <configurationParameter name="TIME ZONE" value="America/New_York"/>
  </database>
  <schema name="dbsteward" owner="ROLE_OWNER">
XML;
        $adoc = new SimpleXMLElement($docxml . $a . "</schema></dbsteward>");
        $bdoc = new SimpleXMLElement($docxml . $b . "</schema></dbsteward>");
        pgsql8::$table_slony_ids = array();
        pgsql8::$sequence_slony_ids = array();
        pgsql8::$known_pg_identifiers = array();
        // for ease in testing, since replica_sets will be the same between
        // adoc and bdoc, just use adoc for iterating over replica sets
        $replica_sets = pgsql8::get_slony_replica_sets($adoc);
        if ($expected !== false) {
            $this->expect_exception($expected, function () use($adoc, $bdoc, $replica_sets) {
                foreach ($replica_sets as $replica_set) {
                    pgsql8::build_upgrade_slonik_replica_set($adoc, $bdoc, $replica_set, $replica_set, __DIR__ . '/../testdata/DuplicateSlonyIdsTest');
                }
            });
        } else {
            $this->expect_no_exception(function () use($adoc, $bdoc, $replica_sets) {
                foreach ($replica_sets as $replica_set) {
                    pgsql8::build_upgrade_slonik_replica_set($adoc, $bdoc, $replica_set, $replica_set, __DIR__ . '/../testdata/DuplicateSlonyIdsTest');
                }
            });
        }
    }
 /**
  * Loads database schema from dump file.
  *
  * @param file input file to be read
  *
  * @return database schema from dump fle
  */
 public static function load_database($files)
 {
     // one or more files to load as database
     if (!is_array($files)) {
         $files = array($files);
     }
     pgsql8::$track_pg_identifiers = true;
     pgsql8::$known_pg_identifiers = array();
     $database = new SimpleXMLElement('<dbsteward></dbsteward>');
     dbx::set_default_schema($database, 'public');
     foreach ($files as $file) {
         dbsteward::notice("Loading " . $file);
         $fp = fopen($file, 'r');
         if ($fp === false) {
             throw new exception("failed to open database dump file " . $file);
         }
         $line = fgets($fp);
         while ($line != null) {
             // blindly include LITERAL_SQL_INCLUDE lines in the database literal_sql collection
             if (preg_match(self::PATTERN_LITERAL_SQL, $line, $matches) > 0) {
                 dbx::add_sql($database, trim($line));
                 // clear the line that was literally obsorbed
                 $line = ' ';
             }
             $line = trim(self::strip_comment(trim($line)));
             if (strlen($line) == 0) {
                 $line = fgets($fp);
                 continue;
             } else {
                 if (preg_match(self::PATTERN_INSERT_INTO, $line, $matches) > 0) {
                     pgsql8_parser_insert_into::parse($database, self::get_whole_command($fp, $line));
                 } else {
                     if (preg_match(self::PATTERN_DELETE_FROM, $line, $matches) > 0) {
                         pgsql8_parser_delete_from::parse($database, self::get_whole_command($fp, $line));
                     } else {
                         if (preg_match(self::PATTERN_CREATE_LANGUAGE, $line, $matches) > 0) {
                             pgsql8_parser_create_language::parse($database, self::get_whole_command($fp, $line));
                         } else {
                             if (preg_match(self::PATTERN_ALTER_LANGUAGE, $line, $matches) > 0) {
                                 pgsql8_parser_alter_language::parse($database, self::get_whole_command($fp, $line));
                             } else {
                                 if (preg_match(self::PATTERN_CREATE_TYPE, $line, $matches) > 0) {
                                     pgsql8_parser_create_type::parse($database, self::get_whole_command($fp, $line));
                                 } else {
                                     if (preg_match(self::PATTERN_CREATE_SCHEMA, $line, $matches) > 0) {
                                         pgsql8_parser_create_schema::parse($database, self::get_whole_command($fp, $line));
                                     } else {
                                         if (preg_match(self::PATTERN_DEFAULT_SCHEMA, $line, $matches) > 0) {
                                             dbx::set_default_schema($database, $matches[1]);
                                         } else {
                                             if (preg_match(self::PATTERN_ALTER_SCHEMA, $line, $matches) > 0) {
                                                 pgsql8_parser_alter_schema::parse($database, self::get_whole_command($fp, $line));
                                             } else {
                                                 if (preg_match(self::PATTERN_CREATE_TABLE, $line, $matches) > 0) {
                                                     pgsql8_parser_create_table::parse($database, self::get_whole_command($fp, $line));
                                                 } else {
                                                     if (preg_match(self::PATTERN_ALTER_TABLE, $line, $matches) > 0) {
                                                         pgsql8_parser_alter_table::parse($database, self::get_whole_command($fp, $line));
                                                     } else {
                                                         if (preg_match(self::PATTERN_CREATE_SEQUENCE, $line, $matches) > 0) {
                                                             pgsql8_parser_create_sequence::parse($database, self::get_whole_command($fp, $line));
                                                         } else {
                                                             if (preg_match(self::PATTERN_ALTER_SEQUENCE, $line, $matches) > 0) {
                                                                 pgsql8_parser_alter_sequence::parse($database, self::get_whole_command($fp, $line));
                                                             } else {
                                                                 if (preg_match(self::PATTERN_CREATE_INDEX, $line, $matches) > 0) {
                                                                     pgsql8_parser_create_index::parse($database, self::get_whole_command($fp, $line));
                                                                 } else {
                                                                     if (preg_match(self::PATTERN_CREATE_VIEW, $line, $matches) > 0) {
                                                                         pgsql8_parser_create_view::parse($database, self::get_whole_command($fp, $line));
                                                                     } else {
                                                                         if (preg_match(self::PATTERN_ALTER_VIEW, $line, $matches) > 0) {
                                                                             pgsql8_parser_alter_view::parse($database, self::get_whole_command($fp, $line));
                                                                         } else {
                                                                             if (preg_match(self::PATTERN_CREATE_TRIGGER, $line, $matches) > 0) {
                                                                                 pgsql8_parser_create_trigger::parse($database, self::get_whole_command($fp, $line));
                                                                             } else {
                                                                                 if (preg_match(self::PATTERN_CREATE_FUNCTION, $line, $matches) > 0) {
                                                                                     pgsql8_parser_create_function::parse($database, self::get_whole_function($fp, $line));
                                                                                 } else {
                                                                                     if (preg_match(self::PATTERN_ALTER_FUNCTION, $line, $matches) > 0) {
                                                                                         pgsql8_parser_alter_function::parse($database, self::get_whole_command($fp, $line));
                                                                                     } else {
                                                                                         if (preg_match(self::PATTERN_GRANT_REVOKE, $line, $matches) > 0) {
                                                                                             pgsql8_parser_grant_revoke::parse($database, self::get_whole_command($fp, $line));
                                                                                         } else {
                                                                                             if (preg_match(self::PATTERN_CONFIG_PARAMETER, $line, $matches) > 0) {
                                                                                                 pgsql8_parser_config_parameter::parse($database, self::get_whole_command($fp, $line));
                                                                                             } else {
                                                                                                 if (preg_match(self::PATTERN_SET, $line, $matches) > 0 || preg_match(self::PATTERN_COMMENT, $line, $matches) > 0 || preg_match(self::PATTERN_SELECT, $line, $matches) > 0 || preg_match(self::PATTERN_BEGIN_END, $line, $matches) > 0) {
                                                                                                     // @TODO: implement these pg_dump modifiers?
                                                                                                     self::get_whole_command($fp, $line);
                                                                                                 } else {
                                                                                                     throw new exception("Line did not match to any patterns: " . $line);
                                                                                                 }
                                                                                             }
                                                                                         }
                                                                                     }
                                                                                 }
                                                                             }
                                                                         }
                                                                     }
                                                                 }
                                                             }
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             $line = fgets($fp);
             /* Development debug: every line, save our current rendition of $database to disk
             xml_parser::save_xml(dirname(__FILE__) . '/../../../../dbsteward_monitor.xml', $database->asXML());
             /**/
             //echo $line . "\n";
         }
         fclose($fp);
     }
     pgsql8::$track_pg_identifiers = false;
     return $database;
 }
 protected function upgrade_db_pgsql8()
 {
     $this->apply_options_pgsql8();
     // make sure we clear these in case we ran something else before this
     pgsql8::$table_slony_ids = array();
     pgsql8::$sequence_slony_ids = array();
     pgsql8::$known_pg_identifiers = array();
     // build the upgrade DDL first, incase dbsteward code wants to throw about something
     $old_db_doc = xml_parser::xml_composite(array($this->xml_file_a));
     $new_db_doc = xml_parser::xml_composite(array($this->xml_file_b));
     pgsql8::build_upgrade('', $old_db_doc, $old_db_doc, array(), $this->output_prefix, $new_db_doc, $new_db_doc, array());
     // upgrade database to "B" with each stage file
     $this->assertStringNotEqualsFile($this->output_prefix . '_upgrade_stage1_schema1.sql', '');
     $this->pgsql8->run_file($this->output_prefix . '_upgrade_stage1_schema1.sql');
     $this->pgsql8->run_file($this->output_prefix . '_upgrade_stage2_data1.sql');
     $this->pgsql8->run_file($this->output_prefix . '_upgrade_stage3_schema1.sql');
     $this->pgsql8->run_file($this->output_prefix . '_upgrade_stage4_data1.sql');
     //@TODO: confirm tables defined in B are present
 }
    /** Generates DDL for a build or upgrade given dbxml fragments **/
    private function common($old, $new = FALSE, $generate_slonik = TRUE)
    {
        pgsql8::$table_slony_ids = array();
        pgsql8::$sequence_slony_ids = array();
        pgsql8::$known_pg_identifiers = array();
        if (is_string($old) && empty($old)) {
            //      $old = <<<XML
            //<table name="foo" owner="ROLE_OWNER" primaryKey="id" slonyId="1">
            //  <column name="id" type="int"/>
            //</table>
            //<sequence name="seq" owner="ROLE_OWNER" slonyId="4"/>
            //XML;
        }
        $xml_a = <<<XML
<dbsteward>
  <database>
    <host>db-host</host>
    <name>dbsteward</name>
    <role>
      <application>dbsteward_phpunit_app</application>
      <owner>deployment</owner>
      <replication/>
      <readonly/>
    </role>
    <slony clusterName="duplicate_slony_ids_testsuite">
      <slonyNode id="1" comment="DSI - Local Primary"  dbName="test" dbHost="db-dev1" dbUser="******" dbPassword="******"/>
      <slonyNode id="2" comment="DSI - Local Backup"   dbName="test" dbHost="db-dev1" dbUser="******" dbPassword="******"/>
      <slonyNode id="3" comment="DSI - Local Backup"   dbName="test" dbHost="db-dev1" dbUser="******" dbPassword="******"/>
      <slonyReplicaSet id="100" originNodeId="1" upgradeSetId="101" comment="common duplicate testing database definition">
        <slonyReplicaSetNode id="2" providerNodeId="1"/>
        <slonyReplicaSetNode id="3" providerNodeId="2"/>
      </slonyReplicaSet>
    </slony>
    <configurationParameter name="TIME ZONE" value="America/New_York"/>
  </database>
  <schema name="dbsteward" owner="ROLE_OWNER">
    {$old}
  </schema>
</dbsteward>
XML;
        $this->set_xml_content_a($xml_a);
        if ($new) {
            $xml_b = <<<XML
<dbsteward>
  <database>
    <host>db-host</host>
    <name>dbsteward</name>
    <role>
      <application>dbsteward_phpunit_app</application>
      <owner>deployment</owner>
      <replication/>
      <readonly/>
    </role>
    <slony clusterName="duplicate_slony_ids_testsuite">
      <slonyNode id="1" comment="DSI - Local Primary"  dbName="test" dbHost="db-dev1" dbUser="******" dbPassword="******"/>
      <slonyNode id="2" comment="DSI - Local Backup"   dbName="test" dbHost="db-dev1" dbUser="******" dbPassword="******"/>
      <slonyNode id="3" comment="DSI - Local Backup"   dbName="test" dbHost="db-dev1" dbUser="******" dbPassword="******"/>
      <slonyReplicaSet id="100" originNodeId="1" upgradeSetId="101" comment="common duplicate testing database definition">
        <slonyReplicaSetNode id="2" providerNodeId="1"/>
        <slonyReplicaSetNode id="3" providerNodeId="2"/>
      </slonyReplicaSet>
    </slony>
    <configurationParameter name="TIME ZONE" value="America/New_York"/>
  </database>
  <schema name="dbsteward" owner="ROLE_OWNER">
    {$new}
  </schema>
</dbsteward>
XML;
            $this->set_xml_content_b($xml_b);
            ob_start();
            try {
                // new parameters for function:
                // $old_output_prefix, $old_composite_file, $old_db_doc, $old_files, $new_output_prefix, $new_composite_file, $new_db_doc, $new_files
                $old_db_doc = simplexml_load_file($this->xml_file_a);
                $new_db_doc = simplexml_load_file($this->xml_file_b);
                dbsteward::$generate_slonik = $generate_slonik;
                pgsql8::build_upgrade('', $old_db_doc, $old_db_doc, array(), $this->output_prefix, $new_db_doc, $new_db_doc, array());
                ob_end_clean();
            } catch (Exception $ex) {
                ob_end_clean();
                throw $ex;
            }
        } else {
            ob_start();
            try {
                $db_doc = simplexml_load_file($this->xml_file_a);
                dbsteward::$generate_slonik = $generate_slonik;
                pgsql8::build($this->output_prefix, $db_doc);
                ob_end_clean();
            } catch (Exception $ex) {
                ob_end_clean();
                throw $ex;
            }
        }
    }
 public function setUp()
 {
     parent::setUp();
     dbsteward::set_sql_format('pgsql8');
     // reset runtime mode flags to their default
     dbsteward::$single_stage_upgrade = FALSE;
     dbsteward::$generate_slonik = FALSE;
     pgsql8_diff::$as_transaction = TRUE;
     // reset runtime tracking variables
     pgsql8::$table_slony_ids = array();
     pgsql8::$sequence_slony_ids = array();
     pgsql8::$known_pg_identifiers = array();
     pgsql8_diff::$new_table_dependency = null;
     pgsql8_diff::$old_table_dependency = null;
 }