diff --git a/Gemfile b/Gemfile index 1650f8fc0..82e6dbf40 100644 --- a/Gemfile +++ b/Gemfile @@ -19,7 +19,7 @@ gem 'activeadmin-xls', git: 'https://github.com/shanser/activeadmin-xls', branch: 'autoload' gem 'acts_as_list' gem 'addressable' -gem 'ancestry', '< 4.3' +gem 'ancestry', '~> 4.3' gem 'auto_strip_attributes', '~> 2.6' gem 'bootsnap', '>= 1.1.0', require: false gem 'bootstrap', '~> 4.3', '>= 4.3.1' diff --git a/Gemfile.lock b/Gemfile.lock index 71ebd9b36..d98eafdcf 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -111,7 +111,7 @@ GEM addressable (2.8.4) public_suffix (>= 2.0.2, < 6.0) afm (0.2.2) - ancestry (4.2.0) + ancestry (4.3.3) activerecord (>= 5.2.6) arbre (1.5.0) activesupport (>= 3.0.0, < 7.1) @@ -555,7 +555,7 @@ DEPENDENCIES activeadmin_reorderable acts_as_list addressable - ancestry (< 4.3) + ancestry (~> 4.3) auto_strip_attributes (~> 2.6) aws-sdk-s3 bootsnap (>= 1.1.0) diff --git a/app/models/structure.rb b/app/models/structure.rb index 20abdce21..f82458c84 100644 --- a/app/models/structure.rb +++ b/app/models/structure.rb @@ -1,8 +1,7 @@ # frozen_string_literal: true class Structure < ApplicationRecord - REGEX_UUID = /\A[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/i.freeze - has_ancestry(primary_key_format: REGEX_UUID) + has_ancestry acts_as_paranoid alias structure_referente parent diff --git a/config/initializers/ancestry.rb b/config/initializers/ancestry.rb new file mode 100644 index 000000000..bfa06fdcb --- /dev/null +++ b/config/initializers/ancestry.rb @@ -0,0 +1,2 @@ +Ancestry.default_primary_key_format = /[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/i +Ancestry.default_ancestry_format = :materialized_path2 diff --git a/db/migrate/20230515100936_change_colonne_collation_ancestry_pour_structures.rb b/db/migrate/20230515100936_change_colonne_collation_ancestry_pour_structures.rb new file mode 100644 index 000000000..00ff886ae --- /dev/null +++ b/db/migrate/20230515100936_change_colonne_collation_ancestry_pour_structures.rb @@ -0,0 +1,14 @@ +class ChangeColonneCollationAncestryPourStructures < ActiveRecord::Migration[7.0] + class Structure < ApplicationRecord + has_ancestry + end + def change + model = Structure + # set all child nodes + model.where.not(model.arel_table[model.ancestry_column].eq(nil)).update_all("#{model.ancestry_column} = CONCAT('#{model.ancestry_delimiter}', #{model.ancestry_column}, '#{model.ancestry_delimiter}')") + # set all root nodes + model.where(model.arel_table[model.ancestry_column].eq(nil)).update_all("#{model.ancestry_column} = '#{model.ancestry_root}'") + + change_column :structures, :ancestry, :string, collation: 'C', null: false + end +end diff --git a/db/schema.rb b/db/schema.rb index 1e45f9943..c633b2002 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_04_19_084251) do +ActiveRecord::Schema[7.0].define(version: 2023_05_15_100936) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -357,7 +357,7 @@ t.datetime "anonymise_le", precision: nil t.string "type" t.datetime "deleted_at" - t.string "ancestry" + t.string "ancestry", null: false, collation: "C" t.index ["ancestry"], name: "index_structures_on_ancestry" t.index ["deleted_at"], name: "index_structures_on_deleted_at" t.index ["latitude", "longitude"], name: "index_structures_on_latitude_and_longitude" diff --git a/docs/erd.png b/docs/erd.png index 42295ce64..e709935d6 100644 Binary files a/docs/erd.png and b/docs/erd.png differ diff --git a/spec/models/structure_spec.rb b/spec/models/structure_spec.rb index 0664f4b64..97878a567 100644 --- a/spec/models/structure_spec.rb +++ b/spec/models/structure_spec.rb @@ -8,9 +8,9 @@ it { is_expected.to validate_presence_of(:nom) } it { is_expected.to validate_uniqueness_of(:nom).scoped_to(:code_postal).case_insensitive } - describe 'REGEX_UUID' do - it { expect('uuid invalide').not_to match(Structure::REGEX_UUID) } - it { expect(SecureRandom.uuid).to match(Structure::REGEX_UUID) } + describe 'Ancestry primary key format' do + it { expect('uuid invalide').not_to match(Ancestry.default_primary_key_format) } + it { expect(SecureRandom.uuid).to match(Ancestry.default_primary_key_format) } end def mock_geo_api(departement, code_region, region)