Skip to content

Commit

Permalink
[ENG-3863] Move schema ensuring and schema blocks update to post-migr…
Browse files Browse the repository at this point in the history
…ate signals (#9974)

* Set noop to all ensure_schema migrations
* Set noop to shema and shema block updates
* Add ensuring schema and updating schema blocks to post-migrate signals
* Set noop to one-time data migrations, which include
  * Preprint permissions
  * Schema to schema blocks
  * Draft node permissions
* Remove manual post-migration signals from PND
* Move generate_object_id into migration utils
* Replace duplicate function with inline imports
  • Loading branch information
cslzchen authored Jul 13, 2022
1 parent 2490d05 commit 4e63d3a
Show file tree
Hide file tree
Showing 26 changed files with 45 additions and 236 deletions.
13 changes: 12 additions & 1 deletion osf/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,42 @@
update_permission_groups,
update_waffle_flags,
create_cache_table,
update_license
update_license,
add_registration_schemas
)

logger = logging.getLogger(__file__)


class AppConfig(BaseAppConfig):

name = 'osf'
app_label = 'osf'
managed = True

def ready(self):
super(AppConfig, self).ready()

post_migrate.connect(
add_registration_schemas,
dispatch_uid='osf.apps.add_registration_schemas'
)

post_migrate.connect(
update_permission_groups,
dispatch_uid='osf.apps.update_permissions_groups'
)

post_migrate.connect(
update_license,
dispatch_uid='osf.apps.ensure_licenses',
)

post_migrate.connect(
update_waffle_flags,
dispatch_uid='osf.apps.update_waffle_flags'
)

post_migrate.connect(
create_cache_table,
dispatch_uid='osf.apps.create_cache_table'
Expand Down
6 changes: 1 addition & 5 deletions osf/migrations/0038_ensure_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import logging

from django.db import migrations
from osf.utils.migrations import ensure_schemas, remove_schemas


logger = logging.getLogger(__file__)

Expand All @@ -17,6 +15,4 @@ class Migration(migrations.Migration):
('osf', '0037_ensure_licenses'),
]

operations = [
migrations.RunPython(ensure_schemas, remove_schemas),
]
operations = []
6 changes: 1 addition & 5 deletions osf/migrations/0077_ensure_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import logging

from django.db import migrations
from osf.utils.migrations import ensure_schemas, remove_schemas


logger = logging.getLogger(__file__)

Expand All @@ -17,6 +15,4 @@ class Migration(migrations.Migration):
('osf', '0076_action_rename'),
]

operations = [
migrations.RunPython(ensure_schemas, remove_schemas),
]
operations = []
6 changes: 1 addition & 5 deletions osf/migrations/0078_ensure_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import logging

from django.db import migrations
from osf.utils.migrations import ensure_schemas, remove_schemas


logger = logging.getLogger(__file__)

Expand All @@ -15,6 +13,4 @@ class Migration(migrations.Migration):
('osf', '0077_add_maintenance_permissions'),
]

operations = [
migrations.RunPython(ensure_schemas, remove_schemas),
]
operations = []
5 changes: 1 addition & 4 deletions osf/migrations/0080_ensure_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging

from django.db import migrations
from osf.utils.migrations import ensure_schemas, remove_schemas


logger = logging.getLogger(__file__)
Expand All @@ -15,6 +14,4 @@ class Migration(migrations.Migration):
('osf', '0079_merge_20180207_1545'),
]

operations = [
migrations.RunPython(ensure_schemas, remove_schemas),
]
operations = []
6 changes: 1 addition & 5 deletions osf/migrations/0096_ensure_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import logging

from django.db import migrations
from osf.utils.migrations import ensure_schemas, remove_schemas


logger = logging.getLogger(__file__)

Expand All @@ -15,6 +13,4 @@ class Migration(migrations.Migration):
('osf', '0095_reset_osf_abstractprovider_licenses_acceptable_id_seq'),
]

operations = [
migrations.RunPython(ensure_schemas, remove_schemas),
]
operations = []
6 changes: 1 addition & 5 deletions osf/migrations/0112_ensure_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import logging

from django.db import migrations
from osf.utils.migrations import ensure_schemas, remove_schemas


logger = logging.getLogger(__file__)

Expand All @@ -15,6 +13,4 @@ class Migration(migrations.Migration):
('osf', '0111_auto_20180605_1240'),
]

operations = [
migrations.RunPython(ensure_schemas, remove_schemas),
]
operations = []
6 changes: 0 additions & 6 deletions osf/migrations/0136_preprint_node_divorce.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from django.contrib.contenttypes.models import ContentType
from django.db import migrations
from django.db.models import Func, Value, F, Q
from django.core.management.sql import emit_post_migrate_signal
from bulk_update.helper import bulk_update

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -89,11 +88,6 @@ def reverse_func(apps, schema_editor):
def format_group(self, name):
return group_format.format(self=self, group=name)

def emit_signals(state, schema):
# this is to make sure that the permissions created earlier exist!
emit_post_migrate_signal(2, False, 'default')
logger.info('Starting preprint node divorce [SQL]:')


class Migration(migrations.Migration):

Expand Down
30 changes: 1 addition & 29 deletions osf/migrations/0137_transfer_preprint_service_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,10 @@
from django.db import migrations


def unmigrate_preprint_service_permissions(state, schema):
Permission = state.get_model('auth', 'permission')

# New permission groups
Permission.objects.filter(codename='add_preprint').update(codename='add_preprintservice', name='Can add preprint service')
Permission.objects.filter(codename='change_preprint').update(codename='change_preprintservice', name='Can change preprint service')
Permission.objects.filter(codename='delete_preprint').update(codename='delete_preprintservice', name='Can delete preprint service')
Permission.objects.filter(codename='view_preprint').update(codename='view_preprintservice', name='Can view preprint service details in the admin app.')

def migrate_preprint_service_permissions(state, schema):
"""
Django permissions on the preprint model have new names.
"""
Permission = state.get_model('auth', 'permission')

Permission.objects.filter(codename='add_preprint').delete()
Permission.objects.filter(codename='change_preprint').delete()
Permission.objects.filter(codename='delete_preprint').delete()
Permission.objects.filter(codename='view_preprint').delete()

# Old permission groups
Permission.objects.filter(codename='add_preprintservice').update(codename='add_preprint', name='Can add preprint')
Permission.objects.filter(codename='change_preprintservice').update(codename='change_preprint', name='Can change preprint')
Permission.objects.filter(codename='delete_preprintservice').update(codename='delete_preprint', name='Can delete preprint')
Permission.objects.filter(codename='view_preprintservice').update(codename='view_preprint', name='Can view preprint details in the admin app')

class Migration(migrations.Migration):

dependencies = [
('osf', '0136_preprint_node_divorce'),
]

operations = [
migrations.RunPython(migrate_preprint_service_permissions, unmigrate_preprint_service_permissions),
]
operations = []
3 changes: 0 additions & 3 deletions osf/migrations/0142_change_registration_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging

from django.db import migrations
from osf.utils.migrations import ensure_schemas


logger = logging.getLogger(__file__)
Expand All @@ -16,6 +15,4 @@ class Migration(migrations.Migration):
]

operations = [
# To reverse this migrations simply revert changes to the schema and re-run
migrations.RunPython(ensure_schemas, ensure_schemas),
]
5 changes: 1 addition & 4 deletions osf/migrations/0152_ensure_schemas.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import unicode_literals

from django.db import migrations
from osf.utils.migrations import ensure_schemas


class Migration(migrations.Migration):
Expand All @@ -10,6 +9,4 @@ class Migration(migrations.Migration):
('osf', '0151_auto_20181215_1911'),
]

operations = [
migrations.RunPython(ensure_schemas, ensure_schemas),
]
operations = []
6 changes: 0 additions & 6 deletions osf/migrations/0162_post_migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@

import logging
from django.db import migrations
from django.core.management.sql import emit_post_migrate_signal

logger = logging.getLogger(__name__)

def post_migrate_signal(state, schema):
# this is to make sure that the permissions created earlier exist!
emit_post_migrate_signal(3, False, 'default')
logger.info('Starting guardian/groups migration [SQL]:')


class Migration(migrations.Migration):

Expand Down
6 changes: 1 addition & 5 deletions osf/migrations/0170_ensure_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
from __future__ import unicode_literals

from django.db import migrations
from osf.utils.migrations import ensure_schemas


class Migration(migrations.Migration):
dependencies = [
('osf', '0169_merge_20190618_1429'),
]

operations = [
# To reverse this migrations simply revert changes to the schema and re-run
migrations.RunPython(ensure_schemas, ensure_schemas),
]
operations = []
6 changes: 1 addition & 5 deletions osf/migrations/0172_ensure_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from __future__ import unicode_literals

from django.db import migrations
from osf.utils.migrations import ensure_schemas


class Migration(migrations.Migration):
Expand All @@ -12,7 +11,4 @@ class Migration(migrations.Migration):
('osf', '0171_add_registration_files_count'),
]

operations = [
# To reverse this migrations simply revert changes to the schema and re-run
migrations.RunPython(ensure_schemas, ensure_schemas),
]
operations = []
6 changes: 1 addition & 5 deletions osf/migrations/0173_ensure_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from __future__ import unicode_literals

from django.db import migrations
from osf.utils.migrations import ensure_schemas


class Migration(migrations.Migration):
Expand All @@ -12,7 +11,4 @@ class Migration(migrations.Migration):
('osf', '0172_ensure_schemas'),
]

operations = [
# To reverse this migrations simply revert changes to the schema and re-run
migrations.RunPython(ensure_schemas, ensure_schemas),
]
operations = []
57 changes: 1 addition & 56 deletions osf/migrations/0191_migrate_schemas_to_schemablocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,9 @@

import logging
from django.db import migrations
from osf.utils.migrations import map_schemas_to_schemablocks, unmap_schemablocks

logger = logging.getLogger(__file__)

def remove_version_1_schemas(state, schema):
RegistrationSchema = state.get_model('osf', 'registrationschema')
assert RegistrationSchema.objects.filter(schema_version=1, abstractnode__isnull=False).count() == 0
assert RegistrationSchema.objects.filter(schema_version=1, draftregistration__isnull=False).count() == 0
RegistrationSchema.objects.filter(schema_version=1).delete()

def update_schemaless_registrations(state, schema):
RegistrationSchema = state.get_model('osf', 'registrationschema')
AbstractNode = state.get_model('osf', 'abstractnode')

open_ended_schema = RegistrationSchema.objects.get(name='Open-Ended Registration', schema_version=2)
open_ended_meta = {
'{}'.format(open_ended_schema._id): {
'summary': {
'comments': [],
'extra': [],
'value': ''
}
}
}

schemaless_regs_with_meta = AbstractNode.objects.filter(type='osf.registration', registered_schema__isnull=True).exclude(registered_meta={})
schemaless_regs_without_meta = AbstractNode.objects.filter(type='osf.registration', registered_schema__isnull=True, registered_meta={})

for reg in schemaless_regs_without_meta.all():
reg.registered_schema.add(open_ended_schema)
reg.registered_meta = open_ended_meta
reg.save()

for reg in schemaless_regs_with_meta.all():
reg.registered_schema.add(RegistrationSchema.objects.get(_id=reg.registered_meta.keys()[0]))

def update_schema_configs(state, schema):
RegistrationSchema = state.get_model('osf', 'registrationschema')
for rs in RegistrationSchema.objects.all():
if rs.schema.get('description', False):
rs.description = rs.schema['description']
if rs.schema.get('config', False):
rs.config = rs.schema['config']
rs.save()

def unset_schema_configs(state, schema):
RegistrationSchema = state.get_model('osf', 'registrationschema')
RegistrationSchema.objects.update(
config=dict(),
description='',
)


def noop(*args, **kwargs):
pass

Expand All @@ -66,9 +16,4 @@ class Migration(migrations.Migration):
('osf', '0190_add_schema_block_models')
]

operations = [
migrations.RunPython(remove_version_1_schemas, noop),
migrations.RunPython(update_schemaless_registrations, noop),
migrations.RunPython(update_schema_configs, unset_schema_configs),
migrations.RunPython(map_schemas_to_schemablocks, unmap_schemablocks)
]
operations = []
Loading

0 comments on commit 4e63d3a

Please sign in to comment.