Example #1
0
 /**
  * return SQL command for dropping the view
  *
  * @return string
  */
 public static function get_drop_sql($node_schema, $node_view)
 {
     // set replica set context for view
     if (pgsql8::set_context_replica_set_id($node_view) === -10) {
         // view doesn't specify one, set from for schema object
         pgsql8::set_context_replica_set_id($node_schema);
     }
     $ddl = "DROP VIEW IF EXISTS " . pgsql8::get_quoted_schema_name($node_schema['name']) . '.' . pgsql8::get_quoted_table_name($node_view['name']) . ";\n";
     return $ddl;
 }
Example #2
0
 /**
  * Creates new schemas (not the objects inside the schemas)
  *
  * @param  object  $ofs output file pointer
  * @return void
  */
 protected static function create_new_schemas($ofs)
 {
     foreach (dbx::get_schemas(dbsteward::$new_database) as $new_schema) {
         if (dbx::get_schema(dbsteward::$old_database, $new_schema['name']) == null) {
             dbsteward::info("Create New Schema " . $new_schema['name']);
             pgsql8::set_context_replica_set_id($new_schema);
             $ofs->write(format_schema::get_creation_sql($new_schema));
         }
     }
 }
 /**
  * Creates views in dependency order
  * @param  output_file_segmenter $ofs        Output file segmenter to write to
  * @param  SimpleXMLElement      $db_doc_old Old database document
  * @param  SimpleXMLElement      $db_doc_new New database document
  */
 public static function create_views_ordered($ofs, $db_doc_old, $db_doc_new)
 {
     static::with_views_in_order($db_doc_new, function ($new_schema, $new_view) use($db_doc_new, $db_doc_old, $ofs) {
         $old_schema = dbx::get_schema($db_doc_old, $new_schema['name']);
         $old_view = dbx::get_view($old_schema, $new_view['name']);
         if (format_diff_views::should_create_view($old_schema, $old_view, $new_schema, $new_view)) {
             // set replica set context for view
             if (pgsql8::set_context_replica_set_id($new_view) === -10) {
                 // view doesn't specify one, set from for schema object
                 pgsql8::set_context_replica_set_id($new_schema);
             }
             $ofs->write(format_view::get_creation_sql($db_doc_new, $new_schema, $new_view) . "\n");
         }
     });
 }
 public static function diff_triggers_table($ofs, $old_schema, $old_table, $new_schema, $new_table)
 {
     // drop triggers that no longer exist or are modified
     foreach (self::get_drop_triggers($old_schema, $old_table, $new_schema, $new_table) as $old_trigger) {
         // only do triggers set to the current sql_format
         if (strcasecmp($old_trigger['sqlFormat'], dbsteward::get_sql_format()) == 0) {
             pgsql8::set_context_replica_set_id($old_trigger);
             $ofs->write(pgsql8_trigger::get_drop_sql($old_schema, $old_trigger) . "\n");
         }
     }
     // add new triggers
     foreach (self::get_new_triggers($old_schema, $old_table, $new_schema, $new_table) as $new_trigger) {
         // only do triggers set to the current sql format
         if (strcasecmp($new_trigger['sqlFormat'], dbsteward::get_sql_format()) == 0) {
             pgsql8::set_context_replica_set_id($new_trigger);
             $ofs->write(pgsql8_trigger::get_creation_sql($new_schema, $new_trigger) . "\n");
         }
     }
 }
 /**
  * Outputs commands for dropping types.
  *
  * @param $ofs          output file pointer
  * @param $old_schema   original schema
  * @param $new_schema   new schema
  */
 private static function drop_types($ofs, $old_schema, $new_schema)
 {
     if ($old_schema != NULL) {
         foreach (dbx::get_types($old_schema) as $type) {
             if (!pgsql8_schema::contains_type($new_schema, $type['name'])) {
                 pgsql8::set_context_replica_set_id($type);
                 $ofs->write(pgsql8_type::get_drop_sql($new_schema, $type) . "\n");
             }
         }
     }
 }
Example #6
0
 /**
  * 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);
         }
     }
 }
 /**
  * Creates and returns SQL for dropping the function.
  *
  * @return created SQL
  */
 public static function get_drop_sql($node_schema, $node_function)
 {
     pgsql8::set_context_replica_set_id($node_function);
     $declaration = self::get_declaration($node_schema, $node_function);
     $declaration = str_ireplace("character varying", "varchar", $declaration);
     $declaration = str_ireplace("varying", "varchar", $declaration);
     return "DROP FUNCTION IF EXISTS " . $declaration . ";";
 }
Example #8
0
 public static function build_staged_sql($db_doc, $ofs, $stage)
 {
     // push all sql stage=$stage elements to the passed $ofs output_file_segmenter
     if ($stage === NULL) {
         $ofs->write("\n-- NON-STAGED SQL COMMANDS\n");
     } else {
         $ofs->write("\n-- SQL STAGE " . $stage . " COMMANDS\n");
     }
     foreach ($db_doc->sql as $sql_statement) {
         pgsql8::set_context_replica_set_id($sql_statement);
         if (isset($sql_statement['stage']) && strcasecmp($sql_statement['stage'], $stage) === 0 || !isset($sql_statement['stage']) && $stage === NULL) {
             if (isset($sql_statement['comment']) && strlen($sql_statement['comment'])) {
                 $ofs->write("-- " . $sql_statement['comment'] . "\n");
             }
             $ofs->write(trim($sql_statement) . "  -- LITERAL_SQL_INCLUDE: this line should be included as-is without any parsing\n");
         }
     }
     $ofs->write("\n");
 }
 /**
  * Returns list of constraints that should be dropped.
  *
  * @param old_table original table or null
  * @param new_table new table or null
  * @param type whether primary keys should be processed or other constraints should be processed
  *
  * @return array of constraints that should be dropped
  *
  * @todo Constraints that are depending on a removed field should not be
  *       added to drop because they are already removed.
  */
 private static function get_drop_constraints($old_schema, $old_table, $new_schema, $new_table, $type)
 {
     pgsql8::set_context_replica_set_id($new_table);
     $list = array();
     if ($new_table != null && $old_table != null) {
         if ($old_table->getName() != 'table') {
             throw new exception("Unexpected element type: " . $old_table->getName() . " panicing");
         }
         foreach (pgsql8_constraint::get_table_constraints(dbsteward::$old_database, $old_schema, $old_table, $type) as $constraint) {
             $new_constraint = pgsql8_constraint::get_table_constraint(dbsteward::$new_database, $new_schema, $new_table, $constraint['name']);
             if (!pgsql8_table::contains_constraint(dbsteward::$new_database, $new_schema, $new_table, $constraint['name']) || !pgsql8_table::constraint_equals($new_constraint, $constraint) || pgsql8_table::constraint_depends_on_renamed_table(dbsteward::$new_database, $new_constraint) || pgsql8_table::constraint_depends_on_renamed_table(dbsteward::$new_database, $constraint)) {
                 $list[] = $constraint;
             }
         }
     }
     return $list;
 }