/** * 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 (!mssql10_schema::contains_type($new_schema, $type['name'])) { $ofs->write(mssql10_type::get_drop_sql($new_schema, $type) . "\n"); } } } }
/** * Outputs DDL for differences in functions * * @param $ofs1 stage1 output pointer * @param $ofs3 stage3 output pointer * @param $old_schema original schema * @param $new_schema new schema */ public static function diff_functions($ofs1, $ofs3, $old_schema, $new_schema) { // drop functions that no longer exist in stage 3 if ($old_schema != NULL) { foreach (dbx::get_functions($old_schema) as $old_function) { if (!mssql10_schema::contains_function($new_schema, mssql10_function::get_declaration($new_schema, $old_function))) { $ofs3->write(mssql10_function::get_drop_sql($old_schema, $old_function) . "\n"); } } } // Add new functions and replace modified functions foreach (dbx::get_functions($new_schema) as $new_function) { $old_function = NULL; if ($old_schema != NULL) { $old_function = dbx::get_function($old_schema, $new_function['name'], mssql10_function::get_declaration($new_schema, $new_function)); } if ($old_function == NULL) { $ofs1->write(mssql10_function::get_creation_sql($new_schema, $new_function) . "\n"); } else { if (!mssql10_function::equals($new_schema, $new_function, $old_function, mssql10_diff::$ignore_function_whitespace)) { // functions are not equal, old_function is not null, it previously existed // for MSSQL, there is no CREATE OR REPLACE FUNCTION, so drop the function explicitly $ofs1->write(mssql10_function::get_drop_sql($old_schema, $old_function) . "\n"); $ofs1->write(mssql10_function::get_creation_sql($new_schema, $new_function) . "\n"); } else { if (isset($new_function['forceRedefine']) && strcasecmp($new_function['forceRedefine'], 'true') == 0) { $ofs1->write("-- DBSteward insists on function recreation: " . $new_schema['name'] . "." . $new_function['name'] . " has forceRedefine set to true\n"); $ofs1->write(mssql10_function::get_creation_sql($new_schema, $new_function) . "\n"); } else { if (mssql10_schema::contains_type($new_schema, $new_function['returns']) && mssql10_schema::contains_type($old_schema, $new_function['returns']) && !mssql10_type::equals(dbx::get_type($old_schema, $new_function['returns']), dbx::get_type($new_schema, $new_function['returns']))) { $ofs1->write("-- dbstward insisting on function re-creation " . $new_function['name'] . " for type " . $new_function['returns'] . " definition change\n"); $ofs1->write(mssql10_function::get_drop_sql($old_schema, $old_function) . "\n"); $ofs1->write(mssql10_function::get_creation_sql($new_schema, $new_function) . "\n"); } } } } } }
/** * column_constraint_temporary_drop's companion - restore $columns list of columns to $node_type type * * @param $columns reference columns returned by reference * @param $node_schema * @param $node_type * @return string DDL */ public static function column_constraint_restore($columns, $node_schema, $node_type) { $ddl = ''; foreach ($columns as $column_map) { $ddl .= mssql10_type::get_add_check_sql($column_map['alter_column_schema'], $column_map['alter_column_table'], $column_map['alter_column_column'], $node_type); } return $ddl; }
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 ? }
public static function enum_type_check($db_doc, $node_schema, $node_table, $node_column, &$drop_sql, &$add_sql) { // if the column type is a defined enum, (re)add a check constraint to enforce the pseudo-enum $foreign = array(); $column_type = mssql10_column::column_type($db_doc, $node_schema, $node_table, $node_column, $foreign, FALSE); if (preg_match('/' . dbx::enum_regex($db_doc) . '/i', $column_type) > 0) { $type_schema_name = sql_parser::get_schema_name($column_type, $db_doc); $type_schema = dbx::get_schema($db_doc, $type_schema_name); $node_type = dbx::get_type($type_schema, sql_parser::get_object_name($column_type, $db_doc)); if (!$node_type) { var_dump($node_type); throw new exception('failed to find column_type ' . $column_type . ' in type_schema_name ' . $type_schema_name); } $drop_sql = mssql10_type::get_drop_check_sql($node_schema, $node_table, $node_column, $node_type); $add_sql = mssql10_type::get_add_check_sql($node_schema, $node_table, $node_column, $node_type); return TRUE; } return FALSE; }