migrate to polymorphic mappings - DB MIGRATION

This commit is contained in:
Devin Howard 2015-10-02 16:04:30 +08:00 committed by Connor Turland
parent a4f910b66d
commit 5dc53543f7
6 changed files with 80 additions and 42 deletions

View file

@ -4,8 +4,8 @@ class Map < ActiveRecord::Base
has_many :topicmappings, -> { Mapping.topicmapping }, class_name: :Mapping, dependent: :destroy
has_many :synapsemappings, -> { Mapping.synapsemapping }, class_name: :Mapping, dependent: :destroy
has_many :topics, through: :topicmappings
has_many :synapses, through: :synapsemappings
has_many :topics, through: :topicmappings, source: :mappable, source_type: "Topic"
has_many :synapses, through: :synapsemappings, source: :mappable, source_type: "Synapse"
# This method associates the attribute ":image" with a file attachment
has_attached_file :screenshot, :styles => {

View file

@ -1,10 +1,10 @@
class Mapping < ActiveRecord::Base
scope :topicmapping, -> { where(category: :Topic) }
scope :synapsemapping, -> { where(category: :Synapse) }
scope :topicmapping, -> { where(mappable_type: :Topic) }
scope :synapsemapping, -> { where(mappable_type: :Synapse) }
belongs_to :mappable, polymorphic: true
belongs_to :topic, :class_name => "Topic", :foreign_key => "topic_id"
belongs_to :synapse, :class_name => "Synapse", :foreign_key => "synapse_id"
belongs_to :map, :class_name => "Map", :foreign_key => "map_id"
belongs_to :user

View file

@ -5,7 +5,7 @@ class Synapse < ActiveRecord::Base
belongs_to :topic1, :class_name => "Topic", :foreign_key => "node1_id"
belongs_to :topic2, :class_name => "Topic", :foreign_key => "node2_id"
has_many :mappings, dependent: :destroy
has_many :mappings, as: :mappable, dependent: :destroy
has_many :maps, :through => :mappings
def user_name

View file

@ -8,7 +8,7 @@ class Topic < ActiveRecord::Base
has_many :topics1, :through => :synapses2, :source => :topic1
has_many :topics2, :through => :synapses1, :source => :topic2
has_many :mappings, dependent: :destroy
has_many :mappings, as: :mappable, dependent: :destroy
has_many :maps, :through => :mappings
# This method associates the attribute ":image" with a file attachment

View file

@ -0,0 +1,31 @@
class MappingPolymorphism < ActiveRecord::Migration
def up
add_column :mappings, :mappable_id, :integer
add_column :mappings, :mappable_type, :string
add_index :mappings, [:mappable_id, :mappable_type]
Mapping.find_each do |mapping|
if mapping.synapse_id.nil? and mapping.topic_id.nil?
puts "Mapping id=#{mapping.id} has no valid id, skipping!"
next
end
if not mapping.synapse_id.nil? and not mapping.topic_id.nil?
puts "Mapping id=#{mapping.id} has both topic and synapse ids, skipping!"
next
end
unless mapping.synapse_id.nil?
mapping.mappable = Synapse.find(mapping.synapse_id)
else
mapping.mappable = Topic.find(mapping.topic_id)
end
mapping.save
end
end
def down
remove_index :mappings, [:mappable_id, :mappable_type]
remove_column :mappings, :mappable_id, :integer
remove_column :mappings, :mappable_type, :string
end
end

View file

@ -9,21 +9,24 @@
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(:version => 20141121204712) do
ActiveRecord::Schema.define(version: 20151001024122) do
create_table "in_metacode_sets", :force => true do |t|
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "in_metacode_sets", force: :cascade do |t|
t.integer "metacode_id"
t.integer "metacode_set_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "in_metacode_sets", ["metacode_id"], :name => "index_in_metacode_sets_on_metacode_id"
add_index "in_metacode_sets", ["metacode_set_id"], :name => "index_in_metacode_sets_on_metacode_set_id"
add_index "in_metacode_sets", ["metacode_id"], name: "index_in_metacode_sets_on_metacode_id", using: :btree
add_index "in_metacode_sets", ["metacode_set_id"], name: "index_in_metacode_sets_on_metacode_set_id", using: :btree
create_table "mappings", :force => true do |t|
create_table "mappings", force: :cascade do |t|
t.text "category"
t.integer "xloc"
t.integer "yloc"
@ -31,18 +34,22 @@ ActiveRecord::Schema.define(:version => 20141121204712) do
t.integer "synapse_id"
t.integer "map_id"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "mappable_id"
t.string "mappable_type"
end
create_table "maps", :force => true do |t|
add_index "mappings", ["mappable_id", "mappable_type"], name: "index_mappings_on_mappable_id_and_mappable_type", using: :btree
create_table "maps", force: :cascade do |t|
t.text "name"
t.boolean "arranged"
t.text "desc"
t.text "permission"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "featured"
t.string "screenshot_file_name"
t.string "screenshot_content_type"
@ -50,26 +57,26 @@ ActiveRecord::Schema.define(:version => 20141121204712) do
t.datetime "screenshot_updated_at"
end
create_table "metacode_sets", :force => true do |t|
create_table "metacode_sets", force: :cascade do |t|
t.string "name"
t.text "desc"
t.integer "user_id"
t.boolean "mapperContributed"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "metacode_sets", ["user_id"], :name => "index_metacode_sets_on_user_id"
add_index "metacode_sets", ["user_id"], name: "index_metacode_sets_on_user_id", using: :btree
create_table "metacodes", :force => true do |t|
create_table "metacodes", force: :cascade do |t|
t.text "name"
t.string "icon"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "color"
end
create_table "synapses", :force => true do |t|
create_table "synapses", force: :cascade do |t|
t.text "desc"
t.text "category"
t.text "weight"
@ -77,19 +84,19 @@ ActiveRecord::Schema.define(:version => 20141121204712) do
t.integer "node1_id"
t.integer "node2_id"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "topics", :force => true do |t|
create_table "topics", force: :cascade do |t|
t.text "name"
t.text "desc"
t.text "link"
t.text "permission"
t.integer "user_id"
t.integer "metacode_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
@ -100,25 +107,25 @@ ActiveRecord::Schema.define(:version => 20141121204712) do
t.datetime "audio_updated_at"
end
create_table "users", :force => true do |t|
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.text "settings"
t.string "code", :limit => 8
t.string "joinedwithcode", :limit => 8
t.string "code", limit: 8
t.string "joinedwithcode", limit: 8
t.string "crypted_password"
t.string "password_salt"
t.string "persistence_token"
t.string "perishable_token"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "encrypted_password", :limit => 128, :default => ""
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "encrypted_password", limit: 128, default: ""
t.string "remember_token"
t.datetime "remember_created_at"
t.string "reset_password_token"
t.datetime "last_sign_in_at"
t.string "last_sign_in_ip"
t.integer "sign_in_count", :default => 0
t.integer "sign_in_count", default: 0
t.datetime "current_sign_in_at"
t.string "current_sign_in_ip"
t.datetime "reset_password_sent_at"
@ -130,6 +137,6 @@ ActiveRecord::Schema.define(:version => 20141121204712) do
t.integer "generation"
end
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end