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

Simple Migrations as create new table with some column #1178

Closed pishguy closed 7 years ago

pishguy commented 7 years ago

i'm trying to find how can i create new table with this library Migrations, is any documentation or sample code to doing this action?

GameDevLlama commented 7 years ago

Nah I think there's only an AlterTable Migratin for adding new columns to your tables. Since your java classes might change over your app's development time it probably could invalidate your migrations anyway.

I'd recomend either using sql files or custom Migrations for creating tables:

@Migration(version = 0, database = YourDatabaseClass.class)
public class TestMigrationextends BaseMigration {

    public TestMigration() {
        MigrationLoader.registerMigrationHandler(this);
    }

    @Override
    public void migrate(DatabaseWrapper database) {
        database.execSQL("CREATE TABLE IF NOT EXISTS `test`(`id`    INTEGER PRIMARY KEY AUTOINCREMENT);");
    }

}

As long as you're adding the annotation to your migration class and extend the BaseMigration you can perform any operations on your database. In this example the migration would run directly after your database has been created. (migration to version 0 before migration to 1)

Also have a look at these examples: https://github.com/Raizlabs/DBFlow/blob/master/usage2/Migrations.md

trevjonez commented 7 years ago

Table creation happens automatically at the beginning of each migration. If it is only a new table just increment the DB version to trigger it.

pishguy commented 7 years ago

@trevjonez ok, so how can i trigger that after increment db version?

trevjonez commented 7 years ago

The next time the database is initialized it will compare the SQLite files version against the version your code says is latest. If it is different it will run migrations.

pishguy commented 7 years ago

@trevjonez after incrementing database version on AppDatabase.class file and running application i get error, so this way i think dont work

trevjonez commented 7 years ago

What is the error and can you share the code you use to initialize DBFlow?

pishguy commented 7 years ago

@trevjonez this error: Table does not exists

trevjonez commented 7 years ago

You can see how the migrations are triggered by looking at BaseDatabaseHelper.java

As you can see, in both onCreate & onUpgrade the code first checks foreignkey support, then runs any necessary table creations, followed by any necessary migrations (files then class based).

So given the error you likely have an issue in either your configuration or initialization.

Please share the database class code, the new tables class declaration info, and the initialization code from your application class.

pishguy commented 7 years ago

@trevjonez ok, so please let me to check again that and share code if happening error, Thanks

agrosner commented 7 years ago

you should just add the table using DBFlow constructs + annotations. What are you trying to do? anything super custom?