Skip to content

Added support for db_alias in migrations #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
19 changes: 12 additions & 7 deletions raster/migrations/0005_auto_20141014_0955.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
# -*- coding: utf-8 -*-
from django.db import migrations

def forward(apps, schema_editor):
migrations.RunSQL(
"DROP INDEX IF EXISTS raster_rastertile_rast_st_convexhull_idx;\
CREATE INDEX raster_rastertile_rast_st_convexhull_idx ON raster_rastertile USING gist( ST_ConvexHull(rast) )"
)
def reverse(apps, schema_editor):
migrations.RunSQL(
"DROP INDEX IF EXISTS raster_rastertile_rast_st_convexhull_idx"
)

class Migration(migrations.Migration):

dependencies = [
('raster', '0004_rasterlayermetadata'),
]

operations = [
migrations.RunSQL(
"DROP INDEX IF EXISTS raster_rastertile_rast_st_convexhull_idx; CREATE INDEX raster_rastertile_rast_st_convexhull_idx\
ON raster_rastertile USING gist( ST_ConvexHull(rast) )",
"DROP INDEX IF EXISTS raster_rastertile_rast_st_convexhull_idx"
)
]
migrations.RunPython(forward, reverse),
]
6 changes: 4 additions & 2 deletions raster/migrations/0017_copy_srids_20150922_0207.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def move_srid_from_layer_to_metadata_forward(apps, schema_editor):
"""
RasterLayer = apps.get_model("raster", "RasterLayer")
RasterLayerMetadata = apps.get_model("raster", "RasterLayerMetadata")
for lyr in RasterLayer.objects.all():
db_alias = schema_editor.connection.alias
for lyr in RasterLayer.objects.using(db_alias).all():
meta, created = RasterLayerMetadata.objects.get_or_create(rasterlayer=lyr)
meta.srid = lyr.srid
meta.save()
Expand All @@ -20,7 +21,8 @@ def move_srid_from_layer_to_metadata_backward(apps, schema_editor):
Copy the srids back to the raster layers.
"""
RasterLayer = apps.get_model("raster", "RasterLayer")
for lyr in RasterLayer.objects.all():
db_alias = schema_editor.connection.alias
for lyr in RasterLayer.objects.using(db_alias).all():
if hasattr(lyr, 'rasterlayermetadata'):
lyr.srid = lyr.rasterlayermetadata.srid
lyr.save()
Expand Down
6 changes: 4 additions & 2 deletions raster/migrations/0022_auto_20151110_0810.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ def move_parse_log_to_parse_status_objects_forward(apps, schema_editor):
"""
RasterLayer = apps.get_model("raster", "RasterLayer")
RasterLayerParseStatus = apps.get_model("raster", "RasterLayerParseStatus")
for lyr in RasterLayer.objects.all():
db_alias = schema_editor.connection.alias
for lyr in RasterLayer.objects.using(db_alias).all():
status, created = RasterLayerParseStatus.objects.get_or_create(rasterlayer=lyr)
status.log = lyr.parse_log
if 'Successfully finished parsing raster' in lyr.parse_log:
Expand All @@ -23,7 +24,8 @@ def move_parse_log_to_parse_status_objects_backward(apps, schema_editor):
Copy the srids back to the raster layers.
"""
RasterLayer = apps.get_model("raster", "RasterLayer")
for lyr in RasterLayer.objects.all():
db_alias = schema_editor.connection.alias
for lyr in RasterLayer.objects.using(db_alias).all():
if hasattr(lyr, 'parsestatus'):
lyr.parse_log = lyr.parsestatus.log
lyr.save()
Expand Down
6 changes: 4 additions & 2 deletions raster/migrations/0034_legendentryorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def move_legend_entries_to_through_model(apps, schema_editor):
"""
Legend = apps.get_model("raster", "Legend")
LegendEntryOrder = apps.get_model("raster", "LegendEntryOrder")
for leg in Legend.objects.all():
db_alias = schema_editor.connection.alias
for leg in Legend.objects.using(db_alias).all():
for entry in leg.entries.all():
# Respect unique contraint.
if LegendEntryOrder.objects.filter(legend=leg, legendentry=entry).exists():
Expand Down Expand Up @@ -43,7 +44,8 @@ def move_legend_entries_from_through_model_to_normal_m2m(apps, schema_editor):
"""
Legend = apps.get_model("raster", "Legend")
LegendEntryOrder = apps.get_model("raster", "LegendEntryOrder")
for leg in Legend.objects.all():
db_alias = schema_editor.connection.alias
for leg in Legend.objects.using(db_alias).all():
for enor in LegendEntryOrder.objects.filter(legend=leg):
leg.entries.add(enor.legendentry)

Expand Down
3 changes: 2 additions & 1 deletion raster/migrations/0036_auto_20170509_0548.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ def set_legend_on_entry(apps, schema_editor):
# Get models.
Legend = apps.get_model("raster", "Legend")
LegendEntryOrder = apps.get_model("raster", "LegendEntryOrder")
db_alias = schema_editor.connection.alias
# Loop through legend and entries.
for legend in Legend.objects.all():
for legend in Legend.objects.using(db_alias).all():
for entry in legend.entries.all():
# Set legend direct foreign key.
entry.legend = legend
Expand Down