summaryrefslogtreecommitdiff
path: root/chimere/migrations/0015_force_creation_of_aggregated.py
blob: 8f2cc1fe26a7a72e3af2eaa6b2dd3814f57314a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# -*- coding: utf-8 -*-
# Generated by Django 1.11.5 on 2018-03-17 12:29
from __future__ import unicode_literals

from django.db import migrations

SQL_POLY = """CREATE OR REPLACE VIEW chimere_aggregated_polygons
        (id, subcategory_id, status, polygon) AS
    SELECT 
        row_number() OVER (
            ORDER BY "chimere_polygon_categories"."subcategory_id",
                     "chimere_polygon"."status"),
        "chimere_polygon_categories"."subcategory_id", 
        "chimere_polygon"."status",
        ST_Multi(ST_Collect("chimere_polygon"."polygon"))
        
    FROM "chimere_polygon"
    
    LEFT OUTER JOIN "chimere_polygon_categories" ON (
        "chimere_polygon"."id" = "chimere_polygon_categories"."polygon_id")
    WHERE ("chimere_polygon"."start_date" IS NULL
           OR ("chimere_polygon"."start_date" <= CURRENT_DATE
               AND "chimere_polygon"."start_date" >= CURRENT_DATE)
           OR ("chimere_polygon"."start_date" <= CURRENT_DATE
               AND "chimere_polygon"."end_date" >= CURRENT_DATE))
    GROUP BY "chimere_polygon_categories"."subcategory_id",
             "chimere_polygon"."status";
    """

SQL_ROUTE = """CREATE OR REPLACE VIEW chimere_aggregated_routes
        (id, subcategory_id, status, route) AS
    SELECT
        row_number() OVER (
            ORDER BY "chimere_route_categories"."subcategory_id",
                     "chimere_route"."status"),
          "chimere_route_categories"."subcategory_id", "chimere_route"."status",
          ST_Multi(ST_Collect("chimere_route"."route"))
          
        FROM "chimere_route"
        
        LEFT OUTER JOIN "chimere_route_categories" ON (
            "chimere_route"."id" = "chimere_route_categories"."route_id")
        WHERE ("chimere_route"."start_date" IS NULL
               OR ("chimere_route"."start_date" <= CURRENT_DATE
                   AND "chimere_route"."start_date" >= CURRENT_DATE)
               OR ("chimere_route"."start_date" <= CURRENT_DATE
                   AND "chimere_route"."end_date" >= CURRENT_DATE))
        GROUP BY "chimere_route_categories"."subcategory_id",
                 "chimere_route"."status";
        """


class Migration(migrations.Migration):

    dependencies = [
        ('chimere', '0014_auto_20180313_1033'),
    ]

    operations = [
        migrations.RunSQL(SQL_POLY),
        migrations.RunSQL(SQL_ROUTE),
    ]