plugin to alter table change column name and update schema

By kitsguru - September 3, 2021

I want to write a plugin that will rename a column and update the schema.

I found code to add a column but not sure how to rename a column and update the schema accordingly.

I have many clients using the same code base and when I upgrade to a newer version of my app, I need to ensure the columns are renamed so I don't lose any data.

When I update the client to the new app with the new schema, the old schema is replaced. When logging into admin, the new column names are created and the old column names have no label. I guess my plugin could just copy the data from the old column to the new and then remove the old column.

What I would like to happen is the column is simply renamed.

Any suggestions would be helpful.

Jeff Shields

By daniel - September 6, 2021

Hi Jeff,

It's possible to make modifications to schemas using the loadSchema() and saveSchema() functions. Specifically, if you want to rename a field, you could do something like this:

  $schema = loadSchema('table_name');
  $schema['field_key']['label'] = 'New Label';
  saveSchema('table_name', $schema);

You'd just need to substitute your own table/field values.

Let me know if you have any questions!

Thanks,

Daniel
Technical Lead
interactivetools.com