Exemplo n.º 1
0
 /**
  * Updates objects in schemas.
  *
  * @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 (mysql5_diff::$new_table_dependency != NULL && count(mysql5_diff::$new_table_dependency) > 0) {
         for ($i = 0; $i < count(mysql5_diff::$new_table_dependency); $i++) {
             // go in reverse when in delete mode
             if ($delete_mode) {
                 $item = mysql5_diff::$new_table_dependency[count(mysql5_diff::$new_table_dependency) - 1 - $i];
             } else {
                 $item = mysql5_diff::$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']);
             }
             $ofs->write(mysql5_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) {
             $old_schema = dbx::get_schema(dbsteward::$old_database, $new_schema['name']);
             mysql5_diff_tables::diff_data($ofs, $old_schema, $new_schema);
         }
     }
 }
Exemplo n.º 2
0
 public static function build_data($db_doc, $ofs, $tables)
 {
     // use the dependency order to then write out the actual data inserts into the data sql file
     $limit_to_tables_count = count(dbsteward::$limit_to_tables);
     foreach ($tables as $dep_table) {
         $schema = $dep_table['schema'];
         $table = $dep_table['table'];
         if ($table['name'] === dbsteward::TABLE_DEPENDENCY_IGNORABLE_NAME) {
             // don't do anything with this table, it is a magic internal DBSteward value
             continue;
         }
         if ($limit_to_tables_count > 0) {
             if (in_array($schema['name'], array_keys(dbsteward::$limit_to_tables))) {
                 if (in_array($table['name'], dbsteward::$limit_to_tables[(string) $schema['name']])) {
                     // table is to be included
                 } else {
                     continue;
                 }
             } else {
                 continue;
             }
         }
         $ofs->write(mysql5_diff_tables::get_data_sql(NULL, NULL, $schema, $table, FALSE));
         $table_primary_keys = mysql5_table::primary_key_columns($table);
         $table_column_names = dbx::to_array($table->column, 'name');
         $node_rows =& dbx::get_table_rows($table);
         // the <rows> element
         $data_column_names = preg_split("/,|\\s/", $node_rows['columns'], -1, PREG_SPLIT_NO_EMPTY);
         // set serial primary keys to the max value after inserts have been performed
         // only if the PRIMARY KEY is not a multi column
         if (count($table_primary_keys) == 1 && in_array($table_primary_keys[0], $data_column_names)) {
             $pk_column_name = $table_primary_keys[0];
             $node_pk_column = dbx::get_table_column($table, $pk_column_name);
             if ($node_pk_column == NULL) {
                 throw new exception("Failed to find primary key column '" . $pk_column_name . "' for " . $schema['name'] . "." . $table['name']);
             }
             // only set the pkey to MAX() if the primary key column is also a serial/bigserial and if serialStart is not defined
             if (mysql5_column::is_serial($node_pk_column['type']) && !isset($node_pk_column['serialStart'])) {
                 $fqtn = mysql5::get_fully_qualified_table_name($schema['name'], $table['name']);
                 $qcol = mysql5::get_quoted_column_name($pk_column_name);
                 $setval = mysql5_sequence::get_setval_call(mysql5_column::get_serial_sequence_name($schema, $table, $node_pk_column), "MAX({$qcol})", "TRUE");
                 $sql = "SELECT {$setval} FROM {$fqtn};\n";
                 $ofs->write($sql);
             }
         }
         // unlike the pg class, we cannot just set identity column start values here with setval without inserting a row
         // check if primary key is a column of this table - FS#17481
         if (count(array_diff($table_primary_keys, $table_column_names)) != 0) {
             throw new exception('Primary key ' . $table['primaryKey'] . ' does not exist as a column in table ' . $table['name']);
         }
     }
     // include all of the unstaged sql elements
     dbx::build_staged_sql($db_doc, $ofs, NULL);
     $ofs->write("\n");
 }