* -- {tablename}_trigger_{optional_string}_before_update_{column_name}(OLD.column, NEW.column}
* -- {tablename}_trigger_{optional_string}_before_insert_{column_name}(OLD.column}
*
- *
*
+ * ------- Importing with triggers disabled.
+ *
+ * SET @DISABLE_TRIGGER=1; (or anything you like except NULL)
+ * do imports
+ * SET @DISABLE_TRIGGER=NULL;
+ *
+ * ------ Call a method disabling a particular set of triggers
+ * SET @DISABLE_TRIGGER_the_table_name=1; (or anything you like except NULL)
+ * do action
+ * SET @DISABLE_TRIGGER_the_table_name=NULL;*
*/
class Pman_Core_UpdateDatabase_MysqlLinks {
$this->loadIniFiles();
$this->updateTableComments();
+
+ $this->updateCharacterSet();
+
$ff = HTML_FlexyFramework::get();
if (!empty($ff->Pman['enable_trigger_tests'])) {
// create a list of source/targets from $this->links
- print_R($this->links);exit;
+
$revmap = array();
foreach($this->links as $tbl => $map) {
if (!isset($this->schema[$tbl])) {
if (!isset($this->schema[$target_table])) {
echo "Skip $target_table = table does not exist in schema\n";
- return;
+ continue;
}
BEFORE DELETE ON `{$target_table}`
FOR EACH ROW
BEGIN
- DECLARE mid INT(11);
-
+ DECLARE mid INT(11);
+ IF (@DISABLE_TRIGGER IS NULL AND @DISABLE_TRIGGER_{$target_table} IS NULL ) THEN
";
foreach($sources as $source=>$target) {
$err = substr("Failed Delete {$target_table} refs {$source_table}:{$source_col}", 0, 64);
$trigger .="
SET mid = 0;
- SELECT count(*) into mid FROM {$source_table} WHERE {$source_col} = OLD.{$target_col} LIMIT 1;
- IF mid > 0 THEN
-
- UPDATE `$err` SET x = 1;
-
+ IF OLD.{$target_col} > 0 THEN
+ SELECT count(*) into mid FROM {$source_table} WHERE {$source_col} = OLD.{$target_col} LIMIT 1;
+ IF mid > 0 THEN
+ UPDATE `$err` SET x = 1;
+ END IF;
END IF;
";
}
}
$trigger .= "
+ END IF;
END
";
FOR EACH ROW
BEGIN
DECLARE mid INT(11);
-
+ IF (@DISABLE_TRIGGER IS NULL AND @DISABLE_TRIGGER_{$tbl} IS NULL ) THEN
";
foreach($map as $source_col=>$target) {
$source_tbl = $tbl;
list($target_table , $target_col) = explode(':', $target);
+
+ if (!isset($this->schema[$target_table])) {
+ // skip... target table does not exist
+ continue;
+ }
+
+
$err = substr("Fail: INSERT referenced {$tbl}:{$source_col}", 0, 64);
$trigger .="
SET mid = 0;
}
$trigger .= "
+ END IF;
END
";
FOR EACH ROW
BEGIN
DECLARE mid INT(11);
-
+ IF (@DISABLE_TRIGGER IS NULL AND @DISABLE_TRIGGER_{$tbl} IS NULL ) THEN
";
foreach($map as $source_col=>$target) {
$source_tbl = $tbl;
list($target_table , $target_col) = explode(':', $target);
+
+ if (!isset($this->schema[$target_table])) {
+ // skip... target table does not exist
+ continue;
+ }
+
$err = substr("Fail: UPDATE referenced {$tbl}:$source_col", 0, 64);
$trigger .="
SET mid = 0;
}
$trigger .= "
+ END IF;
END
";
return $ret;
}
+ function updateCharacterSet()
+ {
+ foreach (array_keys($this->schema) as $tbl){
+
+ if(strpos($tbl, '__keys') !== false ){
+ continue;
+ }
+
+ $ce = DB_DataObject::factory('core_enum');
+
+ $ce->query("
+ SELECT
+ CCSA.character_set_name csname,
+ CCSA.collation_name collatename
+ FROM
+ information_schema.`TABLES` T,
+ information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA
+ WHERE
+ CCSA.collation_name = T.table_collation
+ AND
+ T.table_schema = '{$ce->database()}' -- COLLATE utf8_unicode_ci
+ AND
+ T.table_name = '{$tbl}' -- COLLATE utf8_unicode_ci
+ ");
+
+ $ce->fetch();
+
+ if($ce->csname == 'utf8' && $ce->collatename == 'utf8_unicode_ci'){
+ echo "$tbl is Already utf8 \n";
+ continue;
+ }
+
+ $ce = DB_DataObject::factory('core_enum');
+ $ce->query("ALTER TABLE {$tbl} CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci");
+ echo "FIXED utf8 on {$tbl}\n";
+
+ }
+ }
}