From 6aceceff8172bb00e10e51edf156d919c53aa7e9 Mon Sep 17 00:00:00 2001 From: dahoud Date: Fri, 12 Dec 2025 18:00:44 +0000 Subject: [PATCH] Fix: Add table existence check in V1.2 migration V1.2 now checks if the 'membres' table exists before trying to add the organisation_id column. This prevents failures when Flyway runs before Hibernate creates the initial schema from JPA entities. Changes: - Added table existence check before ALTER TABLE membres - Migration will skip the ALTER if table doesn't exist yet - Allows Hibernate to create initial schema first This ensures migrations work correctly without any test data. --- .../V1.2__Create_Organisation_Table.sql | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/resources/db/migration/V1.2__Create_Organisation_Table.sql b/src/main/resources/db/migration/V1.2__Create_Organisation_Table.sql index 7329794..41a4894 100644 --- a/src/main/resources/db/migration/V1.2__Create_Organisation_Table.sql +++ b/src/main/resources/db/migration/V1.2__Create_Organisation_Table.sql @@ -108,17 +108,24 @@ CREATE INDEX idx_organisation_nom_lower ON organisations(LOWER(nom)); CREATE INDEX idx_organisation_nom_court_lower ON organisations(LOWER(nom_court)); CREATE INDEX idx_organisation_ville_lower ON organisations(LOWER(ville)); --- Ajout de la colonne organisation_id à la table membres (si elle n'existe pas déjà) +-- Ajout de la colonne organisation_id à la table membres (si la table et la colonne existent) DO $$ BEGIN - IF NOT EXISTS ( - SELECT 1 FROM information_schema.columns - WHERE table_name = 'membres' AND column_name = 'organisation_id' + -- Vérifier d'abord si la table membres existe + IF EXISTS ( + SELECT 1 FROM information_schema.tables + WHERE table_name = 'membres' ) THEN - ALTER TABLE membres ADD COLUMN organisation_id BIGINT; - ALTER TABLE membres ADD CONSTRAINT fk_membre_organisation - FOREIGN KEY (organisation_id) REFERENCES organisations(id); - CREATE INDEX idx_membre_organisation ON membres(organisation_id); + -- Puis vérifier si la colonne organisation_id n'existe pas déjà + IF NOT EXISTS ( + SELECT 1 FROM information_schema.columns + WHERE table_name = 'membres' AND column_name = 'organisation_id' + ) THEN + ALTER TABLE membres ADD COLUMN organisation_id BIGINT; + ALTER TABLE membres ADD CONSTRAINT fk_membre_organisation + FOREIGN KEY (organisation_id) REFERENCES organisations(id); + CREATE INDEX idx_membre_organisation ON membres(organisation_id); + END IF; END IF; END $$;