Skip to content

Commit

Permalink
fixed migrations to add feeback type
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyjacquelin committed Dec 17, 2024
1 parent 3a95688 commit 5e76dc3
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 15 deletions.
85 changes: 73 additions & 12 deletions api/alembic/versions/2592138760af_added_fields_to_feeback.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,85 @@


def upgrade() -> None:
# Create Enum types explicitly first in the database
op.execute("CREATE TYPE feedbacktype AS ENUM ('chat', 'evaluations')")
op.execute("CREATE TYPE feedbackpositives AS ENUM ('clair', 'synthetique', 'complet', 'sources_fiables')")
op.execute("CREATE TYPE feedbacknegatives AS ENUM ('incorrect', 'incoherent', 'manque_de_sources')")
# 1. Créer les types ENUM s'ils n'existent pas déjà
op.execute("""
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'feedback_type') THEN
CREATE TYPE feedback_type AS ENUM ('chat', 'evaluations');
END IF;
END$$;
""")
op.execute("""
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'feedbackpositives') THEN
CREATE TYPE feedbackpositives AS ENUM ('clair', 'synthetique', 'complet', 'sources_fiables');
END IF;
END$$;
""")
op.execute("""
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'feedbacknegatives') THEN
CREATE TYPE feedbacknegatives AS ENUM ('incorrect', 'incoherent', 'manque_de_sources');
END IF;
END$$;
""")

# ### commands auto generated by Alembic - please adjust! ###
op.add_column('feedbacks', sa.Column('type', sa.Enum('chat', 'evaluations', name='feedbacktype'), nullable=False))
op.add_column('feedbacks', sa.Column('note', sa.Integer(), nullable=True, comment='Note from 0 to 5'))
op.add_column('feedbacks', sa.Column('positives', sa.Enum('clair', 'synthetique', 'complet', 'sources_fiables', name='feedbackpositives'), nullable=True))
op.add_column('feedbacks', sa.Column('negatives', sa.Enum('incorrect', 'incoherent', 'manque_de_sources', name='feedbacknegatives'), nullable=True))
# ### end Alembic commands ###
# 2. Ajouter la colonne "type" temporairement comme nullable pour éviter les conflits
op.add_column(
'feedbacks',
sa.Column('type', sa.String(), nullable=True)
)

# 3. Mettre à jour les données pour correspondre aux valeurs ENUM
op.execute("""
UPDATE feedbacks
SET type = 'chat'
WHERE type IS NULL OR type NOT IN ('chat', 'evaluations');
""")

# 4. Modifier la colonne "type" pour utiliser l'ENUM feedback_type
op.execute("""
ALTER TABLE feedbacks
ALTER COLUMN type TYPE feedback_type
USING type::text::feedback_type;
""")

# 5. Définir la colonne "type" comme NON NULLABLE
op.alter_column('feedbacks', 'type', nullable=False)

# 6. Ajouter les autres colonnes
op.add_column(
'feedbacks',
sa.Column('note', sa.Integer(), nullable=True, comment='Note from 0 to 5')
)
op.add_column(
'feedbacks',
sa.Column(
'positives',
sa.Enum('clair', 'synthetique', 'complet', 'sources_fiables', name='feedbackpositives'),
nullable=True
)
)
op.add_column(
'feedbacks',
sa.Column(
'negatives',
sa.Enum('incorrect', 'incoherent', 'manque_de_sources', name='feedbacknegatives'),
nullable=True
)
)

def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
# Drop columns
op.drop_column('feedbacks', 'negatives')
op.drop_column('feedbacks', 'positives')
op.drop_column('feedbacks', 'note')
op.drop_column('feedbacks', 'type')
# ### end Alembic commands ###

# Drop Enum types
op.execute("DROP TYPE IF EXISTS feedbacknegatives")
op.execute("DROP TYPE IF EXISTS feedbackpositives")
op.execute("DROP TYPE IF EXISTS feedbacktype")
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ def downgrade() -> None:
# Convert 'negatives' back to ENUM with explicit casting
op.execute("""
ALTER TABLE feedbacks
ALTER COLUMN negatives TYPE VARCHAR USING negatives::TEXT
ALTER COLUMN negatives TYPE feedbacknegatives
USING CASE
WHEN negatives::TEXT = 'incorrect' THEN 'incorrect'::feedbacknegatives
WHEN negatives::TEXT = 'incoherent' THEN 'incoherent'::feedbacknegatives
WHEN negatives::TEXT = 'manque_de_sources' THEN 'manque_de_sources'::feedbacknegatives
ELSE NULL
END
""")
op.alter_column('feedbacks', 'negatives',
type_=postgresql.ENUM('incorrect', 'incoherent', 'manque_de_sources', name='feedbacknegatives'),
Expand All @@ -55,7 +61,14 @@ def downgrade() -> None:
# Convert 'positives' back to ENUM with explicit casting
op.execute("""
ALTER TABLE feedbacks
ALTER COLUMN positives TYPE VARCHAR USING positives::TEXT
ALTER COLUMN positives TYPE feedbackpositives
USING CASE
WHEN positives::TEXT = 'clair' THEN 'clair'::feedbackpositives
WHEN positives::TEXT = 'synthetique' THEN 'synthetique'::feedbackpositives
WHEN positives::TEXT = 'complet' THEN 'complet'::feedbackpositives
WHEN positives::TEXT = 'sources_fiables' THEN 'sources_fiables'::feedbackpositives
ELSE NULL
END
""")
op.alter_column('feedbacks', 'positives',
type_=postgresql.ENUM('clair', 'synthetique', 'complet', 'sources_fiables', name='feedbackpositives'),
Expand Down
2 changes: 1 addition & 1 deletion api/app/models/feedback.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Feedback(Base):
created_at = Column(DateTime, server_default=func.now())
updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now())

type = Column(SQLAlchemyEnum(FeedbackType, name="feedback_type"), nullable=False)
type = Column(SQLAlchemyEnum(FeedbackType, name="feedback_type"), nullable=False, default=FeedbackType.chat)
note = Column(Integer, nullable=True, default=0, comment="Note from 0 to 5")
positives = Column(JSON, nullable=True, comment="List of positive feedback values")
negatives = Column(JSON, nullable=True, comment="List of negative feedback values")
Expand Down

0 comments on commit 5e76dc3

Please sign in to comment.