Prechádzať zdrojové kódy

new schema update method

Martin 8 rokov pred
rodič
commit
c74b5738ca
2 zmenil súbory, kde vykonal 76 pridanie a 34 odobranie
  1. 35 34
      db.py
  2. 41 0
      db_schema.py

+ 35 - 34
db.py

@@ -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()

+ 41 - 0
db_schema.py

@@ -0,0 +1,41 @@
+SCHEMA_STATEMENTS = [
+    '''
+    create table if not exists localities (
+        id integer not null primary key,
+        name varchar(255) not null
+    );
+    ''',
+
+    '''
+    create table if not exists locality_relationships (
+        id varchar(255) not null primary key,
+        parent integer not null,
+        child integer not null
+    )
+    ''',
+
+    '''
+    create table if not exists search_results (
+        id integer not null primary key,
+        time datetime not null,
+        property_id integer not null,
+        price integer not null,
+        raw_json text not null
+    )
+    ''',
+
+    '''
+    create table if not exists properties (
+        id integer not null primary key,
+        last_update datetime not null,
+        raw_json text not null
+    )
+    ''',
+
+    'alter table properties add column processed tinyint default 0 not null;',
+    'alter table properties add column price integer;',
+    'alter table properties add column lat float;',
+    'alter table properties add column lon float;',
+    'alter table properties add column size;',
+    'alter table properties add column ber varchar(255);',
+]