/**
  * Drop removed types
  * Add new types
  * Apply type definition differences, updating the type's tables along the way
  *
  * @param $ofs          output segementer
  * @param $old_schema   original schema
  * @param $new_schema   new schema
  */
 public static function apply_changes($ofs, $old_schema, $new_schema)
 {
     // drop any types that are no longer defined
     self::drop_types($ofs, $old_schema, $new_schema);
     // create any types that are new in the new definition
     self::create_types($ofs, $old_schema, $new_schema);
     // there is no alter for types
     // find types that still exist that are different
     // placehold type data in table columns, and recreate the type
     foreach (dbx::get_types($new_schema) as $new_type) {
         // does type exist in old definition ?
         if ($old_schema == NULL || !pgsql8_schema::contains_type($old_schema, $new_type['name'])) {
             continue;
         }
         $old_type = dbx::get_type($old_schema, $new_type['name']);
         // is there a difference between the old and new type definitions?
         if (pgsql8_type::equals($old_schema, $old_type, $new_schema, $new_type)) {
             continue;
         }
         pgsql8::set_context_replica_set_id($new_type);
         $columns = array();
         $is_domain = strcasecmp($new_type['type'], 'domain') === 0;
         $n = $is_domain ? 3 : 4;
         $word = $is_domain ? 'domain' : 'type';
         $i = 1;
         $old_dependent_functions = dbx::get_functions_with_dependent_type($old_schema, (string) $old_type['name']);
         if (count($old_dependent_functions) > 0) {
             $n += 2;
             $ofs->write("-- {$word} {$new_type['name']} definition migration ({$i}/{$n}): dependent functions return/parameter type alteration\n");
             $i++;
             foreach ($old_dependent_functions as $old_dependent_function) {
                 $ofs->write(pgsql8_function::get_drop_sql($old_schema, $old_dependent_function) . "\n");
             }
             $ofs->write("\n");
         }
         $ofs->write("-- {$word} {$new_type['name']} definition migration ({$i}/{$n}): dependent tables column type alteration\n");
         $i++;
         $ofs->write(pgsql8_type::alter_column_type_placeholder($columns, $old_schema, $old_type) . "\n");
         if ($is_domain) {
             $ofs->write("-- {$word} {$new_type['name']} definition migration ({$i}/{$n}): alter domain\n");
             $i++;
             self::apply_domain_changes($ofs, $old_schema, $old_type, $new_schema, $new_type);
         } else {
             $ofs->write("-- {$word} {$new_type['name']} definition migration ({$i}/{$n}): drop old type\n");
             $i++;
             $ofs->write(pgsql8_type::get_drop_sql($old_schema, $old_type) . "\n\n");
             $ofs->write("-- {$word} {$new_type['name']} definition migration ({$i}/{$n}): recreate type with new definition\n");
             $i++;
             $ofs->write(pgsql8_type::get_creation_sql($new_schema, $new_type) . "\n\n");
         }
         // functions are only recreated if they changed elsewise, so need to create them here
         $new_dependent_functions = dbx::get_functions_with_dependent_type($new_schema, (string) $new_type['name']);
         if (count($new_dependent_functions) > 0) {
             $ofs->write("-- {$word} {$new_type['name']} definition migration ({$i}/{$n}): dependent functions return/parameter type with new definition\n");
             $i++;
             foreach ($new_dependent_functions as $new_dependent_function) {
                 $ofs->write(pgsql8_function::get_creation_sql($new_schema, $new_dependent_function) . "\n");
             }
             $ofs->write("\n\n");
         }
         $ofs->write("-- {$word} {$new_type['name']} definition migration ({$n}/{$n}): dependent tables type restoration\n");
         $ofs->write(pgsql8_type::alter_column_type_restore($columns, $new_schema, $new_type) . "\n");
     }
 }