-
Notifications
You must be signed in to change notification settings - Fork 11
[BACK] pb de nouvelle colonne dans la database #303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merci !
J'ai mis quelques remarques pas bloquantes.
).fetchall() | ||
existing_cols = {col[0] for col in columns_sql} | ||
|
||
missing_cols = set(schema.keys()) - existing_cols |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tu savais que dict.keys() possède une compatibilité partielle avec les set ?
missing_cols = schema.keys() - existing_cols
serait suffisant ici.
return | ||
|
||
# Mapping Polars -> SQL | ||
type_mapping = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Il y a un bout de code qui a l'air de faire l'inverse dans Historisateur.convert_types_for_sql
.
Ce dictionnaire pourrait être utile ailleurs dans le code, je le sortirai de la méthode pour en créer une constante.
for col in missing_cols: | ||
pl_type = schema[col] | ||
sql_type = type_mapping.get(pl_type, "TEXT") | ||
sql = text(f'ALTER TABLE "{table_name}" ADD COLUMN "{col}" {sql_type};') | ||
conn.execute(sql) | ||
conn.commit() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C'est bon sur le principe, idéalement il faudrait réduire les appels à la db en concaténant les requêtes pour n'en faire qu'une seule.
Peut-être quelque chose comme ça ?
add_columns = [… for col in missing_cols]
sql = text(f'ALTER TABLE "{table_name}" {", ".join(add_columns)};)
conn.execute(sql)
conn.commit()
No description provided.