2020-06-12 20:03:43 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
|
|
|
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
2020-05-04 21:31:48 +00:00
|
|
|
class BackupUploaderColumns < ActiveRecord::Migration[5.2]
|
|
|
|
def up
|
|
|
|
# copy all
|
2020-05-05 18:56:17 +00:00
|
|
|
HoudiniUpgrade::UPLOADERS_TO_MIGRATE.each do |uploader_table|
|
|
|
|
create_table uploader_table.backup_table do |table|
|
|
|
|
table.references uploader_table.name, foreign_key: true, index: { name: "idx_#{uploader_table.backup_table}_on_fk"}
|
|
|
|
uploader_table.fields.each do |f|
|
2020-05-04 21:31:48 +00:00
|
|
|
table.string f.migrated_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-05-05 18:56:17 +00:00
|
|
|
|
2020-05-04 21:31:48 +00:00
|
|
|
# copy all
|
|
|
|
HoudiniUpgrade::UPLOADERS_TO_MIGRATE.each do |table|
|
|
|
|
execute <<-SQL
|
|
|
|
INSERT INTO #{table.backup_table} (#{table.foreign_key}, #{table.fields.map(&:migrated_name).join(', ')})
|
2020-05-05 18:56:17 +00:00
|
|
|
( SELECT id, #{table.fields.map(&:migrated_name).join(', ')} FROM #{table.name})
|
|
|
|
SQL
|
2020-05-04 21:31:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# delete columns
|
|
|
|
HoudiniUpgrade::UPLOADERS_TO_MIGRATE.each do |table|
|
2020-05-05 18:56:17 +00:00
|
|
|
table.fields.each do |f|
|
|
|
|
remove_column table.name, f.migrated_name
|
2020-05-04 21:31:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
## readd columns
|
|
|
|
HoudiniUpgrade::UPLOADERS_TO_MIGRATE.each do |table|
|
2020-05-05 18:56:17 +00:00
|
|
|
table.fields.each do |f|
|
2020-05-04 21:31:48 +00:00
|
|
|
add_column table.name, f.migrated_name, :string
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# copy all
|
|
|
|
HoudiniUpgrade::UPLOADERS_TO_MIGRATE.each do |table|
|
|
|
|
execute <<-SQL
|
|
|
|
UPDATE #{table.name} SET (#{table.fields.map(&:migrated_name).join(', ')}) = (
|
2020-05-05 18:56:17 +00:00
|
|
|
SELECT #{table.fields.map{|f| table.backup_table + "." + f.migrated_name}.join(', ')} FROM #{table.backup_table}
|
2020-05-04 21:31:48 +00:00
|
|
|
WHERE #{table.backup_table}.#{table.foreign_key} = #{table.name}.id
|
|
|
|
)
|
2020-05-05 18:56:17 +00:00
|
|
|
SQL
|
2020-05-04 21:31:48 +00:00
|
|
|
end
|
|
|
|
# delete tables
|
2020-05-05 18:56:17 +00:00
|
|
|
HoudiniUpgrade::UPLOADERS_TO_MIGRATE.each{ |entity, _| drop_table entity.backup_table}
|
2020-05-04 21:31:48 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|