greenDao 架构升级 [英] greenDao Schema Upgrade

查看:10
本文介绍了greenDao 架构升级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到另一个关于使用 green dao 的架构升级/迁移的问题(此处)

该答案中有很多链接可用于在进行架构升级时使用的良好模式 - 但是没有示例说明您实际对数据执行的操作以正确迁移数据,而且我无法找到任何内容.

就我而言,我的迁移非常简单 - 我不希望转换任何现有数据,我只需要向我的架构中添加一些新表,我怀疑这是一种相当普遍的情况.

在不删除用户已经保存的数据的情况下向架构添加新表的最简单方法是什么?一个具体的例子将不胜感激.

如果 greenDao 提供一个类似于 DevOpenHelper 的类,它会简单地添加先前不存在于架构中的新表/列,而无需先删除现有的选项卡/数据,那就太棒了.

我终于有时间自己深入研究这个问题,并意识到在保留旧表中的数据的同时添加新表非常容易.

免责声明:虽然我意识到此实现特定于我的场景,但我认为这对像我这样使用 Android ORM 工具 (greenDao) 专门处理 Android 上的 SQLite 的人很有帮助.我知道这对于那些从一开始就编写自己的表创建查询的人来说很常见,但对于那些在 Android 上使用 SQLite 数据库的人来说,我认为这个例子会有所帮助.

答案:您可以修改 DevOpenHelper 内部类或创建自己的类.我暂时选择编辑 DevOpenHelper 以保持我的示例简单 - 但是,请注意,如果您重新生成 greendao 类,DevOpenHelper 将被覆盖.最好创建自己的类,例如MyOpenHelper"并使用它.

在我更改之前,DevOpenHelper.onUpgrade 如下所示:

@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){Log.i("greenDAO", "通过删除所有表将架构从版本 " + oldVersion + " 升级到 " + newVersion + "");dropAllTables(db, true);onCreate(db);}

与其删除所有表,不如看看 GreenDao 自动生成的 createAllTables 方法.

重写 onUpgrade 以检查oldVersion"是否是您要升级的版本,然后只为新"表调用 createTable 方法.这是我的 onUpgrade 方法现在的样子:

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){Log.i("greenDAO", "将架构从版本 " + oldVersion + " 升级到 " +//从旧模式到新模式如果(旧版本 == 3 && 新版本 == 4){布尔值 ifNotExists = false;//保留旧表,只创建不存在的表//在之前的模式中NewTable1Dao.createTable(db, ifNotExists);NewTable2Dao.createTable(db, ifNotExists);NewTable3Dao.createTable(db, ifNotExists);NewTable4Dao.createTable(db, ifNotExists);}别的{dropAllTables(db, true);onCreate(db);}}

添加新列与此类似,只是您必须编写一些 SQL 或查看 greenDao 自动生成的 SQL 创建语句并利用这些语句.

要将单个新列(NEW_COLUMN,假设它是整数类型)添加到现有表 (EXISTING_TABLE),请执行以下操作:

db.execSQL("ALTER TABLE 'EXISTING_TABLE' ADD 'NEW_COLUMN' INTEGER");

对我现在来说,我需要做的就是添加新的表格,所以这最终变得相当简单.希望其他人觉得这很有用.

I have seen another question about schema upgrade/migration using green dao (here)

There are lots of links in that answer for a good pattern to use when doing schema upgrades - however there are no examples of what you actually do to your data to migrate it properly and I'm having trouble finding anything.

In my case, my migration is incredibly straight forward - I do not wish to transform any existing data, I simply need to add some new tables to my schema, which I suspect is a fairly common situation.

What is the easiest way to add new tables to your schema without deleting data your users have already saved? A specific example would be greatly appreciated.

It would be awesome if greenDao provided a class similar to DevOpenHelper that would simply add new tables/columns that didn't previously exist in the schema without dropping existing tabes/data first.

解决方案

I finally had time to dig in to this myself and realized it's quite easy to add a new table while retaining data in old tables.

DISCLAIMER: While I realize this implementation is specific to my scenario, I think it's helpful for someone like me who has used an Android ORM tool (greenDao) exclusively to deal with SQLite on Android. I understand this is pretty common for those of you who have written your own table creation queries from the beginning, but for someone who has been sheltered from the guts of using a SQLite DB with Android, I think this example will be helpful.

ANSWER: You can either modify the DevOpenHelper inner class or create your own class. I chose to edit DevOpenHelper for the time being to keep my example simple - however, note that if you regenerate your greendao classes, DevOpenHelper will be overwritten. It would be a better idea to create your own class like "MyOpenHelper" and use that instead.

Before my changes, DevOpenHelper.onUpgrade looked like this:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{
        Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
        dropAllTables(db, true);
        onCreate(db);
}

Instead of dropping all tables, take a look at the createAllTables method that is auto-generated by GreenDao.

Rewrite onUpgrade to check if the "oldVersion" is the one you want to upgrade from, then only call the createTable methods for "new" tables. Here is what my onUpgrade method looks like now:

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{
        Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + 

        //Going from older schema to new schema
        if(oldVersion == 3 && newVersion == 4)
        {
            boolean ifNotExists = false;

            //Leave old tables alone and only create ones that didn't exist
            //in the previous schema
            NewTable1Dao.createTable(db, ifNotExists);
            NewTable2Dao.createTable(db, ifNotExists);
            NewTable3Dao.createTable(db, ifNotExists);
            NewTable4Dao.createTable(db, ifNotExists);
        }
        else
        {
            dropAllTables(db, true);
            onCreate(db);
        }
}

Adding a new column would be similar, except you'd have to write some SQL or take a look at the auto-generated SQL create statements from greenDao and leverage those.

To add a single new column (NEW_COLUMN, assuming it's an INTEGER type) to an existing table (EXISTING_TABLE), do the following:

db.execSQL("ALTER TABLE 'EXISTING_TABLE' ADD 'NEW_COLUMN' INTEGER");

For me right now, all I needed to do was add new Tables so this ended up being rather straight forward. Hopefully someone else finds this useful.

这篇关于greenDao 架构升级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆