mirror of https://github.com/docusealco/docuseal
parent
9bcf5aacc0
commit
ef8c3cfe7f
@ -0,0 +1,34 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: template_folders
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# deleted_at :datetime
|
||||
# name :string not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# account_id :bigint not null
|
||||
# author_id :bigint not null
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_template_folders_on_account_id (account_id)
|
||||
# index_template_folders_on_author_id (author_id)
|
||||
#
|
||||
# Foreign Keys
|
||||
#
|
||||
# fk_rails_... (account_id => accounts.id)
|
||||
# fk_rails_... (author_id => users.id)
|
||||
#
|
||||
class TemplateFolder < ApplicationRecord
|
||||
DEFAULT_NAME = 'Default'
|
||||
|
||||
belongs_to :author, class_name: 'User'
|
||||
belongs_to :account
|
||||
|
||||
has_many :templates, dependent: :destroy
|
||||
|
||||
scope :active, -> { where(deleted_at: nil) }
|
||||
end
|
||||
@ -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
|
||||
Loading…
Reference in new issue