def _init_table(self, meta, namespace, name, id_type=db.Integer): """ Create the given table if it does not exist, otherwise reflect the current table schema from the database. """ name = namespace + '__' + name self.table = db.Table(name, meta) col = db.Column('id', id_type, primary_key=True) self.table.append_column(col)
def modelmigrate(): from openspending.validation.model.migration import migrate_model dataset = db.Table('dataset', db.metadata, autoload=True) rp = db.engine.execute(dataset.select()) while True: ds = rp.fetchone() if ds is None: break print ds['name'], '...' model = migrate_model(ds['data']) version = model.get('dataset').get('schema_version') if 'dataset' in model: del model['dataset'] q = dataset.update().where(dataset.c.id == ds['id']) q = q.values({'data': model, 'schema_version': version}) db.engine.execute(q) return 0
from datetime import datetime from openspending.model import Account, meta as db # Badges and dataset share a many to many relationship # therefore we need to create an associate table badges_on_datasets = db.Table( 'badges_on_datasets', db.metadata, db.Column('badge_id', db.Integer, db.ForeignKey('badge.id')), db.Column('dataset_id', db.Integer, db.ForeignKey('dataset.id'))) class Badge(db.Model): """ This model allows marking datasets with various badges. Examples could be "World Bank" - data verified by the World bank. Each badge has a name, a representative image and a description. Also stored for historical reasons are badge creator, creation time and modification date. """ __tablename__ = 'badge' id = db.Column(db.Integer, primary_key=True) # Primary information for this badge label = db.Column(db.Unicode) image = db.Column(db.Unicode) description = db.Column(db.Unicode) # Define relationship with datasets via the associate table
from openspending.model import meta as db from openspending.model.dataset import Dataset REGISTER_NAME_RE = r"^[a-zA-Z0-9_\-]{3,255}$" def make_uuid(): return unicode(uuid.uuid4()) account_dataset_table = db.Table( 'account_dataset', db.metadata, db.Column('dataset_id', db.Integer, db.ForeignKey('dataset.id'), primary_key=True), db.Column('account_id', db.Integer, db.ForeignKey('account.id'), primary_key=True)) class Account(db.Model): __tablename__ = 'account' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.Unicode(255), unique=True) fullname = db.Column(db.Unicode(2000)) email = db.Column(db.Unicode(2000)) password = db.Column(db.Unicode(2000)) api_key = db.Column(db.Unicode(2000), default=make_uuid)