<commit_before>#    Copyright (c) 2013 Mirantis, Inc.
#
#    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.

"""
SQLAlchemy models for murano data
"""
from oslo_db.sqlalchemy import models
from oslo_utils import timeutils
import sqlalchemy as sa
from sqlalchemy.ext import declarative
from sqlalchemy import orm as sa_orm

from murano.common import uuidutils
from murano.db.sqla import types as st


class TimestampMixin(object):
    __protected_attributes__ = set(["created", "updated"])

    created = sa.Column(sa.DateTime, default=timeutils.utcnow,
                        nullable=False)
    updated = sa.Column(sa.DateTime, default=timeutils.utcnow,
                        nullable=False, onupdate=timeutils.utcnow)

    def update(self, values):
        """dict.update() behaviour."""
        self.updated = timeutils.utcnow()
        super(TimestampMixin, self).update(values)

    def __setitem__(self, key, value):
        self.updated = timeutils.utcnow()
        super(TimestampMixin, self).__setitem__(key, value)


class _MuranoBase(models.ModelBase):
    def to_dict(self):
        dictionary = self.__dict__.copy()
        return dict((k, v) for k, v in dictionary.iteritems()
                    if k != '_sa_instance_state')


Base = declarative.declarative_base(cls=_MuranoBase)


class Environment(Base, TimestampMixin):
    """Represents a Environment in the metadata-store."""
    __tablename__ = 'environment'

    id = sa.Column(sa.String(255),
                   primary_key=True,
                   default=uuidutils.generate_uuid)
    name = sa.Column(sa.String(255), nullable=False)
    tenant_id = sa.Column(sa.String(36), nullable=False)
    version = sa.Column(sa.BigInteger, nullable=False, default=0)
    description = sa.Column(st.JsonBlob(), nullable=False, default={})
    networking = sa.Column(st.JsonBlob(), nullable=True, default={})

    sessions = sa_orm.relationship("Session", backref='environment',
                                   cascade='save-update, merge, delete')

    tasks = sa_orm.relationship('Task', backref='environment',
                                cascade='save-update, merge, delete')

    def to_dict(self):
        dictionary = super(Environment, self).to_dict()
        del dictionary['description']
        return dictionary


class EnvironmentTemplate(Base, TimestampMixin):
    """Represents a Environment emplate in the metadata-store."""
    __tablename__ = 'environment-template'<commit_msg>Add unique constraint to environment table<commit_after>#    Copyright (c) 2013 Mirantis, Inc.
#
#    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.

"""
SQLAlchemy models for murano data
"""
from oslo_db.sqlalchemy import models
from oslo_utils import timeutils
import sqlalchemy as sa
from sqlalchemy.ext import declarative
from sqlalchemy import orm as sa_orm

from murano.common import uuidutils
from murano.db.sqla import types as st


class TimestampMixin(object):
    __protected_attributes__ = set(["created", "updated"])

    created = sa.Column(sa.DateTime, default=timeutils.utcnow,
                        nullable=False)
    updated = sa.Column(sa.DateTime, default=timeutils.utcnow,
                        nullable=False, onupdate=timeutils.utcnow)

    def update(self, values):
        """dict.update() behaviour."""
        self.updated = timeutils.utcnow()
        super(TimestampMixin, self).update(values)

    def __setitem__(self, key, value):
        self.updated = timeutils.utcnow()
        super(TimestampMixin, self).__setitem__(key, value)


class _MuranoBase(models.ModelBase):
    def to_dict(self):
        dictionary = self.__dict__.copy()
        return dict((k, v) for k, v in dictionary.iteritems()
                    if k != '_sa_instance_state')


Base = declarative.declarative_base(cls=_MuranoBase)


class Environment(Base, TimestampMixin):
    """Represents a Environment in the metadata-store."""
    __tablename__ = 'environment'
    __table_args__ = (sa.Index(
        'ix_name_tenant_id', 'name', 'tenant_id', unique=True),)

    id = sa.Column(sa.String(255),
                   primary_key=True,
                   default=uuidutils.generate_uuid)
    name = sa.Column(sa.String(255), nullable=False)
    tenant_id = sa.Column(sa.String(36), nullable=False)
    version = sa.Column(sa.BigInteger, nullable=False, default=0)
    description = sa.Column(st.JsonBlob(), nullable=False, default={})
    networking = sa.Column(st.JsonBlob(), nullable=True, default={})

    sessions = sa_orm.relationship("Session", backref='environment',
                                   cascade='save-update, merge, delete')

    tasks = sa_orm.relationship('Task', backref='environment',
                                cascade='save-update, merge, delete')

    def to_dict(self):
        dictionary = super(Environment, self).to_dict()
        del dictionary['description']
        return dictionary


class EnvironmentTemplate(Base, TimestampMixin):
    """Represents a Environment emplate in the metadata-store."""
    __tablename__ = 'environment-template'
{'fraction_non_alphanumeric': 0.10344262295081967, 'fraction_numerical': 0.005573770491803279, 'mean_word_length': 3.1960110041265475, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5588624', 'n_tokens_mistral': 1765, 'n_tokens_neox': 1698, 'n_words': 486}