Commit 55b63e80 authored by phenigma's avatar phenigma

Refs #2648 - re-arrange validation - data must be compatible with new datatype...

Refs #2648 - re-arrange validation - data must be compatible with new datatype before modifying the tables or column definitions, data can then be set to proper values after the table/column alterations are complete.
parent ae86332d
......@@ -2674,33 +2674,114 @@ void Table::ValidateTable()
vector<string> vectString;
string s = row[1];
StringUtils::Tokenize(s,"\n",vectString);
// #### make data valid for modify column and alter table
// #### all data in the tables must be valid for any column/table alterations to succeed.
for(vector<string>::iterator it=vectString.begin();it!=vectString.end();++it)
{
vector<string> vectString2;
string sInput = *it;
StringUtils::Tokenize(sInput," ",vectString2);
string sFieldName = vectString2[0];
if( it->find("datetime")!=string::npos && it->find("0000-00-00 00:00:00")!=string::npos )
{
vector<string> vectString2;
string sInput = *it;
StringUtils::Tokenize(sInput," ",vectString2);
cout << "**** Fixing datetime data in column " << sFieldName << ", in table `" << m_sName << "`" << endl;
std::ostringstream sSQL;
sSQL << "UPDATE `" << m_sName << "` set " << sFieldName << "='1970-01-01 00:00:00' WHERE " << sFieldName << "<'1970-01-01 00:00:00'";
m_pDatabase->threaded_mysql_query( sSQL.str() );
break;
}
if( it->find("`psc_mod`")!=string::npos )
{
if( it->find("timestamp")==string::npos )
{
// the myth_sqlcvs table has incorrect data types for the psc_mod column, need to set as timestamp.
cout << "**** Fixing timestamp field of column `psc_mod`, in table `" << m_sName << "`" << endl;
if( it->find("NOT NULL")==string::npos )
{
std::ostringstream sSQL;
sSQL << "UPDATE `" << m_sName << "` set psc_mod=NULL";
m_pDatabase->threaded_mysql_query( sSQL.str() );
}
else
{
cout << "**** ERROR: Cannot fix psc_mod in table `" << m_sName << "`, not a timestamp and NULL not permitted." << endl;
}
}
else
//if( it->find("ON UPDATE CURRENT_TIMESTAMP")==string::npos || it->find("timestamp NULL")==string::npos )
{
cout << "**** Fixing timestamp field of column `psc_mod`, in table `" << m_sName << "`" << endl;
std::ostringstream sSQL;
sSQL << "UPDATE `" << m_sName << "` set psc_mod='1970-01-01 00:00:00' WHERE psc_mod<'1970-01-01 00:00:00'";
m_pDatabase->threaded_mysql_query( sSQL.str() );
}
break;
}
}
// #### modify/alter fields and tables
for(vector<string>::iterator it=vectString.begin();it!=vectString.end();++it)
{
vector<string> vectString2;
string sInput = *it;
StringUtils::Tokenize(sInput," ",vectString2);
string sFieldName = vectString2[0];
string sFieldName = vectString2[0];
cout << "**** Fixing datetime field of column " << sFieldName << ", in table `" << m_sName << "`" << endl;
if( it->find("datetime")!=string::npos && it->find("0000-00-00 00:00:00")!=string::npos )
{
cout << "**** Modifying datetime column " << sFieldName << ", in table `" << m_sName << "`" << endl;
std::ostringstream sSQL;
sSQL << "UPDATE `" << m_sName << "` set " << sFieldName << "='1970-01-01 00:00:00' WHERE " << sFieldName << "=0";
sSQL << "ALTER TABLE `" << m_sName << "` MODIFY COLUMN " << sFieldName << " datetime NULL default NULL";
m_pDatabase->threaded_mysql_query( sSQL.str() );
std::ostringstream sSQL2;
sSQL2 << "ALTER TABLE `" << m_sName << "` MODIFY COLUMN " << sFieldName << " datetime NULL default NULL";
m_pDatabase->threaded_mysql_query( sSQL2.str() );
std::ostringstream sSQL3;
sSQL3 << "UPDATE `" << m_sName << "` set " << sFieldName << "=NULL WHERE " << sFieldName << "='1970-01-01 00:00:00' OR " << sFieldName << "=0";
m_pDatabase->threaded_mysql_query( sSQL3.str() );
break;
}
if( it->find("CHARACTER SET")!=string::npos )
if( it->find("`psc_mod`")!=string::npos )
{
if( it->find("timestamp")==string::npos )
{
// the myth_sqlcvs table has incorrect data types for the psc_mod column, need to set as timestamp.
cout << "**** Modifying non-timestamp field of column `psc_mod`, in table `" << m_sName << "`" << endl;
std::ostringstream sSQL;
sSQL << "ALTER TABLE `" << m_sName << "` MODIFY COLUMN `psc_mod` timestamp NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP";
m_pDatabase->threaded_mysql_query( sSQL.str() );
}
else
if( it->find("ON UPDATE CURRENT_TIMESTAMP")==string::npos || it->find("timestamp NULL")==string::npos )
{
cout << "**** Modifying timestamp field of column `psc_mod`, in table `" << m_sName << "`" << endl;
std::ostringstream sSQL;
sSQL << "ALTER TABLE `" << m_sName << "` MODIFY COLUMN `psc_mod` timestamp NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP";
m_pDatabase->threaded_mysql_query( sSQL.str() );
}
break;
}
if( m_sName.find("psc_")!=string::npos && m_sName.rfind("bathdr")!=string::npos && it->find("IPAddress")!=string::npos && it->find("varchar(50)")==string::npos)
{
vector<string> vectString2;
string sInput = *it;
StringUtils::Tokenize(sInput," ",vectString2);
cout << "**** Modifying IPAddress field in table `" << m_sName << "`" << endl;
// Increase the IP address field size in psc_ tables
std::ostringstream sSQL;
sSQL << "ALTER TABLE `" << m_sName << "` MODIFY COLUMN `IPAddress` varchar(50) DEFAULT NULL";
m_pDatabase->threaded_mysql_query( sSQL.str() );
break;
}
string sFieldName = vectString2[0];
if( it->find("ENGINE=")!=string::npos && it->find("CHARSET=utf8")==string::npos )
{
cout << "**** Modifying charset in table `" << m_sName << "`" << endl;
// Increase the IP address field size in psc_ tables
std::ostringstream sSQL;
sSQL << "ALTER TABLE `" << m_sName << "` CHARACTER SET utf8";
m_pDatabase->threaded_mysql_query( sSQL.str() );
break;
}
if( it->find("CHARACTER SET")!=string::npos )
{
cout << "**** Fixing character set field of column " << sFieldName << ", in table `" << m_sName << "`" << endl;
size_t stBegin = it->find("CHARACTER SET");
......@@ -2712,39 +2793,34 @@ void Table::ValidateTable()
sSQL << "ALTER TABLE `" << m_sName << "` MODIFY COLUMN " << *it;
//cout << "**** " << sSQL.str() << endl;
m_pDatabase->threaded_mysql_query( sSQL.str() );
break;
}
if( it->find("ENGINE=")!=string::npos && it->find("CHARSET=utf8")==string::npos )
{
cout << "**** Fixing charset in table `" << m_sName << "`" << endl;
// Increase the IP address field size in psc_ tables
std::ostringstream sSQL;
sSQL << "ALTER TABLE `" << m_sName << "` CHARACTER SET utf8";
m_pDatabase->threaded_mysql_query( sSQL.str() );
}
if( m_sName.find("psc_")!=string::npos && m_sName.rfind("bathdr")!=string::npos && it->find("IPAddress")!=string::npos && it->find("varchar(50)")==string::npos)
}
// fix data
for(vector<string>::iterator it=vectString.begin();it!=vectString.end();++it)
{
vector<string> vectString2;
string sInput = *it;
StringUtils::Tokenize(sInput," ",vectString2);
string sFieldName = vectString2[0];
if( it->find("datetime")!=string::npos && it->find("0000-00-00 00:00:00")!=string::npos )
{
cout << "**** Fixing IPAddress field in table `" << m_sName << "`" << endl;
// Increase the IP address field size in psc_ tables
cout << "**** Updating datetime data in column " << sFieldName << ", in table `" << m_sName << "`" << endl;
std::ostringstream sSQL;
sSQL << "ALTER TABLE `" << m_sName << "` MODIFY COLUMN `IPAddress` varchar(50) DEFAULT NULL";
sSQL << "UPDATE `" << m_sName << "` set " << sFieldName << "=NULL WHERE " << sFieldName << "<='1970-01-01 00:00:00'";
m_pDatabase->threaded_mysql_query( sSQL.str() );
break;
}
if( it->find("`psc_mod`")!=string::npos )
{
if( it->find("ON UPDATE CURRENT_TIMESTAMP")==string::npos || it->find("timestamp NULL")==string::npos )
{
cout << "**** Fixing timestamp field of column `psc_mod`, in table `" << m_sName << "`" << endl;
std::ostringstream sSQL;
sSQL << "UPDATE `" << m_sName << "` set psc_mod='1970-01-01 00:00:00' WHERE psc_mod=0";
m_pDatabase->threaded_mysql_query( sSQL.str() );
std::ostringstream sSQL2;
sSQL2 << "ALTER TABLE `" << m_sName << "` MODIFY COLUMN `psc_mod` timestamp NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP";
m_pDatabase->threaded_mysql_query( sSQL2.str() );
std::ostringstream sSQL3;
sSQL3 << "UPDATE `" << m_sName << "` set psc_mod=NULL WHERE psc_mod=0 OR psc_mod='1970-01-01 00:00:00'";
m_pDatabase->threaded_mysql_query( sSQL3.str() );
break;
}
cout << "**** Updating timestamp field of column `psc_mod`, in table `" << m_sName << "`" << endl;
std::ostringstream sSQL3;
sSQL3 << "UPDATE `" << m_sName << "` set psc_mod=NULL WHERE psc_mod<='1970-01-01 00:00:00'";
m_pDatabase->threaded_mysql_query( sSQL3.str() );
break;
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment