Commit 8bf68261 authored by Kirill Smelkov's avatar Kirill Smelkov

*: Factor opening sqlite db into separate function

Currently it is just sqlite3.connect(), but we'll want to add connection
autotuning in the next patch, so before that let's consolidate all
sqlite opening places in the code into one.
parent ef138eeb
......@@ -37,6 +37,7 @@ import sqlite3
from slapos.cli.config import ConfigCommand
from slapos.proxy import ProxyConfig
from slapos.proxy.db_version import DB_VERSION
from slapos.util import sqlite_connect
class ProxyShowCommand(ConfigCommand):
......@@ -191,7 +192,7 @@ def log_network(logger, conn):
def do_show(conf):
conf.logger.debug('Using database: %s', conf.database_uri)
conn = sqlite3.connect(conf.database_uri)
conn = sqlite_connect(conf.database_uri)
conn.row_factory = sqlite3.Row
conn.create_function('md5', 1, lambda s: hashlib.md5(s).hexdigest())
......
......@@ -27,11 +27,12 @@
#
##############################################################################
import sqlite3
import os
from time import strftime
import datetime
from slapos.util import sqlite_connect
class Database:
database_name = "collector.db"
......@@ -125,7 +126,7 @@ class Database:
self._bootstrap()
def connect(self):
self.connection = sqlite3.connect(self.uri)
self.connection = sqlite_connect(self.uri)
self.cursor = self.connection.cursor()
def commit(self):
......
......@@ -30,12 +30,12 @@
from lxml import etree
import random
import sqlite3
import string
from slapos.slap.slap import Computer, ComputerPartition, \
SoftwareRelease, SoftwareInstance, NotFoundError
from slapos.proxy.db_version import DB_VERSION
import slapos.slap
from slapos.util import sqlite_connect
from flask import g, Flask, request, abort
import xml_marshaller
......@@ -136,7 +136,7 @@ def execute_db(table, query, args=(), one=False, db_version=None, log=False, db=
def connect_db():
return sqlite3.connect(app.config['DATABASE_URI'])
return sqlite_connect(app.config['DATABASE_URI'])
def _getTableList():
return g.db.execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY Name").fetchall()
......
......@@ -45,6 +45,7 @@ import slapos.proxy
import slapos.proxy.views as views
import slapos.slap
import slapos.slap.slap
from slapos.util import sqlite_connect
import sqlite3
import pkg_resources
......@@ -904,7 +905,7 @@ class TestMultiMasterSupport(MasterMixin):
super(TestMultiMasterSupport, self).setUp()
self.db = sqlite3.connect(self.proxy_db)
self.db = sqlite_connect(self.proxy_db)
self.external_slapproxy_configuration_file_location = os.path.join(
self._tempdir, 'external_slapos.cfg')
self.createExternalProxyConfigurationFile()
......@@ -1181,7 +1182,7 @@ class TestMigrateVersion10To11(TestInformation, TestRequest, TestSlaveRequest, T
super(TestMigrateVersion10To11, self).setUp()
schema = pkg_resources.resource_stream('slapos.tests.slapproxy', 'database_dump_version_10.sql')
schema = schema.read() % dict(version='11')
self.db = sqlite3.connect(self.proxy_db)
self.db = sqlite_connect(self.proxy_db)
self.db.cursor().executescript(schema)
self.db.commit()
......
......@@ -30,6 +30,7 @@
import errno
import os
import subprocess
import sqlite3
def mkdir_p(path, mode=0o700):
......@@ -94,3 +95,8 @@ def string_to_boolean(string):
return False
else:
raise ValueError('%s is neither True nor False.' % string)
def sqlite_connect(dburi):
conn = sqlite3.connect(dburi)
return conn
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment