Skip to content

Commit

Permalink
fixing bugs in migration script for issue #180
Browse files Browse the repository at this point in the history
  • Loading branch information
luissian committed Apr 29, 2024
1 parent 753a73c commit 040736f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
17 changes: 10 additions & 7 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ if [ $upgrade == true ]; then
fi

if [ "$upgrade_type" = "full" ] || [ "$upgrade_type" = "app" ]; then
# Fetch the information from LibraryPool for run_process_id, before the migration

# update installation by sinchronize folders
echo "Copying files to installation folder"
Expand All @@ -390,6 +391,11 @@ if [ $upgrade == true ]; then
--exclude "logs" --exclude "documents" --exclude "migrations" --exclude "__pycache__" \
README.md LICENSE test conf core drylab clinic wetlab django_utils $INSTALL_PATH

# Fetch the values of LibraryPool for run_process_is
mkdir -p $SCRIPT_DIR/tmp
lib_pool_f_name=$SCRIPT_DIR/tmp/library_pool_info.tsv
mysql --user=$DB_USER --password=$DB_PASS --host=$DB_SERVER_IP --port=$DB_PORT iskylims -e "SELECT * FROM wetlab_library_pool" > $lib_pool_f_name

# update the settings.py and the main urls
echo "Update settings and url file."
# update_settings_and_urls
Expand All @@ -416,11 +422,6 @@ if [ $upgrade == true ]; then
echo "activate the virtualenv"
source virtualenv/bin/activate

# Fetch the values of LibraryPool for run_process_is
mkdir -p $SCRIPT_DIR/tmp
lib_pool_f_name=$SCRIPT_DIR/tmp/my_test.csv
upgrade_to_lib_pool_db

# Update the database
echo "checking for database changes"
if python manage.py makemigrations | grep -q "No changes"; then
Expand All @@ -438,15 +439,17 @@ if [ $upgrade == true ]; then
fi

# Restore the values in LibraryPool for run_process on the new structure
echo "Restore the values in LibraryPool for run_process on the new structure"
upgrade_to_lib_pool_db
# remove the tmp folder
# rm -rf $SCRIPT_DIR/tmp
rm -f $lib_pool_f_name


# Collect static files
echo "Running collect statics..."
python manage.py collectstatic
echo "Done collect statics"
rm -rf $SCRIPT_DIR/tmp

if [ $tables == true ] ; then
echo "Loading pre-filled tables..."
python manage.py loaddata conf/first_install_tables.json
Expand Down
1 change: 0 additions & 1 deletion wetlab/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class LibraryPoolAdmin(admin.ModelAdmin):
"platform",
"pool_code_id",
"register_user",
"run_process_id",
)


Expand Down
6 changes: 5 additions & 1 deletion wetlab/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1310,7 +1310,11 @@ class LibraryPool(models.Model):
register_user = models.ForeignKey(User, on_delete=models.CASCADE)
pool_state = models.ForeignKey(PoolStates, on_delete=models.CASCADE)
# To be deleted in release 3.2.0 or higher
# replace by run_process_id from foreign key to many to many
# replace by run_process_id
"""run_process_id = models.ForeignKey(
RunProcess, on_delete=models.CASCADE, null=True, blank=True
)
"""
run_process = models.ManyToManyField(RunProcess, blank=True)
# rum_proc = models.ManyToManyField(RunProcess, blank=True)
platform = models.ForeignKey(
Expand Down
26 changes: 17 additions & 9 deletions wetlab/scripts/library_pool_to_many_relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ def run(f_name):
The second part of the script fetch the data from the file and add the
run_process_pk to the run_process field of the LibraryPool instance
"""
if hasattr(wetlab.models.LibraryPool, "run_process_id"):
with open (f_name, "w") as fo:
for library_pool in wetlab.models.LibraryPool.objects.all():
# if library_pool.run_process_id:
# library_pool.run_process.add(library_pool.run_process_id)
if library_pool.run_process_id is not None:
fo.write(str(library_pool.id) + "," + str(library_pool.run_process_id.id) + "\n")
if hasattr(wetlab.models.LibraryPool, "run_process"):
with open (f_name, "r") as fh:
for line in fh:
library_pool_pk, run_process_pk = line.strip().split(",")
lines = fh.readlines()
heading = lines[0].strip().split("\t")
if "run_process_id_id" in heading:
r_proc_idx = heading.index("run_process_id_id")
pk_idx = 0
for line in lines[1:]:
l_data = line.strip().split("\t")
if l_data[r_proc_idx] == "NULL":
continue
library_pool_pk = l_data[pk_idx]
run_process_pk = l_data[r_proc_idx]

try:
library_pool = wetlab.models.LibraryPool.objects.get(id=library_pool_pk)
run_process = wetlab.models.RunProcess.objects.get(id=run_process_pk)
Expand All @@ -29,6 +32,11 @@ def run(f_name):
continue
if not library_pool.run_process.filter(id=run_process_pk).exists():
library_pool.run_process.add(run_process)
print(f"LibraryPool: {library_pool_pk} added run_process: {run_process_pk}")
else:
print("Data migration for LibraryPool was already done")
else:
print("LibraryPool model does not have run_process field")



0 comments on commit 040736f

Please sign in to comment.