OrmLite support for onUpgrade?

1,059 views
Skip to first unread message

Glenview Jeff

unread,
Aug 10, 2012, 5:54:57 PM8/10/12
to ormlite...@googlegroups.com
Does OrmLite provide any higher abstraction than writing raw code like this to upgrade databases? 

I'm pretty SQL ignorant; I assume I have to explicitly add the columns to maintain database consistency of old rows that don't already have the field?

    @Override
    public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
            int oldVersion, int newVersion) {
       if(oldVersion==123) {
             DaoManager.createDao(connectionSource, MyStuff.class).executeRaw(
               "ALTER TABLE `" + MyStuff.TABLE_NAME + "` ADD COLUMN " + MyStuff.IS_GOOD + " BOOLEAN DEFAULT 1;");

jc

unread,
Aug 17, 2012, 1:21:57 PM8/17/12
to ormlite...@googlegroups.com
Currently, you have to maintain your own logic similar to what you have here because OrmLite doesn't have any ALTER table logic in the TableUtils class.  You could write a method that generates the ALTER SQL for you based on the FieldType column definition which would helps avoid duplicating this in your upgrade logic.  I did this and it helps keep things clean and consistent.

I'd vote for some type of alter table method in TableUtils as well.  I had emailed Gray some thoughts on this a while back but we didn't get very far.  I'll email him again with what I'm currently using to see if he wants to consider adding it.  Keep your fingers crossed!

Eduardo Silva

unread,
Aug 25, 2013, 10:54:33 AM8/25/13
to ormlite...@googlegroups.com
It's possible to migrate data from older version to a new one in this way?


dao = getDao(type);
List<T> all = dao.queryForAll();
TableUtils.dropTable(connectionSource, type, true);
onCreate(db, connectionSource);
if (all != null) {
for(T obj : all) {
dao.create(obj);
}
}

Peter Großmann

unread,
Apr 14, 2014, 3:16:05 PM4/14/14
to ormlite...@googlegroups.com
nice idea, but dao.queryForAll() fails, because the class has already the new property

and you get an exception:

Caused by: java.lang.RuntimeException: java.sql.SQLException: Unknown field 'monospace' from the Android sqlite cursor, not in:[hyphenation, id, night, observeclipboard, observedownloads, resumeonchapterend, size, speed, textAfter, textBefore]

Reply all
Reply to author
Forward
0 new messages