summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2017-06-16 09:20:04 +0000
committerGerrit Code Review <review@openstack.org>2017-06-16 09:20:04 +0000
commitf5a16279531b82ec5b77bde6aae986f1624688c4 (patch)
tree2695e05e990afdcfb38a24059c70a10cfd708a63
parent28f1f0f9370c395023bc5ae01693e5d952b3e1c1 (diff)
parent8c374b1e7f7d0de12651a8985097286411bd9d65 (diff)
Merge "Use oslo_db for create_engine"
-rw-r--r--requirements.txt1
-rw-r--r--zaqar/storage/sqlalchemy/catalogue.py5
-rw-r--r--zaqar/storage/sqlalchemy/driver.py20
-rw-r--r--zaqar/storage/sqlalchemy/flavors.py3
-rw-r--r--zaqar/storage/sqlalchemy/pools.py5
-rw-r--r--zaqar/storage/sqlalchemy/queues.py3
6 files changed, 19 insertions, 18 deletions
diff --git a/requirements.txt b/requirements.txt
index acb580d..6758aed 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -17,6 +17,7 @@ six>=1.9.0 # MIT
17oslo.cache>=1.5.0 # Apache-2.0 17oslo.cache>=1.5.0 # Apache-2.0
18oslo.config>=4.0.0 # Apache-2.0 18oslo.config>=4.0.0 # Apache-2.0
19oslo.context>=2.14.0 # Apache-2.0 19oslo.context>=2.14.0 # Apache-2.0
20oslo.db>=4.21.1 # Apache-2.0
20oslo.i18n!=3.15.2,>=2.1.0 # Apache-2.0 21oslo.i18n!=3.15.2,>=2.1.0 # Apache-2.0
21oslo.log>=3.22.0 # Apache-2.0 22oslo.log>=3.22.0 # Apache-2.0
22oslo.messaging!=5.25.0,>=5.24.2 # Apache-2.0 23oslo.messaging!=5.25.0,>=5.24.2 # Apache-2.0
diff --git a/zaqar/storage/sqlalchemy/catalogue.py b/zaqar/storage/sqlalchemy/catalogue.py
index 4a92d2e..09bc8f5 100644
--- a/zaqar/storage/sqlalchemy/catalogue.py
+++ b/zaqar/storage/sqlalchemy/catalogue.py
@@ -22,6 +22,7 @@ project: string
22queue: string 22queue: string
23""" 23"""
24 24
25import oslo_db.exception
25import sqlalchemy as sa 26import sqlalchemy as sa
26 27
27from zaqar.storage import base 28from zaqar.storage import base
@@ -70,7 +71,9 @@ class CatalogueController(base.CatalogueBase):
70 ) 71 )
71 self.driver.run(stmt) 72 self.driver.run(stmt)
72 73
73 except sa.exc.IntegrityError: 74 except oslo_db.exception.DBReferenceError:
75 self._update(project, queue, pool)
76 except oslo_db.exception.DBDuplicateError:
74 self._update(project, queue, pool) 77 self._update(project, queue, pool)
75 78
76 def delete(self, project, queue): 79 def delete(self, project, queue):
diff --git a/zaqar/storage/sqlalchemy/driver.py b/zaqar/storage/sqlalchemy/driver.py
index fed11e7..394734e 100644
--- a/zaqar/storage/sqlalchemy/driver.py
+++ b/zaqar/storage/sqlalchemy/driver.py
@@ -13,6 +13,7 @@
13# License for the specific language governing permissions and limitations under 13# License for the specific language governing permissions and limitations under
14# the License. 14# the License.
15 15
16from oslo_db.sqlalchemy import engines
16from osprofiler import profiler 17from osprofiler import profiler
17from osprofiler import sqlalchemy as sa_tracer 18from osprofiler import sqlalchemy as sa_tracer
18import sqlalchemy as sa 19import sqlalchemy as sa
@@ -31,11 +32,6 @@ class ControlDriver(storage.ControlDriverBase):
31 group=options.MANAGEMENT_SQLALCHEMY_GROUP) 32 group=options.MANAGEMENT_SQLALCHEMY_GROUP)
32 self.sqlalchemy_conf = self.conf[options.MANAGEMENT_SQLALCHEMY_GROUP] 33 self.sqlalchemy_conf = self.conf[options.MANAGEMENT_SQLALCHEMY_GROUP]
33 34
34 def _sqlite_on_connect(self, conn, record):
35 # NOTE(flaper87): This is necessary in order
36 # to ensure FK are treated correctly by sqlite.
37 conn.execute('pragma foreign_keys=ON')
38
39 def _mysql_on_connect(self, conn, record): 35 def _mysql_on_connect(self, conn, record):
40 # NOTE(flaper87): This is necessary in order 36 # NOTE(flaper87): This is necessary in order
41 # to ensure that all date operations in mysql 37 # to ensure that all date operations in mysql
@@ -43,18 +39,16 @@ class ControlDriver(storage.ControlDriverBase):
43 conn.query('SET time_zone = "+0:00"') 39 conn.query('SET time_zone = "+0:00"')
44 40
45 @decorators.lazy_property(write=False) 41 @decorators.lazy_property(write=False)
46 def engine(self, *args, **kwargs): 42 def engine(self):
47 uri = self.sqlalchemy_conf.uri 43 uri = self.sqlalchemy_conf.uri
48 engine = sa.create_engine(uri, **kwargs) 44 engine = engines.create_engine(uri, sqlite_fk=True)
49
50 # TODO(flaper87): Find a better way
51 # to do this.
52 if uri.startswith('sqlite://'):
53 sa.event.listen(engine, 'connect',
54 self._sqlite_on_connect)
55 45
56 if (uri.startswith('mysql://') or 46 if (uri.startswith('mysql://') or
57 uri.startswith('mysql+pymysql://')): 47 uri.startswith('mysql+pymysql://')):
48 # oslo_db.create_engine makes a test connection, throw that out
49 # first. mysql time_zone can be added to oslo_db as a
50 # startup option
51 engine.dispose()
58 sa.event.listen(engine, 'connect', 52 sa.event.listen(engine, 'connect',
59 self._mysql_on_connect) 53 self._mysql_on_connect)
60 54
diff --git a/zaqar/storage/sqlalchemy/flavors.py b/zaqar/storage/sqlalchemy/flavors.py
index c0c9778..4a0030d 100644
--- a/zaqar/storage/sqlalchemy/flavors.py
+++ b/zaqar/storage/sqlalchemy/flavors.py
@@ -17,6 +17,7 @@ controller for sqlalchemy.
17 17
18""" 18"""
19 19
20import oslo_db.exception
20import sqlalchemy as sa 21import sqlalchemy as sa
21 22
22from zaqar.storage import base 23from zaqar.storage import base
@@ -80,7 +81,7 @@ class FlavorsController(base.FlavorsBase):
80 capabilities=cap 81 capabilities=cap
81 ) 82 )
82 self.driver.run(stmt) 83 self.driver.run(stmt)
83 except sa.exc.IntegrityError: 84 except oslo_db.exception.DBDuplicateEntry:
84 if not self._pools_ctrl.get_pools_by_group(pool_group): 85 if not self._pools_ctrl.get_pools_by_group(pool_group):
85 raise errors.PoolGroupDoesNotExist(pool_group) 86 raise errors.PoolGroupDoesNotExist(pool_group)
86 87
diff --git a/zaqar/storage/sqlalchemy/pools.py b/zaqar/storage/sqlalchemy/pools.py
index 451ccf9..1156479 100644
--- a/zaqar/storage/sqlalchemy/pools.py
+++ b/zaqar/storage/sqlalchemy/pools.py
@@ -19,6 +19,7 @@ controller for sqlalchemy.
19 19
20import functools 20import functools
21 21
22import oslo_db.exception
22import sqlalchemy as sa 23import sqlalchemy as sa
23 24
24from zaqar.common import utils as common_utils 25from zaqar.common import utils as common_utils
@@ -81,7 +82,7 @@ class PoolsController(base.PoolsBase):
81 stmt = sa.sql.expression.insert(tables.PoolGroup).values(name=name) 82 stmt = sa.sql.expression.insert(tables.PoolGroup).values(name=name)
82 self.driver.run(stmt) 83 self.driver.run(stmt)
83 return True 84 return True
84 except sa.exc.IntegrityError: 85 except oslo_db.exception.DBDuplicateEntry:
85 return False 86 return False
86 87
87 # TODO(cpp-cabrera): rename to upsert 88 # TODO(cpp-cabrera): rename to upsert
@@ -98,7 +99,7 @@ class PoolsController(base.PoolsBase):
98 ) 99 )
99 self.driver.run(stmt) 100 self.driver.run(stmt)
100 101
101 except sa.exc.IntegrityError: 102 except oslo_db.exception.DBDuplicateEntry:
102 # TODO(cpp-cabrera): merge update/create into a single 103 # TODO(cpp-cabrera): merge update/create into a single
103 # method with introduction of upsert 104 # method with introduction of upsert
104 self._update(name, weight=weight, uri=uri, 105 self._update(name, weight=weight, uri=uri,
diff --git a/zaqar/storage/sqlalchemy/queues.py b/zaqar/storage/sqlalchemy/queues.py
index 19ca0ba..ea7e962 100644
--- a/zaqar/storage/sqlalchemy/queues.py
+++ b/zaqar/storage/sqlalchemy/queues.py
@@ -12,6 +12,7 @@
12# License for the specific language governing permissions and limitations under 12# License for the specific language governing permissions and limitations under
13# the License. 13# the License.
14 14
15import oslo_db.exception
15import sqlalchemy as sa 16import sqlalchemy as sa
16 17
17from zaqar import storage 18from zaqar import storage
@@ -85,7 +86,7 @@ class QueueController(storage.Queue):
85 name=name, 86 name=name,
86 metadata=smeta) 87 metadata=smeta)
87 res = self.driver.run(ins) 88 res = self.driver.run(ins)
88 except sa.exc.IntegrityError: 89 except oslo_db.exception.DBDuplicateEntry:
89 return False 90 return False
90 91
91 return res.rowcount == 1 92 return res.rowcount == 1