[odb-users] schema validation

Boris Kolpackov boris at codesynthesis.com
Fri Oct 26 09:02:40 EDT 2012


Hi Alexander,

Alexander A. Prokhorov <alexander.prokhorov at datadvance.net> writes:

> Application is quite agile, schema often experiences minor changes, and  
> it's very desirable to perform a meaningful schema check every time I  
> open a new SQLite file.
>
> Otherwise, as I understand, user may encounter some database-related  
> exceptions/errors in runtime, which is highly undesirable.
>
> So, the question is, is there any recommended way to match current  
> schema (in odb::schema_catalog, I suppose) to be subset of schema in  
> some database?

There is no automatic database schema versioning in ODB yet (we may
add something like this in the future as part of the schema evolution
work).

In the meantime, it should be quite easy to implement this by storing
the version of the schema in the database itself (this also seems to
be the recommended way to do schema versioning). You could even create
a small persistent class to assist with this, something along these
lines:

#pragma db object
struct schema_version
{
  schema_version () {}
  schema_version (unsigned short mj, unsigned short mn)
    : id (1), major (mj), minor (mn)

  #pragma db id
  unsigned short id;

  unsigned short major;
  unsigned short minor
};

Then when creating a new database, you would also store the version:

schema_version sv (2, 3);
db.persis (sv);

And when opening an existing database, you would check the version:

schema_version sv;
db.load (1, sv);

if (sv.major < 2)
{
  // Incompatible schema version.
}

Boris



More information about the odb-users mailing list