Easiest way to modify table schema for Persistent Record?

I want to recreate the schema for one of my table. The columns I have now either need to be rename or a different datatype.
The record data isn’t too critical to keep.
I rather not delete the config.idb if there is a better ways to do this.
Any ideas?

During your module’s GatewayHook.setup, you can use the SchemaUpdater to ensure the ORM has an accurate state of your record(s):

void setup(GatewayContext context) {
        context.getSchemaUpdater().updatePersistentRecords(YourRecord.META);
};

There are a few potential gotchas here; for instance, attempting to change the default value of a column won’t work on any existing .idb file due to the way SQLite handles table/column definitions.
If you have a more significant migration you need to do, you can use the updateSchema method on the SchemaUpdater to automatically apply a plain SQL statement exactly once to the DB. Schema ‘features’ are stored in a unique table in the IDB, so Ignition ‘knows’ whether they’ve been applied or need to be applied.

If it’s just a single development gateway, I’d expect the updatePersistentRecords call to be sufficient.

Example of updateSchema yanked from old code:

try {
    List<SchemaFeature> features = Lists.<SchemaFeature>newArrayList(
            new DDLSchemaFeature(
                    "modbus-tcp-property-length-extension",
                    "ALTER TABLE ModbusTcpDriverSettings ALTER AddressMap SET DATA TYPE LONGVARCHAR"));

    getContext().getSchemaUpdater().updateSchema(MODBUS_MODULE_ID, features);
} catch (SQLException e) {
    Logger.getLogger(getClass()).warn("Unable to apply AddressMap schema update.", e);
}

If the changes are not updateable, you can always uninstall your module, use sqlite3 to delete your module’s table from config.idb (preferably while the server is stopped), then install the new module.

1 Like