How to drop and recreate all tables on DB version

2019-05-23 09:08发布

问题:

I'm using ActiveAndroid in my application as the Database system. At current stage I don't want to write the migration files for migrating current tables to altered ones for when I update DB version.

Instead what I want to do is to drop all tables and recreate a new set of tables.

Unfortunately from digging in the ActiveAndroid the Database version test happens at initialization stage in which I pass a ActiveAndroid Configuration object to configure the Database.

At this point their implementation only checks for migration files in the assets folder. I don't mind to create migrations file to do what I need but the problem is that I don't know how to get the current tables initialized in the DB to recreate them.

Does some one knows how to do that with ActiveAndroid?

回答1:

Not sure if I understood correctly but if you are in development then the easiest is to just uninstall the app and let Active Android recreate the DB schema when you install it again.

When in production you will need to use the migration scripts as per their documentation.

I faced a similar issue and I needed to force a few tables to be recreated. Unfortunately you will need to do it manually. However you can use the ActiveAndroid helper classes to help you out.

  1. First you need to process the old table data somehow.
  2. Drop the tables:

    SQLiteUtils.execSql("DROP TABLE IF EXISTS Your_Table_Name");

  3. Recreate the table using the ActiveAndroid classes.

    SQLiteUtils.execSql(SQLiteUtils.createTableDefinition(Cache.getTableInfo(YOUR_MODEL_CLASS.class)));

  4. Insert the old data into your newwly create table.