Skip to content

Commit

Permalink
Migration vers ancestry 4.3 et du model materialized_path2
Browse files Browse the repository at this point in the history
  • Loading branch information
etienneCharignon committed May 24, 2023
1 parent a0e35e4 commit c8fc12b
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions app/models/structure.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions config/initializers/ancestry.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
Binary file modified docs/erd.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions spec/models/structure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c8fc12b

Please sign in to comment.