# -*- coding: utf-8 -*-
# desc 迁移机房
from models.datacenter import Datacenter
from sqlalchemy import *
from config import MIGRATE_DATABSE_URI
from application import db
class dcmigrate:
    def run(self):
        db.engine.execute("truncate table datacenter")
        engine = create_engine(MIGRATE_DATABSE_URI, encoding='utf-8',echo=True)
        connection = engine.connect()
        result = connection.execute("select name ,`desc` from location")
        tmplist=[]
        if result:
            for row in result:
                tmp={'name':row['name'],'desc':row['desc']}
                tmplist.append(tmp)
            for item in tmplist:
                tmpinfo=Datacenter.query.filter(Datacenter.name==item['name']).first()
                if tmpinfo:
                    continue

                dctarget=Datacenter(item['name'],item['desc'],'','SH-IDC','')
                db.session.add(dctarget)
                db.session.commit()
        connection.close()
