add folders migration

This commit is contained in:
Alex Turchyn
2023-09-21 00:23:38 +03:00
parent 9bcf5aacc0
commit ef8c3cfe7f
8 changed files with 129 additions and 2 deletions
@@ -0,0 +1,15 @@
# frozen_string_literal: true
class CreateTemplateFolders < ActiveRecord::Migration[7.0]
def change
create_table :template_folders do |t|
t.string :name, null: false
t.references :author, null: false, foreign_key: { to_table: :users }, index: true
t.references :account, null: false, foreign_key: true, index: true
t.datetime :deleted_at
t.timestamps
end
end
end
@@ -0,0 +1,40 @@
# frozen_string_literal: true
class AddFolderIdToTemplates < ActiveRecord::Migration[7.0]
class MigrationTemplateFolder < ApplicationRecord
self.table_name = 'template_folders'
end
class MigrationAccount < ApplicationRecord
self.table_name = 'accounts'
end
class MigrationTemplate < ApplicationRecord
self.table_name = 'templates'
end
class MigrationUser < ApplicationRecord
self.table_name = 'users'
end
def up
add_reference :templates, :folder, foreign_key: { to_table: :template_folders }, index: true, null: true
MigrationAccount.all.pluck(:id).each do |account_id|
author_id = MigrationUser.where(account_id:).minimum(:id)
next if author_id.blank?
folder = MigrationTemplateFolder.create_with(author_id:)
.find_or_create_by(name: 'Default', account_id:)
MigrationTemplate.where(account_id:).update_all(folder_id: folder.id)
end
change_column_null :templates, :folder_id, false
end
def down
remove_column :templates, :folder_id
end
end