Closed pishguy closed 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
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.
@trevjonez ok, so how can i trigger that after increment db version?
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.
@trevjonez after incrementing database version on AppDatabase.class
file and running application i get error, so this way i think dont work
What is the error and can you share the code you use to initialize DBFlow?
@trevjonez this error: Table does not exists
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.
@trevjonez ok, so please let me to check again that and share code if happening error, Thanks
you should just add the table using DBFlow constructs + annotations. What are you trying to do? anything super custom?
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?