Example #1
0
 public function build_data($db_doc, $ofs, $tables)
 {
     // use the dependency order to then write out the actual data inserts into the data sql file
     $tables_count = count($tables);
     $limit_to_tables_count = count(dbsteward::$limit_to_tables);
     for ($i = 0; $i < $tables_count; $i++) {
         $schema = $tables[$i]['schema'];
         $table = $tables[$i]['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(mssql10_diff_tables::get_data_sql(NULL, NULL, $schema, $table, FALSE));
         // 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
         $primary_keys_exist = self::primary_key_split($table['primaryKey']);
         foreach ($table->column as $column) {
             // while looping through columns, check to see if primary key is one of them
             // if it is remove it from the primary keys array, at the end of loop array should be empty
             $key = array_search($column['name'], $primary_keys_exist);
             if (is_numeric($key)) {
                 unset($primary_keys_exist[$key]);
             }
         }
         // throw an error if the table is using a primaryKey column that does not actually exist
         if (!empty($primary_keys_exist)) {
             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");
 }
Example #2
0
 /**
  * Updates objects in schemas.
  *
  * @param ofs output file segmenter
  */
 private static function update_data($ofs, $delete_mode = FALSE)
 {
     if (mssql10_diff::$new_table_dependency != NULL && count(mssql10_diff::$new_table_dependency) > 0) {
         for ($i = 0; $i < count(mssql10_diff::$new_table_dependency); $i++) {
             // go in reverse when in delete mode
             if ($delete_mode) {
                 $item = mssql10_diff::$new_table_dependency[count(mssql10_diff::$new_table_dependency) - 1 - $i];
             } else {
                 $item = mssql10_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(mssql10_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']);
             mssql10_diff_tables::diff_data($ofs, $old_schema, $new_schema);
         }
     }
 }
 /**
  * Outputs DDL for addition, removal and modifications of table columns
  *
  * @param $ofs1       stage1 output file segmenter
  * @param $ofs3       stage3 output file segmenter
  * @param $old_table  original table
  * @param $new_table  new table
  */
 public static function diff_tables($ofs1, $ofs3, $old_schema, $new_schema, $old_table_target = null, $new_table_target = null)
 {
     self::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) {
             mssql10_diff_tables::update_table_columns($ofs1, $ofs3, $old_table, $new_schema, $new_table);
             mssql10_diff_tables::add_alter_statistics($ofs1, $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;
             }
             mssql10_diff_tables::update_table_columns($ofs1, $ofs3, $old_table, $new_schema, $new_table);
             mssql10_diff_tables::add_alter_statistics($ofs1, $old_table, $new_schema, $new_table);
         }
     }
 }