public static function get_multiple_create_bits($node_schema, $node_table, $constraints) { $bits = array(); foreach ($constraints as $constraint) { $bits[] = mysql5_constraint::get_constraint_sql($constraint, FALSE); if (strcasecmp($constraint['type'], 'PRIMARY KEY') == 0) { // we're adding the PK constraint, so we need to add AUTO_INCREMENT on any affected columns immediately after! $columns = mysql5_table::primary_key_columns($node_table); foreach ($columns as $col) { $node_column = dbx::get_table_column($node_table, $col); if (mysql5_column::is_auto_increment($node_column['type'])) { $bits[] = "MODIFY " . mysql5_column::get_full_definition(dbsteward::$new_database, $node_schema, $node_table, $node_column, FALSE, TRUE, TRUE); break; // there can only be one AI column per table } } } } return $bits; }
/** * if in_both = false, what rows in A are not in B ? * * if in_both = true, what rows are in A and B ? * - when a is empty, all of b's are returned * - a's row members are the ones returned when in_both rows are found * this is important when comparing tables whose rows are the same but have added columns * * @return void */ public static function table_data_rows_compare($table_a, $table_b, $in_both, &$a_rows, &$b_rows, &$changes) { $a_rows = array(); $b_rows = array(); $changes = array(); $table_a_data_rows = dbx::get_table_rows($table_a); $table_b_data_rows = dbx::get_table_rows($table_b); // data_row_overlay_key_search() needs these to do the matching $table_a_data_rows_columns = preg_split("/[\\,\\s]+/", $table_a_data_rows['columns'], -1, PREG_SPLIT_NO_EMPTY); $table_b_data_rows_columns = preg_split("/[\\,\\s]+/", $table_b_data_rows['columns'], -1, PREG_SPLIT_NO_EMPTY); if ($table_a_data_rows == null || $table_a_data_rows_columns == null) { // table_a has no data rows if ($in_both) { // what rows are in A and B? none in A to compare } else { // what rows in A are not in B? none to list } } else { if ($table_b_data_rows == null || $table_b_data_rows_columns == null) { // table_b has no data rows if ($in_both) { // what rows are in A and B? none in B to compare } else { // what rows in A are not in B? omg, all of them! $a_rows = $table_a_data_rows->row; } } else { $table_b_primary_keys = mysql5_table::primary_key_columns($table_b); // @NOTICE: in the case of table row data comparising for DIFFING, the primary keys to use are table B // but the table B and A columns are reversed compared to data_rows_overlay() as the comparison is B vs A not base vs overlay (AvB) // so the columns to match as base and overlay are reversed, comared to other calls to data_row_overlay_primary_key_index() $primary_key_index = xml_parser::data_row_overlay_primary_key_index($table_b_primary_keys, $table_b_data_rows_columns, $table_a_data_rows_columns); $base_pklookup = array(); $i = 0; if (count($primary_key_index['base'])) { foreach ($table_b_data_rows->row as $base_row) { $s = ''; foreach ($primary_key_index['base'] as $index) { $s .= ':' . $base_row->col[$index]; } $base_pklookup[$s] = $i++; } } $table_b_index = 0; foreach ($table_a_data_rows->row as $table_a_data_row) { $s = ''; foreach ($primary_key_index['overlay'] as $index) { $s .= ':' . $table_a_data_row->col[$index]; } if (array_key_exists($s, $base_pklookup)) { $match = TRUE; $table_b_index = $base_pklookup[$s]; } else { $match = FALSE; } if ($match) { // we found a match //echo "rows match\n"; var_dump($table_a_data_row); var_dump($table_b_data_row); $table_b_data_row =& $table_b_data_rows->row[$table_b_index]; if ($in_both) { // looking for rows in both // is the row marked for delete in A? if (self::table_data_row_deleted($table_a_data_row)) { // don't return changes, we are looking for rows in_both } else { if (self::table_data_row_deleted($table_b_data_row)) { // don't return changes, we are looking for rows in_both } else { // do table data row diff, add rows to return by reference rows for both A and B $changed_columns = array(); static::table_data_row_diff($table_a_data_rows_columns, $table_a_data_row, $table_b_data_rows_columns, $table_b_data_row, $changed_columns); $a_rows[] = $table_a_data_row; $b_rows[] = $table_b_data_row; $changes[] = $changed_columns; } } } else { // is the A row marked for delete? if (self::table_data_row_deleted($table_a_data_row)) { // there was a match, but we are looking for A not in B, A row is marked deleted, don't return it } else { if (self::table_data_row_deleted($table_b_data_row)) { // there was a match // A is not deleted // we are looking for A not in B // B is deleted $a_rows[] = $table_a_data_row; } } } } else { // echo "rows don't match\n"; var_dump($table_a_data_row); var_dump($table_b_data_row); // no match if (!$in_both) { // looking for A not in B if (self::table_data_row_deleted($table_a_data_row)) { // but the A row is marked deleted, don't return it } else { $a_rows[] = $table_a_data_row; } } } } } } }
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"); }