Skip to content

How to integrate with an existing database schema

atd edited this page May 25, 2012 · 1 revision

You have an application with your own database schema, and you want to integrate it with Social Stream's migrations.

The most straightforward way is modifying the db/migrate/xxxxxxxxxxxxxx_create_social_stream.social_stream_base_engine.rb migration Social Stream has created in the installation process. This is an example:

class CreateSocialStream < ActiveRecord::Migration
  def up # You will probably have to redefine the method because this example is not revertable

    ...

    change_table "users" do |t| # change_table instead of create_table
      # Comment or remove the fields that you already have
      # t.string   "encrypted_password",     :limit => 128, :default => "",     :null => false
      # t.string   "password_salt"
      # t.string   "reset_password_token"
      # t.datetime "reset_password_sent_at"
      # t.datetime "remember_created_at"
      # t.integer  "sign_in_count",                         :default => 0
      # t.datetime "current_sign_in_at"
      # t.datetime "last_sign_in_at"
      # t.string   "current_sign_in_ip"
      # t.string   "last_sign_in_ip"
      # t.string   "authentication_token"
      # t.datetime "created_at",                                                :null => false
      # t.datetime "updated_at",                                                :null => false
      t.integer  "actor_id"
      t.string   "language"
    end

    # Reassign attributes so they are passed to actor association
    User.all.each do |u|
      u.name = u.read_attribute(:name)
      u.email = u.read_attribute(:email)
      u.actor!.created_at = u.created_at
      u.actor!.updated_at = u.updated_at
      u.save!
    end

    # Remove columns from users, actors' columns are used now
    change_table(:users) do |t|
      t.remove :name
      t.remove :email
    end

    User.reset_column_information

    ...
end
Clone this wiki locally