-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into menu_link_to_producer_space
- Loading branch information
Showing
9 changed files
with
169 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,34 @@ | ||
defmodule DB.EPCI do | ||
@moduledoc """ | ||
EPCI schema | ||
EPCI schema. | ||
Link the EPCI to some Communes. | ||
The EPCI are loaded once and for all by the task transport/lib/transport/import_epci.ex | ||
The EPCI are loaded by the task transport/lib/transport/import_epci.ex. | ||
The EPCI imported are only "à fiscalité propre". This excludes Etablissements Publics Territoriaux. | ||
This allows to have a 1 to 1 relation between a commune and an EPCI. | ||
""" | ||
use Ecto.Schema | ||
use TypedEctoSchema | ||
import Ecto.Changeset | ||
|
||
typed_schema "epci" do | ||
field(:code, :string) | ||
field(:insee, :string) | ||
field(:nom, :string) | ||
field(:type, :string) | ||
field(:mode_financement, :string) | ||
field(:geom, Geo.PostGIS.Geometry) :: Geo.MultiPolygon.t() | ||
has_many(:communes, DB.Commune, foreign_key: :epci_insee) | ||
end | ||
|
||
# for the moment we don't need a link relational link to the Commune table, | ||
# so we only store an array of insee code | ||
field(:communes_insee, {:array, :string}, default: []) | ||
def changeset(epci, attrs) do | ||
epci | ||
|> cast(attrs, [:insee, :nom, :geom, :type, :mode_financement]) | ||
|> validate_required([:insee, :nom, :geom, :type, :mode_financement]) | ||
|> validate_inclusion(:type, allowed_types()) | ||
|> validate_inclusion(:mode_financement, allowed_mode_financement()) | ||
end | ||
|
||
defp allowed_types, | ||
do: ["Communauté d'agglomération", "Communauté urbaine", "Communauté de communes", "Métropole", "Métropole de Lyon"] | ||
|
||
defp allowed_mode_financement, do: ["Fiscalité professionnelle unique", "Fiscalité additionnelle"] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
apps/transport/priv/repo/migrations/20231222145809_improve_epci.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
defmodule DB.Repo.Migrations.ImproveEPCI do | ||
use Ecto.Migration | ||
|
||
def change do | ||
rename(table(:epci), :code, to: :insee) | ||
create(unique_index(:epci, [:insee])) | ||
|
||
alter table(:commune) do | ||
add(:epci_insee, references(:epci, column: :insee, type: :string, on_delete: :nilify_all), null: true) | ||
end | ||
|
||
create(index(:commune, [:epci_insee])) | ||
|
||
# Migrate data from epci communes_insee array column to epci_insee column in commune table | ||
execute( | ||
""" | ||
UPDATE commune | ||
SET epci_insee = epci.insee | ||
FROM epci | ||
WHERE commune.insee = ANY(epci.communes_insee) | ||
""", | ||
""" | ||
UPDATE epci | ||
SET communes_insee = ARRAY( | ||
SELECT insee | ||
FROM commune | ||
WHERE commune.epci_insee = epci.insee | ||
) | ||
""" | ||
) | ||
|
||
alter table(:epci) do | ||
add(:type, :string) | ||
add(:mode_financement, :string) | ||
add(:geom, :geometry) | ||
remove(:communes_insee, {:array, :string}, default: []) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters