안녕하세요.
안녕하세요.

Reputation: 1

android room the reinstall app migration is not running

when I created a migration, I inserted some data, but the newly installed app did not perform the migration.
do I need to insert/update data on the onCreate call every time I migrate?

Upvotes: 0

Views: 284

Answers (1)

MikeT
MikeT

Reputation: 57083

Migrations won't run if you install the app, as no Database exists so it is created.

Migrations are designed to handle new versions of the App being published and specifically new versions that also change the Room database version.

do I need to insert/update data on the onCreate call every time I migrate?

NO! onCreate will not run, it only runs once automatically for the lifetime of the database (unless you call it from the Migration).

  • For an App to be installed, it cannot exist and thus must be uninstalled, the uninstall deletes all the App's data including the databases.

You should be doing what is required in the Mirgation code and then it depends on what you do. e.g. :-

If adding a column for instance and you use the ALTER TABLE your_table ADD COLUMN the_new_column_definition then the existing data is preserved.

If adding a new table then there is no issue.

If however you wanted to change a column's definition (other than it's name) then you have to drop and recreate the table. What would typically be done would be to create the new table, populate it from the original table, rename or drop the original table and the rename the new table to be the name of the original table.

You may wish to have a look at SQLite ALTER TABLE

Upvotes: 1

Related Questions