Skip to content

Commit

Permalink
location is importing in psql
Browse files Browse the repository at this point in the history
  • Loading branch information
despiegk committed Feb 6, 2025
1 parent 048c72b commit 6bdc4a5
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 183 deletions.
2 changes: 2 additions & 0 deletions docker/postgresql/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ services:
db:
image: 'postgres:17.2-alpine3.21'
restart: always
ports:
- 5432:5432
environment:
POSTGRES_PASSWORD: 1234
networks:
Expand Down
5 changes: 5 additions & 0 deletions docker/postgresql/start.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$SCRIPT_DIR"

# Stop any existing containers and remove them
docker compose down

# Start the services in detached mode
docker compose up -d

echo "PostgreSQL is ready"
Binary file added examples/data/location/location_example
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/virt/docker/ai_web_ui/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

- make docker build (see docker_ubuntu example)
- start from docker_ubuntu
- for build use our vlanf approach (example see docker_ubuntu, make sure we have our zinit & ssh working)
- for build use our vlang approach (example see docker_ubuntu, make sure we have our zinit & ssh working)
- install the web UI: openwebui (not by docker but use uv to install this software)
- use https://github.com/astral-sh/uv for the python part
- as last step, clean it all up (remove apt cache, ...)
Expand Down
57 changes: 50 additions & 7 deletions lib/data/location/db.v
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module location

import db.sqlite
import db.pg
import os
import encoding.csv
import freeflowuniverse.herolib.osal
Expand All @@ -9,15 +9,56 @@ import freeflowuniverse.herolib.core.pathlib
// LocationDB handles all database operations for locations
pub struct LocationDB {
mut:
db sqlite.DB
db pg.DB
tmp_dir pathlib.Path
db_dir pathlib.Path
}

// new_location_db creates a new LocationDB instance
pub fn new_location_db(reset bool) !LocationDB {
mut db_dir := pathlib.get_dir(path:'${os.home_dir()}/hero/var/db/location.db',create: true)!
db := sqlite.connect("${db_dir.path}/locations.db")!

// PostgreSQL connection parameters with defaults
mut host := os.getenv('POSTGRES_HOST')
if host == '' {
host = 'localhost'
}
port := os.getenv('POSTGRES_PORT')
port_num := if port == '' { 5432 } else { port.int() }
mut user := os.getenv('POSTGRES_USER')
if user == '' {
user = 'postgres'
}
mut password := os.getenv('POSTGRES_PASSWORD')
if password == '' {
password = '1234'
}
mut dbname := os.getenv('POSTGRES_DB')
if dbname == '' {
dbname = 'locations'
}

// First try to connect to create the database if it doesn't exist
mut init_db := pg.connect(
host: host
port: port_num
user: user
password: password
dbname: 'postgres'
) or { return error('Failed to connect to PostgreSQL: ${err}') }

init_db.exec("CREATE DATABASE ${dbname}") or {}
init_db.close()

// Now connect to our database
db := pg.connect(
host: host
port: port_num
user: user
password: password
dbname: dbname
) or { return error('Failed to connect to PostgreSQL: ${err}') }

mut loc_db := LocationDB{
db: db
tmp_dir: pathlib.get_dir(path: '/tmp/location/',create: true)!
Expand All @@ -30,9 +71,11 @@ pub fn new_location_db(reset bool) !LocationDB {
// init_tables drops and recreates all tables
fn (mut l LocationDB) init_tables(reset bool) ! {
if reset {
l.db.exec('DROP TABLE IF EXISTS AlternateName')!
l.db.exec('DROP TABLE IF EXISTS City')!
l.db.exec('DROP TABLE IF EXISTS Country')!
sql l.db {
drop table AlternateName
drop table City
drop table Country
}!
}

sql l.db {
Expand All @@ -49,5 +92,5 @@ fn (mut l LocationDB) init_tables(reset bool) ! {

// close closes the database connection
pub fn (mut l LocationDB) close() ! {
l.db.close() or { return err }
l.db.close()
}
2 changes: 1 addition & 1 deletion lib/data/location/models.v
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub:
feature_class string @[max_len: 1] // For filtering (P for populated places)
feature_code string @[max_len: 10] // Detailed type (PPL, PPLA, etc.)
search_priority int
accuracy u8 = 1 //1=estimated, 4=geonameid, 6=centroid of addresses or shape
accuracy i16 = 1 //1=estimated, 4=geonameid, 6=centroid of addresses or shape
}

pub struct AlternateName {
Expand Down
5 changes: 5 additions & 0 deletions lib/data/location/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


make sure to do

brew install libpq
Loading

0 comments on commit 6bdc4a5

Please sign in to comment.