/** * 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 (!mysql5_schema::contains_function($new_schema, mysql5_function::get_declaration($new_schema, $old_function))) { $ofs3->write(mysql5_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'], mysql5_function::get_declaration($new_schema, $new_function)); } if ($old_function == null || !mysql5_function::equals($new_schema, $new_function, $old_function, mysql5_diff::$ignore_function_whitespace)) { $ofs1->write(mysql5_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(mysql5_function::get_creation_sql($new_schema, $new_function) . "\n"); } else { if (mysql5_schema::contains_type($new_schema, $new_function['returns']) && mysql5_schema::contains_type($old_schema, $new_function['returns']) && !mysql5_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(mysql5_function::get_creation_sql($new_schema, $new_function) . "\n"); } } } } }
private static function schema_contains_sequence($schema, $sequence_name, $include_oldnames = FALSE) { if (mysql5_schema::contains_sequence($schema, $sequence_name)) { return TRUE; } foreach (dbx::get_tables($schema) as $table) { foreach (mysql5_table::get_sequences_needed($schema, $table) as $sequence) { if (strcasecmp($sequence['name'], $sequence_name) === 0) { return TRUE; } if ($include_oldnames && !dbsteward::$ignore_oldnames && !empty($sequence['oldSequenceName']) && strcasecmp($sequence['oldSequenceName'], $sequence_name) === 0) { return TRUE; } } } return FALSE; }
/** * 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; } } }
/** * Drop tables in old_schema no longer defined in new_schema * * @param type $ofs * @param type $old_schema * @param type $new_schema * @param type $old_table * @param type $new_table */ public static function drop_tables($ofs, $old_schema, $new_schema, $old_table = null, $new_table = null) { if ($old_schema != null) { if ($old_table != null) { $tables = array($old_table); } else { $tables = dbx::get_tables($old_schema); } foreach ($tables as $table) { // does the new schema contain the old table? if (!mysql5_schema::contains_table($new_schema, $table['name'])) { // if the table was renamed, don't drop it if (!dbsteward::$ignore_oldnames && mysql5_schema::table_formerly_known_as(dbsteward::$new_database, $old_schema, $table, $reformed_schema, $reformed_table)) { $old_table_name = mysql5::get_fully_qualified_table_name($old_schema['name'], $table['name']); $reformed_table_name = mysql5::get_fully_qualified_table_name($reformed_schema['name'], $reformed_table['name']); $ofs->write("-- DROP TABLE {$old_table_name} omitted: new table {$reformed_table_name} indicates it is her replacement\n"); } else { $ofs->write(mysql5_table::get_drop_sql($old_schema, $table) . "\n"); } } } } }