/**
  * 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 (!mysql5_schema::contains_function($new_schema, mysql5_function::get_declaration($new_schema, $old_function))) {
                 $ofs3->write(mysql5_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'], mysql5_function::get_declaration($new_schema, $new_function));
         }
         if ($old_function == null || !mysql5_function::equals($new_schema, $new_function, $old_function, mysql5_diff::$ignore_function_whitespace)) {
             $ofs1->write(mysql5_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(mysql5_function::get_creation_sql($new_schema, $new_function) . "\n");
             } else {
                 if (mysql5_schema::contains_type($new_schema, $new_function['returns']) && mysql5_schema::contains_type($old_schema, $new_function['returns']) && !mysql5_type::equals(dbx::get_type($old_schema, $new_function['returns']), dbx::get_type($new_schema, $new_function['returns']))) {
                     $ofs1->write("-- Force function re-creation {$new_function['name']} for type: {$new_function['returns']}\n");
                     $ofs1->write(mysql5_function::get_creation_sql($new_schema, $new_function) . "\n");
                 }
             }
         }
     }
 }
    public function testDelimiters()
    {
        $xml = <<<XML
<schema name="test" owner="ROLE_OWNER">
  <function name="test_fn" returns="text">
    <functionParameter name="a" type="text"/>
    <functionParameter name="b" type="int"/>
    <functionParameter name="c" type="date"/>
    <functionDefinition language="sql" sqlFormat="mysql5">
BEGIN
  DECLARE val BIGINT(20);
  IF @__sequences_lastval IS NULL THEN
    SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'nextval() has not been called yet this session';
  ELSE
    SELECT `currval` INTO val FROM  `__sequences_currvals` WHERE `name` = seq_name;
    RETURN val;
  END IF;
END;
    </functionDefinition>
  </function>
</schema>
XML;
        $schema = new SimpleXMLElement($xml);
        $expected = <<<SQL
DROP FUNCTION IF EXISTS `test_fn`;
CREATE DEFINER = CURRENT_USER FUNCTION `test_fn` (`a` text, `b` int, `c` date)
RETURNS text
LANGUAGE SQL
MODIFIES SQL DATA
NOT DETERMINISTIC
SQL SECURITY INVOKER
BEGIN
  DECLARE val BIGINT(20);
  IF @__sequences_lastval IS NULL THEN
    SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'nextval() has not been called yet this session';
  ELSE
    SELECT `currval` INTO val FROM  `__sequences_currvals` WHERE `name` = seq_name;
    RETURN val;
  END IF;
END;
SQL;
        $actual = trim(mysql5_function::get_creation_sql($schema, $schema->function));
        $this->assertEquals($expected, $actual);
        mysql5::$swap_function_delimiters = TRUE;
        $expected = <<<SQL
DROP FUNCTION IF EXISTS `test_fn`;
DELIMITER \$_\$
CREATE DEFINER = CURRENT_USER FUNCTION `test_fn` (`a` text, `b` int, `c` date)
RETURNS text
LANGUAGE SQL
MODIFIES SQL DATA
NOT DETERMINISTIC
SQL SECURITY INVOKER
BEGIN
  DECLARE val BIGINT(20);
  IF @__sequences_lastval IS NULL THEN
    SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'nextval() has not been called yet this session';
  ELSE
    SELECT `currval` INTO val FROM  `__sequences_currvals` WHERE `name` = seq_name;
    RETURN val;
  END IF;
END\$_\$
DELIMITER ;
SQL;
        $actual = trim(mysql5_function::get_creation_sql($schema, $schema->function));
        $this->assertEquals($expected, $actual);
    }
Exemplo 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
 }