Add basic DB schema

This commit is contained in:
Yuriy Taraday 2015-12-03 12:42:41 +03:00
parent 6cd5de497d
commit 8008d3a423
2 changed files with 100 additions and 0 deletions

74
tuning_box/db.py Normal file
View File

@ -0,0 +1,74 @@
# 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.
import functools
import flask_sqlalchemy
db = flask_sqlalchemy.SQLAlchemy()
pk_type = db.Integer
pk = functools.partial(db.Column, pk_type, primary_key=True)
class Namespace(db.Model):
id = pk()
name = db.Column(db.String(128))
# Component registry
class Component(db.Model):
id = pk()
name = db.Column(db.String(128))
schemas = db.relationship("Schema", backref="component")
templates = db.relationship("Template", backref="component")
class Schema(db.Model):
id = pk()
name = db.Column(db.String(128))
component_id = db.Column(pk_type, db.ForeignKey(Component.id))
namespace_id = db.Column(pk_type, db.ForeignKey(Namespace.id))
content = db.Column(db.Text)
class Template(db.Model):
id = pk()
name = db.Column(db.String(128))
component_id = db.Column(pk_type, db.ForeignKey(Component.id))
content = db.Column(db.Text)
# Environment data storage
_environment_components = db.Table(
'environment_components',
db.Column('environment_id', pk_type, db.ForeignKey('environment.id')),
db.Column('component_id', pk_type, db.ForeignKey('component.id')),
)
class Environment(db.Model):
id = pk()
components = db.relationship(Component, secondary=_environment_components)
class TemplateInstance(db.Model):
id = pk()
template_id = db.Column(pk_type, db.ForeignKey(Template.id))
environment_id = db.Column(pk_type, db.ForeignKey(Environment.id))
content = db.Column(db.Text)
class NamespaceInstance(db.Model):
id = pk()
namespace_id = db.Column(pk_type, db.ForeignKey(Namespace.id))
environment_id = db.Column(pk_type, db.ForeignKey(Environment.id))

View File

@ -0,0 +1,26 @@
# 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.
import flask
from tuning_box import db
from tuning_box.tests import base
class TestDB(base.TestCase):
def test_create_all(self):
app = flask.Flask('test')
db.db.init_app(app)
app.config["SQLALCHEMY_DATABASE_URI"] = 'sqlite:///'
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False # silence warning
with app.app_context():
db.db.create_all()