Skip to content
This repository
Browse code

Refactor. Model schema moved to assets_helper.py from db.py and queri…

…es.py.
  • Loading branch information...
commit 3baafdd58fcb2739dc8c501d80bf03720cc23af4 1 parent 83900b5
Marton Somogyi authored March 01, 2013
7  assets_helper.py
@@ -2,6 +2,11 @@
2 2
 import queries
3 3
 import datetime
4 4
 
  5
+FIELDS = ["asset_id", "name", "uri", "start_date",
  6
+          "end_date", "duration", "mimetype", "is_enabled", "nocache"]
  7
+
  8
+create_assets_table = 'CREATE TABLE assets(asset_id text primary key, name text, uri text, md5 text, start_date timestamp, end_date timestamp, duration text, mimetype text, is_enabled integer default 0, nocache integer default 0)'
  9
+
5 10
 get_time = datetime.datetime.utcnow
6 11
 
7 12
 
@@ -49,7 +54,7 @@ def create(conn, asset):
49 54
     return asset
50 55
 
51 56
 
52  
-def read(conn, asset_id=None, keys=db.FIELDS):
  57
+def read(conn, asset_id=None, keys=FIELDS):
53 58
     """
54 59
     Fetch one or more assets from the database.
55 60
     Returns a list of dicts or one dict.
13  db.py
@@ -3,14 +3,8 @@
3 3
 
4 4
 import queries
5 5
 
6  
-FIELDS = [
7  
-    "asset_id", "name", "uri", "start_date",
8  
-    "end_date", "duration", "mimetype", "is_enabled", "nocache"
9  
-]
10  
-
11 6
 conn = lambda db: sqlite3.connect(db, detect_types=sqlite3.PARSE_DECLTYPES)
12 7
 
13  
-
14 8
 @contextmanager
15 9
 def cursor(connection):
16 10
     cur = connection.cursor()
@@ -24,10 +18,3 @@ def commit(connection):
24 18
     yield cur
25 19
     connection.commit()
26 20
     cur.close()
27  
-
28  
-
29  
-def create_assets_table(cur):
30  
-    try:
31  
-        cur.execute(queries.create_assets_table)
32  
-    except sqlite3.OperationalError as _:
33  
-        pass
15  queries.py
... ...
@@ -1,20 +1,9 @@
1  
-create_assets_table = """
2  
-create table assets(
3  
-asset_id text primary key,
4  
-name text,
5  
-uri text,
6  
-md5 text,
7  
-start_date timestamp,
8  
-end_date timestamp,
9  
-duration text,
10  
-mimetype text,
11  
-is_enabled integer default 0,
12  
-nocache integer default 0)
13  
-"""
14 1
 
15 2
 comma = ','.join
16 3
 quest = lambda l: '=?,'.join(l) + '=?'
17 4
 
  5
+exists_table = "SELECT name FROM sqlite_master WHERE type='table' AND name='assets'"
  6
+
18 7
 read_all = lambda keys: 'select ' + comma(keys) + ' from assets order by name'
19 8
 read = lambda keys: 'select ' + comma(keys) + ' from assets where asset_id=?'
20 9
 create = lambda keys: 'insert into assets (' + comma(keys) + ') values (' + comma(['?'] * len(keys)) + ')'
5  server.py
@@ -26,6 +26,7 @@
26 26
 from bottlehaml import haml_template
27 27
 
28 28
 import db
  29
+import queries
29 30
 import assets_helper
30 31
 
31 32
 from utils import json_dump
@@ -354,7 +355,9 @@ def static(path):
354 355
         global db_conn
355 356
         db_conn = conn
356 357
         with db.cursor(db_conn) as c:
357  
-            db.create_assets_table(c)
  358
+            c.execute(queries.exists_table)
  359
+            if c.fetchone() is None:
  360
+                c.execute(assets_helper.create_assets_table)
358 361
         run(host=settings.get_listen_ip(),
359 362
             port=settings.get_listen_port(),
360 363
             reloader=True)
3  server_test.py
@@ -7,7 +7,6 @@
7 7
 
8 8
 import assets_helper
9 9
 import db
10  
-import queries
11 10
 
12 11
 # fixtures chronology
13 12
 #
@@ -65,7 +64,7 @@ def setUp(self):
65 64
         self.assertEmpty = functools.partial(self.assertEqual, [])
66 65
         self.conn = db.conn(':memory:')
67 66
         with db.commit(self.conn) as cursor:
68  
-            cursor.execute(queries.create_assets_table)
  67
+            cursor.execute(assets_helper.create_assets_table)
69 68
 
70 69
     def tearDown(self):
71 70
         self.conn.close()

0 notes on commit 3baafdd

Please sign in to comment.
Something went wrong with that request. Please try again.