49 lines
1.3 KiB
Ruby
49 lines
1.3 KiB
Ruby
class CreateAddresses < ActiveRecord::Migration
|
|
def self.up
|
|
create_table :addresses do |t|
|
|
t.string :street
|
|
t.string :housenr
|
|
t.string :zipcode
|
|
t.string :place
|
|
t.string :country
|
|
|
|
t.timestamps
|
|
end
|
|
|
|
# There is no such thing as relevant production data, yet.
|
|
# Thus, no need to care about migrating existing data.
|
|
remove_column :contacts, :street
|
|
remove_column :contacts, :housenr
|
|
remove_column :contacts, :plz
|
|
remove_column :contacts, :place
|
|
remove_column :contacts, :country
|
|
|
|
# Add FK reference
|
|
change_table :contacts do |t|
|
|
t.references :address
|
|
end
|
|
# Add the foreign key
|
|
execute <<-SQL
|
|
ALTER TABLE contacts
|
|
ADD CONSTRAINT fk_contacts_addresses
|
|
FOREIGN KEY (address_id)
|
|
REFERENCES addresses(id)
|
|
SQL
|
|
|
|
# Force ActiveRecord to flush the cache and re-read the column information.
|
|
Contact.reset_column_information
|
|
end
|
|
|
|
def self.down
|
|
execute "ALTER TABLE contacts DROP FOREIGN KEY fk_contacts_addresses"
|
|
remove_column :contacts, :address_id
|
|
|
|
add_column :contacts, :country, :string
|
|
add_column :contacts, :place, :string
|
|
add_column :contacts, :plz, :string
|
|
add_column :contacts, :housenr, :string
|
|
add_column :contacts, :street, :string
|
|
|
|
drop_table :addresses
|
|
end
|
|
end
|