Signed-off-by: Michel Loiseleur <[email protected]>
---
 src/db/migrate/001_create_pools.rb                 |    4 +-
 src/db/migrate/002_create_hosts.rb                 |    5 +-
 src/db/migrate/003_create_nics.rb                  |    4 +-
 src/db/migrate/004_create_storage_volumes.rb       |    9 +--
 src/db/migrate/005_create_quotas.rb                |    4 +-
 src/db/migrate/006_create_vms.rb                   |   15 ++----
 src/db/migrate/007_create_tasks.rb                 |    9 +--
 src/db/migrate/010_create_cpus.rb                  |    4 +-
 src/db/migrate/012_denormalize_permissions.rb      |    4 +-
 src/db/migrate/016_add_pools_to_tasks.rb           |    8 ++--
 src/db/migrate/017_add_smart_pools.rb              |   10 ++--
 src/db/migrate/020_create_bondings.rb              |    7 +--
 src/db/migrate/021_create_bondings_nics.rb         |    7 +--
 src/db/migrate/022_create_boot_types.rb            |    3 +-
 src/db/migrate/024_add_boot_type_to_bonding.rb     |    4 +-
 src/db/migrate/025_add_lvm_storage.rb              |    4 +-
 ..._fix_uniqueness_constraints_in_bondings_nics.rb |   11 ++++-
 src/db/migrate/028_refactor_networking_model.rb    |   57 +++++---------------
 .../035_networks_nics_bondings_additions.rb        |   18 ++-----
 src/db/migrate/036_vm_host_history.rb              |    9 +---
 src/db/migrate/037_add_roles_and_privileges.rb     |   11 +---
 src/db/migrate/042_associate_vms_with_nics.rb      |   46 +++++++++-------
 22 files changed, 95 insertions(+), 158 deletions(-)

diff --git a/src/db/migrate/001_create_pools.rb 
b/src/db/migrate/001_create_pools.rb
index 113a7a9..61f4a5c 100644
--- a/src/db/migrate/001_create_pools.rb
+++ b/src/db/migrate/001_create_pools.rb
@@ -23,14 +23,12 @@ class CreatePools < ActiveRecord::Migration
       t.string :name
       t.string :type
       t.integer :parent_id
+      t.foreign_key :pools, :column => 'parent_id', :name => 'fk_pool_parent'
       t.integer :lft
       t.integer :rgt
       t.integer :lock_version, :default => 0
       t.timestamps
     end
-
-    execute "alter table pools add constraint fk_pool_parent
-             foreign key (parent_id) references pools(id)"
   end
 
   def self.down
diff --git a/src/db/migrate/002_create_hosts.rb 
b/src/db/migrate/002_create_hosts.rb
index cef7996..31b2a46 100644
--- a/src/db/migrate/002_create_hosts.rb
+++ b/src/db/migrate/002_create_hosts.rb
@@ -29,14 +29,13 @@ class CreateHosts < ActiveRecord::Migration
       t.integer :memory
       t.integer :is_disabled
       t.integer :hardware_pool_id, :null => false
+      t.foreign_key :pools, :column => 'hardware_pool_id',
+                            :name => 'fk_host_pools'
       t.integer :lock_version,     :default => 0
       t.string  :state
       t.float   :load_average
       t.timestamps
     end
-
-    execute "alter table hosts add constraint fk_host_pools
-             foreign key (hardware_pool_id) references pools(id)"
   end
 
   def self.down
diff --git a/src/db/migrate/003_create_nics.rb 
b/src/db/migrate/003_create_nics.rb
index 31bc33f..b42edf3 100644
--- a/src/db/migrate/003_create_nics.rb
+++ b/src/db/migrate/003_create_nics.rb
@@ -26,12 +26,10 @@ class CreateNics < ActiveRecord::Migration
       t.string  :usage_type
       t.integer :bandwidth
       t.integer :host_id,       :null => false
+      t.foreign_key :hosts, :name => 'fk_nic_hosts'
       t.integer :lock_version,  :default => 0
     end
 
-    execute "alter table nics add constraint fk_nic_hosts
-             foreign key (host_id) references hosts(id)"
-
   end
 
   def self.down
diff --git a/src/db/migrate/004_create_storage_volumes.rb 
b/src/db/migrate/004_create_storage_volumes.rb
index 0b52a95..5ce8a6c 100644
--- a/src/db/migrate/004_create_storage_volumes.rb
+++ b/src/db/migrate/004_create_storage_volumes.rb
@@ -23,6 +23,8 @@ class CreateStorageVolumes < ActiveRecord::Migration
       t.string :ip_addr
       t.string :type
       t.integer :hardware_pool_id, :null => false
+      t.foreign_key :pools, :column => 'hardware_pool_id',
+                            :name => 'fk_storage_pool_pools'
       t.integer :lock_version,     :default => 0
 
       # for IscsiStoragePool
@@ -37,6 +39,7 @@ class CreateStorageVolumes < ActiveRecord::Migration
       t.string :path
       t.integer :size
       t.integer :storage_pool_id,  :null => false
+      t.foreign_key :storage_pools, :name => 'fk_storage_volume_st_pools'
       t.string :type
       t.integer :lock_version,     :default => 0
 
@@ -46,12 +49,6 @@ class CreateStorageVolumes < ActiveRecord::Migration
       # for IscsiStorageVolume
       t.string :filename
     end
-
-    execute "alter table storage_pools add constraint fk_storage_pool_pools
-             foreign key (hardware_pool_id) references pools(id)"
-    execute "alter table storage_volumes add constraint 
fk_storage_volume_st_pools
-             foreign key (storage_pool_id) references storage_pools(id)"
-
   end
 
   def self.down
diff --git a/src/db/migrate/005_create_quotas.rb 
b/src/db/migrate/005_create_quotas.rb
index 9bdada7..d70c3ef 100644
--- a/src/db/migrate/005_create_quotas.rb
+++ b/src/db/migrate/005_create_quotas.rb
@@ -26,11 +26,9 @@ class CreateQuotas < ActiveRecord::Migration
       t.integer :total_storage
       t.integer :total_vms
       t.integer :pool_id
+      t.foreign_key :pools, :name => 'fk_quotas_pools'
       t.integer :lock_version, :default => 0
     end
-
-    execute "alter table quotas add constraint fk_quotas_pools
-             foreign key (pool_id) references pools(id)"
   end
 
   def self.down
diff --git a/src/db/migrate/006_create_vms.rb b/src/db/migrate/006_create_vms.rb
index 74794b6..9fb819a 100644
--- a/src/db/migrate/006_create_vms.rb
+++ b/src/db/migrate/006_create_vms.rb
@@ -29,27 +29,22 @@ class CreateVms < ActiveRecord::Migration
       t.string  :vnic_mac_addr
       t.string  :state
       t.integer :host_id
+      t.foreign_key :hosts, :name => 'fk_vms_hosts'
       t.integer :vm_resource_pool_id
+      t.foreign_key :pools, :column => 'vm_resource_pool_id',
+                            :name => 'fk_vms_vm_pools'
       t.integer :needs_restart
       t.string  :boot_device,    :null => false
       t.integer :vnc_port
       t.integer :lock_version,   :default => 0
     end
-    execute "alter table vms add constraint fk_vms_hosts
-             foreign key (host_id) references hosts(id)"
-    execute "alter table vms add constraint fk_vms_vm_pools
-             foreign key (vm_resource_pool_id) references pools(id)"
 
     create_table :storage_volumes_vms, :id => false do |t|
       t.integer :vm_id,             :null => false
+      t.foreign_key :vms, :name => 'fk_stor_vol_vms_vm_id'
       t.integer :storage_volume_id, :null => false
+      t.foreign_key :storage_volumes, :name => 'fk_stor_vol_vms_stor_vol_id'
     end
-    execute "alter table storage_volumes_vms add constraint
-             fk_stor_vol_vms_vm_id
-             foreign key (vm_id) references vms(id)"
-    execute "alter table storage_volumes_vms add constraint
-             fk_stor_vol_vms_stor_vol_id
-             foreign key (storage_volume_id) references storage_volumes(id)"
   end
 
   def self.down
diff --git a/src/db/migrate/007_create_tasks.rb 
b/src/db/migrate/007_create_tasks.rb
index 30b6a1d..edfea51 100644
--- a/src/db/migrate/007_create_tasks.rb
+++ b/src/db/migrate/007_create_tasks.rb
@@ -33,19 +33,16 @@ class CreateTasks < ActiveRecord::Migration
 
       # VmTask columns
       t.integer   :vm_id
+      t.foreign_key :vms, :name => 'fk_tasks_vms'
 
       # StorageTask columns
       t.integer   :storage_pool_id
+      t.foreign_key :storage_pools, :name => 'fk_tasks_pools'
 
       # HostTask columns
       t.integer   :host_id
+      t.foreign_key :hosts, :name => 'fk_tasks_hosts'
     end
-    execute "alter table tasks add constraint fk_tasks_vms
-             foreign key (vm_id) references vms(id)"
-    execute "alter table tasks add constraint fk_tasks_pools
-             foreign key (storage_pool_id) references storage_pools(id)"
-    execute "alter table tasks add constraint fk_tasks_hosts
-             foreign key (host_id) references hosts(id)"
   end
 
   def self.down
diff --git a/src/db/migrate/010_create_cpus.rb 
b/src/db/migrate/010_create_cpus.rb
index bd17274..ef4f59a 100644
--- a/src/db/migrate/010_create_cpus.rb
+++ b/src/db/migrate/010_create_cpus.rb
@@ -2,6 +2,7 @@ class CreateCpus < ActiveRecord::Migration
   def self.up
     create_table :cpus do |t|
         t.integer :host_id
+        t.foreign_key :hosts, :name => 'fk_host_cpus'
         t.integer :cpu_number
         t.integer :core_number
         t.integer :number_of_cores
@@ -16,9 +17,6 @@ class CreateCpus < ActiveRecord::Migration
         t.timestamps
     end
 
-    execute "alter table cpus add constraint fk_host_cpus
-             foreign key (host_id) references hosts(id)"
-
     remove_column :hosts, :cpu_speed
     remove_column :hosts, :num_cpus
   end
diff --git a/src/db/migrate/012_denormalize_permissions.rb 
b/src/db/migrate/012_denormalize_permissions.rb
index f68f555..e167f04 100644
--- a/src/db/migrate/012_denormalize_permissions.rb
+++ b/src/db/migrate/012_denormalize_permissions.rb
@@ -1,8 +1,8 @@
 class DenormalizePermissions < ActiveRecord::Migration
   def self.up
     add_column :permissions, :inherited_from_id, :integer
-    execute "alter table permissions add constraint fk_perm_parent
-             foreign key (inherited_from_id) references permissions(id)"
+    add_foreign_key :permissions, :permissions, :column => 'inherited_from_id',
+                                                :name => 'fk_perm_parent'
 
     Permission.transaction do
       Permission.find(:all,
diff --git a/src/db/migrate/016_add_pools_to_tasks.rb 
b/src/db/migrate/016_add_pools_to_tasks.rb
index 58ff83d..5206540 100644
--- a/src/db/migrate/016_add_pools_to_tasks.rb
+++ b/src/db/migrate/016_add_pools_to_tasks.rb
@@ -20,11 +20,11 @@
 class AddPoolsToTasks < ActiveRecord::Migration
   def self.up
     add_column :tasks, :vm_resource_pool_id, :integer
+    add_foreign_key :tasks, :pools, :column => 'vm_resource_pool_id',
+                                    :name => 'fk_tasks_vm_pools'
     add_column :tasks, :hardware_pool_id, :integer
-    execute "alter table tasks add constraint fk_tasks_vm_pools
-             foreign key (vm_resource_pool_id) references pools(id)"
-    execute "alter table tasks add constraint fk_tasks_hw_pools
-             foreign key (hardware_pool_id) references pools(id)"
+    add_foreign_key :tasks, :pools, :column => 'hardware_pool_id',
+                                    :name => 'fk_tasks_hw_pools'
     Task.transaction do
       VmTask.find(:all).each do |task|
         vm = task.vm
diff --git a/src/db/migrate/017_add_smart_pools.rb 
b/src/db/migrate/017_add_smart_pools.rb
index 5550a89..bcadc77 100644
--- a/src/db/migrate/017_add_smart_pools.rb
+++ b/src/db/migrate/017_add_smart_pools.rb
@@ -21,15 +21,13 @@ class AddSmartPools < ActiveRecord::Migration
   def self.up
     create_table :smart_pool_tags  do |t|
       t.integer :smart_pool_id, :null => false
+      t.foreign_key :pools, :column => 'smart_pool_id',
+                            :name => 'fk_smart_pool_tags_pool_id'
       t.integer :tagged_id,     :null => false
       t.string :tagged_type,    :null => false
     end
-    execute "alter table smart_pool_tags add constraint
-             fk_smart_pool_tags_pool_id
-             foreign key (smart_pool_id) references pools(id)"
-    execute "alter table smart_pool_tags add constraint
-             unique_smart_pool_tags
-             unique (smart_pool_id, tagged_id, tagged_type)"
+    add_index :smart_pool_tags, [:smart_pool_id, :tagged_id, :tagged_type],
+              :unique => true, :name => 'unique_smart_pool_tags'
 
     begin
       dir_root = DirectoryPool.get_directory_root
diff --git a/src/db/migrate/020_create_bondings.rb 
b/src/db/migrate/020_create_bondings.rb
index e02adc3..cb2d7d2 100644
--- a/src/db/migrate/020_create_bondings.rb
+++ b/src/db/migrate/020_create_bondings.rb
@@ -23,7 +23,9 @@ class CreateBondings < ActiveRecord::Migration
       t.string  :name,            :null => false, :limit => 50
       t.string  :interface_name,  :null => false, :limit => 20
       t.integer :bonding_type_id, :null => false
+      t.foreign_key :bonding_types, :name => 'fk_bonding_bonding_type'
       t.integer :host_id,         :null => false
+      t.foreign_key :hosts, :name => 'fk_bonding_host'
       t.string  :ip_addr,         :null => true, :limit => 15
       t.string  :netmask,         :null => true, :limit => 15
       t.string  :broadcast,       :null => true, :limit => 15
@@ -34,11 +36,6 @@ class CreateBondings < ActiveRecord::Migration
     end
 
     add_index :bondings, [:interface_name, :host_id], :unique => true
-
-    execute 'alter table bondings add constraint fk_bonding_bonding_type
-             foreign key (bonding_type_id) references bonding_types(id)'
-    execute 'alter table bondings add constraint fk_bonding_host
-             foreign key (host_id) references hosts(id)'
   end
 
   def self.down
diff --git a/src/db/migrate/021_create_bondings_nics.rb 
b/src/db/migrate/021_create_bondings_nics.rb
index 1fe0789..b30940e 100644
--- a/src/db/migrate/021_create_bondings_nics.rb
+++ b/src/db/migrate/021_create_bondings_nics.rb
@@ -21,17 +21,14 @@ class CreateBondingsNics < ActiveRecord::Migration
   def self.up
     create_table :bondings_nics do |t|
       t.integer :bonding_id, :null => false
+      t.foreign_key :bondings, :name => 'fk_bondings_nics_bonding'
       t.integer :nic_id,     :null => false
+      t.foreign_key :nics, :name => 'fk_bondings_nics_nic'
 
       t.timestamps
     end
 
     add_index :bondings_nics, [:bonding_id, :nic_id], :unique => true
-
-    execute 'alter table bondings_nics add constraint fk_bondings_nics_bonding
-             foreign key (bonding_id) references bondings(id)'
-    execute 'alter table bondings_nics add constraint fk_bondings_nics_nic
-             foreign key (nic_id) references nics(id)'
   end
 
   def self.down
diff --git a/src/db/migrate/022_create_boot_types.rb 
b/src/db/migrate/022_create_boot_types.rb
index e18d5c5..a80718b 100644
--- a/src/db/migrate/022_create_boot_types.rb
+++ b/src/db/migrate/022_create_boot_types.rb
@@ -35,8 +35,7 @@ class CreateBootTypes < ActiveRecord::Migration
 
     add_column :nics, :boot_type_id, :integer, :null => true
 
-    execute 'alter table nics add constraint fk_nic_boot_type
-             foreign key (boot_type_id) references boot_types(id)'
+    add_foreign_key :nics, :boot_types, :name => 'fk_nic_boot_type'
 
     boot_type = BootType.find_by_proto('static')
 
diff --git a/src/db/migrate/024_add_boot_type_to_bonding.rb 
b/src/db/migrate/024_add_boot_type_to_bonding.rb
index 17e085f..4fe630a 100644
--- a/src/db/migrate/024_add_boot_type_to_bonding.rb
+++ b/src/db/migrate/024_add_boot_type_to_bonding.rb
@@ -20,9 +20,7 @@
 class AddBootTypeToBonding < ActiveRecord::Migration
   def self.up
     add_column :bondings, :boot_type_id, :integer, :null => true
-
-    execute 'alter table bondings add constraint fk_bondings_boot_type
-             foreign key (boot_type_id) references boot_types(id)'
+    add_foreign_key :bondings, :boot_types, :name => 'fk_bondings_boot_type'
   end
 
   def self.down
diff --git a/src/db/migrate/025_add_lvm_storage.rb 
b/src/db/migrate/025_add_lvm_storage.rb
index 697df4d..392f052 100644
--- a/src/db/migrate/025_add_lvm_storage.rb
+++ b/src/db/migrate/025_add_lvm_storage.rb
@@ -37,8 +37,8 @@ class AddLvmStorage < ActiveRecord::Migration
 
     # VG pool ID
     add_column :storage_volumes, :lvm_pool_id, :integer
-    execute "alter table storage_volumes add constraint 
fk_storage_volumes_lvm_pools
-             foreign key (lvm_pool_id) references storage_pools(id)"
+    add_foreign_key :storage_volumes, :storage_pools, :column => 'lvm_pool_id',
+                    :name => 'fk_storage_volumes_lvm_pools'
 
     # use polymorphic tasks association
     add_column :tasks, :task_target_id, :integer
diff --git a/src/db/migrate/027_fix_uniqueness_constraints_in_bondings_nics.rb 
b/src/db/migrate/027_fix_uniqueness_constraints_in_bondings_nics.rb
index 5f58c5c..e2c638c 100644
--- a/src/db/migrate/027_fix_uniqueness_constraints_in_bondings_nics.rb
+++ b/src/db/migrate/027_fix_uniqueness_constraints_in_bondings_nics.rb
@@ -1,8 +1,18 @@
 
 class FixUniquenessConstraintsInBondingsNics < ActiveRecord::Migration
   def self.up
+    # Mysql 5.* forces foreign key to have an index, for performance reason.
+    # One cannot remove an index before drop foreign key on it.
+    remove_foreign_key :bondings_nics, :name => 'fk_bondings_nics_bonding'
+    remove_foreign_key :bondings_nics, :name => 'fk_bondings_nics_nic'
+
     remove_index :bondings_nics, [:bonding_id, :nic_id]
     add_index :bondings_nics, :nic_id, :unique => true
+
+    # it can be re-added afterwards, without any problem
+    add_foreign_key :bondings_nics, :bondings,
+                    :name => 'fk_bondings_nics_bonding'
+    add_foreign_key :bondings_nics, :nics, :name => 'fk_bondings_nics_nic'
   end
 
   def self.down
@@ -10,4 +20,3 @@ class FixUniquenessConstraintsInBondingsNics < 
ActiveRecord::Migration
     add_index :bondings_nics, [:bonding_id, :nic_id], :unique => true
   end
 end
-
diff --git a/src/db/migrate/028_refactor_networking_model.rb 
b/src/db/migrate/028_refactor_networking_model.rb
index 946b976..ce00264 100644
--- a/src/db/migrate/028_refactor_networking_model.rb
+++ b/src/db/migrate/028_refactor_networking_model.rb
@@ -30,6 +30,7 @@ class RefactorNetworkingModel < ActiveRecord::Migration
       t.string  :type, :null => false
       t.string  :name, :null => false
       t.integer :boot_type_id, :null => false
+      t.foreign_key :boot_types, :name => 'fk_network_boot_types'
 
       # attributes for Vlan (type=Vlan)
       t.integer :number
@@ -42,7 +43,9 @@ class RefactorNetworkingModel < ActiveRecord::Migration
 
     create_table :networks_usages, :id => false do |t|
       t.integer :network_id, :null => false
+      t.foreign_key :networks, :name => 'fk_networks_usages_network_id'
       t.integer :usage_id, :null => false
+      t.foreign_key :networks, :name => 'fk_networks_usages_usage_id'
     end
 
     add_index :networks_usages, [:network_id, :usage_id], :unique => true
@@ -52,33 +55,13 @@ class RefactorNetworkingModel < ActiveRecord::Migration
     Usage.create(:label => 'Management', :usage => 'management')
     Usage.create(:label => 'Storage', :usage => 'storage')
 
-    # referential integrity for networks tables
-    execute "alter table networks add constraint
-             fk_network_boot_types
-             foreign key (boot_type_id) references
-             boot_types(id)"
-    execute "alter table networks_usages add constraint
-             fk_networks_usages_network_id
-             foreign key (network_id) references
-             networks(id)"
-    execute "alter table networks_usages add constraint
-             fk_networks_usages_usage_id
-             foreign key (usage_id) references
-             usages(id)"
-
     # add foreign keys to nics / bondings table
     add_column :nics, :physical_network_id, :integer
+    add_foreign_key :nics, :networks, :column => 'physical_network_id',
+                                      :name => 'fk_nic_networks'
     add_column :bondings, :vlan_id, :integer
-
-    # referential integrity for nic/bondings network ids
-    execute "alter table nics add constraint
-             fk_nic_networks
-             foreign key (physical_network_id) references
-             networks(id)"
-    execute "alter table bondings add constraint
-             fk_bonding_networks
-             foreign key (vlan_id) references
-             networks(id)"
+    add_foreign_key :bondings, :networks, :column => 'vlan_id',
+                                          :name => 'fk_bonding_networks'
 
     ####################################################
     # create ip_addresses table
@@ -87,8 +70,11 @@ class RefactorNetworkingModel < ActiveRecord::Migration
 
       # foreign keys to associated entities
       t.integer :nic_id
+      t.foreign_key :nics, :name => 'fk_nic_ip_addresses'
       t.integer :bonding_id
+      t.foreign_key :bondings, :name => 'fk_bonding_ip_addresses'
       t.integer :network_id
+      t.foreign_key :networks, :name => 'fk_network_ip_addresses'
 
       # common attributes
       t.string :address,   :limit => 39, :null => false
@@ -103,17 +89,6 @@ class RefactorNetworkingModel < ActiveRecord::Migration
       t.timestamps
     end
 
-    # referential integrity for ip_addresses table
-    execute "alter table ip_addresses add constraint
-             fk_nic_ip_addresses
-             foreign key (nic_id) references nics(id)"
-    execute "alter table ip_addresses add constraint
-             fk_bonding_ip_addresses
-             foreign key (bonding_id) references bondings(id)"
-    execute "alter table ip_addresses add constraint
-             fk_network_ip_addresses
-             foreign key (network_id) references networks(id)"
-
     ###################################################################
     static_boot_type_id =
       BootType.find(:first,
@@ -176,10 +151,12 @@ class RefactorNetworkingModel < ActiveRecord::Migration
     remove_column :nics,     :ip_addr
     remove_column :nics,     :netmask
     remove_column :nics,     :broadcast
+    remove_foreign_key :nics, :name => 'fk_nic_boot_type'
     remove_column :nics,     :boot_type_id
     remove_column :bondings, :ip_addr
     remove_column :bondings, :netmask
     remove_column :bondings, :broadcast
+    remove_foreign_key :bondings, :name => 'fk_bondings_boot_type'
     remove_column :bondings, :boot_type_id
 
 
@@ -197,14 +174,8 @@ class RefactorNetworkingModel < ActiveRecord::Migration
     add_column :bondings, :broadcast, :string, :limit => 16
     add_column :bondings, :boot_type_id, :integer
 
-    execute "alter table nics add constraint
-             fk_nic_boot_types
-             foreign key (boot_type_id) references
-             boot_types(id)"
-    execute "alter table bondings add constraint
-             fk_bonding_boot_types
-             foreign key (boot_type_id) references
-             boot_types(id)"
+    add_foreign_key :nics, :boot_types, :name => 'fk_nic_boot_type'
+    add_foreign_key :bondings, :boot_types, :name => 'fk_bondings_boot_type'
 
     ##############################################################
     # attempt to migrate ip information back into nics table.
diff --git a/src/db/migrate/035_networks_nics_bondings_additions.rb 
b/src/db/migrate/035_networks_nics_bondings_additions.rb
index 242eb50..ba021fd 100644
--- a/src/db/migrate/035_networks_nics_bondings_additions.rb
+++ b/src/db/migrate/035_networks_nics_bondings_additions.rb
@@ -33,18 +33,11 @@ class NetworksNicsBondingsAdditions < 
ActiveRecord::Migration
     execute 'update nics set physical_network_id = NULL'
     execute 'update bondings set vlan_id = NULL'
 
-    execute "alter table nics add constraint
-             host_physical_network_unique unique
-             (host_id, physical_network_id)"
-
-    execute "alter table bondings add constraint
-             host_vlan_unique unique
-             (host_id, vlan_id)"
-
-    execute "alter table vms add constraint
-             fk_vm_network_id
-             foreign key (network_id) references
-             networks(id)"
+    add_index :nics, [:host_id, :physical_network_id], :unique => true,
+              :name => 'host_physical_network_unique'
+    add_index :bondings, [:host_id, :vlan_id], :unique => true,
+              :name => 'host_vlan_unique'
+    add_foreign_key :vms, :networks, :name => 'fk_vm_network_id'
   end
 
   def self.down
@@ -52,4 +45,3 @@ class NetworksNicsBondingsAdditions < ActiveRecord::Migration
     remove_column :vms,  :network_id
   end
 end
-
diff --git a/src/db/migrate/036_vm_host_history.rb 
b/src/db/migrate/036_vm_host_history.rb
index beff9bf..98cdae9 100644
--- a/src/db/migrate/036_vm_host_history.rb
+++ b/src/db/migrate/036_vm_host_history.rb
@@ -24,7 +24,9 @@ class VmHostHistory < ActiveRecord::Migration
     create_table :vm_host_histories do |t|
        # vm / host association
        t.integer :vm_id
+       t.foreign_key :vms, :name => 'fk_vm_host_histories_vms'
        t.integer :host_id
+       t.foreign_key :hosts, :name => 'fk_vm_host_histories_hosts'
 
        # records operating info of vm
        #  (most likey we will want to add a
@@ -37,13 +39,6 @@ class VmHostHistory < ActiveRecord::Migration
        t.timestamp :time_started
        t.timestamp :time_ended
     end
-
-    execute "alter table vm_host_histories add constraint
-             fk_vm_host_histories_vms foreign key (vm_id) references vms(id)"
-
-    execute "alter table vm_host_histories add constraint
-             fk_vm_host_histories_hosts foreign key (host_id) references
-             hosts(id)"
   end
 
   def self.down
diff --git a/src/db/migrate/037_add_roles_and_privileges.rb 
b/src/db/migrate/037_add_roles_and_privileges.rb
index aa00ef8..ad48ede 100644
--- a/src/db/migrate/037_add_roles_and_privileges.rb
+++ b/src/db/migrate/037_add_roles_and_privileges.rb
@@ -30,18 +30,13 @@ class AddRolesAndPrivileges < ActiveRecord::Migration
 
     create_table :privileges_roles, :id => false do |t|
       t.integer :privilege_id,             :null => false
+      t.foreign_key :privileges, :name => 'fk_priv_roles_priv_id'
       t.integer :role_id, :null => false
+      t.foreign_key :roles, :name => 'fk_priv_roles_role_id'
     end
-    execute "alter table privileges_roles add constraint
-             fk_priv_roles_role_id
-             foreign key (role_id) references roles(id)"
-    execute "alter table privileges_roles add constraint
-             fk_priv_roles_priv_id
-             foreign key (privilege_id) references privileges(id)"
 
     add_column :permissions, :role_id, :integer
-    execute "alter table permissions add constraint fk_perm_roles
-             foreign key (role_id) references roles(id)"
+    add_foreign_key :permissions, :roles, :name => 'fk_perm_roles'
 
     #create default roles and privileges
     Role.transaction do
diff --git a/src/db/migrate/042_associate_vms_with_nics.rb 
b/src/db/migrate/042_associate_vms_with_nics.rb
index 0e457c0..725fdd8 100644
--- a/src/db/migrate/042_associate_vms_with_nics.rb
+++ b/src/db/migrate/042_associate_vms_with_nics.rb
@@ -21,18 +21,13 @@ class AssociateVmsWithNics < ActiveRecord::Migration
   def self.up
      # assocate nics w/ vms
      add_column :nics, :vm_id, :integer
-     execute "alter table nics add constraint
-              fk_nics_vm_id
-              foreign key (vm_id) references
-              vms(id)"
-     execute "alter table nics alter column host_id DROP NOT NULL"
+     add_foreign_key :nics, :vms, :name => 'fk_nics_vm_id'
+     change_column :nics, :host_id, :integer, :null => true
 
      # change physical_network_id column in nic table to network_id
-     execute 'alter table nics drop constraint fk_nic_networks'
-     execute 'alter table nics rename column physical_network_id to network_id'
-     execute 'alter table nics add constraint fk_nic_networks
-              foreign key (network_id) references networks(id)'
-
+     remove_foreign_key :nics, :name => 'fk_nic_networks'
+     rename_column :nics, :physical_network_id, :network_id
+     add_foreign_key :nics, :networks, :name => 'fk_nic_networks'
 
      # create a nic for each vm / network
      Vm.find(:all, :conditions => "network_id IS NOT NULL").each{ |vm|
@@ -50,10 +45,11 @@ class AssociateVmsWithNics < ActiveRecord::Migration
      # removes 1toM networks/vms relationship
      # remove network_id column from vms table
      # remove vnic_mac_addr column from vms table
-     execute 'alter table vms drop constraint fk_vm_network_id'
+     remove_foreign_key :vms, :name => 'fk_vm_network_id'
      remove_column :vms, :network_id
      remove_column :vms, :vnic_mac_addr
 
+    begin
      # add to the db some validations
      #   host_id is set xor vm_id  is set
      execute 'alter table nics add constraint host_xor_vm
@@ -63,18 +59,28 @@ class AssociateVmsWithNics < ActiveRecord::Migration
      execute 'alter table nics add constraint vm_nic_has_network
               check (vm_id IS NULL or network_id IS NOT NULL)'
      #   vm_id is set if network is vlan (TBD)
+    rescue ActiveRecord::StatementInvalid => e
+      # this kind of validation cannot be made on sqlite
+    rescue Exception => e
+      throw e
+    end
   end
 
   def self.down
     # drop constraints added to nics table
-    execute 'alter table nics drop constraint host_xor_vm'
-    execute 'alter table nics drop constraint vm_nic_has_network'
+    begin
+     execute 'alter table nics drop constraint host_xor_vm'
+     execute 'alter table nics drop constraint vm_nic_has_network'
+    rescue ActiveRecord::StatementInvalid => e
+      # this kind of validation cannot be made on sqlite
+    rescue Exception => e
+      throw e
+    end
 
     # add network_id, vnic_mac_addr column back to vm table
     add_column :vms, :network_id, :integer
     add_column :vms, :vnic_mac_addr, :string
-    execute 'alter table vms add constraint fk_vm_network_id
-             foreign key (network_id) references networks(id)'
+    add_foreign_key :vms, :networks, :name => 'fk_vm_network_id'
 
     # copy network id over
     # NOTE since we're going from a MtoM relationship to a 1toM
@@ -90,12 +96,12 @@ class AssociateVmsWithNics < ActiveRecord::Migration
 
     # unassociate nics / vms
     remove_column :nics, :vm_id
-    execute "alter table nics alter column host_id SET NOT NULL"
+    change_column :nics, :host_id, :integer, :null => false
 
     # change nic::network_id back to nic::physical_network_id
-    execute 'alter table nics drop constraint fk_nic_networks'
-    execute 'alter table nics rename column network_id to physical_network_id'
-    execute 'alter table nics add constraint fk_nic_networks
-              foreign key (physical_network_id) references networks(id)'
+    remove_foreign_key :nics, :name => 'fk_nic_networks'
+    rename_column :nics, :network_id, :physical_network_id
+    add_foreign_key :nics, :networks, :column => 'physical_network_id',
+                                      :name => 'fk_nic_networks'
   end
 end
-- 
1.6.2.5

_______________________________________________
Ovirt-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/ovirt-devel

Reply via email to