advanced image management
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
class CreateSoftwareImages < ActiveRecord::Migration[8.0]
|
||||
def change
|
||||
create_table :software_images do |t|
|
||||
t.bigint :software_id, null: false, unsigned: true
|
||||
t.bigint :image_id, null: false
|
||||
t.boolean :is_default, null: false, default: false
|
||||
t.integer :position, null: false, default: 0
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
add_index :software_images, [:software_id, :image_id], unique: true
|
||||
add_index :software_images, [:software_id, :position]
|
||||
|
||||
add_foreign_key :software_images, :softwares
|
||||
add_foreign_key :software_images, :images
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,24 @@
|
||||
class MigrateAndRemoveSoftwareImageId < ActiveRecord::Migration[8.0]
|
||||
def up
|
||||
Software.unscoped.where.not(image_id: nil).find_each do |sw|
|
||||
SoftwareImage.create!(
|
||||
software_id: sw.id,
|
||||
image_id: sw.image_id,
|
||||
is_default: true,
|
||||
position: 0
|
||||
)
|
||||
end
|
||||
|
||||
remove_foreign_key :softwares, :images
|
||||
remove_column :softwares, :image_id
|
||||
end
|
||||
|
||||
def down
|
||||
add_column :softwares, :image_id, :bigint
|
||||
add_foreign_key :softwares, :images
|
||||
|
||||
SoftwareImage.where(is_default: true).find_each do |si|
|
||||
Software.unscoped.where(id: si.software_id).update_all(image_id: si.image_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user