-
Notifications
You must be signed in to change notification settings - Fork 14
[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?
Changes from 1 commit
c4fb10c
5707507
4de4a8a
8b13c15
2a409ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,7 +59,43 @@ def _send_to_postgres(self): | |
| f"SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name= '{table_name}')" | ||
| ) | ||
| table_exists = conn.execute(table_exists_query).scalar() | ||
|
|
||
| if table_exists: | ||
| conn.execute(text(f"TRUNCATE {table_name}")) | ||
|
|
||
| add_missing_columns_to_sql_table(conn, table_name, df) | ||
| df.write_database(table_name, conn, if_table_exists=if_table_exists) | ||
|
|
||
| def add_missing_columns_to_sql_table(conn, table_name: str, df: pl.DataFrame): | ||
| """Ajoute les colonnes manquantes dans la table SQL à partir du DataFrame Polars.""" | ||
|
|
||
| schema = df.schema | ||
| columns_sql = conn.execute( | ||
| text(f""" | ||
| SELECT column_name FROM information_schema.columns | ||
| WHERE table_name = '{table_name}' | ||
| """) | ||
| ).fetchall() | ||
| existing_cols = {col[0] for col in columns_sql} | ||
|
|
||
| missing_cols = set(schema.keys()) - existing_cols | ||
| if not missing_cols: | ||
| return | ||
|
|
||
| # Mapping Polars -> SQL | ||
| type_mapping = { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| pl.Int64: "BIGINT", | ||
| pl.Int32: "INTEGER", | ||
| pl.Float64: "DOUBLE PRECISION", | ||
| pl.Float32: "REAL", | ||
| pl.Boolean: "BOOLEAN", | ||
| pl.Utf8: "TEXT", | ||
| pl.Date: "DATE", | ||
| pl.Datetime: "TIMESTAMP", | ||
| } | ||
|
|
||
| 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.
Tu savais que dict.keys() possède une compatibilité partielle avec les set ?
missing_cols = schema.keys() - existing_colsserait suffisant ici.