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.
This commit is contained in:
dahoud
2025-12-12 18:00:44 +00:00
parent 5ba96ce33c
commit 6aceceff81

View File

@@ -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_nom_court_lower ON organisations(LOWER(nom_court));
CREATE INDEX idx_organisation_ville_lower ON organisations(LOWER(ville)); 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 $$ DO $$
BEGIN BEGIN
IF NOT EXISTS ( -- Vérifier d'abord si la table membres existe
SELECT 1 FROM information_schema.columns IF EXISTS (
WHERE table_name = 'membres' AND column_name = 'organisation_id' SELECT 1 FROM information_schema.tables
WHERE table_name = 'membres'
) THEN ) THEN
ALTER TABLE membres ADD COLUMN organisation_id BIGINT; -- Puis vérifier si la colonne organisation_id n'existe pas déjà
ALTER TABLE membres ADD CONSTRAINT fk_membre_organisation IF NOT EXISTS (
FOREIGN KEY (organisation_id) REFERENCES organisations(id); SELECT 1 FROM information_schema.columns
CREATE INDEX idx_membre_organisation ON membres(organisation_id); 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 IF;
END $$; END $$;