| 1 |
# Copyright (C) 2014 Yahoo! Inc. All Rights Reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
import collections |
| 16 |
|
| 17 |
from oslo_serialization import jsonutils |
| 18 |
from oslo_utils import timeutils |
| 19 |
from oslo_utils import uuidutils |
| 20 |
from sqlalchemy import Table, Column, String, ForeignKey, DateTime, Enum |
| 21 |
from sqlalchemy_utils.types import json as json_type |
| 22 |
|
| 23 |
from taskflow.persistence import models |
| 24 |
from taskflow import states |
| 25 |
|
| 26 |
Tables = collections.namedtuple('Tables', |
| 27 |
['logbooks', 'flowdetails', 'atomdetails']) |
| 28 |
|
| 29 |
# Column length limits... |
| 30 |
NAME_LENGTH = 255 |
| 31 |
UUID_LENGTH = 64 |
| 32 |
STATE_LENGTH = 255 |
| 33 |
VERSION_LENGTH = 64 |
| 34 |
|
| 35 |
|
| 36 |
class JSONType(json_type.JSONType): |
| 37 |
"""Customized JSONType using oslo.serialization for json operations""" |
| 38 |
|
| 39 |
def process_bind_param(self, value, dialect): |
| 40 |
if dialect.name == 'postgresql' and json_type.has_postgres_json: |
| 41 |
return value |
| 42 |
if value is not None: |
| 43 |
value = jsonutils.dumps(value) |
| 44 |
return value |
| 45 |
|
| 46 |
def process_result_value(self, value, dialect): |
| 47 |
if dialect.name == 'postgresql': |
| 48 |
return value |
| 49 |
if value is not None: |
| 50 |
value = jsonutils.loads(value) |
| 51 |
return value |
| 52 |
|
| 53 |
|
| 54 |
def fetch(metadata): |
| 55 |
"""Returns the master set of table objects (which is also there schema).""" |
| 56 |
logbooks = Table('logbooks', metadata, |
| 57 |
Column('created_at', DateTime, |
| 58 |
default=timeutils.utcnow), |
| 59 |
Column('updated_at', DateTime, |
| 60 |
onupdate=timeutils.utcnow), |
| 61 |
Column('meta', JSONType), |
| 62 |
Column('name', String(length=NAME_LENGTH)), |
| 63 |
Column('uuid', String(length=UUID_LENGTH), |
| 64 |
primary_key=True, nullable=False, unique=True, |
| 65 |
default=uuidutils.generate_uuid)) |
| 66 |
flowdetails = Table('flowdetails', metadata, |
| 67 |
Column('created_at', DateTime, |
| 68 |
default=timeutils.utcnow), |
| 69 |
Column('updated_at', DateTime, |
| 70 |
onupdate=timeutils.utcnow), |
| 71 |
Column('parent_uuid', String(length=UUID_LENGTH), |
| 72 |
ForeignKey('logbooks.uuid', |
| 73 |
ondelete='CASCADE')), |
| 74 |
Column('meta', JSONType), |
| 75 |
Column('name', String(length=NAME_LENGTH)), |
| 76 |
Column('state', String(length=STATE_LENGTH)), |
| 77 |
Column('uuid', String(length=UUID_LENGTH), |
| 78 |
primary_key=True, nullable=False, unique=True, |
| 79 |
default=uuidutils.generate_uuid)) |
| 80 |
atomdetails = Table('atomdetails', metadata, |
| 81 |
Column('created_at', DateTime, |
| 82 |
default=timeutils.utcnow), |
| 83 |
Column('updated_at', DateTime, |
| 84 |
onupdate=timeutils.utcnow), |
| 85 |
Column('meta', JSONType), |
| 86 |
Column('parent_uuid', String(length=UUID_LENGTH), |
| 87 |
ForeignKey('flowdetails.uuid', |
| 88 |
ondelete='CASCADE')), |
| 89 |
Column('name', String(length=NAME_LENGTH)), |
| 90 |
Column('version', String(length=VERSION_LENGTH)), |
| 91 |
Column('state', String(length=STATE_LENGTH)), |
| 92 |
Column('uuid', String(length=UUID_LENGTH), |
| 93 |
primary_key=True, nullable=False, unique=True, |
| 94 |
default=uuidutils.generate_uuid), |
| 95 |
Column('failure', JSONType), |
| 96 |
Column('results', JSONType), |
| 97 |
Column('revert_results', JSONType), |
| 98 |
Column('revert_failure', JSONType), |
| 99 |
Column('atom_type', Enum(*models.ATOM_TYPES, |
| 100 |
name='atom_types')), |
| 101 |
Column('intention', Enum(*states.INTENTIONS, |
| 102 |
name='intentions'))) |
| 103 |
return Tables(logbooks, flowdetails, atomdetails) |