private static function schema_contains_sequence($schema, $sequence_name, $include_oldnames = FALSE)
 {
     if (mysql5_schema::contains_sequence($schema, $sequence_name)) {
         return TRUE;
     }
     foreach (dbx::get_tables($schema) as $table) {
         foreach (mysql5_table::get_sequences_needed($schema, $table) as $sequence) {
             if (strcasecmp($sequence['name'], $sequence_name) === 0) {
                 return TRUE;
             }
             if ($include_oldnames && !dbsteward::$ignore_oldnames && !empty($sequence['oldSequenceName']) && strcasecmp($sequence['oldSequenceName'], $sequence_name) === 0) {
                 return TRUE;
             }
         }
     }
     return FALSE;
 }
Esempio n. 2
0
 protected static function drop_old_schemas($ofs)
 {
     $drop_sequences = array();
     if (is_array(mysql5_diff::$old_table_dependency)) {
         $deps = mysql5_diff::$old_table_dependency;
         $processed_schemas = array();
         foreach ($deps as $dep) {
             $old_schema = $dep['schema'];
             if (!dbx::get_schema(dbsteward::$new_database, $old_schema['name'])) {
                 // this schema is being dropped, drop all children objects in it
                 if (!in_array(trim($old_schema['name']), $processed_schemas)) {
                     // this schema hasn't been processed yet, go ahead and drop views, types, functions, sequences
                     // only do it once per schema
                     foreach ($old_schema->type as $node_type) {
                         $ofs->write(mysql5_type::get_drop_sql($old_schema, $node_type) . "\n");
                     }
                     foreach ($old_schema->function as $node_function) {
                         $ofs->write(mysql5_function::get_drop_sql($old_schema, $node_function) . "\n");
                     }
                     foreach ($old_schema->sequence as $node_sequence) {
                         $ofs->write(mysql5_sequence::get_drop_sql($old_schema, $node_sequence) . "\n");
                     }
                     $processed_schemas[] = trim($old_schema['name']);
                 }
                 if ($dep['table']['name'] === dbsteward::TABLE_DEPENDENCY_IGNORABLE_NAME) {
                     // don't do anything with this table, it is a magic internal DBSteward value
                     continue;
                 }
                 // constraints, indexes, triggers will be deleted along with the tables they're attached to
                 // tables will drop themselves later on
                 // $ofs->write(mysql5_table::get_drop_sql($old_schema, $dep['table']) . "\n");
                 $table_name = mysql5::get_fully_qualified_table_name($dep['schema']['name'], $dep['table']['name']);
                 $ofs->write("-- {$table_name} triggers, indexes, constraints will be implicitly dropped when the table is dropped\n");
                 $ofs->write("-- {$table_name} will be dropped later according to table dependency order\n");
                 // table sequences need dropped separately
                 foreach (mysql5_table::get_sequences_needed($old_schema, $dep['table']) as $node_sequence) {
                     $ofs->write(mysql5_sequence::get_drop_sql($old_schema, $node_sequence) . "\n");
                 }
             }
         }
     } else {
         foreach (dbsteward::$old_database->schema as $old_schema) {
             if (!dbx::get_schema(dbsteward::$new_database, $old_schema['name'])) {
                 foreach ($old_schema->type as $node_type) {
                     $ofs->write(mysql5_type::get_drop_sql($old_schema, $node_type) . "\n");
                 }
                 foreach ($old_schema->function as $node_function) {
                     $ofs->write(mysql5_function::get_drop_sql($old_schema, $node_function) . "\n");
                 }
                 foreach ($old_schema->sequence as $node_sequence) {
                     $ofs->write(mysql5_sequence::get_drop_sql($old_schema, $node_sequence) . "\n");
                 }
                 foreach ($old_schema->table as $node_table) {
                     // tables will drop themselves later on
                     // $ofs->write(mysql5_table::get_drop_sql($old_schema, $node_table) . "\n");
                     $table_name = mysql5::get_fully_qualified_table_name($old_schema['name'], $node_table['name']);
                     $ofs->write("-- {$table_name} triggers, indexes, constraints will be implicitly dropped when the table is dropped\n");
                     $ofs->write("-- {$table_name} will be dropped later according to table dependency order\n");
                     foreach (mysql5_table::get_sequences_needed($old_schema, $node_table) as $node_sequence) {
                         $ofs->write(mysql5_sequence::get_drop_sql($old_schema, $node_sequence) . "\n");
                     }
                 }
             }
         }
     }
 }
Esempio n. 3
0
 public static function build_schema($db_doc, $ofs, $table_depends)
 {
     // schema creation
     if (static::$use_schema_name_prefix) {
         dbsteward::info("MySQL schema name prefixing mode turned on");
     } else {
         if (count($db_doc->schema) > 1) {
             throw new Exception("You cannot use more than one schema in mysql5 without schema name prefixing\nPass the --useschemaprefix flag to turn this on");
         }
     }
     foreach ($db_doc->schema as $schema) {
         // database grants
         foreach ($schema->grant as $grant) {
             $ofs->write(mysql5_permission::get_permission_sql($db_doc, $schema, $schema, $grant) . "\n");
         }
         // enums
         foreach ($schema->type as $type) {
             $ofs->write(mysql5_type::get_creation_sql($schema, $type) . "\n");
         }
         // function definitions
         foreach ($schema->function as $function) {
             if (mysql5_function::has_definition($function)) {
                 $ofs->write(mysql5_function::get_creation_sql($schema, $function) . "\n\n");
             }
             // function grants
             foreach ($function->grant as $grant) {
                 $ofs->write(mysql5_permission::get_permission_sql($db_doc, $schema, $function, $grant) . "\n");
             }
         }
         $sequences = array();
         $triggers = array();
         // create defined tables
         foreach ($schema->table as $table) {
             // get sequences and triggers needed to make this table work
             $sequences = array_merge($sequences, mysql5_table::get_sequences_needed($schema, $table));
             $triggers = array_merge($triggers, mysql5_table::get_triggers_needed($schema, $table));
             // table definition
             $ofs->write(mysql5_table::get_creation_sql($schema, $table) . "\n\n");
             // table indexes
             // mysql5_diff_indexes::diff_indexes_table($ofs, NULL, NULL, $schema, $table);
             // table grants
             if (isset($table->grant)) {
                 foreach ($table->grant as $grant) {
                     $ofs->write(mysql5_permission::get_permission_sql($db_doc, $schema, $table, $grant) . "\n");
                 }
             }
             $ofs->write("\n");
         }
         // sequences contained in the schema + sequences used by serials
         $sequences = array_merge($sequences, dbx::to_array($schema->sequence));
         if (count($sequences) > 0) {
             $ofs->write(mysql5_sequence::get_shim_creation_sql() . "\n\n");
             $ofs->write(mysql5_sequence::get_creation_sql($schema, $sequences) . "\n\n");
             // sequence grants
             foreach ($sequences as $sequence) {
                 foreach ($sequence->grant as $grant) {
                     $ofs->write("-- grant for the {$sequence['name']} sequence applies to ALL sequences\n");
                     $ofs->write(mysql5_permission::get_permission_sql($db_doc, $schema, $sequence, $grant) . "\n");
                 }
             }
         }
         // trigger definitions + triggers used by serials
         $triggers = array_merge($triggers, dbx::to_array($schema->trigger));
         $unique_triggers = array();
         foreach ($triggers as $trigger) {
             // only do triggers set to the current sql format
             if (strcasecmp($trigger['sqlFormat'], dbsteward::get_sql_format()) == 0) {
                 // check that this table/timing/event combo hasn't been defined, because MySQL only
                 // allows one trigger per table per BEFORE/AFTER per action
                 $unique_name = "{$trigger['table']}-{$trigger['when']}-{$trigger['event']}";
                 if (array_key_exists($unique_name, $unique_triggers)) {
                     throw new Exception("MySQL will not allow trigger {$trigger['name']} to be created because it happens on the same table/timing/event as trigger {$unique_triggers[$unique_name]}");
                 }
                 $unique_triggers[$unique_name] = $trigger['name'];
                 $ofs->write(mysql5_trigger::get_creation_sql($schema, $trigger) . "\n");
             }
         }
     }
     foreach ($db_doc->schema as $schema) {
         // define table primary keys before foreign keys so unique requirements are always met for FOREIGN KEY constraints
         foreach ($schema->table as $table) {
             mysql5_diff_constraints::diff_constraints_table($ofs, NULL, NULL, $schema, $table, 'primaryKey', FALSE);
         }
         $ofs->write("\n");
     }
     // foreign key references
     // use the dependency order to specify foreign keys in an order that will satisfy nested foreign keys and etc
     for ($i = 0; $i < count($table_depends); $i++) {
         $dep_schema = $table_depends[$i]['schema'];
         $table = $table_depends[$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;
         }
         mysql5_diff_constraints::diff_constraints_table($ofs, NULL, NULL, $dep_schema, $table, 'constraint', FALSE);
     }
     $ofs->write("\n");
     mysql5_diff_views::create_views_ordered($ofs, null, $db_doc);
     // view permission grants
     foreach ($db_doc->schema as $schema) {
         foreach ($schema->view as $view) {
             if (isset($view->grant)) {
                 foreach ($view->grant as $grant) {
                     $ofs->write(mysql5_permission::get_permission_sql($db_doc, $schema, $view, $grant) . "\n");
                 }
             }
         }
     }
     // @TODO: database configurationParameter support
 }