add template_id to submitters

This commit is contained in:
Pete Matsyburka committed 2026-10-25 09:39:47 +02:00
1 parent 2d2eb7ce4d
commit 5413fbc248
5 files changed
+72 -3

No files matched your search

+9 -1
View File
@@ -25,15 +25,17 @@
# account_id :bigint not null # account_id :bigint not null
# external_id :string # external_id :string
# submission_id :bigint not null # submission_id :bigint not null
# template_id :bigint
# #
# Indexes # Indexes
# #
# index_submitters_on_account_id_and_completed_at (account_id,completed_at) WHERE (completed_at IS NOT NULL) # index_submitters_on_account_id_and_completed_at (account_id,completed_at) WHERE (completed_at IS NOT NULL)
# index_submitters_on_account_id_and_id (account_id,id) # index_submitters_on_account_id_and_id (account_id,id)
# index_submitters_on_email (email) # index_submitters_on_email_and_template_id (email,template_id) WHERE (email IS NOT NULL)
# index_submitters_on_external_id (external_id) # index_submitters_on_external_id (external_id)
# index_submitters_on_slug (slug) UNIQUE # index_submitters_on_slug (slug) UNIQUE
# index_submitters_on_submission_id (submission_id) # index_submitters_on_submission_id (submission_id)
# index_submitters_on_template_id_and_id (template_id,id)
# #
# Foreign Keys # Foreign Keys
# #
@@ -68,6 +70,8 @@ class Submitter < ApplicationRecord
scope :completed, -> { where.not(completed_at: nil) } scope :completed, -> { where.not(completed_at: nil) }
before_validation :set_template_id, on: :create
after_destroy :anonymize_email_events, if: -> { Docuseal.multitenant? } after_destroy :anonymize_email_events, if: -> { Docuseal.multitenant? }
def status def status
@@ -124,6 +128,10 @@ class Submitter < ApplicationRecord
private private
def set_template_id
self.template_id = submission&.template_id
end
def anonymize_email_events def anonymize_email_events
email_events.each do |event| email_events.each do |event|
event.update!(email: Digest::MD5.base64digest(event.email)) event.update!(email: Digest::MD5.base64digest(event.email))
@@ -0,0 +1,7 @@
# frozen_string_literal: true
class AddTemplateIdToSubmitters < ActiveRecord::Migration[8.1]
def change
add_column :submitters, :template_id, :bigint, if_not_exists: true
end
end
@@ -0,0 +1,37 @@
# frozen_string_literal: true
class PopulateSubmitterTemplateId < ActiveRecord::Migration[8.1]
disable_ddl_transaction!
class MigrationSubmitter < ApplicationRecord
self.table_name = 'submitters'
end
class MigrationSubmission < ApplicationRecord
self.table_name = 'submissions'
end
def up
return if ENV['MULTITENANT'] == 'true'
max_id = MigrationSubmitter.maximum(:id)
return unless max_id
submission_template_id =
Arel::Nodes::Grouping.new(
MigrationSubmission.arel_table.project(MigrationSubmission.arel_table[:template_id])
.where(MigrationSubmission.arel_table[:id].eq(MigrationSubmitter.arel_table[:submission_id]))
.ast
)
(1..max_id).step(10_000) do |start_id|
MigrationSubmitter.where(template_id: nil, id: start_id...(start_id + 10_000))
.update_all(template_id: submission_template_id)
end
end
def down
nil
end
end
@@ -0,0 +1,15 @@
# frozen_string_literal: true
class AddSubmittersTemplateIdIndexes < ActiveRecord::Migration[8.1]
def up
add_index :submitters, %i[email template_id], where: 'email IS NOT NULL', if_not_exists: true
add_index :submitters, %i[template_id id], if_not_exists: true
remove_index :submitters, :email, if_exists: true
end
def down
add_index :submitters, :email, if_not_exists: true
remove_index :submitters, %i[template_id id], if_exists: true
remove_index :submitters, %i[email template_id], if_exists: true
end
end
+4 -2
View File
@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.1].define(version: 2026_09_30_061927) do ActiveRecord::Schema[8.1].define(version: 2026_10_25_120002) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "btree_gin" enable_extension "btree_gin"
enable_extension "pg_catalog.plpgsql" enable_extension "pg_catalog.plpgsql"
@@ -424,6 +424,7 @@ ActiveRecord::Schema[8.1].define(version: 2026_09_30_061927) do
t.datetime "sent_at" t.datetime "sent_at"
t.string "slug", null: false t.string "slug", null: false
t.bigint "submission_id", null: false t.bigint "submission_id", null: false
t.bigint "template_id"
t.string "timezone" t.string "timezone"
t.string "ua" t.string "ua"
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
@@ -431,10 +432,11 @@ ActiveRecord::Schema[8.1].define(version: 2026_09_30_061927) do
t.text "values", null: false t.text "values", null: false
t.index ["account_id", "completed_at"], name: "index_submitters_on_account_id_and_completed_at", where: "(completed_at IS NOT NULL)" t.index ["account_id", "completed_at"], name: "index_submitters_on_account_id_and_completed_at", where: "(completed_at IS NOT NULL)"
t.index ["account_id", "id"], name: "index_submitters_on_account_id_and_id" t.index ["account_id", "id"], name: "index_submitters_on_account_id_and_id"
t.index ["email"], name: "index_submitters_on_email" t.index ["email", "template_id"], name: "index_submitters_on_email_and_template_id", where: "(email IS NOT NULL)"
t.index ["external_id"], name: "index_submitters_on_external_id" t.index ["external_id"], name: "index_submitters_on_external_id"
t.index ["slug"], name: "index_submitters_on_slug", unique: true t.index ["slug"], name: "index_submitters_on_slug", unique: true
t.index ["submission_id"], name: "index_submitters_on_submission_id" t.index ["submission_id"], name: "index_submitters_on_submission_id"
t.index ["template_id", "id"], name: "index_submitters_on_template_id_and_id"
end end
create_table "template_accesses", force: :cascade do |t| create_table "template_accesses", force: :cascade do |t|