Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tools] added back ticks to avoid sql error on table names with "-" #3168

Merged
merged 2 commits into from
Oct 10, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions tools/DB_date_zeros_removal.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,33 +84,33 @@
$autoUpdateSQL = '';
if (array_key_exists($field['TABLE_NAME'], $autoUpdateFields)) {
foreach ($autoUpdateFields[$field['TABLE_NAME']] as $col) {
$autoUpdateSQL .= ", $col=$col";
$autoUpdateSQL .= ", $db->escape($col)=$col";
}
}

if ($field['COLUMN_DEFAULT']=='0000-00-00') {
echo "The script will modify the date schema for TABLE: `".$field['TABLE_NAME']."` FIELD: `".$field['COLUMN_NAME']."` to default to NULL\n";
$alters .= "ALTER TABLE `".$field['TABLE_NAME']."` MODIFY `".$field['COLUMN_NAME']."` ".$field['COLUMN_TYPE']." DEFAULT NULL;\n";
$alters .= "ALTER TABLE ".$db->escape($field['TABLE_NAME'])." MODIFY ".$db->escape($field['COLUMN_NAME'])." ".$field['COLUMN_TYPE']." DEFAULT NULL;\n";
} else if ($field['COLUMN_DEFAULT']=='0000-00-00 00:00:00') {
echo "The script will modify the date schema for TABLE: `".$field['TABLE_NAME']."` FIELD: `".$field['COLUMN_NAME']."` to default to NULL\n";
$alters .= "ALTER TABLE `".$field['TABLE_NAME']."` MODIFY `".$field['COLUMN_NAME']."` ".$field['COLUMN_TYPE']." DEFAULT NULL;\n";
$alters .= "ALTER TABLE ".$db->escape($field['TABLE_NAME'])." MODIFY ".$db->escape($field['COLUMN_NAME'])." ".$field['COLUMN_TYPE']." DEFAULT NULL;\n";
}


if ($field['DATA_TYPE'] == 'date' && $field['IS_NULLABLE']=='YES') {
$updates .= "UPDATE ".$database['database'].".".$field['TABLE_NAME'].
" SET ".$field['COLUMN_NAME']."=NULL".$autoUpdateSQL.
" WHERE CAST(".$field['COLUMN_NAME']." AS CHAR(20))='0000-00-00';\n";
$updates .= "UPDATE ".$db->escape($database['database']).".".$db->escape($field['TABLE_NAME']).
" SET ".$db->escape($field['COLUMN_NAME'])."=NULL".$autoUpdateSQL.
" WHERE CAST(".$db->escape($field['COLUMN_NAME'])." AS CHAR(20))='0000-00-00';\n";
} else if (($field['DATA_TYPE'] == 'datetime' || $field['DATA_TYPE'] == 'timestamp') && $field['IS_NULLABLE']=='YES') {
$updates .= "UPDATE ".$database['database'].".".$field['TABLE_NAME'].
" SET ".$field['COLUMN_NAME']."=NULL".$autoUpdateSQL.
" WHERE CAST(".$field['COLUMN_NAME']." AS CHAR(20))='0000-00-00 00:00:00';\n";
$updates .= "UPDATE ".$db->escape($database['database']).".".$db->escape($field['TABLE_NAME']).
" SET ".$db->escape($field['COLUMN_NAME'])."=NULL".$autoUpdateSQL.
" WHERE CAST(".$db->escape($field['COLUMN_NAME'])." AS CHAR(20))='0000-00-00 00:00:00';\n";
} else {
echo "COLUMN ".$field['COLUMN_NAME']." in TABLE ".$field['TABLE_NAME']." is NOT NULLABLE. ".
"A date '1000-01-01' will be entered instead of '0000-00-00' values.\n";
$nonNullUpdates .= "UPDATE ".$database['database'].".".$field['TABLE_NAME'].
" SET ".$field['COLUMN_NAME']."='1000-01-01'".$autoUpdateSQL.
" WHERE CAST(".$field['COLUMN_NAME']." AS CHAR(20))='0000-00-00';\n";
$nonNullUpdates .= "UPDATE ".$db->escape($database['database']).".".$db->escape($field['TABLE_NAME']).
" SET ".$db->escape($field['COLUMN_NAME'])."='1000-01-01'".$autoUpdateSQL.
" WHERE CAST(".$db->escape($field['COLUMN_NAME'])." AS CHAR(20))='0000-00-00';\n";
}

}
Expand Down