#!/usr/bin/env bash # Update a field on a star system record in systems.db. # Automatically detects which table the field belongs to. # # Usage: atlas-update-field "GJ 687" settlement_wave wave_3 # atlas-update-field "GJ 53A" star_type G set -euo pipefail DB="$(cd "$(dirname "$0")/../server/data" && pwd)/systems.db" if [[ $# -ne 3 ]]; then echo "Usage: atlas-update-field SYSTEM_ID FIELD VALUE" >&2 exit 1 fi SYS="$1" FIELD="$2" VALUE="$3" python3 -c " import sqlite3, sys FIELD_TABLE = { # star_systems 'star_type': 'star_systems', 'spectral_class': 'star_systems', 'proper_name': 'star_systems', 'geographic_sector': 'star_systems', 'habitable_planet_count': 'star_systems', 'inhabited_planet_count': 'star_systems', # system_gates 'gate_topology': 'system_gates', 'aperture_count': 'system_gates', 'hop_distance_from_gateway': 'system_gates', # system_history 'settlement_wave': 'system_history', 'founding_culture_primary': 'system_history', 'founding_culture_secondary': 'system_history', } field = sys.argv[1] value = sys.argv[2] system_id = sys.argv[3] if field not in FIELD_TABLE: print(f'ERROR: field \"{field}\" not in whitelist', file=sys.stderr) print(f'Allowed: {sorted(FIELD_TABLE.keys())}', file=sys.stderr) sys.exit(1) table = FIELD_TABLE[field] conn = sqlite3.connect(sys.argv[4]) cur = conn.cursor() # Ensure row exists in target table (history/gates may not have a row yet) if table != 'star_systems': cur.execute(f'INSERT OR IGNORE INTO {table} (system_id) VALUES (?)', (system_id,)) cur.execute(f'UPDATE {table} SET {field} = ? WHERE system_id = ?', (value, system_id)) print(f'updated {cur.rowcount} row(s): {system_id}.{field} = {value} ({table})') conn.commit() conn.close() " "$FIELD" "$VALUE" "$SYS" "$DB"