mysql - Best way to migrate table data to other table -


considering 4 models (very simplified)

class group1 << ar::base   has_many group1_items end class group2 << ar::base   has_many group2_items end  class groupitem << ar::base   belongs_to :group1   belongs_to :thing end class group2item << ar::base   belongs_to :group2   belongs_to :thing end 

i want "merge" group2 , group2items group1 , group1items. group2 inherit group1. want :

class group2 << group1 

group2item model inused.

i need create migration 'move' group2 , group2items data group1 , group1item tables.

my migration must accessible whatever application state, i.e. group2 , group2item tables can not present, need juste in mysql syntax.

is there simple way ?

def   change_table :group1s |t|     t.string :type       # add 'type' column sti     t.integer :old_id    # add temporary column hold original 'id'   end    execute("update group1s set `type` = 'group1';")    merge_items_sql = <<sql     insert group1s (`type`, old_id, other_field_names...)      select 'group2', group2s.id, other_field_values...     group2s;      insert group1_items(group1_id, thing_id )     select group1s.id, group2_items.thing_id     group2_items     join group1s on group2_items.group2_id = group1s.old_id; sql    execute(merge_items_sql)    # leave out until have verified data has merged   drop_table :group2_items   drop_table :group2s   remove_column :group1s, :old_id end 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -