Skip to content

Commit

Permalink
[Etablissement] Add new data & remove sync for null fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Ldouziech committed Feb 27, 2024
1 parent 21d0db3 commit df45018
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 42 deletions.
13 changes: 13 additions & 0 deletions migrations/2024-02-27-161209_siren_3_11/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ALTER TABLE "public"."etablissement"
DROP COLUMN IF EXISTS "dernier_numero_voie",
DROP COLUMN IF EXISTS "indice_repetition_dernier_numero_voie",
DROP COLUMN IF EXISTS "identifiant_adresse",
DROP COLUMN IF EXISTS "coordonnee_lambert_x",
DROP COLUMN IF EXISTS "coordonnee_lambert_y";

ALTER TABLE "public"."etablissement_staging"
DROP COLUMN IF EXISTS "dernier_numero_voie",
DROP COLUMN IF EXISTS "indice_repetition_dernier_numero_voie",
DROP COLUMN IF EXISTS "identifiant_adresse",
DROP COLUMN IF EXISTS "coordonnee_lambert_x",
DROP COLUMN IF EXISTS "coordonnee_lambert_y";
13 changes: 13 additions & 0 deletions migrations/2024-02-27-161209_siren_3_11/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ALTER TABLE "public"."etablissement"
ADD COLUMN "dernier_numero_voie" text DEFAULT NULL,
ADD COLUMN "indice_repetition_dernier_numero_voie" text DEFAULT NULL,
ADD COLUMN "identifiant_adresse" text DEFAULT NULL,
ADD COLUMN "coordonnee_lambert_x" text DEFAULT NULL,
ADD COLUMN "coordonnee_lambert_y" text DEFAULT NULL;

ALTER TABLE "public"."etablissement_staging"
ADD COLUMN "dernier_numero_voie" text DEFAULT NULL,
ADD COLUMN "indice_repetition_dernier_numero_voie" text DEFAULT NULL,
ADD COLUMN "identifiant_adresse" text DEFAULT NULL,
ADD COLUMN "coordonnee_lambert_x" text DEFAULT NULL,
ADD COLUMN "coordonnee_lambert_y" text DEFAULT NULL;
60 changes: 25 additions & 35 deletions src/connectors/insee/types/etablissement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ pub struct InseeEtablissementInner {
pub etablissement_siege: bool,
pub nombre_periodes_etablissement: Option<i32>,
pub adresse_etablissement: InseeAdresseEtablissement,
pub adresse2_etablissement: InseeAdresse2Etablissement,
}

#[derive(Deserialize, Debug)]
Expand All @@ -55,6 +54,8 @@ pub struct InseeAdresseEtablissement {
pub complement_adresse_etablissement: Option<String>,
pub numero_voie_etablissement: Option<String>,
pub indice_repetition_etablissement: Option<String>,
pub dernier_numero_voie_etablissement: Option<String>,
pub indice_repetition_dernier_numero_voie_etablissement: Option<String>,
pub type_voie_etablissement: Option<String>,
pub libelle_voie_etablissement: Option<String>,
pub code_postal_etablissement: Option<String>,
Expand All @@ -66,25 +67,9 @@ pub struct InseeAdresseEtablissement {
pub libelle_cedex_etablissement: Option<String>,
pub code_pays_etranger_etablissement: Option<String>,
pub libelle_pays_etranger_etablissement: Option<String>,
}

#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct InseeAdresse2Etablissement {
complement_adresse2_etablissement: Option<String>,
numero_voie2_etablissement: Option<String>,
indice_repetition2_etablissement: Option<String>,
type_voie2_etablissement: Option<String>,
libelle_voie2_etablissement: Option<String>,
code_postal2_etablissement: Option<String>,
libelle_commune2_etablissement: Option<String>,
libelle_commune_etranger2_etablissement: Option<String>,
distribution_speciale2_etablissement: Option<String>,
code_commune2_etablissement: Option<String>,
code_cedex2_etablissement: Option<String>,
libelle_cedex2_etablissement: Option<String>,
code_pays_etranger2_etablissement: Option<String>,
libelle_pays_etranger2_etablissement: Option<String>,
pub identifiant_adresse_etablissement: Option<String>,
pub coordonnee_lambert_abscisse_etablissement: Option<String>,
pub coordonnee_lambert_ordonnee_etablissement: Option<String>,
}

#[derive(Deserialize, Debug, Clone)]
Expand Down Expand Up @@ -121,7 +106,6 @@ impl From<&InseeEtablissement> for Option<Etablissement> {
impl From<InseeEtablissementWithPeriode> for Etablissement {
fn from(e: InseeEtablissementWithPeriode) -> Self {
let adresse = e.content.adresse_etablissement;
let adresse2 = e.content.adresse2_etablissement;

Etablissement {
siret: e.content.siret,
Expand Down Expand Up @@ -151,20 +135,26 @@ impl From<InseeEtablissementWithPeriode> for Etablissement {
libelle_cedex: adresse.libelle_cedex_etablissement,
code_pays_etranger: adresse.code_pays_etranger_etablissement,
libelle_pays_etranger: adresse.libelle_pays_etranger_etablissement,
complement_adresse2: adresse2.complement_adresse2_etablissement,
numero_voie_2: adresse2.numero_voie2_etablissement,
indice_repetition_2: adresse2.indice_repetition2_etablissement,
type_voie_2: adresse2.type_voie2_etablissement,
libelle_voie_2: adresse2.libelle_voie2_etablissement,
code_postal_2: adresse2.code_postal2_etablissement,
libelle_commune_2: adresse2.libelle_commune2_etablissement,
libelle_commune_etranger_2: adresse2.libelle_commune_etranger2_etablissement,
distribution_speciale_2: adresse2.distribution_speciale2_etablissement,
code_commune_2: adresse2.code_commune2_etablissement,
code_cedex_2: adresse2.code_cedex2_etablissement,
libelle_cedex_2: adresse2.libelle_cedex2_etablissement,
code_pays_etranger_2: adresse2.code_pays_etranger2_etablissement,
libelle_pays_etranger_2: adresse2.libelle_pays_etranger2_etablissement,
dernier_numero_voie: adresse.dernier_numero_voie_etablissement,
identifiant_adresse: adresse.identifiant_adresse_etablissement,
indice_repetition_dernier_numero_voie: adresse
.indice_repetition_dernier_numero_voie_etablissement,
coordonnee_lambert_x: adresse.coordonnee_lambert_abscisse_etablissement,
coordonnee_lambert_y: adresse.coordonnee_lambert_ordonnee_etablissement,
complement_adresse2: None,
numero_voie_2: None,
indice_repetition_2: None,
type_voie_2: None,
libelle_voie_2: None,
code_postal_2: None,
libelle_commune_2: None,
libelle_commune_etranger_2: None,
distribution_speciale_2: None,
code_commune_2: None,
code_cedex_2: None,
libelle_cedex_2: None,
code_pays_etranger_2: None,
libelle_pays_etranger_2: None,
date_debut: e.periode.date_debut,
etat_administratif: e.periode.etat_administratif_etablissement,
enseigne_1: e.periode.enseigne1_etablissement,
Expand Down
5 changes: 5 additions & 0 deletions src/models/etablissement/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ nombre_periodes,
complement_adresse,
numero_voie,
indice_repetition,
dernier_numero_voie,
indice_repetition_dernier_numero_voie,
type_voie,
libelle_voie,
code_postal,
Expand All @@ -24,6 +26,9 @@ code_cedex,
libelle_cedex,
code_pays_etranger,
libelle_pays_etranger,
identifiant_adresse,
coordonnee_lambert_x,
coordonnee_lambert_y,
complement_adresse2,
numero_voie_2,
indice_repetition_2,
Expand Down
7 changes: 6 additions & 1 deletion src/models/etablissement/common.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use diesel::prelude::*;
use super::super::schema::etablissement;
use chrono::{NaiveDate, NaiveDateTime};
use diesel::prelude::*;
use serde::Serialize;

#[derive(Insertable, Queryable, Serialize, Clone, Debug)]
Expand Down Expand Up @@ -54,4 +54,9 @@ pub struct Etablissement {
pub activite_principale: Option<String>,
pub nomenclature_activite_principale: Option<String>,
pub caractere_employeur: Option<String>,
pub dernier_numero_voie: Option<String>,
pub indice_repetition_dernier_numero_voie: Option<String>,
pub identifiant_adresse: Option<String>,
pub coordonnee_lambert_x: Option<String>,
pub coordonnee_lambert_y: Option<String>,
}
22 changes: 16 additions & 6 deletions src/models/etablissement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ pub fn get(connection: &mut Connection, siret: &str) -> Result<Etablissement, Er
.map_err(|error| error.into())
}

pub fn get_with_siren(connection: &mut Connection, siren: &str) -> Result<Vec<Etablissement>, Error> {
pub fn get_with_siren(
connection: &mut Connection,
siren: &str,
) -> Result<Vec<Etablissement>, Error> {
dsl::etablissement
.filter(dsl::siren.eq(siren))
.load::<Etablissement>(connection)
.map_err(|error| error.into())
}

pub fn get_siege_with_siren(connection: &mut Connection, siren: &str) -> Result<Etablissement, Error> {
pub fn get_siege_with_siren(
connection: &mut Connection,
siren: &str,
) -> Result<Etablissement, Error> {
dsl::etablissement
.filter(dsl::siren.eq(siren).and(dsl::etablissement_siege.eq(true)))
.first::<Etablissement>(connection)
Expand Down Expand Up @@ -77,10 +83,8 @@ impl UpdatableModel for EtablissementModel {
fn swap(&self, connectors: &Connectors) -> Result<(), UpdatableError> {
let mut connection = connectors.local.pool.get()?;
connection.build_transaction().read_write().run(|conn| {
sql_query("ALTER TABLE etablissement RENAME TO etablissement_temp")
.execute(conn)?;
sql_query("ALTER TABLE etablissement_staging RENAME TO etablissement")
.execute(conn)?;
sql_query("ALTER TABLE etablissement RENAME TO etablissement_temp").execute(conn)?;
sql_query("ALTER TABLE etablissement_staging RENAME TO etablissement").execute(conn)?;
sql_query("ALTER TABLE etablissement_temp RENAME TO etablissement_staging")
.execute(conn)?;
sql_query("TRUNCATE etablissement_staging").execute(conn)?;
Expand Down Expand Up @@ -203,6 +207,12 @@ impl UpdatableModel for EtablissementModel {
dsl::nomenclature_activite_principale
.eq(excluded(dsl::nomenclature_activite_principale)),
dsl::caractere_employeur.eq(excluded(dsl::caractere_employeur)),
dsl::dernier_numero_voie.eq(excluded(dsl::dernier_numero_voie)),
dsl::indice_repetition_dernier_numero_voie
.eq(excluded(dsl::indice_repetition_dernier_numero_voie)),
dsl::identifiant_adresse.eq(excluded(dsl::identifiant_adresse)),
dsl::coordonnee_lambert_x.eq(excluded(dsl::coordonnee_lambert_x)),
dsl::coordonnee_lambert_y.eq(excluded(dsl::coordonnee_lambert_y)),
))
.execute(&mut connection)?;

Expand Down
10 changes: 10 additions & 0 deletions src/models/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ diesel::table! {
activite_principale -> Nullable<Text>,
nomenclature_activite_principale -> Nullable<Text>,
caractere_employeur -> Nullable<Text>,
dernier_numero_voie -> Nullable<Text>,
indice_repetition_dernier_numero_voie -> Nullable<Text>,
identifiant_adresse -> Nullable<Text>,
coordonnee_lambert_x -> Nullable<Text>,
coordonnee_lambert_y -> Nullable<Text>,
}
}

Expand Down Expand Up @@ -113,6 +118,11 @@ diesel::table! {
activite_principale -> Nullable<Text>,
nomenclature_activite_principale -> Nullable<Text>,
caractere_employeur -> Nullable<Text>,
dernier_numero_voie -> Nullable<Text>,
indice_repetition_dernier_numero_voie -> Nullable<Text>,
identifiant_adresse -> Nullable<Text>,
coordonnee_lambert_x -> Nullable<Text>,
coordonnee_lambert_y -> Nullable<Text>,
}
}

Expand Down

0 comments on commit df45018

Please sign in to comment.