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

create new Table on new version of application #1142

Closed pishguy closed 7 years ago

pishguy commented 7 years ago

when i try to have some new table on application i increase app version and i expectation to create new table, but i get error such as:

no such table: UserChannelInformation (code 1): , while compiling: INSERT INTO UserChannelInformation(channelId) VALUES (?)

agrosner commented 7 years ago

increase the database version. app version has nothing to do with database version. https://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#onUpgrade(android.database.sqlite.SQLiteDatabase, int, int) is where we do new tables, migrations,etc.

pishguy commented 7 years ago

ok so how can i create custom class to have migration file? dbflow has any helper to upgrade database?

i cant find any documentation about that on this library migration

agrosner commented 7 years ago

Just add your table and increment the database version. Restart the app.

agrosner commented 7 years ago

https://github.com/Raizlabs/DBFlow/blob/master/usage2/Migrations.md

abiemann commented 6 years ago

Mahdi, below is how I've done it. I assume a CREATE TABLE sqlite command is not needed - I haven' tried adding a completely new table yet.

@Migration(version = 3, database = AppDatabase.class)
public class MigrateTo03 extends BaseMigration {
    @Override
    public void migrate(@NonNull DatabaseWrapper database) {
        database.execSQL("ALTER TABLE `Person` ADD COLUMN `birthState` TEXT;");
        database.execSQL("ALTER TABLE `Person` ADD COLUMN `birthCountry` TEXT;");
        //database.execSQL("CREATE TABLE `HealthTopic` ... etc... ;"); not needed ?
        Timber.i("Upgraded Database to version 3");
    }
}
abiemann commented 6 years ago

I can confirm that if you add a new table and don't increase the database version, then you'll see

Caused by: android.database.sqlite.SQLiteException: no such table: ... etc

When you increase the DB version, it'll work without needing to specifically add migration code (CREATE TABLE)