Handling class changes when using ORM such as ODB

谁说我不能喝 提交于 2019-12-05 16:04:48

From the outset, full disclosure: I work on ODB. And to answer your third question, no, there aren't any ;-).

Seriously, though, schema evolution is a hard problem and it is one of the three big items on our TODO list (the other two are multi-database support and SQL-to-C++ compiler). The good news is that we are pretty much done with multi-database support and the next one in line is schema evolution.

Generally, it seems the best approach is to bring your schema (and the data, if necessary) to the latest version. The alternative of making the application capable of reading multiple different versions just doesn't seem to scale in the real world.

As an example, let's say we added a data member to the class which in the database schema level translates to adding a column to the corresponding table. The way to handle this is to make this new column NULL-able (with, say, odb::nullable or boost::optional). The idea here is that old data that doesn't have a value for this column will be NULL (which the application can detect and handle).

Next we need to upgrade the schema in the database. In this case we will need to execute the ALTER TABLE ADD COLUMN statement which will add the new column. Once ODB supports schema evolution, it will generate these migration statements automatically. Right now you will have to write them yourself (pain in the ass, I know). All the existing rows in the table will be automatically assigned NULL value for this column.

So normally an application will contain sets of such statements that upgrade the schema from one version to the next. E.g., from 1 to 2, from 2 to 3, etc. The database will store the schema version and the application will know its latest schema version. Immediately after opening the database, the application will check the database version and if it is below the application schema version, it will start running these migration sets to upgrade the schema to the latest version.

If you're still open to an altenative to ODB, you might consider quince: http://quince-lib.com (and full disclosure: I wrote it).

On the specific issue of upgrading your data type: quince doesn't automatically detect the need to evolve, or design a strategy for evolution, or anything like that. What it gives you is a C++ interface to ALTER TABLE. But on the positive side: it is all at the C++ level: you describe your alteration in terms of your C++ data types, and it's all statically type-checked.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!