violet_rails icon indicating copy to clipboard operation
violet_rails copied to clipboard

allow API form attribute ordering with JSON representation

Open donrestarone opened this issue 2 years ago • 4 comments
trafficstars

Is your feature request related to a problem? Please describe. Currently, we use JSONB for the form field mapping that does not preserve the order of the attributes submitted by the client-side

Screen Shot 2023-01-26 at 1 45 58 PM

Describe the solution you'd like use a JSON column to keep track of the form field mapping and use that to derive the form field order

donrestarone avatar Jan 26 '23 18:01 donrestarone

affecting nikean.org -- we have written custom scripts for the form attribute ordering

donrestarone avatar Mar 20 '23 12:03 donrestarone

@donrestarone , To change the type of properties from jsonb to json with zero downtime, It will be a 5 steps process as suggested by strong_migration:

First deployment

Create a temporary column temp_properties Write data to both columns: properties and temp_properties

Second deployment

Route the properties attribute to read/write to the temp_properties column, with read fallback on properties and ultimately on _properties column, which will be introduced in the third deployment.

Remove the before_save callback that populates data to temp_properties column from properties.

Third deployment

Rename properties column to _properties.

Rename temp_properties column to properties. After the third deployment, verify in the production database that _properties column has the exact same values as properties.

Fourth deployment

Ignore _properties column with the ignored_columns method.

Fifth deployment

Remove _properties column.

Reference: https://wrapbook.engineering/5-steps-to-change-a-column-type-with-zero-downtime-deployment-in-rails.html

Pralish avatar Mar 26 '23 16:03 Pralish

@donrestarone , Can we remove creating ApiNamespace from migration here :

db/migrate/20220420153229_add_plugin_subdomain_events_to_api.rb

and make it part of the seed file or a rake task or Insert record to database with SQL instead of doing ApiNamespace.create!

Model class isn't guaranteed to still be around in the same form in a future version of the application, and running the migrations from scratch in the future might yield errors.

https://stackoverflow.com/a/2667747/12489083

Facing same issue here:

I added a column in ApiNamespace and added a validation/callback on that column. Now when I rerun the migration from scratch, it gives me error since the column isn't yet created at this point.

Although rails suggest not to change old migration files, we are bound to face this issue again in the future.

Pralish avatar Mar 27 '23 11:03 Pralish

discussed during the monday call, we can use the solution discussed and use this moving forward to migrate data: https://github.com/ilyakatz/data-migrate

donrestarone avatar Mar 27 '23 12:03 donrestarone