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()
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'])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With