agrosner / DBFlow

A blazing fast, powerful, and very simple ORM android database library that writes database code for you.
MIT License
4.87k stars 598 forks source link

How to define indexes on initial release #160

Closed petroski77 closed 9 years ago

petroski77 commented 9 years ago

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

agrosner commented 9 years ago

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

petroski77 commented 9 years ago

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 commented 9 years ago

@agrosner any news?

petroski77 commented 9 years ago

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 commented 9 years ago

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

agrosner commented 9 years ago

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.

petroski77 commented 9 years ago

Hi @agrosner No worries :)

Ok so a version 0 migration works?

Yes, that definitely seems to be the case.

//Christian

zplesac commented 9 years ago

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

brucexia commented 8 years ago

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