Permalink
Browse files

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

…es.py.
  • Loading branch information...
1 parent 83900b5 commit 3baafdd58fcb2739dc8c501d80bf03720cc23af4 @mcbuddha mcbuddha committed Mar 1, 2013
Showing with 13 additions and 30 deletions.
  1. +6 −1 assets_helper.py
  2. +0 −13 db.py
  3. +2 −13 queries.py
  4. +4 −1 server.py
  5. +1 −2 server_test.py
View
@@ -2,6 +2,11 @@
import queries
import datetime
+FIELDS = ["asset_id", "name", "uri", "start_date",
+ "end_date", "duration", "mimetype", "is_enabled", "nocache"]
+
+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)'
+
get_time = datetime.datetime.utcnow
@@ -49,7 +54,7 @@ def create(conn, asset):
return asset
-def read(conn, asset_id=None, keys=db.FIELDS):
+def read(conn, asset_id=None, keys=FIELDS):
"""
Fetch one or more assets from the database.
Returns a list of dicts or one dict.
View
@@ -3,14 +3,8 @@
import queries
-FIELDS = [
- "asset_id", "name", "uri", "start_date",
- "end_date", "duration", "mimetype", "is_enabled", "nocache"
-]
-
conn = lambda db: sqlite3.connect(db, detect_types=sqlite3.PARSE_DECLTYPES)
-
@contextmanager
def cursor(connection):
cur = connection.cursor()
@@ -24,10 +18,3 @@ def commit(connection):
yield cur
connection.commit()
cur.close()
-
-
-def create_assets_table(cur):
- try:
- cur.execute(queries.create_assets_table)
- except sqlite3.OperationalError as _:
- pass
View
@@ -1,20 +1,9 @@
-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)
-"""
comma = ','.join
quest = lambda l: '=?,'.join(l) + '=?'
+exists_table = "SELECT name FROM sqlite_master WHERE type='table' AND name='assets'"
+
read_all = lambda keys: 'select ' + comma(keys) + ' from assets order by name'
read = lambda keys: 'select ' + comma(keys) + ' from assets where asset_id=?'
create = lambda keys: 'insert into assets (' + comma(keys) + ') values (' + comma(['?'] * len(keys)) + ')'
View
@@ -26,6 +26,7 @@
from bottlehaml import haml_template
import db
+import queries
import assets_helper
from utils import json_dump
@@ -354,7 +355,9 @@ def static(path):
global db_conn
db_conn = conn
with db.cursor(db_conn) as c:
- db.create_assets_table(c)
+ c.execute(queries.exists_table)
+ if c.fetchone() is None:
+ c.execute(assets_helper.create_assets_table)
run(host=settings.get_listen_ip(),
port=settings.get_listen_port(),
reloader=True)
View
@@ -7,7 +7,6 @@
import assets_helper
import db
-import queries
# fixtures chronology
#
@@ -65,7 +64,7 @@ def setUp(self):
self.assertEmpty = functools.partial(self.assertEqual, [])
self.conn = db.conn(':memory:')
with db.commit(self.conn) as cursor:
- cursor.execute(queries.create_assets_table)
+ cursor.execute(assets_helper.create_assets_table)
def tearDown(self):
self.conn.close()

0 comments on commit 3baafdd

Please sign in to comment.