/**
  * Outputs DDL for differences in functions
  *
  * @param $ofs1       stage1 output pointer
  * @param $ofs3       stage3 output pointer
  * @param $old_schema original schema
  * @param $new_schema new schema
  */
 public static function diff_functions($ofs1, $ofs3, $old_schema, $new_schema)
 {
     // drop functions that no longer exist in stage 3
     if ($old_schema != null) {
         foreach (dbx::get_functions($old_schema) as $old_function) {
             if (!pgsql8_schema::contains_function($new_schema, pgsql8_function::get_declaration($new_schema, $old_function, FALSE))) {
                 $ofs3->write(pgsql8_function::get_drop_sql($old_schema, $old_function) . "\n");
             }
         }
     }
     // Add new functions and replace modified functions
     foreach (dbx::get_functions($new_schema) as $new_function) {
         $old_function = null;
         if ($old_schema != null) {
             $old_function = dbx::get_function($old_schema, $new_function['name'], pgsql8_function::get_declaration($new_schema, $new_function));
         }
         if ($old_function == null || !pgsql8_function::equals($new_schema, $new_function, $old_function, pgsql8_diff::$ignore_function_whitespace)) {
             $ofs1->write(pgsql8_function::get_creation_sql($new_schema, $new_function) . "\n");
         } else {
             if (isset($new_function['forceRedefine']) && strcasecmp($new_function['forceRedefine'], 'true') == 0) {
                 $ofs1->write("-- DBSteward insists on function recreation: " . $new_schema['name'] . "." . $new_function['name'] . " has forceRedefine set to true\n");
                 $ofs1->write(pgsql8_function::get_creation_sql($new_schema, $new_function) . "\n");
             } else {
                 if (pgsql8_schema::contains_type($new_schema, $new_function['returns']) && pgsql8_schema::contains_type($old_schema, $new_function['returns']) && !pgsql8_type::equals(dbx::get_type($old_schema, $new_function['returns']), dbx::get_type($new_schema, $new_function['returns']))) {
                     $ofs1->write("-- Force function re-creation " . $new_function['name'] . " for type: " . $new_function['returns'] . "\n");
                     $ofs1->write(pgsql8_function::get_creation_sql($new_schema, $new_function) . "\n");
                 }
             }
         }
     }
 }
Ejemplo n.º 2
0
 private function build($xml, $expected)
 {
     $schema = simplexml_load_string($xml);
     $type = $schema->type;
     $sql = pgsql8_type::get_creation_sql($schema, $type);
     $this->assertEquals($expected, $sql);
 }
Ejemplo n.º 3
0
 /**
  * change all tables columns thare are $node_type to a placeholder type
  *
  * @param $columns      reference columns returned by reference
  * @param $node_schema
  * @param $node_type
  * @return string DDL
  */
 public static function alter_column_type_placeholder(&$columns, $node_schema, $node_type)
 {
     $ddl = '';
     for ($i = 0; $i < count(pgsql8_diff::$new_table_dependency); $i++) {
         // find the necessary pointers
         $table_item = pgsql8_diff::$new_table_dependency[$i];
         if ($table_item['table']['name'] === dbsteward::TABLE_DEPENDENCY_IGNORABLE_NAME) {
             // don't do anything with this table, it is a magic internal DBSteward value
             continue;
         }
         $new_schema = $table_item['schema'];
         $new_table = $table_item['table'];
         foreach (dbx::get_table_columns($new_table) as $new_column) {
             // is the column the passed type?
             $unquoted_type_name = $node_schema['name'] . '.' . $node_type['name'];
             if (strcasecmp($new_column['type'], $unquoted_type_name) == 0) {
                 $ddl .= "ALTER TABLE " . pgsql8::get_quoted_schema_name($new_schema['name']) . '.' . pgsql8::get_quoted_table_name($new_table['name']) . " ALTER COLUMN " . pgsql8::get_quoted_column_name($new_column['name']) . " TYPE " . pgsql8_type::alter_column_type_placeholder_type($node_type) . ";\n";
                 // add column to the beginning of the list so it will be done before earlier changes (foreign key ordering)
                 array_unshift($columns, array('alter_column_schema' => $new_schema, 'alter_column_table' => $new_table, 'alter_column_column' => $new_column));
             }
         }
     }
     return $ddl;
 }
Ejemplo n.º 4
0
 /**
  * Outputs commands for dropping types.
  *
  * @param $ofs          output file pointer
  * @param $old_schema   original schema
  * @param $new_schema   new schema
  */
 private static function drop_types($ofs, $old_schema, $new_schema)
 {
     if ($old_schema != NULL) {
         foreach (dbx::get_types($old_schema) as $type) {
             if (!pgsql8_schema::contains_type($new_schema, $type['name'])) {
                 pgsql8::set_context_replica_set_id($type);
                 $ofs->write(pgsql8_type::get_drop_sql($new_schema, $type) . "\n");
             }
         }
     }
 }
Ejemplo n.º 5
0
 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);
 }