-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for using SQLite as a DB (#271)
* Implement support for Sqlite DB. Remove unused query method. * Document how to create SQLite DB * DB tests using SQLite * [MegaLinter] Apply linters fixes * Make sqlite-detection (by file-extension) a little more generic * [MegaLinter] Apply linters fixes * Script to convert docker/postgresql DB to sqlite * Create a release with address_principals.sqllite * Fix sqlite output filename * Ignore the sqlite DB from git * Fix a couple of documentation references to SQLite
- Loading branch information
Showing
8 changed files
with
237 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
cache | ||
code/__pycache__ | ||
megalinter-reports/ | ||
address_principals.sqlite |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/bin/bash | ||
|
||
set -ex | ||
|
||
# Extract CSV from the DB if we don't have it already. | ||
# It's also available as part of the docker-build process, but this is a bit more flexible. | ||
CSV_FILENAME=address_principals.csv | ||
if [ -f $CSV_FILENAME ]; then | ||
echo "CSV file already exists, skipping extract..." | ||
else | ||
docker run -d --name db --publish=5433:5432 lukeprior/nbn-upgrade-map-db:latest | ||
sleep 5 # it takes a few seconds to be ready | ||
psql -h localhost -p 5433 -U postgres -c 'COPY gnaf_cutdown.address_principals TO stdout WITH CSV HEADER' > $CSV_FILENAME | ||
docker rm -f db | ||
fi | ||
|
||
# Create a new sqlite DB with the contents of the CSV | ||
DB_FILENAME=address_principals.sqlite | ||
if [ -f $DB_FILENAME ]; then | ||
echo "SQLite file $DB_FILENAME already exists, skipping creation..." | ||
else | ||
sqlite3 $DB_FILENAME <<EOF | ||
CREATE TABLE address_principals | ||
( | ||
gnaf_pid text NOT NULL, | ||
address text NOT NULL, | ||
locality_name text NOT NULL, | ||
postcode INTEGER NULL, | ||
state text NOT NULL, | ||
latitude numeric(10,8) NOT NULL, | ||
longitude numeric(11,8) NOT NULL | ||
); | ||
CREATE INDEX address_name_state ON address_principals(locality_name, state); | ||
.mode csv | ||
.import $CSV_FILENAME address_principals | ||
.exit | ||
EOF | ||
|
||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
To create sample data in SQLite use the following process: | ||
|
||
- create empty DB per process described in DB: | ||
|
||
``` | ||
sqlite3 tests/data/sample-addresses.db | ||
-- create table and index per process described in DB | ||
CREATE TABLE address_principals | ||
( | ||
gnaf_pid text NOT NULL, | ||
address text NOT NULL, | ||
locality_name text NOT NULL, | ||
postcode INTEGER NULL, | ||
state text NOT NULL, | ||
latitude numeric(10,8) NOT NULL, | ||
longitude numeric(11,8) NOT NULL | ||
); | ||
CREATE INDEX address_name_state ON address_principals(locality_name, state); | ||
-- attach and import a subset of the data | ||
attach database './extra/db/address_principals.db' as full_db; | ||
INSERT INTO main.address_principals SELECT * FROM full_db.address_principals WHERE locality_name like '%SOMER%' ORDER BY RANDOM() LIMIT 100; | ||
``` | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import os | ||
from argparse import ArgumentParser, Namespace | ||
|
||
import db | ||
|
||
SAMPLE_ADDRESSES_DB_FILE = f"{os.path.dirname(os.path.realpath(__file__))}/data/sample-addresses.sqlite" | ||
|
||
|
||
def test_get_address(): | ||
address_db = db.connect_to_db(Namespace(dbhost=SAMPLE_ADDRESSES_DB_FILE)) | ||
addresses = address_db.get_addresses("SOMERVILLE", "VIC") | ||
assert len(addresses) == 30 | ||
assert addresses[0].name == "83 GUELPH STREET SOMERVILLE 3912" | ||
assert addresses[0].gnaf_pid == "GAVIC421048228" | ||
|
||
|
||
def test_get_counts_by_suburb(): | ||
address_db = db.connect_to_db(Namespace(dbhost=SAMPLE_ADDRESSES_DB_FILE)) | ||
counts = address_db.get_counts_by_suburb() | ||
assert counts["VIC"]["SOMERVILLE"] == 30 | ||
assert counts["VIC"]["SOMERS"] == 10 | ||
assert counts["VIC"]["SOMERTON"] == 1 | ||
assert len(counts["NSW"]) == 2 | ||
assert len(counts["SA"]) == 1 | ||
assert len(counts["TAS"]) == 1 | ||
assert len(counts["WA"]) == 1 | ||
|
||
|
||
def test_get_extents_by_suburb(): | ||
address_db = db.connect_to_db(Namespace(dbhost=SAMPLE_ADDRESSES_DB_FILE)) | ||
extents = address_db.get_extents_by_suburb() | ||
assert extents["VIC"]["SOMERVILLE"] == ( | ||
(-38.23846838, 145.162399), | ||
(-38.21306546, 145.22678832), | ||
) | ||
|
||
|
||
def test_add_db_arguments(): | ||
parser = ArgumentParser() | ||
db.add_db_arguments(parser) | ||
args = parser.parse_args([]) | ||
assert args.dbuser == "postgres" | ||
assert args.dbpassword == "password" | ||
assert args.dbhost == "localhost" | ||
assert args.dbport == "5433" | ||
assert args.create_index | ||
|
||
|
||
# TODO: test postgres with mocks |