DBFlow icon indicating copy to clipboard operation
DBFlow copied to clipboard

How to define indexes on initial release

Open petroski77 opened this issue 11 years ago • 9 comments

Hey

I'm having a hard time figuring out where to define the indexes for my tables especially if the app hasn't been released yet.

It would be cool if one could have an initial migration where indexes, triggers and so on could be defined for the initial release/version.

I was kinda hoping that if I defined the database as having version 1 and then added a migration with version 1 that it would work, but unfortunately the migration doesn't get called.

Am I missing something here?

Best regards Christian

petroski77 avatar Mar 19 '15 14:03 petroski77

Can you send me your migration class to see if I can spot anything wrong?

agrosner avatar Mar 19 '15 18:03 agrosner

The migrations works fine if the database is already created and I go from version 1 to 2. The problem only occurs in the initial state where the database is not yet created.

AppDatabase class:

@Database(name = AppDatabase.NAME, version = AppDatabase.VERSION, foreignKeysSupported = true)
public class AppDatabase {
    public static final String NAME = "SHelperDB";
    public static final int VERSION = 1;
}

Migration class:

@Migration(version = 1, databaseName = AppDatabase.NAME)
public class MigrationNameIndex extends IndexMigration<ShoppingList> {

    public MigrationNameIndex() {
        super("nameIdx",ShoppingList.class);
    }

    @Override
    public void onPreMigrate() {
        super.onPreMigrate();

        addColumn(ShoppingList$Table.NAME);
    }
}

petroski77 avatar Mar 19 '15 19:03 petroski77

@agrosner any news?

petroski77 avatar Mar 27 '15 12:03 petroski77

I guess another problem also would be If I create an android app, and it goes through 7 database versions, and thus the database schema changes, how does one ensure that the currently available app on google play contains the previous migrations (indexes and so on)?

petroski77 avatar Mar 27 '15 12:03 petroski77

Ok maybe I have figured this one out ;) On initial release do the following:

  1. set AppDatabase.VERSION = 1
  2. set migration version = 0 eg. @Migration(version = 0, databaseName = AppDatabase.NAME)

On later releases

  1. set AppDatabase.VERSION = old AppDatabase.VERSION + 1
  2. set migration version = old AppDatabase.VERSION + 1

//Christian

petroski77 avatar Mar 27 '15 17:03 petroski77

hey sorry for taking so long to get back to you @petroski77 . on app launch, the library iterates through each migration version in succession and version order until the most recent version of the app is satisfied. Ok so a version 0 migration works? let me know and I can add this to the documentation.

agrosner avatar Apr 05 '15 23:04 agrosner

Hi @agrosner No worries :)

Ok so a version 0 migration works?

Yes, that definitely seems to be the case.

//Christian

petroski77 avatar Apr 06 '15 10:04 petroski77

Hi @agrosner confirming that this is also working OK with my application, so you can add it to the documentation.

zplesac avatar Apr 09 '15 13:04 zplesac

This appears to be too hacky. Really the indexes createIfNotExist should be called without going through migration.

brucexia avatar Apr 04 '16 15:04 brucexia