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 ...@@ -37,6 +37,7 @@ import sqlite3
from slapos.cli.config import ConfigCommand from slapos.cli.config import ConfigCommand
from slapos.proxy import ProxyConfig from slapos.proxy import ProxyConfig
from slapos.proxy.db_version import DB_VERSION from slapos.proxy.db_version import DB_VERSION
from slapos.util import sqlite_connect
class ProxyShowCommand(ConfigCommand): class ProxyShowCommand(ConfigCommand):
...@@ -191,7 +192,7 @@ def log_network(logger, conn): ...@@ -191,7 +192,7 @@ def log_network(logger, conn):
def do_show(conf): def do_show(conf):
conf.logger.debug('Using database: %s', conf.database_uri) 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.row_factory = sqlite3.Row
conn.create_function('md5', 1, lambda s: hashlib.md5(s).hexdigest()) conn.create_function('md5', 1, lambda s: hashlib.md5(s).hexdigest())
......
...@@ -27,11 +27,12 @@ ...@@ -27,11 +27,12 @@
# #
############################################################################## ##############################################################################
import sqlite3
import os import os
from time import strftime from time import strftime
import datetime import datetime
from slapos.util import sqlite_connect
class Database: class Database:
database_name = "collector.db" database_name = "collector.db"
...@@ -125,7 +126,7 @@ class Database: ...@@ -125,7 +126,7 @@ class Database:
self._bootstrap() self._bootstrap()
def connect(self): def connect(self):
self.connection = sqlite3.connect(self.uri) self.connection = sqlite_connect(self.uri)
self.cursor = self.connection.cursor() self.cursor = self.connection.cursor()
def commit(self): def commit(self):
......
...@@ -30,12 +30,12 @@ ...@@ -30,12 +30,12 @@
from lxml import etree from lxml import etree
import random import random
import sqlite3
import string import string
from slapos.slap.slap import Computer, ComputerPartition, \ from slapos.slap.slap import Computer, ComputerPartition, \
SoftwareRelease, SoftwareInstance, NotFoundError SoftwareRelease, SoftwareInstance, NotFoundError
from slapos.proxy.db_version import DB_VERSION from slapos.proxy.db_version import DB_VERSION
import slapos.slap import slapos.slap
from slapos.util import sqlite_connect
from flask import g, Flask, request, abort from flask import g, Flask, request, abort
import xml_marshaller import xml_marshaller
...@@ -136,7 +136,7 @@ def execute_db(table, query, args=(), one=False, db_version=None, log=False, db= ...@@ -136,7 +136,7 @@ def execute_db(table, query, args=(), one=False, db_version=None, log=False, db=
def connect_db(): def connect_db():
return sqlite3.connect(app.config['DATABASE_URI']) return sqlite_connect(app.config['DATABASE_URI'])
def _getTableList(): def _getTableList():
return g.db.execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY Name").fetchall() return g.db.execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY Name").fetchall()
......
...@@ -45,6 +45,7 @@ import slapos.proxy ...@@ -45,6 +45,7 @@ import slapos.proxy
import slapos.proxy.views as views import slapos.proxy.views as views
import slapos.slap import slapos.slap
import slapos.slap.slap import slapos.slap.slap
from slapos.util import sqlite_connect
import sqlite3 import sqlite3
import pkg_resources import pkg_resources
...@@ -904,7 +905,7 @@ class TestMultiMasterSupport(MasterMixin): ...@@ -904,7 +905,7 @@ class TestMultiMasterSupport(MasterMixin):
super(TestMultiMasterSupport, self).setUp() 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.external_slapproxy_configuration_file_location = os.path.join(
self._tempdir, 'external_slapos.cfg') self._tempdir, 'external_slapos.cfg')
self.createExternalProxyConfigurationFile() self.createExternalProxyConfigurationFile()
...@@ -1181,7 +1182,7 @@ class TestMigrateVersion10To11(TestInformation, TestRequest, TestSlaveRequest, T ...@@ -1181,7 +1182,7 @@ class TestMigrateVersion10To11(TestInformation, TestRequest, TestSlaveRequest, T
super(TestMigrateVersion10To11, self).setUp() super(TestMigrateVersion10To11, self).setUp()
schema = pkg_resources.resource_stream('slapos.tests.slapproxy', 'database_dump_version_10.sql') schema = pkg_resources.resource_stream('slapos.tests.slapproxy', 'database_dump_version_10.sql')
schema = schema.read() % dict(version='11') 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.cursor().executescript(schema)
self.db.commit() self.db.commit()
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
import errno import errno
import os import os
import subprocess import subprocess
import sqlite3
def mkdir_p(path, mode=0o700): def mkdir_p(path, mode=0o700):
...@@ -94,3 +95,8 @@ def string_to_boolean(string): ...@@ -94,3 +95,8 @@ def string_to_boolean(string):
return False return False
else: else:
raise ValueError('%s is neither True nor False.' % string) 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