<?php
$sql_query = 'INSERT INTO ' . $config['table_prefix'] . 'cron ';
$sql_query .= <<<'EOT'
(`description`, `period`, `thread_name`, `last_start_time`, `last_stop_time`, `command`) VALUES ('_remove_null_from_db_', '*/30 19-23 * * *', 'db_update', now(), now(), '$result = sql_query(\"SELECT * FROM information_schema.columns WHERE \r\n    table_schema = \'\" . $config[\'dbname\'] . \"\' AND table_name LIKE \'\" . $config[\'table_prefix\'] . \"%\' AND column_name != \'id\' AND column_default IS NULL\");\r\nwhile ($field = sql_fetch_assoc($result)) {\r\n    if ($field[\'DATA_TYPE\'] == \'enum\' && $field[\'IS_NULLABLE\'] == \'NO\') {\r\n        sql_query(\"ALTER TABLE \" . $field[\'TABLE_NAME\'] . \" MODIFY `\" . $field[\'COLUMN_NAME\'] . \"` \" . $field[\'COLUMN_TYPE\'] . \" DEFAULT NULL\");\r\n    } elseif ($field[\'DATA_TYPE\'] != \'enum\') {\r\n        if ($field[\'DATA_TYPE\'] == \'decimal\' || $field[\'DATA_TYPE\'] == \'float\' || strpos($field[\'DATA_TYPE\'], \'int\') !== false) {\r\n            $field[\'COLUMN_DEFAULT\'] = 0;\r\n        } elseif ($field[\'DATA_TYPE\'] == \'datetime\') {\r\n            $field[\'COLUMN_DEFAULT\'] = \'0000-00-00 00:00:00\';\r\n        } else {\r\n            $field[\'COLUMN_DEFAULT\'] = \'\';\r\n        }\r\n        sql_query(\"ALTER TABLE \" . $field[\'TABLE_NAME\'] . \" MODIFY `\" . $field[\'COLUMN_NAME\'] . \"` \" . $field[\'COLUMN_TYPE\'] . \" NOT NULL DEFAULT \'\" . $field[\'COLUMN_DEFAULT\'] . \"\'\");\r\n    }\r\n}\r\nsql_query(\"UPDATE \" . $config[\'table_prefix\'] . \"fields SET `int_name` = \'\' WHERE `int_name` IS NULL OR `int_name` = \'NULL\'\");\r\nsql_query(\"DELETE FROM \" . $config[\'table_prefix\'] . \"cron WHERE description = \'_remove_null_from_db_\'\");\r\nsql_query(\"DELETE FROM \" . $config[\'table_prefix\'] . \"cron_queue WHERE thread_name = \'db_update\'\");')
EOT;
run_query($sql_query);