diff --git a/app/models/attestation_template.rb b/app/models/attestation_template.rb index eea88355c..c56c030b0 100644 --- a/app/models/attestation_template.rb +++ b/app/models/attestation_template.rb @@ -7,6 +7,11 @@ class AttestationTemplate < ApplicationRecord has_one_attached :logo has_one_attached :signature + enum state: { + draft: 'draft', + published: 'published' + } + validates :title, tags: true, if: -> { procedure.present? && version == 1 } validates :body, tags: true, if: -> { procedure.present? && version == 1 } validates :json_body, tags: true, if: -> { procedure.present? && version == 2 } diff --git a/db/migrate/20240514164228_add_state_to_attestation_templates.rb b/db/migrate/20240514164228_add_state_to_attestation_templates.rb new file mode 100644 index 000000000..8a7842502 --- /dev/null +++ b/db/migrate/20240514164228_add_state_to_attestation_templates.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddStateToAttestationTemplates < ActiveRecord::Migration[7.0] + def change + add_column :attestation_templates, :state, :string, default: 'published' + end +end diff --git a/db/migrate/20240522084927_add_attestation_template_unicity_index.rb b/db/migrate/20240522084927_add_attestation_template_unicity_index.rb new file mode 100644 index 000000000..5dd74d831 --- /dev/null +++ b/db/migrate/20240522084927_add_attestation_template_unicity_index.rb @@ -0,0 +1,12 @@ +class AddAttestationTemplateUnicityIndex < ActiveRecord::Migration[7.0] + disable_ddl_transaction! + + def change + # this index was not created on production + if index_exists?(:attestation_templates, [:procedure_id, :version]) + remove_index :attestation_templates, [:procedure_id, :version], unique: true, algorithm: :concurrently + end + + add_index :attestation_templates, [:procedure_id, :version, :state], name: "index_attestation_templates_on_procedure_version_state", unique: true, algorithm: :concurrently + end +end diff --git a/db/schema.rb b/db/schema.rb index b33a10582..28c317cac 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -175,10 +175,11 @@ ActiveRecord::Schema[7.0].define(version: 2024_05_27_090508) do t.string "label_logo" t.boolean "official_layout", default: true, null: false t.integer "procedure_id" + t.string "state", default: "published" t.text "title" t.datetime "updated_at", precision: nil, null: false t.integer "version", default: 1, null: false - t.index ["procedure_id", "version"], name: "index_attestation_templates_on_procedure_id_and_version", unique: true + t.index ["procedure_id", "version", "state"], name: "index_attestation_templates_on_procedure_version_state", unique: true end create_table "attestations", id: :serial, force: :cascade do |t|