[odb-users] Schema evolution on existing database
Monika Idzik
moni.idzik at gmail.com
Mon Oct 28 10:01:09 EDT 2019
Hi,
I'm trying to add new columns to existing table in a MySQL database.
I followed the instructions regarding schema evolution, generating the code:
odb::core::schema_version v(m_db->schema_version());
odb::core::schema_version
bv(odb::core::schema_catalog::base_version(*m_db));
odb::core::schema_version
cv(odb::core::schema_catalog::current_version(*m_db));
if (v == 0) {
odb::core::transaction t(m_db->begin());
odb::core::schema_catalog::create_schema(*m_db);
t.commit();
} else if (v < cv) {
if (v < bv) {
std::cout << "Error: migration from this version is no longer
supported." << std::endl;
}
for (v = odb::core::schema_catalog::next_version(*m_db, v); v <= cv; v
= odb::core::schema_catalog::next_version(*m_db, v))
{
odb::core::transaction t(m_db->begin());
odb::core::schema_catalog::migrate_schema_pre(*m_db, v);
// Data migration goes here.
odb::core::schema_catalog::migrate_schema_post(*m_db, v);
t.commit();
}
} else if (v > cv) {
std::cout << "Error: old application trying to access new database." <<
std::endl;
}
}
I also added
#pragma db model version(1,1)
It all works well: when I'm changing to version (1,2) and adding new
columns.
However, I already have data in my current database that was created
without model versions and schema evolution code: this means that when I'm
adding the above code, the existing data is being removed (I assume while
doing odb::core::schema_catalog::create_schema(*m_db);)
Is there a way to add new columns to the existing database (so changing
database schema) without losing existing data in the database?
Thanks,
Monika
More information about the odb-users
mailing list