Rails 4迁移将列数据类型从字符串更改为整数并返回保留数据(postgres)

时间:2015-09-30 09:50:16

标签: ruby-on-rails enums rails-migrations

我需要将string字段转换为integer并改为使用enum。 在不丢失数据的情况下,最好的方法是什么?

这是当前的迁移:

class CreateSystems < ActiveRecord::Migration
  def change
    create_table :systems do |t|
      t.string :operation
      t.string :status

      t.timestamps null: false
    end
  end
end

然后我改变了字段的类型:

class ChangeColumnsForSystems < ActiveRecord::Migration
  def change
    change_column :systems, :operation, :integer
    change_column :systems, :status, :integer
  end
end

并更新模型文件。

/app/models/system.rb

...
enum operation { start: 0, stop: 1 }
enum status { init: 0, working: 1, complete: 2 }
...

如何更新旧数据?

2 个答案:

答案 0 :(得分:10)

经过一番研究后,我发现这是一个合适的解决方案。

class ChangeColumnsForSystems < ActiveRecord::Migration
  def change
    change_column :systems, :operation, "integer USING (CASE operation WHEN 'start' THEN '0'::integer ELSE '1'::integer END)", null: false
    change_column :systems, :status, "integer USING (CASE status WHEN 'init' THEN '0'::integer WHEN 'working' THEN '1'::integer ELSE '2'::integer END)", null: false
  end
end

<强>更新 在某些情况下,您必须在更改类型之前删除默认值。 这是带回滚的版本。

class ChangeColumnsForSystems < ActiveRecord::Migration
  def up
    change_column_default :systems, :status, nil
    change_column :systems, :operation, "integer USING (CASE operation WHEN 'start' THEN '0'::integer ELSE '1'::integer END)", null: false
    change_column :systems, :status, "integer USING (CASE status WHEN 'init' THEN '0'::integer WHEN 'working' THEN '1'::integer ELSE '2'::integer END)", null: false, default: 0
  end

  def down
    change_column_default :systems, :status, nil
    change_column :systems, :operation, "varchar USING (CASE operation WHEN '0' THEN 'start'::varchar ELSE 'stop'::varchar END)", null: false
    change_column :systems, :status, "varchar USING (CASE status WHEN '0' THEN 'init'::varchar WHEN '1' THEN 'working'::varchar ELSE 'complete'::varchar END)", null: false, default: 'init'
  end
end

答案 1 :(得分:2)

您可以在2个迁移步骤中执行此操作

1。重命名当前from django.shortcuts import render, render_to_response from django.http import HttpRequest, HttpResponse, HttpResponseRedirect from django.template import RequestContext from datetime import datetime from app.models import *; def artistcreate(request): if request.method == "GET": form = ArtistForm(); return render(request, 'app/create.html', { 'form', form }); elif request.method == "POST": form = ArtistForm(request.POST); form.save(); return HttpResponseRedirect('/artists'); def artists(request): #return HttpResponse('<html><head><title>Hello Django! </title></head><body><h1>Hello, Django</h1></body></html>'); artists = Artist.objects.all(); return render_to_response('app/artists.html', { 'artists': artists }); def artistdetails(request, id): artist = Artist.objects.get(pk = id); return render_to_response('app/artistdetails.html', {'artist', artist}); def home(request): """Renders the home page.""" assert isinstance(request, HttpRequest) return render( request, 'app/index.html', context_instance = RequestContext(request, { 'title':'Home Page', 'year':datetime.now().year, }) ) def contact(request): """Renders the contact page.""" assert isinstance(request, HttpRequest) return render( request, 'app/contact.html', context_instance = RequestContext(request, { 'title':'Contact', 'message':'Your contact page.', 'year':datetime.now().year, }) ) def about(request): """Renders the about page.""" assert isinstance(request, HttpRequest) return render( request, 'app/about.html', context_instance = RequestContext(request, { 'title':'About', 'message':'Your application description page.', 'year':datetime.now().year, }) ) 列,并添加新的必要类型

operation

2。将值从旧列移动到新列并删除旧列

def up
    rename_column :systems, :operation, :operation_str
    add_column :systems, :operation, ... # your options
end

如果需要,请不要忘记写回滚代码