PostgreSQL
 sql >> Database >  >> RDS >> PostgreSQL

Rails &Postgres:la migrazione a change_colomn fornisce un errore che non può essere trasmesso per digitare timestamp senza fuso orario

In Rails sembrerebbe qualcosa di simile a

class ChangeStatusUpdatedAtToDateTime < ActiveRecord::Migration
  def up
    remove_column :bookings, :status_updated_at
    add_column :bookings, :status_updated_at, :datetime
  end

  def down
    remove_column :bookings, :status_updated_at
    add_column :bookings, :status_updated_at, :time
  end
end

Se avessi dei dati che volevi trasferire potresti usare il seguente codice (non testato!):

class ChangeStatusUpdatedAtToDateTime < ActiveRecord::Migration
  def up
    add_column :bookings, :temp_status_updated_at, :datetime
    Booking.update_all("temp_status_updated_at = updated_at")
    remove_column :bookings, :status_updated_at
    rename_column :bookings, :temp_status_updated_at, :status_updated_at
  end

  def down
    add_column :bookings, :temp_status_updated_at, :time
    Booking.update_all("temp_status_updated_at = updated_at")
    remove_column :bookings, :status_updated_at
    rename_column :bookings, :temp_status_updated_at, :status_updated_at
  end
end