forked from geosolutions-it/geonode-mapstore-client
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fixes #9252] Migration form Version 3.2 to 4 – map config is missing (…
…#959) * [Fixes #9252] Migration form Version 3.2 to 4 – map config is missing * [Fixes #9252] Add missing table drop * [Fixes #9252] Fix PR comments
- Loading branch information
1 parent
f8e3bd7
commit edb5c92
Showing
1 changed file
with
94 additions
and
0 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
geonode_mapstore_client/migrations/0002_migrate_map_blob.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# Generated by Django 3.2.13 on 2022-04-28 07:32 | ||
import ast | ||
import base64 | ||
from django.db import migrations, connections | ||
|
||
|
||
drop_mapstore2_adapter_mapstoreattribute = "DROP TABLE IF EXISTS mapstore2_adapter_mapstoreattribute CASCADE;" | ||
drop_mapstore2_adapter_mapstoredata = "DROP TABLE IF EXISTS mapstore2_adapter_mapstoredata CASCADE;" | ||
drop_mapstore2_adapter_mapstoreresource = "DROP TABLE IF EXISTS mapstore2_adapter_mapstoreresource CASCADE;" | ||
drop_mapstore2_adapter_mapstoreresource_attributes = "DROP TABLE IF EXISTS mapstore2_adapter_mapstoreresource_attributes CASCADE;" | ||
|
||
|
||
def migrate_map_forward(apps, schema_editor): | ||
|
||
exists = False | ||
sql_exists = "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'mapstore2_adapter_mapstoredata');" | ||
with connections['default'].cursor() as cursor: | ||
cursor.execute(sql_exists) | ||
result = cursor.fetchall() | ||
if result: | ||
exists = result[0][0] | ||
if exists: | ||
# We can't import the Map model directly as it may be a newer | ||
# version than this migration expects. We use the historical version. | ||
ResourceBase = apps.get_model('base', 'ResourceBase') | ||
for _resource in ResourceBase.objects.filter(resource_type='map').iterator(): | ||
to_update = {} | ||
# We can't use map.resourcebase_ptr, we need to explicitly retrieve the resourcebase | ||
# mapstore2_adapter does not exist anymore as an app. So we need raw sql to get the blob from the old table. | ||
sql_string = f'SELECT blob from mapstore2_adapter_mapstoredata where resource_id={_resource.id};' | ||
''' | ||
Getting the Data Blob | ||
''' | ||
with connections['default'].cursor() as cursor: | ||
cursor.execute(sql_string) | ||
result = cursor.fetchall() | ||
if result: | ||
to_update['blob'] = result[0][0] | ||
|
||
sql_string = f'SELECT name, value from mapstore2_adapter_mapstoreattribute where resource_id={_resource.id};' | ||
''' | ||
Getting the attributes | ||
''' | ||
with connections['default'].cursor() as cursor: | ||
cursor.execute(sql_string) | ||
result = cursor.fetchall() | ||
if result: | ||
for x in result: | ||
try: | ||
''' | ||
If is a byte we have to decode it | ||
''' | ||
to_update[x[0]] = base64.b64decode(ast.literal_eval(x[1])).decode() | ||
except: | ||
to_update[x[0]] = x[1] | ||
|
||
thumb = to_update.pop('thumbnail', None) | ||
if thumb and 'data:image/' not in thumb: | ||
to_update['thumbnail_url'] = to_update.pop('thumbnail') | ||
''' | ||
Updating the resource | ||
''' | ||
if to_update: | ||
ResourceBase.objects.filter(id=_resource.id).update(**to_update) | ||
|
||
|
||
|
||
def migrate_map_reverse(apps, schema_editor): | ||
# We can't import the Map model directly as it may be a newer | ||
# version than this migration expects. We use the historical version. | ||
|
||
Map = apps.get_model('maps', 'Map') | ||
ResourceBase = apps.get_model('base', 'ResourceBase') | ||
for _map in Map.objects.iterator(): | ||
# We can't use map.resourcebase_ptr, we need to explicitly retrieve the resourcebase | ||
resourcebase = ResourceBase.objects.get(id=_map.resourcebase_ptr_id) | ||
resourcebase.blob = dict() | ||
resourcebase.save() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('geonode_mapstore_client', '0001_clean_prev_version_geoapps'), | ||
('maps', '0042_remove_maplayer_styles'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(migrate_map_forward, migrate_map_reverse), | ||
migrations.RunSQL(drop_mapstore2_adapter_mapstoreattribute), | ||
migrations.RunSQL(drop_mapstore2_adapter_mapstoredata), | ||
migrations.RunSQL(drop_mapstore2_adapter_mapstoreresource), | ||
migrations.RunSQL(drop_mapstore2_adapter_mapstoreresource_attributes) | ||
] |