Created
February 27, 2023 21:37
-
-
Save IamMiracleAlex/03f6ffbaf23bf8e9cf1ade5e5817381f to your computer and use it in GitHub Desktop.
Migrate model (Table) data from one database to another database in chunks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from django.core import serializers | |
def migrate(model, dest_db, size=2000, start=0, old_db="default"): | |
""" | |
Migrate data from one database to another database in chunks | |
Args: | |
- model: Model class | |
- dest_db: Destination database | |
- size: Chunk size, default is 2000 | |
- start: The start of the chunk, default 0 | |
- old_db: The Old database, default is `default` | |
""" | |
count = model.objects.using(old_db).count() | |
print("%s objects in model %s" % (count, model)) | |
for i in range(start, count, size): | |
print(i) | |
sys.stdout.flush() | |
original_data = model.objects.using(old_db).all()[i : i + size] | |
original_data_json = serializers.serialize("json", original_data) | |
new_data = serializers.deserialize("json", original_data_json, using=dest_db) | |
for n in new_data: | |
n.save(using=dest_db) | |
print("copied all for: ", model) | |
for model in desired_models: | |
migrate(model, dest_db) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment