Home > database >  How can i convert string and datetime column type in binary in rails migration?
How can i convert string and datetime column type in binary in rails migration?

Time:11-24

i am learning rails and working on a project in which i need to convert string column type to binary i tried

change_column :workers, :sin,:binary

But i am not able to do so as i am getting.

PG::DatatypeMismatch: ERROR:  column "sin" cannot be cast automatically to type bytea

i want that my sin data should be stored in encrypted form and i am using this gem https://github.com/stas/active_record-pgcrypto i also want to decrypt the data while showing in the UI. I am open for suggestions please. Thank you

CodePudding user response:

I think the easiest approach is to do a data migration. Create a new binary column, then for each record transform your current column to the encrypted form with ActiveRecord::PGCrypto::SymmetricCoder.encrypt(value) and store this encrypted value in the new binary column. Note I haven't tested it, I just checked the code of the gem.

  • Related