/**
  * 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");
                 }
             }
         }
     }
 }
Beispiel #2
0
 /**
  * Given a type name like `schema`.`enum_name`, find the type node in schema 'schema' with name 'enum_name'
  * @return SimpleXMLElement
  */
 public static function get_type_node($db_doc, $node_schema, $name_ref)
 {
     if (preg_match('/(?:["`]?(\\w+)["`]?\\.)?["`]?(.+)["`]?/', $name_ref, $matches) > 0) {
         $schema_ref = $matches[1];
         $type_name = $matches[2];
         // if we found a schema name in the name reference, then attempt to override the given node_schema with the named one
         if (!$schema_ref) {
             if (!$node_schema) {
                 throw new Exception("No schema node given and no schema name found in type name reference '{$name_ref}'");
             }
         } else {
             $node_schema = dbx::get_schema($db_doc, $schema_ref);
             if (!$node_schema) {
                 throw new Exception("Could not find schema '{$schema_ref}', given by type name reference '{$name_ref}'");
             }
         }
         $node_type = dbx::get_type($node_schema, $type_name);
         if (!$node_type) {
             // we did not find the given type - this is not exceptional because we might just be testing to see if it exists
             return NULL;
         }
         // if we got this far, we found the referenced type node
         return $node_type;
     }
     throw new Exception("Unrecognizable type name reference: '{$name_ref}'");
 }
 /**
  * Drop removed types
  * Add new types
  * Apply type definition differences, updating the type's tables along the way
  *
  * @param $ofs          output file segmenter
  * @param $old_schema   original schema
  * @param $new_schema   new schema
  */
 public static function apply_changes($ofs, $old_schema, $new_schema)
 {
     // drop any types that are no longer defined
     self::drop_types($ofs, $old_schema, $new_schema);
     // create any types that are new in the new definition
     self::create_types($ofs, $old_schema, $new_schema);
     // there is no alter for types
     // find types that still exist that are different
     // placehold type data in table columns, and recreate the type
     foreach (dbx::get_types($new_schema) as $new_type) {
         // does type exist in old definition ?
         if ($old_schema == NULL || !mssql10_schema::contains_type($old_schema, $new_type['name'])) {
             continue;
         }
         $old_type = dbx::get_type($old_schema, $new_type['name']);
         // is there a difference between the old and new type definitions?
         if (mssql10_type::equals($old_schema, $old_type, $new_schema, $new_type)) {
             continue;
         }
         $columns = array();
         $ofs->write("-- type " . $new_type['name'] . " definition migration (1/4): dependant tables column constraint drop\n");
         $ofs->write(mssql10_type::column_constraint_temporary_drop($columns, $old_schema, $old_type) . "\n");
         $ofs->write("-- type " . $new_type['name'] . " definition migration (2/4): delete type list values\n");
         $ofs->write(mssql10_type::get_enum_value_delete($old_schema, $old_type) . "\n");
         $ofs->write("-- type " . $new_type['name'] . " definition migration (3/4): recreate type list values\n");
         $ofs->write(mssql10_type::get_enum_value_insert($new_schema, $new_type) . "\n");
         $ofs->write("-- type " . $new_type['name'] . " definition migration (4/4): restore dependant tables column constraint add\n");
         $ofs->write(mssql10_type::column_constraint_restore($columns, $new_schema, $new_type) . "\n");
     }
 }
 /**
  * Drop removed types
  * Add new types
  * Apply type definition differences, updating the type's tables along the way
  *
  * @param $ofs          output segementer
  * @param $old_schema   original schema
  * @param $new_schema   new schema
  */
 public static function apply_changes($ofs, $old_schema, $new_schema)
 {
     // drop any types that are no longer defined
     self::drop_types($ofs, $old_schema, $new_schema);
     // create any types that are new in the new definition
     self::create_types($ofs, $old_schema, $new_schema);
     // there is no alter for types
     // find types that still exist that are different
     // placehold type data in table columns, and recreate the type
     foreach (dbx::get_types($new_schema) as $new_type) {
         // does type exist in old definition ?
         if ($old_schema == NULL || !mysql5_schema::contains_type($old_schema, $new_type['name'])) {
             continue;
         }
         $old_type = dbx::get_type($old_schema, $new_type['name']);
         // is there a difference between the old and new type definitions?
         if (mysql5_type::equals($old_schema, $old_type, $new_schema, $new_type)) {
             continue;
         }
     }
 }
 /**
  * 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 (!mssql10_schema::contains_function($new_schema, mssql10_function::get_declaration($new_schema, $old_function))) {
                 $ofs3->write(mssql10_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'], mssql10_function::get_declaration($new_schema, $new_function));
         }
         if ($old_function == NULL) {
             $ofs1->write(mssql10_function::get_creation_sql($new_schema, $new_function) . "\n");
         } else {
             if (!mssql10_function::equals($new_schema, $new_function, $old_function, mssql10_diff::$ignore_function_whitespace)) {
                 // functions are not equal, old_function is not null, it previously existed
                 // for MSSQL, there is no CREATE OR REPLACE FUNCTION, so drop the function explicitly
                 $ofs1->write(mssql10_function::get_drop_sql($old_schema, $old_function) . "\n");
                 $ofs1->write(mssql10_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(mssql10_function::get_creation_sql($new_schema, $new_function) . "\n");
                 } else {
                     if (mssql10_schema::contains_type($new_schema, $new_function['returns']) && mssql10_schema::contains_type($old_schema, $new_function['returns']) && !mssql10_type::equals(dbx::get_type($old_schema, $new_function['returns']), dbx::get_type($new_schema, $new_function['returns']))) {
                         $ofs1->write("-- dbstward insisting on function re-creation " . $new_function['name'] . " for type " . $new_function['returns'] . " definition change\n");
                         $ofs1->write(mssql10_function::get_drop_sql($old_schema, $old_function) . "\n");
                         $ofs1->write(mssql10_function::get_creation_sql($new_schema, $new_function) . "\n");
                     }
                 }
             }
         }
     }
 }
 /**
  * Drop removed types
  * Add new types
  * Apply type definition differences, updating the type's tables along the way
  *
  * @param $ofs          output segementer
  * @param $old_schema   original schema
  * @param $new_schema   new schema
  */
 public static function apply_changes($ofs, $old_schema, $new_schema)
 {
     // drop any types that are no longer defined
     self::drop_types($ofs, $old_schema, $new_schema);
     // create any types that are new in the new definition
     self::create_types($ofs, $old_schema, $new_schema);
     // there is no alter for types
     // find types that still exist that are different
     // placehold type data in table columns, and recreate the type
     foreach (dbx::get_types($new_schema) as $new_type) {
         // does type exist in old definition ?
         if ($old_schema == NULL || !pgsql8_schema::contains_type($old_schema, $new_type['name'])) {
             continue;
         }
         $old_type = dbx::get_type($old_schema, $new_type['name']);
         // is there a difference between the old and new type definitions?
         if (pgsql8_type::equals($old_schema, $old_type, $new_schema, $new_type)) {
             continue;
         }
         pgsql8::set_context_replica_set_id($new_type);
         $columns = array();
         $is_domain = strcasecmp($new_type['type'], 'domain') === 0;
         $n = $is_domain ? 3 : 4;
         $word = $is_domain ? 'domain' : 'type';
         $i = 1;
         $old_dependent_functions = dbx::get_functions_with_dependent_type($old_schema, (string) $old_type['name']);
         if (count($old_dependent_functions) > 0) {
             $n += 2;
             $ofs->write("-- {$word} {$new_type['name']} definition migration ({$i}/{$n}): dependent functions return/parameter type alteration\n");
             $i++;
             foreach ($old_dependent_functions as $old_dependent_function) {
                 $ofs->write(pgsql8_function::get_drop_sql($old_schema, $old_dependent_function) . "\n");
             }
             $ofs->write("\n");
         }
         $ofs->write("-- {$word} {$new_type['name']} definition migration ({$i}/{$n}): dependent tables column type alteration\n");
         $i++;
         $ofs->write(pgsql8_type::alter_column_type_placeholder($columns, $old_schema, $old_type) . "\n");
         if ($is_domain) {
             $ofs->write("-- {$word} {$new_type['name']} definition migration ({$i}/{$n}): alter domain\n");
             $i++;
             self::apply_domain_changes($ofs, $old_schema, $old_type, $new_schema, $new_type);
         } else {
             $ofs->write("-- {$word} {$new_type['name']} definition migration ({$i}/{$n}): drop old type\n");
             $i++;
             $ofs->write(pgsql8_type::get_drop_sql($old_schema, $old_type) . "\n\n");
             $ofs->write("-- {$word} {$new_type['name']} definition migration ({$i}/{$n}): recreate type with new definition\n");
             $i++;
             $ofs->write(pgsql8_type::get_creation_sql($new_schema, $new_type) . "\n\n");
         }
         // functions are only recreated if they changed elsewise, so need to create them here
         $new_dependent_functions = dbx::get_functions_with_dependent_type($new_schema, (string) $new_type['name']);
         if (count($new_dependent_functions) > 0) {
             $ofs->write("-- {$word} {$new_type['name']} definition migration ({$i}/{$n}): dependent functions return/parameter type with new definition\n");
             $i++;
             foreach ($new_dependent_functions as $new_dependent_function) {
                 $ofs->write(pgsql8_function::get_creation_sql($new_schema, $new_dependent_function) . "\n");
             }
             $ofs->write("\n\n");
         }
         $ofs->write("-- {$word} {$new_type['name']} definition migration ({$n}/{$n}): dependent tables type restoration\n");
         $ofs->write(pgsql8_type::alter_column_type_restore($columns, $new_schema, $new_type) . "\n");
     }
 }
 public static function column_type($db_doc, $schema, $table, $column, &$foreign)
 {
     // if it is a foreign keyed column, solve for the foreignKey type
     if (isset($column['foreignTable'])) {
         dbx::foreign_key($db_doc, $schema, $table, $column, $foreign);
         $column_type = $foreign['column']['type'];
         // for foreign keys, translate serial types to their integer base
         if (strcasecmp('serial', $column_type) == 0) {
             $column_type = 'int';
         } else {
             if (strcasecmp('bigserial', $column_type) == 0) {
                 $column_type = 'bigint';
             }
         }
     } else {
         if (!isset($column['type']) || strlen($column['type']) == 0) {
             throw new Exception("column missing type -- " . $schema['name'] . "." . $table['name'] . "." . $column['name']);
         }
         $column_type = $column['type'];
         if (dbx::get_type($schema, $column_type) != NULL) {
             // this is a user defined type or enum, enforce quoting if set
             $column_type = pgsql8::get_quoted_object_name($column_type);
         }
     }
     return $column_type;
 }
 public static function enum_type_check($db_doc, $node_schema, $node_table, $node_column, &$drop_sql, &$add_sql)
 {
     // if the column type is a defined enum, (re)add a check constraint to enforce the pseudo-enum
     $foreign = array();
     $column_type = mssql10_column::column_type($db_doc, $node_schema, $node_table, $node_column, $foreign, FALSE);
     if (preg_match('/' . dbx::enum_regex($db_doc) . '/i', $column_type) > 0) {
         $type_schema_name = sql_parser::get_schema_name($column_type, $db_doc);
         $type_schema = dbx::get_schema($db_doc, $type_schema_name);
         $node_type = dbx::get_type($type_schema, sql_parser::get_object_name($column_type, $db_doc));
         if (!$node_type) {
             var_dump($node_type);
             throw new exception('failed to find column_type ' . $column_type . ' in type_schema_name ' . $type_schema_name);
         }
         $drop_sql = mssql10_type::get_drop_check_sql($node_schema, $node_table, $node_column, $node_type);
         $add_sql = mssql10_type::get_add_check_sql($node_schema, $node_table, $node_column, $node_type);
         return TRUE;
     }
     return FALSE;
 }