Donald
Donald

Reputation: 176

Converting a serialized array to a PSQL array, error

So I'm trying to do a migration that takes an existing column, which is a serialized array(text), and convert it into a psql array. I've tried a great number of things but keep getting an error.

This is what my migration looks like:

 class RenameColumn < ActiveRecord::Migration
    def up
      rename_column :table_things, :column, :old_column

      add_column :table_things, :column, :text, :array => true, null: false, :default => []

      TableThing.class_eval do
        serialize :old_column, Array
      end

      TableThing.reset_column_information

      TableThing.find_each{|tt|
        tt.column = tt.old_column
        tt.save!
      }
     TableThing.reset_column_information
       remove_column :table_things, :old_column
    end

and this is the stack trace:

rake aborted!
An error has occurred, this and all later migrations canceled:    ActiveRecord::RecordNotSaved/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/persistence.rb:125:in `save!'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/validations.rb:57:in `save!'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/attribute_methods/dirty.rb:41:in `save!'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/transactions.rb:275:in `block in save!'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/transactions.rb:330:in `block in with_transaction_returning_status'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/transactions.rb:209:in `transaction'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/transactions.rb:327:in `with_transaction_returning_status'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/transactions.rb:275:in `save!'
    /Users/username/code/work/Jobber/db/migrate/20140317215526_rename_addons.rb:15:in `block in up'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/relation/batches.rb:26:in `block (2 levels) in find_each'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/relation/batches.rb:26:in `each'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/relation/batches.rb:26:in `block in find_each'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/relation/batches.rb:76:in `find_in_batches'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-deprecated_finders-1.0.3/lib/active_record/deprecated_finders/relation.rb:70:in `find_in_batches'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/relation/batches.rb:25:in `find_each'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/querying.rb:8:in `find_each'
    /Users/username/code/work/Jobber/db/migrate/20140317215526_rename_addons.rb:13:in `up'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:578:in `exec_migration'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:559:in `block (2 levels) in migrate'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:558:in `block in migrate'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:557:in `migrate'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:713:in `migrate'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:963:in `block in execute_migration_in_transaction'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:1009:in `block in ddl_transaction'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:221:in `within_new_transaction'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/transactions.rb:209:in `transaction'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:1009:in `ddl_transaction'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:962:in `execute_migration_in_transaction'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:924:in `block in migrate'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:920:in `each'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:920:in `migrate'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:768:in `up'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:746:in `migrate'
    /Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/railties/databases.rake:42:in `block (2 levels) in <top (required)>'
    /Users/username/.rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in `eval'
    /Users/username/.rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in `<main>'

Anyone have any ideas of what I'm doing wrong, or things I can try to fix my problem?

Upvotes: 2

Views: 64

Answers (1)

Donald
Donald

Reputation: 176

Changing from .save! to .save helped show better error messages, in the end the issue was with callbacks so changing from

  TableThing.find_each{|tt|
    tt.column = tt.old_column
    tt.save!
  }

to

  TableThing.find_each{|tt|
    tt.update_column(:column, tt.old_column)
  }

Resolved the issue.

Upvotes: 1

Related Questions