Support postgresql in dblayer

Change-Id: I7131273d137c0d42991a0cbd17dd9bc2dbf04d5c
Closes-bug: #1545659
This commit is contained in:
Jedrzej Nowak 2016-02-15 18:13:25 +01:00
parent 2f454ded5a
commit 3881dc9750
8 changed files with 40 additions and 9 deletions

View File

@ -1,7 +1,7 @@
celery_broker: sqla+sqlite:////tmp/celery.db celery_broker: sqla+sqlite:////tmp/celery.db
celery_backend: db+sqlite:////tmp/celery.db celery_backend: db+sqlite:////tmp/celery.db
solar_db: sqlite:////tmp/solar.db solar_db: sqlite:////tmp/solar.db
# solar_db: postgresql:///postgres:password@10.0.0.2:5432/solardb
# solar_db: riak://10.0.0.2:8087 # solar_db: riak://10.0.0.2:8087
riak_ensemble: False riak_ensemble: False
lock_bucket_type: '' lock_bucket_type: ''

View File

@ -38,6 +38,9 @@
# for tests on jenkins # for tests on jenkins
- sshpass - sshpass
# for pg backend
- python-psycopg2
- name: Uninstall packages - name: Uninstall packages
apt: name={{ item }} state=absent apt: name={{ item }} state=absent
with_items: with_items:

View File

@ -22,10 +22,14 @@ pbr
pydot pydot
bunch bunch
wrapt wrapt
# if you want to use riak backend then # if you want to use riak backend then
# riak # riak
# if you want to use sql backend then # if you want to use sql backend then
peewee peewee
# if you want to use sql + postgresql then
# psycopg2
# if you want to use lua computable inputs # if you want to use lua computable inputs
# lupa # lupa
@ -38,4 +42,3 @@ stevedore
#zerorpc doesnt consume messages with >13.0.2, need to debug #zerorpc doesnt consume messages with >13.0.2, need to debug
pyzmq==13.0.2 pyzmq==13.0.2
zerorpc>=0.5.2 zerorpc>=0.5.2

View File

@ -40,6 +40,7 @@ if _connection.mode == 'sqlite':
'pragmas': (('journal_mode', 'WAL'), 'pragmas': (('journal_mode', 'WAL'),
('synchronous', 'NORMAL'))} ('synchronous', 'NORMAL'))}
opts.update(_connection_details.toDict()) opts.update(_connection_details.toDict())
opts.setdefault('db_class', 'SqliteDatabase')
client = SqlClient( client = SqlClient(
_connection.database, _connection.database,
**opts) **opts)
@ -60,6 +61,24 @@ elif _connection.mode == 'riak':
**opts) **opts)
else: else:
raise Exception('Unknown riak protocol %s', proto) raise Exception('Unknown riak protocol %s', proto)
elif _connection.mode == 'postgresql':
# TODO: collation has to be `C`
from solar.dblayer.sql_client import SqlClient
opts = {'autocommit': False}
opts.update(_connection_details.toDict())
if _connection.port:
_connection.port = int(_connection.port)
else:
_connection.port = None
opts["user"] = _connection.username
opts["host"] = _connection.host
opts["port"] = _connection.port
opts["password"] = _connection.password
# TODO: allow set Postgresql classes from playhouse
opts.setdefault('db_class', 'PostgresqlDatabase')
client = SqlClient(_connection.database,
**opts)
else: else:
raise Exception('Unknown dblayer backend %s', C.solar_db) raise Exception('Unknown dblayer backend %s', C.solar_db)

View File

@ -230,7 +230,7 @@ class RiakLock(_CRDTishLock):
pass pass
class SQLiteLock(_CRDTishLock): class SQLLock(_CRDTishLock):
@classmethod @classmethod
def _end_start_session(cls, uid, identity): def _end_start_session(cls, uid, identity):
@ -267,12 +267,14 @@ class RiakEnsembleLock(_Lock):
raise raise
if _connection.mode == 'sqlite': if _connection.type == 'sql':
Lock = SQLiteLock Lock = SQLLock
elif _connection.mode == 'riak': elif _connection.mode == 'riak':
if C.riak_ensemble: if C.riak_ensemble:
Lock = RiakEnsembleLock Lock = RiakEnsembleLock
else: else:
Lock = RiakLock Lock = RiakLock
else:
raise RuntimeError("Unsupported database connection setting")
Waiter = SemaWaiter Waiter = SemaWaiter

View File

@ -1072,7 +1072,7 @@ system log
""" """
_connection, _connection_details = parse_database_conn(C.solar_db) _connection, _connection_details = parse_database_conn(C.solar_db)
if _connection.mode == 'sqlite': if _connection.type == 'sql':
class NegativeCounter(Model): class NegativeCounter(Model):
count = Field(int, default=int) count = Field(int, default=int)

View File

@ -268,7 +268,10 @@ class Bucket(object):
self._sql_model = type(table_name, (_SqlBucket, ), {'Meta': ModelMeta, self._sql_model = type(table_name, (_SqlBucket, ), {'Meta': ModelMeta,
'bucket': self}) 'bucket': self})
_idx_key = ForeignKeyField(self._sql_model, null=False, index=True) _idx_key = ForeignKeyField(self._sql_model,
null=False,
index=True,
on_delete='cascade')
class IdxMeta(object): class IdxMeta(object):
db_table = idx_table_name db_table = idx_table_name
@ -405,9 +408,9 @@ class SqlClient(object):
search_dir = None search_dir = None
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
db_class_str = kwargs.pop("db_class", 'SqliteDatabase') db_class_str = kwargs.pop("db_class")
try: try:
mod, fromlist = db_class_str.split('.') mod, fromlist = db_class_str.rsplit('.', 1)
except ValueError: except ValueError:
mod = 'peewee' mod = 'peewee'
fromlist = db_class_str fromlist = db_class_str

View File

@ -167,6 +167,7 @@ def parse_database_conn(name):
m = regex.match(name) m = regex.match(name)
if m is not None: if m is not None:
groups = m.groupdict() groups = m.groupdict()
groups['type'] = 'riak' if groups['mode'] == 'riak' else 'sql'
return Bunch(groups), Bunch(opts) return Bunch(groups), Bunch(opts)
else: else:
raise Exception("Invalid database connection string: %r " raise Exception("Invalid database connection string: %r "