|
|
@@ -2,6 +2,7 @@ import logging
|
|
|
import sqlite3
|
|
|
|
|
|
import config
|
|
|
+import db_schema
|
|
|
|
|
|
class connect:
|
|
|
conn = None
|
|
|
@@ -31,42 +32,42 @@ class connect:
|
|
|
|
|
|
return res
|
|
|
|
|
|
+def _get_version(conn):
|
|
|
+ try:
|
|
|
+ v = conn.select('select value from _metadata where key="schema_version"')
|
|
|
+ if v:
|
|
|
+ return int(v[0])
|
|
|
+ else:
|
|
|
+ return -1
|
|
|
+
|
|
|
+ except sqlite3.OperationalError as e:
|
|
|
+ if 'no such table' in str(e):
|
|
|
+ conn.execute('''
|
|
|
+ create table _metadata (
|
|
|
+ key varchar(255) not null primary key,
|
|
|
+ value varchar(1023) not null
|
|
|
+ )
|
|
|
+ ''')
|
|
|
+
|
|
|
+ return -1
|
|
|
+
|
|
|
+ else:
|
|
|
+ raise
|
|
|
+
|
|
|
def init():
|
|
|
- logging.info('Initializing database at %s...', config.DB_PATH)
|
|
|
+ print('Initializing database at %s...' % config.DB_PATH)
|
|
|
|
|
|
with connect() as conn:
|
|
|
- # conn.execute('drop table if exists localities;')
|
|
|
- conn.execute('''
|
|
|
- create table if not exists localities (
|
|
|
- id integer not null primary key,
|
|
|
- name varchar(255) not null
|
|
|
- );
|
|
|
- ''')
|
|
|
-
|
|
|
- # conn.execute('drop table if exists locality_relationships;')
|
|
|
- conn.execute('''
|
|
|
- create table if not exists locality_relationships (
|
|
|
- id varchar(255) not null primary key,
|
|
|
- parent integer not null,
|
|
|
- child integer not null
|
|
|
- )
|
|
|
- ''')
|
|
|
-
|
|
|
- conn.execute('''
|
|
|
- create table if not exists search_results (
|
|
|
- id integer not null primary key,
|
|
|
- time datetime not null,
|
|
|
- property_id integer not null,
|
|
|
- raw_json text not null
|
|
|
- )
|
|
|
- ''')
|
|
|
-
|
|
|
- conn.execute('''
|
|
|
- create table if not exists properties (
|
|
|
- id integer not null primary key,
|
|
|
- last_update datetime not null,
|
|
|
- raw_json text not null
|
|
|
- )
|
|
|
- ''')
|
|
|
+ version = _get_version(conn)
|
|
|
+
|
|
|
+ if version+1 < len(db_schema.SCHEMA_STATEMENTS):
|
|
|
+ print('Upgrading db schema from %i to %i...' % (version, len(db_schema.SCHEMA_STATEMENTS)-1))
|
|
|
+
|
|
|
+ while version+1 < len(db_schema.SCHEMA_STATEMENTS):
|
|
|
+ version += 1
|
|
|
+ stmt = db_schema.SCHEMA_STATEMENTS[version]
|
|
|
+
|
|
|
+ conn.execute(stmt)
|
|
|
+ conn.execute('replace into _metadata (key,value) values (?, ?)', 'schema_version', version)
|
|
|
|
|
|
init()
|