public static function diff_tables($ofs1, $ofs3, $old_schema, $new_schema, $old_table_target = null, $new_table_target = null)
 {
     static::create_tables($ofs1, $old_schema, $new_schema, $old_table_target, $new_table_target);
     // were specific tables passed?
     if ($old_table_target !== null || $new_table_target !== null) {
         $old_table = $old_table_target;
         $new_table = $new_table_target;
         if ($old_table && $new_table) {
             static::update_table_options($ofs1, $ofs3, $old_schema, $old_table, $new_schema, $new_table);
             static::update_table_columns($ofs1, $ofs3, $old_schema, $old_table, $new_schema, $new_table);
             static::update_table_partitions($ofs1, $ofs3, $old_schema, $old_table, $new_schema, $new_table);
         }
     } else {
         foreach (dbx::get_tables($new_schema) as $new_table) {
             if (!$old_schema) {
                 // old_schema not defined
                 continue;
             }
             $old_table = dbx::get_table($old_schema, $new_table['name']);
             dbx::renamed_table_check_pointer($old_schema, $old_table, $new_schema, $new_table);
             if (!$old_table) {
                 // old_table not defined
                 continue;
             }
             static::update_table_options($ofs1, $ofs3, $old_schema, $old_table, $new_schema, $new_table);
             static::update_table_columns($ofs1, $ofs3, $old_schema, $old_table, $new_schema, $new_table);
             static::update_table_partitions($ofs1, $ofs3, $old_schema, $old_table, $new_schema, $new_table);
         }
     }
 }
Beispiel #2
0
 /**
  * Updates objects in schemas.
  *
  * @param $ofs1  stage1 output file segmenter
  * @param $ofs3  stage3 output file segmenter
  */
 public static function update_structure($ofs1, $ofs3)
 {
     if (!mysql5::$use_schema_name_prefix) {
         if (count(dbsteward::$new_database->schema) > 1) {
             throw new Exception("You cannot use more than one schema in mysql5 without schema name prefixing\nPass the --useschemaprefix flag to turn this on");
         }
         if (count(dbsteward::$old_database->schema) > 1) {
             throw new Exception("You cannot use more than one schema in mysql5 without schema name prefixing\nPass the --useschemaprefix flag to turn this on");
         }
     } else {
         dbsteward::info("Drop Old Schemas");
         self::drop_old_schemas($ofs3);
     }
     mysql5_diff_views::drop_views_ordered($ofs1, dbsteward::$old_database, dbsteward::$new_database);
     //@TODO: implement mysql5_language ? no relevant conversion exists see other TODO's stating this
     //mysql5_diff_languages::diff_languages($ofs1);
     // if the table dependency order is unknown, bang them in natural order
     if (!is_array(mysql5_diff::$new_table_dependency)) {
         foreach (dbx::get_schemas(dbsteward::$new_database) as $new_schema) {
             //@NOTICE: @TODO: this does not honor old*Name attributes, does it matter?
             $old_schema = dbx::get_schema(dbsteward::$old_database, $new_schema['name']);
             mysql5_diff_types::apply_changes($ofs1, $old_schema, $new_schema);
             mysql5_diff_functions::diff_functions($ofs1, $ofs3, $old_schema, $new_schema);
             mysql5_diff_sequences::diff_sequences($ofs1, $ofs3, $old_schema, $new_schema);
             // remove old constraints before table contraints, so the SQL statements succeed
             mysql5_diff_constraints::diff_constraints($ofs1, $old_schema, $new_schema, 'constraint', TRUE);
             mysql5_diff_constraints::diff_constraints($ofs1, $old_schema, $new_schema, 'primaryKey', TRUE);
             mysql5_diff_tables::drop_tables($ofs3, $old_schema, $new_schema);
             mysql5_diff_tables::diff_tables($ofs1, $ofs3, $old_schema, $new_schema);
             // mysql5_diff_indexes::diff_indexes($ofs1, $old_schema, $new_schema);
             mysql5_diff_constraints::diff_constraints($ofs1, $old_schema, $new_schema, 'primaryKey', FALSE);
             mysql5_diff_triggers::diff_triggers($ofs1, $old_schema, $new_schema);
         }
         // non-primary key constraints may be inter-schema dependant, and dependant on other's primary keys
         // and therefore should be done after object creation sections
         foreach (dbx::get_schemas(dbsteward::$new_database) as $new_schema) {
             $old_schema = dbx::get_schema(dbsteward::$old_database, $new_schema['name']);
             mysql5_diff_constraints::diff_constraints($ofs1, $old_schema, $new_schema, 'constraint', FALSE);
         }
     } else {
         $processed_schemas = array();
         for ($i = 0; $i < count(mysql5_diff::$new_table_dependency); $i++) {
             // find the necessary pointers
             $item = mysql5_diff::$new_table_dependency[$i];
             // @NOTICE: dbsteward::TABLE_DEPENDENCY_IGNORABLE_NAME is NOT checked here because these are schema operations
             $new_schema = dbx::get_schema(dbsteward::$new_database, $item['schema']['name']);
             $old_schema = dbx::get_schema(dbsteward::$old_database, $item['schema']['name']);
             // do all types and functions on their own before table creation
             // see next loop for other once per schema work
             if (!in_array(trim($new_schema['name']), $processed_schemas)) {
                 mysql5_diff_types::apply_changes($ofs1, $old_schema, $new_schema);
                 mysql5_diff_functions::diff_functions($ofs1, $ofs3, $old_schema, $new_schema);
                 $processed_schemas[] = trim($new_schema['name']);
             }
         }
         // remove all old constraints before new contraints, in reverse dependency order
         for ($i = count(mysql5_diff::$old_table_dependency) - 1; $i >= 0; $i--) {
             // find the necessary pointers
             $item = mysql5_diff::$old_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;
             }
             $new_schema = dbx::get_schema(dbsteward::$new_database, $item['schema']['name']);
             $new_table = NULL;
             if ($new_schema != NULL) {
                 $new_table = dbx::get_table($new_schema, $item['table']['name']);
             }
             $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']);
             }
             if ($old_table == NULL) {
                 throw new exception("old_table " . $item['schema']['name'] . "." . $item['table']['name'] . " not found. This is not expected as this reverse constraint loop was based on the old_table_dependency list!");
             }
             // @NOTICE: when dropping constraints, dbx::renamed_table_check_pointer() is not called for $old_table
             // as mysql5_diff_tables::diff_constraints_table() will do rename checking when recreating constraints for renamed tables
             mysql5_diff_constraints::diff_constraints_table($ofs1, $old_schema, $old_table, $new_schema, $new_table, 'constraint', TRUE);
             mysql5_diff_constraints::diff_constraints_table($ofs1, $old_schema, $old_table, $new_schema, $new_table, 'primaryKey', TRUE);
         }
         $processed_schemas = array();
         for ($i = 0; $i < count(mysql5_diff::$new_table_dependency); $i++) {
             // find the necessary pointers
             $item = mysql5_diff::$new_table_dependency[$i];
             $new_schema = dbx::get_schema(dbsteward::$new_database, $item['schema']['name']);
             $new_table = NULL;
             if ($new_schema != NULL) {
                 $new_table = dbx::get_table($new_schema, $item['table']['name']);
             }
             $old_schema = dbx::get_schema(dbsteward::$old_database, $item['schema']['name']);
             // schema level stuff should only be done once, keep track of which ones we have done
             // see above for pre table creation stuff
             // see below for post table creation stuff
             if (!in_array($new_schema['name'], $processed_schemas)) {
                 mysql5_diff_sequences::diff_sequences($ofs1, $ofs3, $old_schema, $new_schema);
                 $processed_schemas[] = $new_schema['name'];
             }
             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_table = NULL;
             if ($old_schema != NULL) {
                 $old_table = dbx::get_table($old_schema, $item['table']['name']);
             }
             dbx::renamed_table_check_pointer($old_schema, $old_table, $new_schema, $new_table);
             mysql5_diff_tables::diff_tables($ofs1, $ofs3, $old_schema, $new_schema, $old_table, $new_table);
             // mysql5_diff_indexes::diff_indexes_table($ofs1, $old_schema, $old_table, $new_schema, $new_table);
             mysql5_diff_constraints::diff_constraints_table($ofs1, $old_schema, $old_table, $new_schema, $new_table, 'primaryKey', FALSE);
             mysql5_diff_triggers::diff_triggers_table($ofs1, $old_schema, $old_table, $new_schema, $new_table);
             mysql5_diff_constraints::diff_constraints_table($ofs1, $old_schema, $old_table, $new_schema, $new_table, 'constraint', FALSE);
         }
         // drop old tables in reverse dependency order
         for ($i = count(mysql5_diff::$old_table_dependency) - 1; $i >= 0; $i--) {
             // find the necessary pointers
             $item = mysql5_diff::$old_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;
             }
             $new_schema = dbx::get_schema(dbsteward::$new_database, $item['schema']['name']);
             $new_table = NULL;
             if ($new_schema != NULL) {
                 $new_table = dbx::get_table($new_schema, $item['table']['name']);
             }
             $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']);
             }
             if ($old_table == NULL) {
                 throw new exception("old_table " . $item['schema']['name'] . "." . $item['table']['name'] . " not found. This is not expected as this reverse constraint loop was based on the old_table_dependency list!");
             }
             mysql5_diff_tables::drop_tables($ofs3, $old_schema, $new_schema, $old_table, $new_table);
         }
     }
     mysql5_diff_views::create_views_ordered($ofs3, dbsteward::$old_database, dbsteward::$new_database);
 }
Beispiel #3
0
 /**
  * Updates objects in schemas.
  *
  * @param $ofs1  stage1 output file segmenter
  * @param $ofs3  stage3 output file segmenter
  */
 public static function update_structure($ofs1, $ofs3)
 {
     $type_modified_columns = array();
     pgsql8_diff_languages::diff_languages($ofs1);
     // drop all views in all schemas, regardless whether dependency order is known or not
     pgsql8_diff_views::drop_views_ordered($ofs1, dbsteward::$old_database, dbsteward::$new_database);
     // if the table dependency order is unknown, bang them in natural order
     if (!is_array(self::$new_table_dependency)) {
         foreach (dbx::get_schemas(dbsteward::$new_database) as $new_schema) {
             pgsql8::set_context_replica_set_id($new_schema);
             //@NOTICE: @TODO: this does not honor old*Name attributes, does it matter?
             $old_schema = dbx::get_schema(dbsteward::$old_database, $new_schema['name']);
             pgsql8_diff_types::apply_changes($ofs1, $old_schema, $new_schema, $type_modified_columns);
             pgsql8_diff_functions::diff_functions($ofs1, $ofs3, $old_schema, $new_schema);
             pgsql8_diff_sequences::diff_sequences($ofs1, $old_schema, $new_schema);
             // remove old constraints before table contraints, so the SQL statements succeed
             pgsql8_diff_tables::diff_constraints($ofs1, $old_schema, $new_schema, 'constraint', true);
             pgsql8_diff_tables::diff_constraints($ofs1, $old_schema, $new_schema, 'primaryKey', true);
             pgsql8_diff_tables::drop_tables($ofs3, $old_schema, $new_schema);
             pgsql8_diff_tables::diff_tables($ofs1, $ofs3, $old_schema, $new_schema);
             pgsql8_diff_indexes::diff_indexes($ofs1, $old_schema, $new_schema);
             pgsql8_diff_tables::diff_clusters($ofs1, $old_schema, $new_schema);
             pgsql8_diff_tables::diff_constraints($ofs1, $old_schema, $new_schema, 'primaryKey', false);
             pgsql8_diff_triggers::diff_triggers($ofs1, $old_schema, $new_schema);
         }
         // non-primary key constraints may be inter-schema dependant, and dependant on other's primary keys
         // and therefore should be done after object creation sections
         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_constraints($ofs1, $old_schema, $new_schema, 'constraint', false);
         }
     } else {
         // use table dependency order to do structural changes in an intelligent order
         $processed_schemas = array();
         for ($i = 0; $i < count(self::$new_table_dependency); $i++) {
             // find the necessary pointers
             $item = self::$new_table_dependency[$i];
             // @NOTICE: dbsteward::TABLE_DEPENDENCY_IGNORABLE_NAME is NOT checked here because these are schema operations
             $new_schema = dbx::get_schema(dbsteward::$new_database, $item['schema']['name']);
             $old_schema = dbx::get_schema(dbsteward::$old_database, $item['schema']['name']);
             pgsql8::set_context_replica_set_id($new_schema);
             // do all types and functions on their own before table creation
             // see next loop for other once per schema work
             if (!in_array(trim($new_schema['name']), $processed_schemas)) {
                 pgsql8::set_context_replica_set_id($new_schema);
                 pgsql8_diff_types::apply_changes($ofs1, $old_schema, $new_schema, $type_modified_columns);
                 pgsql8_diff_functions::diff_functions($ofs1, $ofs3, $old_schema, $new_schema);
                 $processed_schemas[] = trim($new_schema['name']);
             }
         }
         // remove all old constraints before new contraints, in reverse dependency order
         for ($i = count(self::$old_table_dependency) - 1; $i >= 0; $i--) {
             // find the necessary pointers
             $item = self::$old_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;
             }
             $new_schema = dbx::get_schema(dbsteward::$new_database, $item['schema']['name']);
             $new_table = null;
             if ($new_schema != null) {
                 pgsql8::set_context_replica_set_id($new_schema);
                 $new_table = dbx::get_table($new_schema, $item['table']['name']);
             }
             $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']);
             }
             if ($old_table == null) {
                 throw new exception("old_table " . $item['schema']['name'] . "." . $item['table']['name'] . " not found. This is not expected as this reverse constraint loop was based on the old_table_dependency list!");
             }
             // @NOTICE: when dropping constraints, dbx::renamed_table_check_pointer() is not called for $old_table
             // as pgsql8_diff_tables::diff_constraints_table() will do rename checking when recreating constraints for renamed tables
             pgsql8_diff_tables::diff_constraints_table($ofs1, $old_schema, $old_table, $new_schema, $new_table, 'constraint', true);
             pgsql8_diff_tables::diff_constraints_table($ofs1, $old_schema, $old_table, $new_schema, $new_table, 'primaryKey', true);
         }
         $processed_schemas = array();
         for ($i = 0; $i < count(self::$new_table_dependency); $i++) {
             // find the necessary pointers
             $item = self::$new_table_dependency[$i];
             $new_schema = dbx::get_schema(dbsteward::$new_database, $item['schema']['name']);
             $new_table = null;
             if ($new_schema != null) {
                 pgsql8::set_context_replica_set_id($new_schema);
                 $new_table = dbx::get_table($new_schema, $item['table']['name']);
             }
             $old_schema = dbx::get_schema(dbsteward::$old_database, $item['schema']['name']);
             // schema level stuff should only be done once, keep track of which ones we have done
             // see above for pre table creation stuff
             // see below for post table creation stuff
             if (!in_array($new_schema['name'], $processed_schemas)) {
                 pgsql8_diff_sequences::diff_sequences($ofs1, $old_schema, $new_schema);
                 $processed_schemas[] = $new_schema['name'];
             }
             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_table = null;
             if ($old_schema != null) {
                 $old_table = dbx::get_table($old_schema, $item['table']['name']);
             }
             // if they are defined in the old definition,
             // old_schema and old_table are already established pointers
             // when a table has an oldTableName oldSchemaName specified,
             // dbx::renamed_table_check_pointer() will modify these pointers to be the old table
             dbx::renamed_table_check_pointer($old_schema, $old_table, $new_schema, $new_table);
             if ($old_table) {
                 if (!$old_schema) {
                     // nkiraly: is this still happening when dbx::renamed_table_check_pointer() changes the $old_table pointer?
                     throw new exception("old_table '" . $old_table['name'] . "' error: old_table is defined but old_schema is not - this cannot be for differs to diff");
                 }
             }
             pgsql8_diff_tables::diff_tables($ofs1, $ofs3, $old_schema, $new_schema, $old_table, $new_table);
             pgsql8_diff_indexes::diff_indexes_table($ofs1, $old_schema, $old_table, $new_schema, $new_table);
             pgsql8_diff_tables::diff_clusters_table($ofs1, $old_schema, $old_table, $new_schema, $new_table);
             pgsql8_diff_tables::diff_constraints_table($ofs1, $old_schema, $old_table, $new_schema, $new_table, 'primaryKey', false);
             pgsql8_diff_triggers::diff_triggers_table($ofs1, $old_schema, $old_table, $new_schema, $new_table);
             pgsql8_diff_tables::diff_constraints_table($ofs1, $old_schema, $old_table, $new_schema, $new_table, 'constraint', false);
         }
         // drop old tables in reverse dependency order
         for ($i = count(self::$old_table_dependency) - 1; $i >= 0; $i--) {
             // find the necessary pointers
             $item = self::$old_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;
             }
             $new_schema = dbx::get_schema(dbsteward::$new_database, $item['schema']['name']);
             $new_table = null;
             if ($new_schema != null) {
                 $new_table = dbx::get_table($new_schema, $item['table']['name']);
             }
             $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']);
             }
             if ($old_table == null) {
                 throw new exception("old_table " . $item['schema']['name'] . "." . $item['table']['name'] . " not found. This is not expected as this reverse constraint loop was based on the old_table_dependency list!");
             }
             pgsql8_diff_tables::drop_tables($ofs3, $old_schema, $new_schema, $old_table, $new_table);
         }
     }
     pgsql8_diff_views::create_views_ordered($ofs3, dbsteward::$old_database, dbsteward::$new_database);
 }