Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions db/migrate/20151015160035_set_default_for_author_and_text.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class SetDefaultForAuthorAndText < ActiveRecord::Migration
def up
change_column_default(:comments, :author, "")
change_column_default(:comments, :text, "")
end

def down
change_column_default(:comments, :author, nil)
change_column_default(:comments, :text, nil)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ChangeAuthorAndTextToNotNull < ActiveRecord::Migration
def change
change_column_null(:comments, :author, false, "")
change_column_null(:comments, :text, false, "")
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 2 migrations?

Does change_column_null handle migrating old records? We might need a couple lines of SQL to update the existing records. Then again, there probably aren't any, or else the UI would have crashed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, yes, that last parameter (empty string) of change_column_null replaces null values of existing records, so we don't need SQL statements here.

But it still doesn't set the column's default for new records, thus we do need a second migration to change_column_default.

10 changes: 5 additions & 5 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20140823052830) do
ActiveRecord::Schema.define(version: 20151015160334) do

create_table "comments", force: :cascade do |t|
t.string "author"
t.text "text"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "author", default: "", null: false
t.text "text", default: "", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

end