/** * Drop removed types * Add new types * Apply type definition differences, updating the type's tables along the way * * @param $ofs output file segmenter * @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 || !mssql10_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 (mssql10_type::equals($old_schema, $old_type, $new_schema, $new_type)) { continue; } $columns = array(); $ofs->write("-- type " . $new_type['name'] . " definition migration (1/4): dependant tables column constraint drop\n"); $ofs->write(mssql10_type::column_constraint_temporary_drop($columns, $old_schema, $old_type) . "\n"); $ofs->write("-- type " . $new_type['name'] . " definition migration (2/4): delete type list values\n"); $ofs->write(mssql10_type::get_enum_value_delete($old_schema, $old_type) . "\n"); $ofs->write("-- type " . $new_type['name'] . " definition migration (3/4): recreate type list values\n"); $ofs->write(mssql10_type::get_enum_value_insert($new_schema, $new_type) . "\n"); $ofs->write("-- type " . $new_type['name'] . " definition migration (4/4): restore dependant tables column constraint add\n"); $ofs->write(mssql10_type::column_constraint_restore($columns, $new_schema, $new_type) . "\n"); } }
public static function get_creation_sql($node_schema, $node_type) { // notice in mssql10_column::column_type() enumerated types are defined as varchar(255) with a CHECK CONSTRAINT added // this is due to the fact there are no enumerated types in MSSQL // so, for application reference without VIEW DEFINITION permissions given to the application role // create a value reference table for the enumeration's possible values, for the application to refer to $reference_table_name = mssql10::get_quoted_schema_name($node_schema['name']) . '.' . mssql10::get_quoted_table_name($node_type['name'] . '_enum_values'); // enum types rewritten as varchar(255) -- see mssql10_column::column_type() $ddl = "CREATE TABLE " . $reference_table_name . " (\n enum_value varchar(255)\n );\n"; $ddl .= mssql10_type::get_enum_value_insert($node_schema, $node_type); $reference_table_node = new SimpleXMLElement('<table name="' . $node_type['name'] . '_enum_values' . '"> <grant operation="SELECT" role="ROLE_APPLICATION"/> </table>'); $ddl .= mssql10_permission::get_sql(dbsteward::$new_database, $node_schema, $reference_table_node, $reference_table_node->grant); return $ddl; }