Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Peewee ORM - Copying data from multiple database in a main one

I want to copy the data from multiple database, processing them, and after that moving them to a main database. All the databases have the same schema.

Even if I open and close the databases, peewee always connect to the same database(the 3rd in the list) and is not respecting the connections order.

databases = [spie_db, opticsorg_db, phcom_db]


# map to a dictionary the values from the record
def mapping(record):
    comp_d = {'name': record.name,
              'address': record.address,
              'country': record.country,
              'website': record.website,
              'domain':  record.domain
              }
    return comp_d


def merge_data():
    company_list = []
    for database in databases:
        database.connect()
        # cycle trough db
        for record in Company.select():
            # append each record to the list
            company_list.append(mapping(record))
        database.close()
    return company_list


# get data from the other databases
companies = merge_data()
# the merge database
db.connect()
# add records in the merge db
for company in companies:

    Company.create(name=company['name'], address=company['address'], country=company['country'],
                   website=company['website'], domain=company['domain'])

db.close()
like image 983
user3541631 Avatar asked Jan 19 '17 12:01

user3541631


1 Answers

You'll want to manually set the database in the model's Meta options:

Company._meta.database = db
db.connect()
for company in companies:

    Company.create(name=company['name'], address=company['address'], country=company['country'],
                   website=company['website'], domain=company['domain'])
like image 104
coleifer Avatar answered Sep 18 '22 22:09

coleifer