Skip to content

Commit

Permalink
Create command to copy packages and related
Browse files Browse the repository at this point in the history
    * Add minecode database

Signed-off-by: Jono Yang <[email protected]>
  • Loading branch information
JonoYang committed Oct 17, 2023
1 parent b38be78 commit 76cd5b3
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
71 changes: 71 additions & 0 deletions packagedb/management/commands/copy_legacy_package_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# purldb is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/purldb for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#

import logging
import sys

from minecode.management.commands import VerboseCommand
from packagedb.models import Package, DependentPackage, Party

TRACE = False

logger = logging.getLogger(__name__)
logging.basicConfig(stream=sys.stdout)
logger.setLevel(logging.INFO)


class Command(VerboseCommand):
def handle(self, *args, **options):
packages = Package.objects.using('minecode').all()
package_count = packages.count()
iterator = packages.iterator(chunk_size=2000)
unsaved_packages = []
unsaved_dependencies = []
unsaved_parties = []

i = 0
for package in iterator:
if Package.objects.filter(download_url=package.download_url).exists():
continue
if not (i % 100) and unsaved_packages:
Package.objects.bulk_create(
unsaved_packages
)
DependentPackage.objects.bulk_create(
unsaved_dependencies
)
Party.objects.bulk_create(
unsaved_parties
)
unsaved_packages = []
unsaved_dependencies = []
unsaved_parties = []
print(f" {i:,} / {package_count:,} saved")
else:
unsaved_packages.append(package)
dependencies = package.dependencies.all()
unsaved_dependencies.extend(list(dependencies))
parties = package.parties.all()
unsaved_parties.extend(list(parties))
i += 1

if unsaved_packages:
Package.objects.bulk_create(
unsaved_packages
)
DependentPackage.objects.bulk_create(
unsaved_dependencies
)
Party.objects.bulk_create(
unsaved_parties
)
unsaved_packages = []
unsaved_dependencies = []
unsaved_parties = []
print(f" {i:,} / {package_count:,} saved")
11 changes: 10 additions & 1 deletion purldb/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,16 @@
'PASSWORD': env.str('PACKAGEDB_DB_PASSWORD', 'packagedb'),
'PORT': env.str('PACKAGEDB_DB_PORT', '5432'),
'ATOMIC_REQUESTS': True,
}
},
'minecode': {
'ENGINE': env.str('PACKAGEDB_DB_ENGINE', 'django.db.backends.postgresql'),
'HOST': env.str('PACKAGEDB_DB_HOST', 'localhost'),
'NAME': 'minecode',
'USER': 'minecode',
'PASSWORD': 'minecode',
'PORT': env.str('PACKAGEDB_DB_PORT', '5432'),
'ATOMIC_REQUESTS': True,
},
}

DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
Expand Down

0 comments on commit 76cd5b3

Please sign in to comment.