# Path: chat/field.py
# class Field(object):
#     def __init__(self, name, field_type):
#         self.name = name
#         self.field_type = field_type
# 
#     def to_dict(self):
#         return {'name': self.name,
#                 'type': self.field_type}
# 
# Path: chat/schema.py
# class Schema(object):
#     def __init__(self, record_type, fields):
#         self.record_type = record_type
#         self.fields = fields
# 
#     def to_dict(self):
#         return {self.record_type:
#                 {'fields': [field.to_dict() for field in self.fields]}}
# 
# class SchemaHelper(object):
#     def __init__(self, container):
#         self.container = container
# 
#     def create(self, schemas, plugin_request=False):
#         record_types = {}
#         for schema in schemas:
#             record_types.update(schema.to_dict())
#         payload = {'record_types': record_types}
#         return self.container.send_action('schema:create',
#                                           payload,
#                                           plugin_request=plugin_request)
# 
# Path: chat/utils.py
# def _get_schema_name():
#     return "app_%s" % skyoptions.appname


from psycopg2.extensions import AsIs
from skygear.container import SkygearContainer
from skygear.options import options as skyoptions
from skygear.utils import db
from .field import Field
from .schema import Schema, SchemaHelper
from .utils import _get_schema_name
import skygear
                                           Field('is_admin',
                                                 'boolean')])
        receipt_schema = Schema('receipt',
                                [Field('user', 'ref(user)'),
                                 Field('message', 'ref(message)'),
                                 Field('read_at', 'datetime'),
                                 Field('delivered_at', 'datetime')])
        user_channel_schema = Schema('user_channel',
                                     [Field('name', 'string')])
        message_schema = _message_schema()
        message_history_schema = _message_history_schema()
        schema_helper.create([user_schema,
                              user_conversation_schema,
                              conversation_schema,
                              message_schema,
                              message_history_schema,
                              receipt_schema,
                              user_channel_schema],
                             plugin_request=True)

        # Create unique constraint to _database_id in user_channel table
        # to ensure there is only one user_channel for each user
        with db.conn() as conn:
            result = conn.execute("""
                select 1
                    FROM information_schema.constraint_column_usage
                WHERE table_schema = '%(schema_name)s'
                    AND table_name = 'user_channel'
                    AND constraint_name = 'user_channel_database_id_key'
                """, {

# Path: chat/field.py
# class Field(object):
#     def __init__(self, name, field_type):
#         self.name = name
#         self.field_type = field_type
# 
#     def to_dict(self):
#         return {'name': self.name,
#                 'type': self.field_type}
# 
# Path: chat/schema.py
# class Schema(object):
#     def __init__(self, record_type, fields):
#         self.record_type = record_type
#         self.fields = fields
# 
#     def to_dict(self):
#         return {self.record_type:
#                 {'fields': [field.to_dict() for field in self.fields]}}
# 
# class SchemaHelper(object):
#     def __init__(self, container):
#         self.container = container
# 
#     def create(self, schemas, plugin_request=False):
#         record_types = {}
#         for schema in schemas:
#             record_types.update(schema.to_dict())
#         payload = {'record_types': record_types}
#         return self.container.send_action('schema:create',
#                                           payload,
#                                           plugin_request=plugin_request)
# 
# Path: chat/utils.py
# def _get_schema_name():
#     return "app_%s" % skyoptions.appname


# Path: chat/initialize.py
from psycopg2.extensions import AsIs
from skygear.container import SkygearContainer
from skygear.options import options as skyoptions
from skygear.utils import db
from .field import Field
from .schema import Schema, SchemaHelper
from .utils import _get_schema_name
import skygear

                                           Field('is_admin',
                                                 'boolean')])
        receipt_schema = Schema('receipt',
                                [Field('user', 'ref(user)'),
                                 Field('message', 'ref(message)'),
                                 Field('read_at', 'datetime'),
                                 Field('delivered_at', 'datetime')])
        user_channel_schema = Schema('user_channel',
                                     [Field('name', 'string')])
        message_schema = _message_schema()
        message_history_schema = _message_history_schema()
        schema_helper.create([user_schema,
                              user_conversation_schema,
                              conversation_schema,
                              message_schema,
                              message_history_schema,
                              receipt_schema,
                              user_channel_schema],
                             plugin_request=True)

        # Create unique constraint to _database_id in user_channel table
        # to ensure there is only one user_channel for each user
        with db.conn() as conn:
            result = conn.execute("""
                select 1
                    FROM information_schema.constraint_column_usage
                WHERE table_schema = '%(schema_name)s'
                    AND table_name = 'user_channel'
                    AND constraint_name = 'user_channel_database_id_key'
                """, {

                    'schema_name': AsIs(_get_schema_name())