Change column name in schema using autoMigration

This commit is contained in:
jacques 2022-06-23 11:23:59 +02:00
parent 0d7995fc53
commit c659146d67
3 changed files with 127 additions and 12 deletions

View file

@ -0,0 +1,100 @@
{
"formatVersion": 1,
"database": {
"version": 2,
"identityHash": "5d25ce5aa04f81e6c61efbdabce94266",
"entities": [
{
"tableName": "prescriptions",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`cis` TEXT NOT NULL, `cip13` TEXT, `name` TEXT, `administration_mode` TEXT, `presentation` TEXT, `stock` REAL, `take` REAL, `warning` INTEGER, `alert` INTEGER, `last_update` INTEGER, `label_group` TEXT, `generic_type` INTEGER, PRIMARY KEY(`cis`))",
"fields": [
{
"fieldPath": "cis",
"columnName": "cis",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "cip13",
"columnName": "cip13",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "name",
"columnName": "name",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "administration_mode",
"columnName": "administration_mode",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "presentation",
"columnName": "presentation",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "stock",
"columnName": "stock",
"affinity": "REAL",
"notNull": false
},
{
"fieldPath": "take",
"columnName": "take",
"affinity": "REAL",
"notNull": false
},
{
"fieldPath": "warning",
"columnName": "warning",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "alert",
"columnName": "alert",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "last_update",
"columnName": "last_update",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "label_group",
"columnName": "label_group",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "generic_type",
"columnName": "generic_type",
"affinity": "INTEGER",
"notNull": false
}
],
"primaryKey": {
"columnNames": [
"cis"
],
"autoGenerate": false
},
"indices": [],
"foreignKeys": []
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '5d25ce5aa04f81e6c61efbdabce94266')"
]
}
}

View file

@ -4,8 +4,10 @@ import android.content.Context;
import androidx.room.AutoMigration; import androidx.room.AutoMigration;
import androidx.room.Database; import androidx.room.Database;
import androidx.room.RenameColumn;
import androidx.room.Room; import androidx.room.Room;
import androidx.room.RoomDatabase; import androidx.room.RoomDatabase;
import androidx.room.migration.AutoMigrationSpec;
import net.foucry.pilldroid.dao.PrescriptionsDAO; import net.foucry.pilldroid.dao.PrescriptionsDAO;
import net.foucry.pilldroid.models.Prescription; import net.foucry.pilldroid.models.Prescription;
@ -14,21 +16,34 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@Database( @Database(
version = 1, version = 2,
entities = {Prescription.class} entities = {Prescription.class},
autoMigrations = {
@AutoMigration(
from = 1,
to = 2,
spec = PrescriptionDatabase.generic_typeMigration.class
)
}
) )
public abstract class PrescriptionDatabase extends RoomDatabase { public abstract class PrescriptionDatabase extends RoomDatabase {
private static PrescriptionDatabase INSTANCE; private static PrescriptionDatabase INSTANCE;
public abstract PrescriptionsDAO getPrescriptionsDAO(); public abstract PrescriptionsDAO getPrescriptionsDAO();
@RenameColumn(tableName = "prescriptions", fromColumnName = "genetic_type", toColumnName = "generic_type")
static class generic_typeMigration implements AutoMigrationSpec { }
public static PrescriptionDatabase getInstanceDatabase(Context context) { public static PrescriptionDatabase getInstanceDatabase(Context context) {
if (INSTANCE == null) { if (INSTANCE == null) {
INSTANCE = INSTANCE =
Room Room
.databaseBuilder(context.getApplicationContext(), .databaseBuilder(context.getApplicationContext(),
PrescriptionDatabase.class, "prescriptions") PrescriptionDatabase.class, "prescriptions")
.allowMainThreadQueries() .allowMainThreadQueries()
.build(); .build();
} }
return INSTANCE; return INSTANCE;
} }

View file

@ -24,7 +24,7 @@ public class Prescription implements Serializable {
private Integer alert; private Integer alert;
private Long last_update; private Long last_update;
private String label_group; private String label_group;
private Integer genetic_type; private Integer generic_type;
public void setCis(@NonNull String cis) public void setCis(@NonNull String cis)
{ {
@ -122,12 +122,12 @@ public class Prescription implements Serializable {
this.label_group = label_group; this.label_group = label_group;
} }
public Integer getGenetic_type() { public Integer getGeneric_type() {
return genetic_type; return generic_type;
} }
public void setGenetic_type(Integer genetic_type) { public void setGeneric_type(Integer generic_type) {
this.genetic_type = genetic_type; this.generic_type = generic_type;
} }
public Date getDateEndOfStock() { public Date getDateEndOfStock() {
int numberDayOfTake; int numberDayOfTake;