public function testCreateTable() { $xml = <<<XML <schema name="public" owner="NOBODY"> <table name="test" primaryKey="id" description="test description"> <tableOption sqlFormat="pgsql8" name="tablespace" value="schmableschpace"/> <tableOption sqlFormat="pgsql8" name="with" value="(oids=true,fillfactor=70)"/> <tableOption sqlFormat="mysql5" name="auto_increment" value="5"/> <column name="id" type="int"/> <column name="foo" type="int"/> </table> </schema> XML; $schema = new SimpleXMLElement($xml); $expected = <<<SQL CREATE TABLE "public"."test" ( \t"id" int, \t"foo" int ) WITH (oids=true,fillfactor=70) TABLESPACE schmableschpace; COMMENT ON TABLE "public"."test" IS 'test description'; SQL; $actual = pgsql8_table::get_creation_sql($schema, $schema->table); $this->assertEquals($expected, trim($actual)); }
public static function build_schema($db_doc, $ofs, $table_depends) { // schema creation foreach ($db_doc->schema as $schema) { $ofs->write(pgsql8_schema::get_creation_sql($schema)); // schema grants if (isset($schema->grant)) { foreach ($schema->grant as $grant) { $ofs->write(pgsql8_permission::get_sql($db_doc, $schema, $schema, $grant) . "\n"); } } } // types: enumerated list, etc foreach ($db_doc->schema as $schema) { foreach ($schema->type as $type) { $ofs->write(pgsql8_type::get_creation_sql($schema, $type) . "\n"); } } // table structure creation foreach ($db_doc->schema as $schema) { // create defined tables pgsql8_table::$include_column_default_nextval_in_create_sql = FALSE; foreach ($schema->table as $table) { // table definition $ofs->write(pgsql8_table::get_creation_sql($schema, $table) . "\n"); // table indexes pgsql8_diff_indexes::diff_indexes_table($ofs, NULL, NULL, $schema, $table); // table grants if (isset($table->grant)) { foreach ($table->grant as $grant) { $ofs->write(pgsql8_permission::get_sql($db_doc, $schema, $table, $grant) . "\n"); } } $ofs->write("\n"); } pgsql8_table::$include_column_default_nextval_in_create_sql = TRUE; // sequences contained in the schema if (isset($schema->sequence)) { foreach ($schema->sequence as $sequence) { $ofs->write(pgsql8_sequence::get_creation_sql($schema, $sequence)); // sequence permission grants if (isset($sequence->grant)) { foreach ($sequence->grant as $grant) { $ofs->write(pgsql8_permission::get_sql($db_doc, $schema, $sequence, $grant) . "\n"); } } } } // add table nextvals that were omitted foreach ($schema->table as $table) { if (pgsql8_table::has_default_nextval($table)) { $ofs->write(pgsql8_table::get_default_nextval_sql($schema, $table) . "\n"); } } } $ofs->write("\n"); // function definitions foreach ($db_doc->schema as $schema) { foreach ($schema->function as $function) { if (pgsql8_function::has_definition($function)) { $ofs->write(pgsql8_function::get_creation_sql($schema, $function)); // when pg:build_schema() is doing its thing for straight builds, include function permissions // they are not included in pg_function::get_creation_sql() foreach (dbx::get_permissions($function) as $function_permission) { $ofs->write(pgsql8_permission::get_sql($db_doc, $schema, $function, $function_permission) . "\n"); } } } } $ofs->write("\n"); // maybe move this but here we're defining column defaults fo realz foreach ($db_doc->schema as $schema) { foreach ($schema->table as $table) { $ofs->write(pgsql8_table::define_table_column_defaults($schema, $table)); } } // define table primary keys before foreign keys so unique requirements are always met for FOREIGN KEY constraints foreach ($db_doc->schema as $schema) { foreach ($schema->table as $table) { pgsql8_diff_tables::diff_constraints_table($ofs, NULL, NULL, $schema, $table, 'primaryKey', FALSE); } } $ofs->write("\n"); // foreign key references // use the dependency order to specify foreign keys in an order that will satisfy nested foreign keys and etc for ($i = 0; $i < count($table_depends); $i++) { $schema = $table_depends[$i]['schema']; $table = $table_depends[$i]['table']; if ($table['name'] === dbsteward::TABLE_DEPENDENCY_IGNORABLE_NAME) { // don't do anything with this table, it is a magic internal DBSteward value continue; } pgsql8_diff_tables::diff_constraints_table($ofs, NULL, NULL, $schema, $table, 'constraint', FALSE); } $ofs->write("\n"); // trigger definitions foreach ($db_doc->schema as $schema) { foreach ($schema->trigger as $trigger) { // only do triggers set to the current sql format if (strcasecmp($trigger['sqlFormat'], dbsteward::get_sql_format()) == 0) { $ofs->write(pgsql8_trigger::get_creation_sql($schema, $trigger)); } } } $ofs->write("\n"); pgsql8_diff_views::create_views_ordered($ofs, null, $db_doc); // view permission grants foreach ($db_doc->schema as $schema) { foreach ($schema->view as $view) { if (isset($view->grant)) { foreach ($view->grant as $grant) { $ofs->write(pgsql8_permission::get_sql($db_doc, $schema, $view, $grant) . "\n"); } } } } $ofs->write("\n"); // use pgdiff to add any configurationParameters that are defined pgsql8_diff::update_database_config_parameters($ofs, null, $db_doc); }
/** * @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); }
/** * Outputs commands for creation of new tables. * * @param $ofs output file pointer * @param $old_schema original schema * @param $new_schema new schema */ private static function create_tables($ofs, $old_schema, $new_schema, $old_table = null, $new_table = null) { foreach (dbx::get_tables($new_schema) as $table) { if ($new_table != null) { if (strcasecmp($table['name'], $new_table['name']) != 0) { continue; } } if ($old_schema == null || !pgsql8_schema::contains_table($old_schema, $table['name'])) { if (!dbsteward::$ignore_oldnames && pgsql8_diff_tables::is_renamed_table($new_schema, $table)) { // oldTableName renamed table ? rename table instead of create new one $old_table_schema = pgsql8_table::get_old_table_schema($new_schema, $table); $old_table_name = pgsql8::get_quoted_schema_name($old_table_schema['name']) . '.' . pgsql8::get_quoted_table_name($table['oldTableName']); // ALTER TABLE ... RENAME TO does not accept schema qualifiers when renaming a table // ALTER TABLE message.message_report RENAME TO report ; $new_table_name = pgsql8::get_quoted_table_name($table['name']); $ofs->write("-- table rename from oldTableName specification" . "\n" . "ALTER TABLE {$old_table_name} RENAME TO {$new_table_name} ;" . "\n"); // if the schema changed for the table as well, set it in the new schema if (strcasecmp($old_table_schema['name'], $new_schema['name']) != 0) { // the table has been renamed, but is still in the old schema, resolve this fully qualified name $old_schema_new_table_name = pgsql8::get_quoted_schema_name($old_table_schema['name']) . '.' . $new_table_name; $new_schema_name = pgsql8::get_quoted_schema_name($new_schema['name']); $ofs->write("-- table reschema from oldSchemaName specification" . "\n" . "ALTER TABLE {$old_schema_new_table_name} SET SCHEMA {$new_schema_name} ;" . "\n"); } } else { $ofs->write(pgsql8_table::get_creation_sql($new_schema, $table) . "\n"); // the table is getting added in an upgrade diff. // need to apply column defaults and not nulls $ofs->write(pgsql8_table::define_table_column_defaults($new_schema, $table) . "\n"); } } } }