Permalink
Browse files

Migrate data from shouts to text shouts.

  • Loading branch information...
1 parent e5187ed commit 1a6976759738001a7025197afcf515a69faa5aac @halogenandtoast halogenandtoast committed Mar 12, 2012
@@ -0,0 +1,2 @@
+class TextShout < ActiveRecord::Base
+end
@@ -0,0 +1,9 @@
+class CreateTextShouts < ActiveRecord::Migration
+ def change
+ create_table :text_shouts do |t|
+ t.text :body
+
+ t.timestamps
+ end
+ end
+end
@@ -0,0 +1,34 @@
+class MoveBodyFromShoutsToTextShouts < ActiveRecord::Migration
+ def up
+ add_column :shouts, :medium_type, :string
+ add_column :shouts, :medium_id, :integer
+
+ select_all("SELECT id, body, created_at, updated_at FROM shouts").each do |shout|
+ id = insert <<-SQL
+ INSERT INTO text_shouts (body, created_at, updated_at)
+ VALUES("#{shout["body"]}", "#{shout["created_at"]}", "#{shout["updated_at"]}")
+ SQL
+ update <<-SQL
+ UPDATE shouts
+ SET medium_type="TextShout", medium_id=#{id}
+ WHERE shouts.id = #{shout["id"]}
+ SQL
+ end
+
+ remove_column :shouts, :body
+ end
+
+ def down
+ add_column :shouts, :body, :text
+ select_all("SELECT id, body FROM text_shouts").each do |text_shout|
+ update <<-SQL
+ UPDATE shouts
+ SET body="#{text_shout["body"]}"
+ WHERE medium_id=#{text_shout["id"]}
+ SQL
+ end
+ remove_column :shouts, :medium_type
+ remove_column :shouts, :medium_id
+ delete("DELETE FROM text_shouts")
+ end
+end
View
@@ -11,17 +11,24 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120312143222) do
+ActiveRecord::Schema.define(:version => 20120312172709) do
create_table "shouts", :force => true do |t|
t.integer "user_id"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ t.string "medium_type"
+ t.integer "medium_id"
+ end
+
+ add_index "shouts", ["user_id"], :name => "index_shouts_on_user_id"
+
+ create_table "text_shouts", :force => true do |t|
t.text "body"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
- add_index "shouts", ["user_id"], :name => "index_shouts_on_user_id"
-
create_table "users", :force => true do |t|
t.string "email"
t.string "encrypted_password", :limit => 128
@@ -0,0 +1,7 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+one:
+ body: MyText
+
+two:
+ body: MyText
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class TextShoutTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end

0 comments on commit 1a69767

Please sign in to comment.