Skip to content

re-implement import-sql using Python #378

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

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix hashing, docs
  • Loading branch information
nyurik committed Oct 2, 2021
commit 7671ed20248245f84611a12cae6588a80c6d4bc5
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,9 @@ import-borders load borders.csv # Load borders.csv into a table
This utility requires PostgreSQL's PG* environment variables, and optionally uses `PBF_DATA_DIR, BORDERS_PBF_FILE, BORDERS_CSV_FILE, BORDERS_TABLE_NAME`.

## Importing into Postgres
The `import-sql` script can execute a single SQL file in Postgres when the file is given as the first parameter.
The `run-psql` helper script will run psql with one or more files provided. It can also run them in parallel. In case of an error, `run-psql` will not start any new files, but will wait for all started files to finish. Once finished, it will re-print the last few lines of STDERR to simplify debugging. Unlike legacy `import-sql`, `run-psql` does not make any assumptions about SQL directories.

The legacy `import-sql` script can execute a single SQL file in Postgres when the file is given as the first parameter. The script is obsolete and should not be used.

If ran without any arguments, `import-sql` executes all of the following:
* SQL files from `$SQL_TOOLS_DIR` - contains all SQL files to be imported before the ones generated by OpenMapTiles project. By default, contains OMT fork of the Mapbox's [postgis-vt-util.sql](https://github.com/openmaptiles/postgis-vt-util) helper functions and contains the [sql/language.sql](sql/zzz_language.sql) script. (as `10_postgis-vt-util.sql` - the file name has to be imported before files in `/sql`)
Expand All @@ -438,7 +440,7 @@ If ran without any arguments, `import-sql` executes all of the following:
Generating and importing SQL could be done in a single step with `&&`, e.g.

```bash
generate-sqltomvt openmaptiles.yaml > "$SQL_DIR/mvt.sql" && import-sql
generate-sqltomvt openmaptiles.yaml > "$SQL_DIR/mvt.sql" && run-psql "$SQL_DIR/mvt.sql"
```

Optionally you may pass extra arguments to `psql` by using `PSQL_OPTIONS` environment variable. For example `PSQL_OPTIONS=-a` makes psql echo all commands read from a file into stdout.
Expand Down
4 changes: 2 additions & 2 deletions bin/run-psql
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ def run_psql(file: Path, params: Params):
'PGPORT': params.pgport,
}

sql_content = file.read_text()
sql_content = file.read_bytes()
md5sum = md5(sql_content).hexdigest()
lines = sql_content.count('\n')
lines = sql_content.decode('utf-8').count('\n')
print(f'Importing {file} (md5 {md5sum} {lines} lines) into Postgres...')
if params.verbose:
print(f' {list2cmdline(cmd)}')
Expand Down