Search code examples
androidsqliteormactiveandroid

ActiveAndroid schema migration add column that is an object


I am fairly new to SQLite and ActiveAndroid.

I am working to roll out an update for an app and need to migrate my database. Some of the tables have had columns added or removed, etc. In ActiveAndroid, schema migrations are performed by writing a migration script. It's easy for me to understand how to do this when dealing with basic data types, but I am confused how to add columns that are mapped to other objects/classes.

For example, below is an existing Model:

@Table(name = "GatewayDevices", id = BaseColumns._ID)
public class GatewayDevice extends Model {

    private static final String TAG = GatewayDevice.class.getSimpleName();


    @Column(name = "identifier", unique = true, onUniqueConflict = Column.ConflictAction.REPLACE, notNull = true)
    private String identifier;

    @Column(name = "deviceType", notNull = true)
    private String deviceType;

    @Column(name = "name")
    private String name; 

    public GatewayDevice() {
        super();
    }

    ...

}

And I am updating it to be:

@Table(name = "GatewayDevices", id = BaseColumns._ID)
public class GatewayDevice extends Model {

    private static final String TAG = GatewayDevice.class.getSimpleName();


    @Column(name = "identifier", unique = true, onUniqueConflict = Column.ConflictAction.REPLACE, notNull = true)
    private String identifier;

    @Column(name = "deviceType", notNull = true)
    private String deviceType;

    @Column(name = "name")
    private String name;

    @Column(name = "controller", onUpdate = Column.ForeignKeyAction.CASCADE, onDelete = Column.ForeignKeyAction.CASCADE)
    private Controller controller;


    public GatewayDevice() {
        super();
    }

    ...

}

How do I add this column using the basic ALTER TABLE GatewayDevice ADD COLUMN controller <type> structure?


Solution

  • You can write your own serializer for Controller class, and make migration

    ALTER TABLE GatewayDevice ADD COLUMN controller TEXT

    (String type)