Пример #1
0
def insert_(fixtures): 
    engine = DBSession.bind
    metadata = MetaData(engine)
    tablenames = []
    for tablename, data in fixtures:
        if 'csv' in data:
            data['data'] = csv2fixture(data['csv'])
        if tablename == 'users':
            T = User
            table = T.__table__
        else:
            schema, tablename_ = split_tablename(tablename)
            table = Table(tablename_, metadata, autoload=True, schema=schema)
            class T(Base, BaseModel, CommonModel):
                __table__ = table
        if type(data) == DictType:
            options = data['options']        
            data = data['data']
        else:
            options = []
        if options and 'insert if not exists' not in options:
            q = DBSession.query(T).limit(1)
            if q.first():
                continue
        if 'delete first' in options:
            engine.execute('DELETE FROM {table}'.format(table=tablename)) #DBSession.Query(table).delete()
            DBSession.flush()
        tablenames.append(tablename)                
        keys = get_pkeys(table)
        id = 1
        print '----------->', tablename, data
        for d in data:
            filter_ = {}
            for key in keys:
                if key in d:
                    val = d[key]
                else:
                    if key=='id':
                      d.update({key:id})
                      val = id
                filter_[key] = val
            
            if filter_:
                q = DBSession.query(T).filter_by(**filter_)
                if q.first():
                    continue
            tbl = T()
            
            tbl.from_dict(d)
            print '----------->', tablename, d
            
            if tablename == 'users' and 'password' in d:
                tbl.password = d['password']
            DBSession.add(tbl)
            id+=1
            
    DBSession.flush()
    update_sequence(tablenames)
Пример #2
0
def insert_(fixtures):
    engine = DBSession.bind
    metadata = MetaData(engine)
    tablenames = []
    for tablename, data in fixtures:
        if "csv" in data:
            data["data"] = csv2fixture(data["csv"])
        if tablename == "users":
            T = User
            table = T.__table__
        else:
            schema, tablename_ = split_tablename(tablename)
            table = Table(tablename_, metadata, autoload=True, schema=schema)

            class T(Base, BaseModel, CommonModel):
                __table__ = table

        if type(data) == DictType:
            options = data["options"]
            data = data["data"]
        else:
            options = []
        if "insert if not exists" not in options:
            q = DBSession.query(T).limit(1)
            if q.first():
                continue
        tablenames.append(tablename)
        keys = get_pkeys(table)
        for d in data:
            filter_ = {}
            for key in keys:
                val = d[key]
                filter_[key] = val
            q = DBSession.query(T).filter_by(**filter_)
            if q.first():
                continue
            tbl = T()
            tbl.from_dict(d)
            if tablename == "users" and "password" in d:
                tbl.password = d["password"]
            DBSession.add(tbl)
    DBSession.flush()
    update_sequence(tablenames)
Пример #3
0
def insert_(fixtures):
    engine = DBSession.bind
    metadata = MetaData(engine)
    tablenames = []
    for tablename, data in fixtures:
        if 'csv' in data:
            data['data'] = csv2fixture(data['csv'])
        if tablename == 'users':
            T = User
            table = T.__table__
        else:
            schema, tablename_ = split_tablename(tablename)
            table = Table(tablename_, metadata, autoload=True, schema=schema)

            class T(Base, BaseModel, CommonModel):
                __table__ = table

        if type(data) == DictType:
            options = data['options']
            data = data['data']
        else:
            options = []
        if 'insert if not exists' not in options:
            q = DBSession.query(T).limit(1)
            if q.first():
                continue
        tablenames.append(tablename)
        keys = get_pkeys(table)
        for d in data:
            filter_ = {}
            for key in keys:
                val = d[key]
                filter_[key] = val
            q = DBSession.query(T).filter_by(**filter_)
            if q.first():
                continue
            tbl = T()
            tbl.from_dict(d)
            if tablename == 'users' and 'password' in d:
                tbl.password = d['password']
            DBSession.add(tbl)
    DBSession.flush()
    update_sequence(tablenames)