summaryrefslogtreecommitdiff
path: root/taskflow/persistence/backends/sqlalchemy
diff options
context:
space:
mode:
authorJoshua Harlow <harlowja@gmail.com>2015-06-03 21:55:15 -0700
committerJoshua Harlow <harlowja@yahoo-inc.com>2015-09-09 17:54:11 -0700
commit598e09fb062daed36fd4f10943ce9b4381843c9e (patch)
tree833f5452062c370ec7806ebae62047eb8659139d /taskflow/persistence/backends/sqlalchemy
parent52bd5e89fdddec49907a45e83a7a4b1abd1d1291 (diff)
downloadtaskflow-598e09fb062daed36fd4f10943ce9b4381843c9e.tar.gz
Use the sqlalchemy-utils json type instead of our own
Change-Id: Ie01ea85e74f1daed6bfa5158c0faa476d06873ba
Diffstat (limited to 'taskflow/persistence/backends/sqlalchemy')
-rw-r--r--taskflow/persistence/backends/sqlalchemy/alembic/versions/2ad4984f2864_switch_postgres_to_json_native.py58
-rw-r--r--taskflow/persistence/backends/sqlalchemy/tables.py31
2 files changed, 66 insertions, 23 deletions
diff --git a/taskflow/persistence/backends/sqlalchemy/alembic/versions/2ad4984f2864_switch_postgres_to_json_native.py b/taskflow/persistence/backends/sqlalchemy/alembic/versions/2ad4984f2864_switch_postgres_to_json_native.py
new file mode 100644
index 0000000..e795dfc
--- /dev/null
+++ b/taskflow/persistence/backends/sqlalchemy/alembic/versions/2ad4984f2864_switch_postgres_to_json_native.py
@@ -0,0 +1,58 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (C) 2012-2013 Yahoo! Inc. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+"""Switch postgres to json native type.
+
+Revision ID: 2ad4984f2864
+Revises: 589dccdf2b6e
+Create Date: 2015-06-04 13:08:36.667948
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = '2ad4984f2864'
+down_revision = '589dccdf2b6e'
+
+from alembic import op
+
+
+_ALTER_TO_JSON_TPL = 'ALTER TABLE %s ALTER COLUMN %s TYPE JSON USING %s::JSON'
+_TABLES_COLS = tuple([
+ ('logbooks', 'meta'),
+ ('flowdetails', 'meta'),
+ ('atomdetails', 'meta'),
+ ('atomdetails', 'failure'),
+ ('atomdetails', 'revert_failure'),
+ ('atomdetails', 'results'),
+ ('atomdetails', 'revert_results'),
+])
+_ALTER_TO_TEXT_TPL = 'ALTER TABLE %s ALTER COLUMN %s TYPE TEXT'
+
+
+def upgrade():
+ b = op.get_bind()
+ if b.dialect.name.startswith('postgresql'):
+ for (table_name, col_name) in _TABLES_COLS:
+ q = _ALTER_TO_JSON_TPL % (table_name, col_name, col_name)
+ op.execute(q)
+
+
+def downgrade():
+ b = op.get_bind()
+ if b.dialect.name.startswith('postgresql'):
+ for (table_name, col_name) in _TABLES_COLS:
+ q = _ALTER_TO_TEXT_TPL % (table_name, col_name)
+ op.execute(q)
diff --git a/taskflow/persistence/backends/sqlalchemy/tables.py b/taskflow/persistence/backends/sqlalchemy/tables.py
index 65969fb..b9b065e 100644
--- a/taskflow/persistence/backends/sqlalchemy/tables.py
+++ b/taskflow/persistence/backends/sqlalchemy/tables.py
@@ -16,11 +16,10 @@
import collections
-from oslo_serialization import jsonutils
from oslo_utils import timeutils
from oslo_utils import uuidutils
from sqlalchemy import Table, Column, String, ForeignKey, DateTime, Enum
-from sqlalchemy import types
+import sqlalchemy_utils as su
from taskflow.persistence import models
from taskflow import states
@@ -35,20 +34,6 @@ STATE_LENGTH = 255
VERSION_LENGTH = 64
-class Json(types.TypeDecorator):
- impl = types.Text
-
- def process_bind_param(self, value, dialect):
- if not value:
- return None
- return jsonutils.dumps(value)
-
- def process_result_value(self, value, dialect):
- if not value:
- return None
- return jsonutils.loads(value)
-
-
def fetch(metadata):
"""Returns the master set of table objects (which is also there schema)."""
logbooks = Table('logbooks', metadata,
@@ -56,7 +41,7 @@ def fetch(metadata):
default=timeutils.utcnow),
Column('updated_at', DateTime,
onupdate=timeutils.utcnow),
- Column('meta', Json),
+ Column('meta', su.JSONType),
Column('name', String(length=NAME_LENGTH)),
Column('uuid', String(length=UUID_LENGTH),
primary_key=True, nullable=False, unique=True,
@@ -69,7 +54,7 @@ def fetch(metadata):
Column('parent_uuid', String(length=UUID_LENGTH),
ForeignKey('logbooks.uuid',
ondelete='CASCADE')),
- Column('meta', Json),
+ Column('meta', su.JSONType),
Column('name', String(length=NAME_LENGTH)),
Column('state', String(length=STATE_LENGTH)),
Column('uuid', String(length=UUID_LENGTH),
@@ -80,7 +65,7 @@ def fetch(metadata):
default=timeutils.utcnow),
Column('updated_at', DateTime,
onupdate=timeutils.utcnow),
- Column('meta', Json),
+ Column('meta', su.JSONType),
Column('parent_uuid', String(length=UUID_LENGTH),
ForeignKey('flowdetails.uuid',
ondelete='CASCADE')),
@@ -90,10 +75,10 @@ def fetch(metadata):
Column('uuid', String(length=UUID_LENGTH),
primary_key=True, nullable=False, unique=True,
default=uuidutils.generate_uuid),
- Column('failure', Json),
- Column('results', Json),
- Column('revert_results', Json),
- Column('revert_failure', Json),
+ Column('failure', su.JSONType),
+ Column('results', su.JSONType),
+ Column('revert_results', su.JSONType),
+ Column('revert_failure', su.JSONType),
Column('atom_type', Enum(*models.ATOM_TYPES,
name='atom_types')),
Column('intention', Enum(*states.INTENTIONS,