/** * @group pgsql8 */ public function testTableColumnTypeQuotingPgsql8() { dbsteward::set_sql_format('pgsql8'); dbsteward::$quote_all_names = TRUE; dbsteward::$single_stage_upgrade = TRUE; $doc_empty = simplexml_load_string($this->xml_empty); $doc_empty = xml_parser::composite_doc(FALSE, $doc_empty); dbsteward::$old_database = $doc_empty; $doc = simplexml_load_string($this->xml); $doc = xml_parser::composite_doc(FALSE, $doc); dbsteward::$new_database = $doc; $table_dependency = xml_parser::table_dependency_order($doc); //var_dump(xml_parser::format_xml($doc_empty->saveXML())); //var_dump(xml_parser::format_xml($doc->saveXML())); $schema = $doc->schema; $table = $schema->table; // make sure the type is named with quoting as part of a definition build $expected = "CREATE TYPE \"schema1\".\"enumCamelCaseType\" AS ENUM ('Read','Write','Delete');"; $mofs = new mock_output_file_segmenter(); pgsql8::build_schema($doc, $mofs, $table_dependency); $actual = trim($mofs->_get_output()); $this->assertContains($expected, $actual); // make sure the type is referred to with quoting in a table creation as part of a definition build $expected_column = '"table_shable_mode" "enumCamelCaseType"'; $this->assertContains($expected_column, $actual); // make sure the type is referred to with quoting when generating table create statements $expected = '"table_shable_mode" "enumCamelCaseType"'; $sql = pgsql8_table::get_creation_sql($schema, $table); $this->assertContains($expected, $sql); // make sure create table quotes the type name $expected = '"table_shable_mode" "enumCamelCaseType"'; $mofs = new mock_output_file_segmenter(); var_dump(dbx::get_tables($schema)); pgsql8_diff_tables::diff_tables($mofs, $mofs, NULL, $schema); $actual = trim($mofs->_get_output()); $this->assertContains($expected, $actual); // make sure insert statements are made that match the XML definition $expected = "INSERT INTO \"schema1\".\"table_shable\" (\"table_shable_id\", \"table_shable_value\", \"table_shable_mode\") VALUES (1, E'shim sham', BETA);"; $actual = trim(pgsql8_diff_tables::get_data_sql(NULL, NULL, $schema, $table, FALSE)); $this->assertContains($expected, $actual); }
private function diff($old, $new, $expected) { $ofs = new mock_output_file_segmenter(); $old = '<dbsteward><database/>' . $old . '</dbsteward>'; $new = '<dbsteward><database/>' . $new . '</dbsteward>'; $old_doc = simplexml_load_string($old); $new_doc = simplexml_load_string($new); dbsteward::$old_database = $old_doc; dbsteward::$new_database = $new_doc; pgsql8_diff::$old_table_dependency = xml_parser::table_dependency_order($old_doc); pgsql8_diff::$new_table_dependency = xml_parser::table_dependency_order($new_doc); pgsql8_diff_types::apply_changes($ofs, $old_doc->schema, $new_doc->schema); $sql = trim(preg_replace('/\\n\\n+/', "\n", preg_replace('/^--.*$/m', '', $ofs->_get_output()))); $this->assertEquals($expected, $sql); }
private function common_structure($old, $new) { dbsteward::$old_database = new SimpleXMLElement($old); dbsteward::$new_database = new SimpleXMLElement($new); mysql5_diff::$new_table_dependency = xml_parser::table_dependency_order(dbsteward::$new_database); $ofs1 = new mock_output_file_segmenter(); $ofs3 = new mock_output_file_segmenter(); mysql5_diff::revoke_permissions($ofs1, $ofs3); mysql5_diff::update_structure($ofs1, $ofs3); mysql5_diff::update_permissions($ofs1, $ofs3); // @TODO: assert expected = actual // echo "\n\nofs 1:\n\n"; // echo $ofs1->_get_output(); // echo "\n\nofs 3:\n\n"; // echo $ofs3->_get_output(); }
protected function upgrade_db($format, $ofs1, $ofs2, $ofs3, $ofs4) { dbsteward::set_sql_format($format); $doc_a = new SimpleXMLElement($this->{$format . '_xml_a'}); $doc_b = new SimpleXMLElement($this->{$format . '_xml_b'}); dbsteward::$old_database = $doc_a; dbsteward::$new_database = $doc_b; $diff_class = $format . '_diff'; $diff_class::$old_table_dependency = xml_parser::table_dependency_order($doc_a); $diff_class::$new_table_dependency = xml_parser::table_dependency_order($doc_b); $diff_class::diff_doc_work($ofs1, $ofs2, $ofs3, $ofs4); }
private function diff($old, $new, $expected1, $expected3, $message = '') { dbsteward::$old_database = xml_parser::composite_doc(NULL, simplexml_load_string($this->db_doc_xml . $old . '</dbsteward>')); dbsteward::$new_database = xml_parser::composite_doc(NULL, simplexml_load_string($this->db_doc_xml . $new . '</dbsteward>')); $ofs1 = new mock_output_file_segmenter(); $ofs3 = new mock_output_file_segmenter(); mysql5_diff::$old_table_dependency = xml_parser::table_dependency_order(dbsteward::$old_database); mysql5_diff::$new_table_dependency = xml_parser::table_dependency_order(dbsteward::$new_database); mysql5_diff::update_structure($ofs1, $ofs3); $actual1 = trim($ofs1->_get_output()); $actual3 = trim($ofs3->_get_output()); $this->assertEquals($expected1, $actual1, "during stage 1: {$message}"); $this->assertEquals($expected3, $actual3, "during stage 3: {$message}"); }
private function common($xml, $expected) { $dbs = new SimpleXMLElement($xml); $ofs = new mock_output_file_segmenter(); dbsteward::$new_database = $dbs; $table_dependency = xml_parser::table_dependency_order($dbs); mysql5::build_data($dbs, $ofs, $table_dependency); $actual = $ofs->_get_output(); // get rid of extra whitespace $expected = preg_replace("/^ +/m", "", $expected); $expected = trim(preg_replace("/\n+/", "\n", $expected)); // echo $actual; // get rid of comments $actual = preg_replace("/\\s*-- .*\$/m", '', $actual); // get rid of extra whitespace $actual = preg_replace("/^ +/m", "", $actual); $actual = trim(preg_replace("/\n+/", "\n", $actual)); $this->assertEquals($expected, $actual); }
public static function build_upgrade($old_output_prefix, $old_composite_file, $old_db_doc, $old_files, $new_output_prefix, $new_composite_file, $new_db_doc, $new_files) { // place the upgrade files with the new_files set $upgrade_prefix = $new_output_prefix . '_upgrade'; // mysql5_diff needs these to intelligently create SQL difference statements in dependency order dbsteward::info("Calculating old table foreign key dependency order.."); mysql5_diff::$old_table_dependency = xml_parser::table_dependency_order($old_db_doc); dbsteward::info("Calculating new table foreign key dependency order.."); mysql5_diff::$new_table_dependency = xml_parser::table_dependency_order($new_db_doc); mysql5_diff::diff_doc($old_composite_file, $new_composite_file, $old_db_doc, $new_db_doc, $upgrade_prefix); return $new_db_doc; }
public function testBuildSchema() { $xml = <<<XML <dbsteward> <database> <host>db-host</host> <name>dbsteward</name> <role> <application>dbsteward_phpunit_app</application> <owner>deployment</owner> <replication/> <readonly/> </role> <!-- should be ignored --> <slony> <masterNode id="1"/> <replicaNode id="2" providerId="1"/> <replicaNode id="3" providerId="2"/> <replicationSet id="1"/> <replicationUpgradeSet id="2"/> </slony> <!-- should be ignored --> <configurationParameter name="TIME ZONE" value="America/New_York"/> </database> <!-- should be ignored --> <language name="plpgsql" procedural="true" owner="ROLE_OWNER"/> <schema name="public" owner="ROLE_OWNER"> <grant operation="SELECT,UPDATE,DELETE" role="ROLE_OWNER"/> <type type="enum" name="permission_level"> <enum name="guest"/> <enum name="user"/> <enum name="admin"/> </type> <function name="a_function" returns="text" owner="ROLE_OWNER" cachePolicy="VOLATILE" description="a test function"> <functionParameter name="config_parameter" type="text"/> <functionParameter name="config_value" type="text"/> <!-- should be ignored --> <functionDefinition language="plpgsql" sqlFormat="pgsql8"> DECLARE q text; name text; n text; BEGIN SELECT INTO name current_database(); q := 'ALTER DATABASE ' || name || ' SET ' || config_parameter || ' ''' || config_value || ''';'; n := 'DB CONFIG CHANGE: ' || q; RAISE NOTICE '%', n; EXECUTE q; RETURN n; END; </functionDefinition> <functionDefinition language="sql" sqlFormat="mysql5"> BEGIN RETURN config_parameter; END </functionDefinition> <grant operation="EXECUTE" role="ROLE_APPLICATION"/> </function> <table name="user" owner="ROLE_OWNER" primaryKey="user_id" slonyId="1"> <column name="user_id" type="int auto_increment" null="false"/> <column name="group_id" foreignSchema="public" foreignTable="group" foreignColumn="group_id" null="false"/> <column name="username" type="varchar(80)"/> <column name="user_age" type="numeric"/> <constraint name="username_unq" type="Unique" definition="(`username`)"/> <grant operation="SELECT,UPDATE,DELETE" role="ROLE_APPLICATION"/> </table> <table name="group" owner="ROLE_OWNER" primaryKey="group_id" slonyId="2"> <column name="group_id" type="int auto_increment" null="false"/> <column name="permission_level" type="permission_level"/> <!-- enum type --> <column name="group_name" type="character varying(100)" unique="true"/> <column name="group_enabled" type="boolean" null="false" default="true"/> <grant operation="SELECT,UPDATE,DELETE" role="ROLE_APPLICATION"/> </table> <sequence name="a_sequence" owner="ROLE_OWNER"> <grant operation="SELECT,UPDATE,DELETE" role="ROLE_APPLICATION"/> </sequence> <trigger name="a_trigger" sqlFormat="mysql5" table="user" when="before" event="insert" function="EXECUTE xyz"/> <!-- should be ignored --> <trigger name="a_trigger" sqlFormat="pgsql8" table="group" when="before" event="delete" function="EXECUTE xyz;"/> <view name="a_view" owner="ROLE_OWNER" description="Description goes here"> <viewQuery sqlFormat="mysql5">SELECT * FROM user, group</viewQuery> <!-- should be ignored --> <viewQuery sqlFormat="pgsql8">SELECT * FROM pgsql8table</viewQuery> <grant operation="SELECT,UPDATE,DELETE" role="ROLE_APPLICATION"/> </view> </schema> <schema name="hotel" owner="ROLE_OWNER"> <table name="rate" owner="ROLE_OWNER" primaryKey="rate_id" slonyId="1"> <column name="rate_id" type="integer" null="false"/> <column name="rate_group_id" foreignSchema="hotel" foreignTable="rate_group" foreignColumn="rate_group_id" null="false"/> <column name="rate_name" type="character varying(120)"/> <column name="rate_value" type="numeric"/> </table> <table name="rate_group" owner="ROLE_OWNER" primaryKey="rate_group_id" slonyId="2"> <column name="rate_group_id" type="integer" null="false"/> <column name="rate_group_name" type="character varying(100)"/> <column name="rate_group_enabled" type="boolean" null="false" default="true"/> </table> </schema> </dbsteward> XML; $expected = <<<SQL GRANT SELECT, UPDATE, DELETE ON * TO `deployment`; DROP FUNCTION IF EXISTS `public_a_function`; CREATE DEFINER = deployment FUNCTION `public_a_function` (`config_parameter` text, `config_value` text) RETURNS text LANGUAGE SQL MODIFIES SQL DATA NOT DETERMINISTIC SQL SECURITY INVOKER COMMENT 'a test function' BEGIN RETURN config_parameter; END; GRANT EXECUTE ON FUNCTION `public_a_function` TO `dbsteward_phpunit_app`; CREATE TABLE `public_user` ( `user_id` int NOT NULL, `group_id` int NOT NULL, `username` varchar(80), `user_age` numeric ); GRANT SELECT, UPDATE, DELETE ON `public_user` TO `dbsteward_phpunit_app`; CREATE TABLE `public_group` ( `group_id` int NOT NULL, `permission_level` ENUM('guest','user','admin'), `group_name` character varying(100), `group_enabled` boolean NOT NULL DEFAULT true ); GRANT SELECT, UPDATE, DELETE ON `public_group` TO `dbsteward_phpunit_app`; CREATE TABLE IF NOT EXISTS `__sequences` ( `name` VARCHAR(100) NOT NULL, `increment` INT(11) unsigned NOT NULL DEFAULT 1, `min_value` INT(11) unsigned NOT NULL DEFAULT 1, `max_value` BIGINT(20) unsigned NOT NULL DEFAULT 18446744073709551615, `cur_value` BIGINT(20) unsigned DEFAULT 1, `start_value` BIGINT(20) unsigned DEFAULT 1, `cycle` BOOLEAN NOT NULL DEFAULT FALSE, `should_advance` BOOLEAN NOT NULL DEFAULT TRUE, PRIMARY KEY (`name`) ) ENGINE = MyISAM; DROP FUNCTION IF EXISTS `currval`; CREATE FUNCTION `currval` (`seq_name` varchar(100)) RETURNS BIGINT(20) NOT DETERMINISTIC BEGIN DECLARE val BIGINT(20); IF @__sequences_lastval IS NULL THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'nextval() has not been called yet this session'; ELSE SELECT `currval` INTO val FROM `__sequences_currvals` WHERE `name` = seq_name; RETURN val; END IF; END; DROP FUNCTION IF EXISTS `lastval`; CREATE FUNCTION `lastval` () RETURNS BIGINT(20) NOT DETERMINISTIC BEGIN IF @__sequences_lastval IS NULL THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'nextval() has not been called yet this session'; ELSE RETURN @__sequences_lastval; END IF; END; DROP FUNCTION IF EXISTS `nextval`; CREATE FUNCTION `nextval` (`seq_name` varchar(100)) RETURNS BIGINT(20) NOT DETERMINISTIC BEGIN DECLARE advance BOOLEAN; CREATE TEMPORARY TABLE IF NOT EXISTS `__sequences_currvals` ( `name` VARCHAR(100) NOT NULL, `currval` BIGINT(20), PRIMARY KEY (`name`) ); SELECT `cur_value` INTO @__sequences_lastval FROM `__sequences` WHERE `name` = seq_name; SELECT `should_advance` INTO advance FROM `__sequences` WHERE `name` = seq_name; IF @__sequences_lastval IS NOT NULL THEN IF advance = TRUE THEN UPDATE `__sequences` SET `cur_value` = IF ( (`cur_value` + `increment`) > `max_value`, IF (`cycle` = TRUE, `min_value`, NULL), `cur_value` + `increment` ) WHERE `name` = seq_name; SELECT `cur_value` INTO @__sequences_lastval FROM `__sequences` WHERE `name` = seq_name; ELSE UPDATE `__sequences` SET `should_advance` = TRUE WHERE `name` = seq_name; END IF; REPLACE INTO `__sequences_currvals` (`name`, `currval`) VALUE (seq_name, @__sequences_lastval); END IF; RETURN @__sequences_lastval; END; DROP FUNCTION IF EXISTS `setval`; CREATE FUNCTION `setval` (`seq_name` varchar(100), `value` bigint(20), `advance` BOOLEAN) RETURNS bigint(20) NOT DETERMINISTIC BEGIN UPDATE `__sequences` SET `cur_value` = value, `should_advance` = advance WHERE `name` = seq_name; IF advance = FALSE THEN CREATE TEMPORARY TABLE IF NOT EXISTS `__sequences_currvals` ( `name` VARCHAR(100) NOT NULL, `currval` BIGINT(20), PRIMARY KEY (`name`) ); REPLACE INTO `__sequences_currvals` (`name`, `currval`) VALUE (seq_name, value); SET @__sequences_lastval = value; END IF; RETURN value; END; INSERT INTO `__sequences` (`name`, `increment`, `min_value`, `max_value`, `cur_value`, `start_value`, `cycle`) VALUES ('a_sequence', DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT); GRANT SELECT, UPDATE, DELETE ON `__sequences` TO `dbsteward_phpunit_app`; DROP TRIGGER IF EXISTS `public_a_trigger`; CREATE TRIGGER `public_a_trigger` BEFORE INSERT ON `public_user` FOR EACH ROW EXECUTE xyz; CREATE TABLE `hotel_rate` ( `rate_id` integer NOT NULL, `rate_group_id` integer NOT NULL, `rate_name` character varying(120), `rate_value` numeric ); CREATE TABLE `hotel_rate_group` ( `rate_group_id` integer NOT NULL, `rate_group_name` character varying(100), `rate_group_enabled` boolean NOT NULL DEFAULT true ); ALTER TABLE `public_user` ADD INDEX `group_id` (`group_id`) USING BTREE, ADD PRIMARY KEY (`user_id`), MODIFY `user_id` int NOT NULL AUTO_INCREMENT; ALTER TABLE `public_group` ADD UNIQUE INDEX `group_name` (`group_name`) USING BTREE, ADD PRIMARY KEY (`group_id`), MODIFY `group_id` int NOT NULL AUTO_INCREMENT; ALTER TABLE `hotel_rate` ADD INDEX `rate_group_id` (`rate_group_id`) USING BTREE, ADD PRIMARY KEY (`rate_id`); ALTER TABLE `hotel_rate_group` ADD PRIMARY KEY (`rate_group_id`); ALTER TABLE `public_user` ADD UNIQUE INDEX `username_unq` (`username`), ADD CONSTRAINT `user_group_id_fkey` FOREIGN KEY `user_group_id_fkey` (`group_id`) REFERENCES `public_group` (`group_id`); ALTER TABLE `hotel_rate` ADD CONSTRAINT `rate_rate_group_id_fkey` FOREIGN KEY `rate_rate_group_id_fkey` (`rate_group_id`) REFERENCES `hotel_rate_group` (`rate_group_id`); CREATE OR REPLACE DEFINER = deployment SQL SECURITY DEFINER VIEW `public_a_view` AS SELECT * FROM user, group; SQL; $dbs = new SimpleXMLElement($xml); $ofs = new mock_output_file_segmenter(); dbsteward::$new_database = $dbs; $table_dependency = xml_parser::table_dependency_order($dbs); mysql5::build_schema($dbs, $ofs, $table_dependency); $actual = $ofs->_get_output(); // var_dump($actual); // get rid of comments // $expected = preg_replace('/\s*-- .*(\n\s*)?/','',$expected); // // get rid of extra whitespace // $expected = trim(preg_replace("/\n\n/","\n",$expected)); $expected = preg_replace("/^ +/m", "", $expected); $expected = trim(preg_replace("/\n+/", "\n", $expected)); // echo $actual; // get rid of comments $actual = preg_replace("/\\s*-- .*\$/m", '', $actual); // get rid of extra whitespace // $actual = trim(preg_replace("/\n\n+/","\n",$actual)); $actual = preg_replace("/^ +/m", "", $actual); $actual = trim(preg_replace("/\n+/", "\n", $actual)); $this->assertEquals($expected, $actual); }
public static function build_upgrade($old_output_prefix, $old_composite_file, $old_db_doc, $old_files, $new_output_prefix, $new_composite_file, $new_db_doc, $new_files) { // place the upgrade files with the new_files set $upgrade_prefix = $new_output_prefix . '_upgrade'; // pgsql8_diff needs these to intelligently create SQL difference statements in dependency order dbsteward::info("Calculating old table foreign key dependency order.."); pgsql8_diff::$old_table_dependency = xml_parser::table_dependency_order($old_db_doc); dbsteward::info("Calculating new table foreign key dependency order.."); pgsql8_diff::$new_table_dependency = xml_parser::table_dependency_order($new_db_doc); pgsql8_diff::diff_doc($old_composite_file, $new_composite_file, $old_db_doc, $new_db_doc, $upgrade_prefix); if (dbsteward::$generate_slonik) { $replica_sets = pgsql8::get_slony_replica_sets($new_db_doc); foreach ($replica_sets as $replica_set) { dbsteward::info("Generating replica set " . $replica_set['id'] . " upgrade slonik"); // separate upgrade slonik file sets for each replica set $slonik_upgrade_prefix = $upgrade_prefix . "_slony_replica_set_" . $replica_set['id']; // generate upgrade slonik to apply generated sql changes $old_new_slonik_header = "# Old definition: " . implode(', ', $old_files) . "\n" . "# New definition: " . implode(', ', $new_files) . "\n" . "# Replica set ID " . $replica_set['id'] . "\n"; $old_replica_set = pgsql8::get_slony_replica_set($old_db_doc, (string) $replica_set['id']); pgsql8::build_upgrade_slonik_replica_set($old_db_doc, $new_db_doc, $old_replica_set, $replica_set, $slonik_upgrade_prefix, $old_new_slonik_header); } } return $new_db_doc; }
protected function transaction_statement_check($is_transactional) { dbsteward::$old_database = new SimpleXMLElement($this->oldxml); dbsteward::$new_database = new SimpleXMLElement($this->newxml); pgsql8_diff::$new_table_dependency = xml_parser::table_dependency_order(dbsteward::$new_database); pgsql8_diff::$old_table_dependency = xml_parser::table_dependency_order(dbsteward::$old_database); $ofs = new mock_output_file_segmenter(); pgsql8_diff::diff_doc_work($ofs, $ofs, $ofs, $ofs); if ($is_transactional) { $this->assertRegExp('/^BEGIN;/im', $ofs->_get_output(), 'output contains BEGIN statement'); $this->assertRegExp('/^COMMIT;/im', $ofs->_get_output(), 'output contains COMMIT statement'); } else { $this->assertRegExp('/^(?!BEGIN;).*$/im', $ofs->_get_output(), 'output contains BEGIN statement'); $this->assertRegExp('/^(?!COMMIT;).*$/im', $ofs->_get_output(), 'output contains COMMIT statement'); } }
protected function build_db($format) { dbsteward::set_sql_format($format); $ofs = new mock_output_file_segmenter(); $doc_a = new SimpleXMLElement($this->{$format . '_xml_a'}); dbsteward::$new_database = $doc_a; $table_dependency_a = xml_parser::table_dependency_order($doc_a); $format::build_schema($doc_a, $ofs, $table_dependency_a); }
public function testExceptionIsThrownWithDupes() { dbsteward::set_sql_format('pgsql8'); dbsteward::$quote_all_names = TRUE; dbsteward::$single_stage_upgrade = TRUE; $doc_empty = simplexml_load_string($this->xml_empty); $doc_empty = xml_parser::composite_doc(FALSE, $doc_empty); dbsteward::$old_database = $doc_empty; $doc = simplexml_load_string($this->xml); $doc = xml_parser::composite_doc(FALSE, $doc); dbsteward::$new_database = $doc; $table_dependency = xml_parser::table_dependency_order($doc); $schema = $doc->schema; $table = $schema->table; // make sure the type is named with quoting as part of a definition build $mofs = new mock_output_file_segmenter(); try { pgsql8::build_schema($doc, $mofs, $table_dependency); } catch (Exception $e) { $this->assertContains('duplicate index name', strtolower($e->getMessage())); return; } $this->fail("build_schema did not detect duplicate index names"); }
protected function diff_definitions($output_prefix) { dbsteward::$old_database = new SimpleXMLElement($this->oldxml); dbsteward::$new_database = new SimpleXMLElement($this->newxml); pgsql8_diff::$new_table_dependency = xml_parser::table_dependency_order(dbsteward::$new_database); pgsql8_diff::$old_table_dependency = xml_parser::table_dependency_order(dbsteward::$old_database); $output_prefix_path = dirname(__FILE__) . '/../testdata/' . $output_prefix; pgsql8::build_upgrade('', 'old_SlonyStageTransactionalityTest', dbsteward::$old_database, array(), $output_prefix_path, 'new_SlonyStageTransactionalityTest', dbsteward::$new_database, array()); return $output_prefix_path; }