On 30/08/18 09:51, Randall Smith wrote:
> is how to convert existing data from one DB format to another, given some 
> arbitrary set of changes in the database schema in the interim.

I use SQLite's user pragma.  It starts at zero.

https://sqlite.org/pragma.html#pragma_user_version

My code ends up looking like this:

if user_version==0:
    CREATE TABLE IF NOT EXISTS events(key, time, message);
    PRAGMA user_version=1;

if user_version==1:
    CREATE INDEX IF NOT EXISTS [events:message] ON events(message);
    PRAGMA user_version=2;

if user_version==2:
    ALTER TABLE events ADD COLUMN severity;
    PRAGMA user_version=3;

This ensures that the currently running code will upgrade the schema as
needed.  Ensure the commands are wrapped in a transaction so they either
completely happen or not.

I am helped by having low complexity schemas.  If yours are large you
could probably generate something like the above.  Some ORM style
engines also have schema and data upgrade functionality.

Roger

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to