I've been trying to do an auto migration, where a table is to be deleted, to my room database. I specify an automigration as follows:
@Database(
version = 9,
entities = [...],
views = [...],
exportSchema = true,
autoMigrations = [
...
AutoMigration(8, 9)
]
)
@TypeConverters(
value = [...],
builtInTypeConverters = BuiltInTypeConverters(enums = BuiltInTypeConverters.State.ENABLED)
)
Once I try to build the project, I get the following error:
AutoMigration Failure: Please declare an interface extending 'AutoMigrationSpec',
and annotate with the @RenameTable or @DeleteTable annotation to specify the change
to be performed:
1) RENAME:
@RenameTable.Entries(
@RenameTable(
fromTableName = "some_table",
toTableName = <NEW_TABLE_NAME>
)
)
2) DELETE:
@DeleteTable.Entries(
@DeleteTable(
tableName = "some_table"
)
)
I've also removed the table class i.e SomeTable::class from the list of entities.
After the error I declare an AutoMigrationSpec class Annotated with @DeleteTable as follows:
@DeleteTable("some_table")
class Version9 : AutoMigrationSpec
and adding declare it as a spec as follows:
@Database(
version = 9,
entities = [...],
views = [...],
exportSchema = true,
autoMigrations = [
...
AutoMigration(8, 9, Version9::class)
]
)
@TypeConverters(
value = [...],
builtInTypeConverters = BuiltInTypeConverters(enums = BuiltInTypeConverters.State.ENABLED)
)
I get the following same error.
If I use the spec as an interface instead of a class i.e.
@DeleteTable("some_table")
interface Version9 : AutoMigrationSpec
I get an additional error that, The AutoMigration spec type must be a class.
I've also tried using the following format while declaring the AutoMigrationSpec:
@DeleteTable.Entries(DeleteTable("some_table"))
class Version9 : AutoMigrationSpec
However, I get the initial error.
I can't see anything wrong from the code you have provided.
Based upon your code, assuming other code, coding to make running before and after relatively simple, using KAPT rather than KSP and using 2.5.2 Room libraries then the code works as expected as per the output from the log (see code below):-
Part 1 for the first version with 2 tables (T1 and T2):-
Part 2 for the second version (the migration) with just 1 table (T1):-
The full code being:-
All the DB Code (as per 2nd run (see comments for 1st run)):-
The Activity code (run on the main thread for brevity, again for the 2nd run, again refer to the comments for first version):-
Conclusion
Basically as originally stated there is no obvious issue with the code.