Esempio n. 1
0
 public function build_schema($db_doc, $ofs, $table_depends)
 {
     // explicitly create the ROLE_APPLICATION
     // webservers connect as a user granted this role
     $ofs->write("CREATE ROLE " . $db_doc->database->role->application . ";\n");
     // schema creation
     foreach ($db_doc->schema as $schema) {
         $ofs->write(mssql10_schema::get_creation_sql($schema));
         // schema grants
         if (isset($schema->grant)) {
             foreach ($schema->grant as $grant) {
                 $ofs->write(mssql10_permission::get_sql($db_doc, $schema, $schema, $grant) . "\n");
             }
         }
     }
     // types: enumerated list, etc
     foreach ($db_doc->schema as $schema) {
         foreach ($schema->type as $type) {
             $ofs->write(mssql10_type::get_creation_sql($schema, $type) . "\n");
         }
     }
     // function definitions
     foreach ($db_doc->schema as $schema) {
         foreach ($schema->function as $function) {
             if (mssql10_function::has_definition($function)) {
                 $ofs->write(mssql10_function::get_creation_sql($schema, $function));
             }
         }
     }
     $ofs->write("\n");
     // table structure creation
     foreach ($db_doc->schema as $schema) {
         // create defined tables
         foreach ($schema->table as $table) {
             // table definition
             $ofs->write(mssql10_table::get_creation_sql($schema, $table) . "\n");
             // table indexes
             mssql10_diff_indexes::diff_indexes_table($ofs, NULL, NULL, $schema, $table);
             // table grants
             if (isset($table->grant)) {
                 foreach ($table->grant as $grant) {
                     $ofs->write(mssql10_permission::get_sql($db_doc, $schema, $table, $grant) . "\n");
                 }
             }
             $ofs->write("\n");
         }
         // sequences contained in the schema
         if (isset($schema->sequence)) {
             foreach ($schema->sequence as $sequence) {
                 $ofs->write(mssql10_bit_table::get_creation_sql($schema, $sequence) . "\n");
                 // sequence permission grants
                 if (isset($sequence->grant)) {
                     foreach ($sequence->grant as $grant) {
                         $ofs->write(mssql10_permission::get_sql($db_doc, $schema, $sequence, $grant) . "\n");
                     }
                 }
             }
         }
     }
     $ofs->write("\n");
     // define table primary keys before foreign keys so unique requirements are always met for FOREIGN KEY constraints
     foreach ($db_doc->schema as $schema) {
         foreach ($schema->table as $table) {
             mssql10_diff_tables::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++) {
         $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;
         }
         mssql10_diff_tables::diff_constraints_table($ofs, NULL, NULL, $schema, $table, 'constraint', FALSE);
     }
     $ofs->write("\n");
     // trigger definitions
     foreach ($db_doc->schema as $schema) {
         foreach ($schema->trigger as $trigger) {
             // only do triggers set to the current sql format
             if (strcasecmp($trigger['sqlFormat'], dbsteward::get_sql_format()) == 0) {
                 $ofs->write(mssql10_trigger::get_creation_sql($schema, $trigger));
             }
         }
     }
     $ofs->write("\n");
     // view creation
     foreach ($db_doc->schema as $schema) {
         foreach ($schema->view as $view) {
             $ofs->write(mssql10_view::get_creation_sql($schema, $view));
             // view permission grants
             if (isset($view->grant)) {
                 foreach ($view->grant as $grant) {
                     $ofs->write(mssql10_permission::get_sql($db_doc, $schema, $view, $grant) . "\n");
                 }
             }
         }
     }
     $ofs->write("\n");
     // @TODO: database configurationParameter support needed ?
 }
Esempio n. 2
0
 /**
  * Updates objects in schemas.
  *
  * @param ofs1 stage1 output file segmenter
  * @param ofs3 stage3 output file segmenter
  * @param $old_database original database
  * @param $new_database new database
  */
 private static function update_structure($ofs1, $ofs3)
 {
     $type_modified_columns = array();
     // drop all views in all schemas, regardless whether dependency order is known or not
     foreach (dbx::get_schemas(dbsteward::$new_database) as $new_schema) {
         $old_schema = dbx::get_schema(dbsteward::$old_database, $new_schema['name']);
         $new_schema = dbx::get_schema(dbsteward::$new_database, $new_schema['name']);
         mssql10_diff_views::drop_views($ofs1, $old_schema, $new_schema);
     }
     //@TODO: implement mssql10_language ? no relevant conversion exists see other TODO's stating this
     //mssql10_diff_languages::diff_languages($ofs1);
     // if the table dependency order is unknown, bang them in natural order
     if (!is_array(mssql10_diff::$new_table_dependency)) {
         foreach (dbx::get_schemas(dbsteward::$new_database) as $new_schema) {
             //@NOTICE: @TODO: this does not honor old*Name attributes, does it matter?
             $old_schema = dbx::get_schema(dbsteward::$old_database, $new_schema['name']);
             mssql10_diff_types::apply_changes($ofs1, $old_schema, $new_schema, $type_modified_columns);
             mssql10_diff_functions::diff_functions($ofs1, $ofs3, $old_schema, $new_schema);
             mssql10_diff_sequences::diff_sequences($ofs1, $old_schema, $new_schema);
             // remove old constraints before table contraints, so the SQL statements succeed
             mssql10_diff_tables::diff_constraints($ofs1, $old_schema, $new_schema, 'constraint', TRUE);
             mssql10_diff_tables::diff_constraints($ofs1, $old_schema, $new_schema, 'primaryKey', TRUE);
             mssql10_diff_tables::drop_tables($ofs3, $old_schema, $new_schema);
             mssql10_diff_tables::diff_tables($ofs1, $ofs3, $old_schema, $new_schema);
             mssql10_diff_indexes::diff_indexes($ofs1, $old_schema, $new_schema);
             mssql10_diff_tables::diff_clusters($ofs1, $old_schema, $new_schema);
             mssql10_diff_tables::diff_constraints($ofs1, $old_schema, $new_schema, 'primaryKey', FALSE);
             mssql10_diff_triggers::diff_triggers($ofs1, $old_schema, $new_schema);
         }
         // non-primary key constraints may be inter-schema dependant, and dependant on other's primary keys
         // and therefore should be done after object creation sections
         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_constraints($ofs1, $old_schema, $new_schema, 'constraint', FALSE);
         }
     } else {
         $processed_schemas = array();
         for ($i = 0; $i < count(mssql10_diff::$new_table_dependency); $i++) {
             // find the necessary pointers
             $item = mssql10_diff::$new_table_dependency[$i];
             // @NOTICE: dbsteward::TABLE_DEPENDENCY_IGNORABLE_NAME is NOT checked here because these are schema operations
             $new_schema = dbx::get_schema(dbsteward::$new_database, $item['schema']['name']);
             $old_schema = dbx::get_schema(dbsteward::$old_database, $item['schema']['name']);
             // do all types and functions on their own before table creation
             // see next loop for other once per schema work
             if (!in_array(trim($new_schema['name']), $processed_schemas)) {
                 mssql10_diff_types::apply_changes($ofs1, $old_schema, $new_schema, $type_modified_columns);
                 mssql10_diff_functions::diff_functions($ofs1, $ofs3, $old_schema, $new_schema);
                 $processed_schemas[] = trim($new_schema['name']);
             }
         }
         // remove all old constraints before new contraints, in reverse dependency order
         for ($i = count(mssql10_diff::$old_table_dependency) - 1; $i >= 0; $i--) {
             // find the necessary pointers
             $item = mssql10_diff::$old_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;
             }
             $new_schema = dbx::get_schema(dbsteward::$new_database, $item['schema']['name']);
             $new_table = NULL;
             if ($new_schema != NULL) {
                 $new_table = dbx::get_table($new_schema, $item['table']['name']);
             }
             $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']);
             }
             if ($old_table == NULL) {
                 throw new exception("old_table " . $item['schema']['name'] . "." . $item['table']['name'] . " not found. This is not expected as this reverse constraint loop was based on the old_table_dependency list!");
             }
             // @NOTICE: when dropping constraints, dbx::renamed_table_check_pointer() is not called for $old_table
             // as mssql10_diff_tables::diff_constraints_table() will do rename checking when recreating constraints for renamed tables
             mssql10_diff_tables::diff_constraints_table($ofs1, $old_schema, $old_table, $new_schema, $new_table, 'constraint', TRUE);
             mssql10_diff_tables::diff_constraints_table($ofs1, $old_schema, $old_table, $new_schema, $new_table, 'primaryKey', TRUE);
         }
         $processed_schemas = array();
         for ($i = 0; $i < count(mssql10_diff::$new_table_dependency); $i++) {
             // find the necessary pointers
             $item = mssql10_diff::$new_table_dependency[$i];
             $new_schema = dbx::get_schema(dbsteward::$new_database, $item['schema']['name']);
             $new_table = NULL;
             if ($new_schema != NULL) {
                 $new_table = dbx::get_table($new_schema, $item['table']['name']);
             }
             $old_schema = dbx::get_schema(dbsteward::$old_database, $item['schema']['name']);
             // schema level stuff should only be done once, keep track of which ones we have done
             // see above for pre table creation stuff
             // see below for post table creation stuff
             if (!in_array($new_schema['name'], $processed_schemas)) {
                 mssql10_diff_sequences::diff_sequences($ofs1, $old_schema, $new_schema);
                 $processed_schemas[] = $new_schema['name'];
             }
             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_table = NULL;
             if ($old_schema != NULL) {
                 $old_table = dbx::get_table($old_schema, $item['table']['name']);
             }
             dbx::renamed_table_check_pointer($old_schema, $old_table, $new_schema, $new_table);
             mssql10_diff_tables::diff_tables($ofs1, $ofs3, $old_schema, $new_schema, $old_table, $new_table);
             mssql10_diff_indexes::diff_indexes_table($ofs1, $old_schema, $old_table, $new_schema, $new_table);
             mssql10_diff_tables::diff_clusters_table($ofs1, $old_schema, $old_table, $new_schema, $new_table);
             mssql10_diff_tables::diff_constraints_table($ofs1, $old_schema, $old_table, $new_schema, $new_table, 'primaryKey', FALSE);
             mssql10_diff_triggers::diff_triggers_table($ofs1, $old_schema, $old_table, $new_schema, $new_table);
             mssql10_diff_tables::diff_constraints_table($ofs1, $old_schema, $old_table, $new_schema, $new_table, 'constraint', FALSE);
         }
         // drop old tables in reverse dependency order
         for ($i = count(mssql10_diff::$old_table_dependency) - 1; $i >= 0; $i--) {
             // find the necessary pointers
             $item = mssql10_diff::$old_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;
             }
             $new_schema = dbx::get_schema(dbsteward::$new_database, $item['schema']['name']);
             $new_table = NULL;
             if ($new_schema != NULL) {
                 $new_table = dbx::get_table($new_schema, $item['table']['name']);
             }
             $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']);
             }
             if ($old_table == NULL) {
                 throw new exception("old_table " . $item['schema']['name'] . "." . $item['table']['name'] . " not found. This is not expected as this reverse constraint loop was based on the old_table_dependency list!");
             }
             mssql10_diff_tables::drop_tables($ofs3, $old_schema, $new_schema, $old_table, $new_table);
         }
     }
     // create all views in all schemas, regardless whether dependency order is known or not
     foreach (dbx::get_schemas(dbsteward::$new_database) as $new_schema) {
         $old_schema = dbx::get_schema(dbsteward::$old_database, $new_schema['name']);
         $new_schema = dbx::get_schema(dbsteward::$new_database, $new_schema['name']);
         mssql10_diff_views::create_views($ofs1, $old_schema, $new_schema);
     }
 }
 /**
  * Outputs commands for differences in constraints.
  *
  * @param object  $ofs              output file pointer
  * @param object  $old_schema       original schema
  * @param object  $new_schema       new schema
  * @param string  $type             type of constraints to process
  * @param boolean $drop_constraints
  */
 public static function diff_constraints($ofs, $old_schema, $new_schema, $type, $drop_constraints)
 {
     foreach (dbx::get_tables($new_schema) as $new_table) {
         if ($old_schema == NULL) {
             $old_table = NULL;
         } else {
             $old_table = dbx::get_table($old_schema, $new_table['name']);
         }
         mssql10_diff_tables::diff_constraints_table($ofs, $old_schema, $old_table, $new_schema, $new_table, $type, $drop_constraints);
     }
 }