|
15 | 15 | from .headers.specific_fixes import specific_fixes |
16 | 16 | from .headers.value_fixes import value_fixes |
17 | 17 | from .io.local import write_local |
18 | | -from .io.postgres import PostgresNotConfiguredError, write_postgres |
| 18 | +from .io.postgres import PostgresNotConfiguredError, push_csv_to_postgres |
19 | 19 | from .logging_config import setup_logging |
20 | 20 | from .sheet_config import SheetConfig |
21 | 21 | from .stats import compute_header_stats, write_stats_report |
@@ -299,15 +299,36 @@ def build(checkpoint, mapping_path, strict, output, output_dir): |
299 | 299 | f'{len(unresolved)} unresolved headers remain - refusing to build in --strict mode' |
300 | 300 | ) |
301 | 301 | targets = [t.strip() for t in output.split(',') if t.strip()] |
302 | | - if 'local' in targets: |
| 302 | + # Postgres is pushed from the local CSV (via psql \copy - see push_csv_to_postgres), so the |
| 303 | + # local write always happens first when postgres is requested, even if 'local' wasn't asked |
| 304 | + # for explicitly. |
| 305 | + if 'local' in targets or 'postgres' in targets: |
303 | 306 | write_local(resolved_rows, output_dir) |
304 | 307 | click.echo(f'Wrote local output to {output_dir}') |
305 | 308 | if 'postgres' in targets: |
| 309 | + csv_path = Path(output_dir) / 'res_1.csv' |
306 | 310 | try: |
307 | | - write_postgres(resolved_rows) |
| 311 | + push_csv_to_postgres(csv_path) |
308 | 312 | except PostgresNotConfiguredError as e: |
309 | 313 | raise click.ClickException(str(e)) |
310 | | - click.echo('Wrote to Postgres table lamas_muni') |
| 314 | + click.echo('Pushed to Postgres table lamas_muni') |
| 315 | + |
| 316 | + |
| 317 | +@main.command('push-postgres') |
| 318 | +@click.option('--csv', 'csv_path', type=click.Path(exists=True), default=str(DEFAULT_OUTPUT_DIR / 'res_1.csv')) |
| 319 | +@click.option('--table', default='lamas_muni') |
| 320 | +@click.option('--truncate/--no-truncate', default=True, help='Truncate the table before copying in (default: on).') |
| 321 | +def push_postgres(csv_path, table, truncate): |
| 322 | + """Push an already-built CSV file (see `lamas build --output local`) into Postgres via `psql \\copy`. |
| 323 | +
|
| 324 | + Reads the connection string from the DATAFLOWS_DB_ENGINE environment variable. This is a |
| 325 | + shared-system, destructive write (truncates the table by default) - never run automatically. |
| 326 | + """ |
| 327 | + try: |
| 328 | + push_csv_to_postgres(csv_path, table=table, truncate=truncate) |
| 329 | + except PostgresNotConfiguredError as e: |
| 330 | + raise click.ClickException(str(e)) |
| 331 | + click.echo(f'Pushed {csv_path} -> Postgres table {table!r}') |
311 | 332 |
|
312 | 333 |
|
313 | 334 | @main.command('full-run') |
|
0 commit comments