Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ DATABASE_PORT=3306
MYSQL_ROOT_PASSWORD=password
DATABASE_NAME=specify

# Decide whether to run key migration functions on startup.
RUN_KEY_MIGRATION_ON_STARTUP=0

# The following are database users with specific roles and privileges.
# If the migrator and app user are not defined, the system will use the master user credentials.
# See documenation https://discourse.specifysoftware.org/t/new-blank-database-creation-database-user-levels/3023
Expand Down
5 changes: 4 additions & 1 deletion docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ if [ "$1" = 've/bin/gunicorn' ] || [ "$1" = 've/bin/python' ]; then
./sp7_db_setup_check.sh # Setup db users and run mirgations
# ve/bin/python manage.py base_specify_migration
# ve/bin/python manage.py migrate
ve/bin/python manage.py run_key_migration_functions # Uncomment if you want the key migration functions to run on startup.
if [ "${RUN_KEY_MIGRATION_ON_STARTUP:-0}" = "1" ]; then
echo "Running key migration functions."
ve/bin/python manage.py run_key_migration_functions
fi
set -e
fi
exec "$@"
16 changes: 11 additions & 5 deletions specifyweb/backend/permissions/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,22 @@ def is_sp6_user_permissions_migrated(user, apps=apps) -> bool:
return UserRole.objects.filter(specifyuser=user).exists() or \
UserPolicy.objects.filter(specifyuser=user).exists()

def initialize(wipe: bool=False, apps=apps) -> None:
def initialize(
wipe: bool = False,
apps=apps,
*,
migrate_sp6_users: bool = True,
) -> None:
with transaction.atomic():
if wipe:
wipe_permissions(apps)
create_admins(apps)
create_roles(apps)
if 'test' in ''.join(sys.argv):
assign_users_to_roles_during_testing(apps)
else:
assign_users_to_roles(apps)
if migrate_sp6_users:
if 'test' in ''.join(sys.argv):
assign_users_to_roles_during_testing(apps)
else:
assign_users_to_roles(apps)

def create_admins(apps=apps) -> None:
UserPolicy = apps.get_model('permissions', 'UserPolicy')
Expand Down
8 changes: 6 additions & 2 deletions specifyweb/backend/setup_tool/schema_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def enqueue() -> None:
@app.task(bind=True, max_retries=SCHEMA_DEFAULTS_MISSING_DISCIPLINE_MAX_RETRIES)
def apply_schema_defaults_task(self, discipline_id: int):
"""Run schema localization defaults for one discipline in a background worker."""
task_id = getattr(self.request, 'id', None)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(pretty optional)

Right now I see we're calling this function using the run method?

apply_schema_defaults_task.run(discipline.id)

I see the Celery docs reccommend using apply to run tasks synchronously/locally:
https://docs.celeryq.dev/en/latest/reference/celery.app.task.html#celery.app.task.Task.apply

I'm not entirely sure what run does, but I'm assuming apply boils down to run. run generally states:

When called tasks apply the run() method. This method must be defined by all tasks (that is unless the call() method is overridden).
Run
The body of the task executed by workers.

https://docs.celeryq.dev/en/latest/reference/celery.app.task.html#celery.app.task.Task.run

Perhaps we can be more in-line with what Celery expects by instead using apply here. I'm not sure what the tradeoffs are for going with the current approach over apply, but it seems to run the function anyway, so not too big of a deal.


try:
discipline = Discipline.objects.get(id=discipline_id)
except Discipline.DoesNotExist as exc:
Expand All @@ -98,9 +100,11 @@ def apply_schema_defaults_task(self, discipline_id: int):
discipline_id,
SCHEMA_DEFAULTS_MISSING_DISCIPLINE_MAX_RETRIES,
)
finish_discipline_background_task(discipline_id, self.request.id)
if task_id is not None:
finish_discipline_background_task(discipline_id, task_id)
return
try:
apply_schema_defaults(discipline)
finally:
finish_discipline_background_task(discipline_id, self.request.id)
if task_id is not None:
finish_discipline_background_task(discipline_id, task_id)
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def fix_business_rules(stdout: WriteToStdOut | None = None):
log_and_run(funcs, stdout)

def initialize_permissions(apps):
initialize(False, apps)
initialize(False, apps, migrate_sp6_users=False)

def fix_permissions(stdout: WriteToStdOut | None = None):
funcs = [
Expand Down
Loading