Gather data
This commit is contained in:
38
tools/hint/dbjsonl.sh
Executable file
38
tools/hint/dbjsonl.sh
Executable file
@@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
# Usage:
|
||||
# ./import_jsonl.sh "postgresql://user:pass@host:5432/dbname" gloss doc /path/to/file.jsonl
|
||||
#
|
||||
# Notes:
|
||||
# - Creates table if it doesn't exist.
|
||||
# - Inserts each JSON line into a jsonb column.
|
||||
# - Skips blank lines.
|
||||
|
||||
DB_URL="${1:?db url}"
|
||||
TABLE="${2:?table name}"
|
||||
COL="${3:?json column name}"
|
||||
FILE="${4:?jsonl file path}"
|
||||
|
||||
psql "$DB_URL" -v ON_ERROR_STOP=1 <<SQL
|
||||
CREATE TABLE IF NOT EXISTS ${TABLE} (
|
||||
id bigserial PRIMARY KEY,
|
||||
${COL} jsonb NOT NULL
|
||||
);
|
||||
SQL
|
||||
|
||||
# \copy runs on the client, so we can feed it from a local file.
|
||||
# We copy into a 1-column staging table, then cast to jsonb and insert.
|
||||
psql "$DB_URL" -v ON_ERROR_STOP=1 <<SQL
|
||||
CREATE TEMP TABLE _jsonl_stage(line text);
|
||||
|
||||
\\copy _jsonl_stage(line) FROM '${FILE}' WITH (FORMAT text);
|
||||
|
||||
INSERT INTO ${TABLE}(${COL})
|
||||
SELECT line::jsonb
|
||||
FROM _jsonl_stage
|
||||
WHERE btrim(line) <> '';
|
||||
|
||||
-- optional: show count inserted this run
|
||||
SELECT count(*) AS inserted_now FROM _jsonl_stage WHERE btrim(line) <> '';
|
||||
SQL
|
||||
Reference in New Issue
Block a user