77 lines
2.2 KiB
Bash
Executable File
77 lines
2.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
# create app-readable DB env for eva_memory user
|
|
install -m 700 -o eva_memory -g eva_memory -d /home/eva_memory/.config/eva-memory
|
|
source /etc/eva-memory/connection.env
|
|
cat > /home/eva_memory/.config/eva-memory/db.env <<EOF
|
|
DB_HOST=${DB_HOST}
|
|
DB_PORT=${DB_PORT}
|
|
DB_NAME=${DB_NAME}
|
|
DB_USER=${DB_USER}
|
|
DB_PASS=${DB_PASS}
|
|
EOF
|
|
chown eva_memory:eva_memory /home/eva_memory/.config/eva-memory/db.env
|
|
chmod 600 /home/eva_memory/.config/eva-memory/db.env
|
|
|
|
# unique key for upserts
|
|
su - postgres -c "psql -d eva_memory -v ON_ERROR_STOP=1 -c \"ALTER TABLE mem_items ADD CONSTRAINT mem_items_source_path_uniq UNIQUE (source_path);\"" || true
|
|
|
|
# importer script (runs as eva_memory)
|
|
install -m 700 -o eva_memory -g eva_memory -d /home/eva_memory/bin
|
|
cat > /home/eva_memory/bin/import-memory-jsonl.sh <<'EOF'
|
|
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
source /home/eva_memory/.config/eva-memory/db.env
|
|
export PGPASSWORD="$DB_PASS"
|
|
|
|
JSONL="${1:-}"
|
|
if [[ -z "$JSONL" || ! -f "$JSONL" ]]; then
|
|
echo "usage: $0 /path/to/memory.jsonl" >&2
|
|
exit 1
|
|
fi
|
|
|
|
while IFS= read -r line; do
|
|
[[ -z "$line" ]] && continue
|
|
source_path=$(python3 - <<'PY' "$line"
|
|
import json,sys
|
|
print(json.loads(sys.argv[1])["source_path"])
|
|
PY
|
|
)
|
|
title=$(python3 - <<'PY' "$line"
|
|
import json,sys
|
|
print(json.loads(sys.argv[1]).get("title",""))
|
|
PY
|
|
)
|
|
item_type=$(python3 - <<'PY' "$line"
|
|
import json,sys
|
|
print(json.loads(sys.argv[1]).get("item_type","note"))
|
|
PY
|
|
)
|
|
content=$(python3 - <<'PY' "$line"
|
|
import json,sys
|
|
print(json.loads(sys.argv[1])["content"])
|
|
PY
|
|
)
|
|
|
|
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -v ON_ERROR_STOP=1 \
|
|
-v v_source="$source_path" -v v_title="$title" -v v_type="$item_type" -v v_content="$content" <<'SQL'
|
|
INSERT INTO mem_items (item_type, title, content, source_path, updated_at)
|
|
VALUES (:'v_type', NULLIF(:'v_title',''), :'v_content', :'v_source', now())
|
|
ON CONFLICT (source_path)
|
|
DO UPDATE SET
|
|
item_type = EXCLUDED.item_type,
|
|
title = EXCLUDED.title,
|
|
content = EXCLUDED.content,
|
|
updated_at = now();
|
|
SQL
|
|
|
|
done < "$JSONL"
|
|
|
|
echo "import done"
|
|
EOF
|
|
chown eva_memory:eva_memory /home/eva_memory/bin/import-memory-jsonl.sh
|
|
chmod 700 /home/eva_memory/bin/import-memory-jsonl.sh
|
|
|
|
echo "REMOTE_MEMORY_SETUP_OK"
|