/** * Updates data in table definitions * * @param ofs output file segmenter * @param $old_database original database * @param $new_database new database */ private static function update_data($ofs, $delete_mode = false) { if (self::$new_table_dependency != null && count(self::$new_table_dependency) > 0) { for ($i = 0; $i < count(self::$new_table_dependency); $i++) { // go in reverse when in delete mode if ($delete_mode) { $item = self::$new_table_dependency[count(self::$new_table_dependency) - 1 - $i]; } else { $item = self::$new_table_dependency[$i]; } if ($item['table']['name'] === dbsteward::TABLE_DEPENDENCY_IGNORABLE_NAME) { // don't do anything with this table, it is a magic internal DBSteward value continue; } $old_schema = dbx::get_schema(dbsteward::$old_database, $item['schema']['name']); $old_table = null; if ($old_schema != null) { $old_table = dbx::get_table($old_schema, $item['table']['name']); } $new_schema = dbx::get_schema(dbsteward::$new_database, $item['schema']['name']); if ($new_schema == null) { throw new exception("schema " . $item['schema']['name'] . " not found in new database"); } $new_table = dbx::get_table($new_schema, $item['table']['name']); if ($new_table == null) { throw new exception("table " . $item['table']['name'] . " not found in new database schema " . $new_schema['name']); } pgsql8::set_context_replica_set_id($new_schema); // if the table was renamed, get old definition pointers for comparison if (pgsql8_diff_tables::is_renamed_table($new_schema, $new_table)) { dbsteward::info("NOTICE: " . $new_schema['name'] . "." . $new_table['name'] . " used to be called " . $new_table['oldTableName'] . " -- will diff data against that definition"); $old_schema = pgsql8_table::get_old_table_schema($new_schema, $new_table); $old_table = pgsql8_table::get_old_table($new_schema, $new_table); } $ofs->write(pgsql8_diff_tables::get_data_sql($old_schema, $old_table, $new_schema, $new_table, $delete_mode)); } } else { // dependency order unknown, hit them in natural order foreach (dbx::get_schemas(dbsteward::$new_database) as $new_schema) { pgsql8::set_context_replica_set_id($new_schema); $old_schema = dbx::get_schema(dbsteward::$old_database, $new_schema['name']); pgsql8_diff_tables::diff_data($ofs, $old_schema, $new_schema); } } }
public static function diff_constraints_table($ofs, $old_schema, $old_table, $new_schema, $new_table, $type, $drop_constraints = false) { pgsql8::set_context_replica_set_id($new_table); if ($drop_constraints) { // drop constraints that no longer exist or are modified foreach (self::get_drop_constraints($old_schema, $old_table, $new_schema, $new_table, $type) as $constraint) { $ofs->write(pgsql8_table::get_constraint_drop_sql($constraint) . "\n"); } } else { if (!dbsteward::$ignore_oldnames) { // if it is a renamed table if (pgsql8_diff_tables::is_renamed_table($new_schema, $new_table)) { // remove all constraints and recreate with new table name conventions foreach (pgsql8_constraint::get_table_constraints(dbsteward::$old_database, $old_schema, $old_table, $type) as $constraint) { // rewrite the constraint definer to refer to the new table name // so the constraint by the old name, but part of the new table // will be referenced properly in the drop statement $constraint['schema_name'] = $new_schema['name']; $constraint['table_name'] = $new_table['name']; $ofs->write(pgsql8_table::get_constraint_drop_sql($constraint) . "\n"); } // add all still-define constraints back and any new ones to the table foreach (pgsql8_constraint::get_table_constraints(dbsteward::$new_database, $new_schema, $new_table, $type) as $constraint) { $ofs->write(pgsql8_table::get_constraint_sql($constraint) . "\n"); } // this gets any new constraints as well. // return so that get_new_costraint() doesnt duplicate any new constraints return; } } // add new constraints foreach (self::get_new_constraints($old_schema, $old_table, $new_schema, $new_table, $type) as $constraint) { $ofs->write(pgsql8_table::get_constraint_sql($constraint) . "\n"); } } }