from __future__ import unicode_literals

from django.db import migrations, connection


def reverse_func(state, schema):
    with connection.cursor() as cursor:
        cursor.execute(
            """
            UPDATE osf_pagecounter
            SET (action, guid_id, file_id, version) = ('download', NULL, NULL, NULL);
            """
        )

def separate_pagecounter_id(state, schema):
    """
    Splits the data in pagecounter _id field of form action:guid_id:file_id:version into
    four new columns: action(char), guid(fk), file(fk), version(int)
    """
    with connection.cursor() as cursor:
        cursor.execute(
            """
            UPDATE osf_pagecounter PC
            SET (action, guid_id, file_id, version) = (split_part(PC._id, ':', 1), G.id, F.id, NULLIF(split_part(PC._id, ':', 4), '')::int)
            FROM osf_guid G, osf_basefilenode F
            WHERE PC._id LIKE '%' || G._id || '%'
            AND PC._id LIKE '%' || F._id || '%';
            """
        )

class Migration(migrations.Migration):

    dependencies = [
        ('osf', '0144_pagecounter_index'),
    ]

    operations = [
        migrations.RunPython(
            separate_pagecounter_id, reverse_func
        ),
    ]

from __future__ import unicode_literals
from django.db import migrations


reverse_func = [
    """
    UPDATE osf_pagecounter
    SET (action, guid_id, file_id, version) = ('download', NULL, NULL, NULL);
    """
]

# Splits the data in pagecounter _id field of form action:guid_id:file_id:version into
# four new columns: action(char), guid(fk), file(fk), version(int)
separate_pagecounter_id = [
    """
    UPDATE osf_pagecounter PC
    SET (action, guid_id, file_id, version) = (split_part(PC._id, ':', 1), G.id, F.id, NULLIF(split_part(PC._id, ':', 4), '')::int)
    FROM osf_guid G, osf_basefilenode F
    WHERE PC._id LIKE '%' || G._id || '%'
    AND PC._id LIKE '%' || F._id || '%';
    """
]


class Migration(migrations.Migration):

    dependencies = [
        ('osf', '0144_pagecounter_index'),
    ]

    operations = [
        migrations.RunSQL(
            separate_pagecounter_id, reverse_func
        ),
    ]

Call RunSQL instead of RunPython in pagecounter data migration.
Call RunSQL instead of RunPython in pagecounter data migration.

  from __future__ import unicode_literals
- 
- from django.db import migrations, connection
+ from django.db import migrations
  
  
+ reverse_func = [
+     """
- def reverse_func(state, schema):
-     with connection.cursor() as cursor:
-         cursor.execute(
-             """
-             UPDATE osf_pagecounter
+     UPDATE osf_pagecounter
-             SET (action, guid_id, file_id, version) = ('download', NULL, NULL, NULL);
+     SET (action, guid_id, file_id, version) = ('download', NULL, NULL, NULL);
-             """
-         )
+     """
+ ]
  
- def separate_pagecounter_id(state, schema):
+ # Splits the data in pagecounter _id field of form action:guid_id:file_id:version into
+ # four new columns: action(char), guid(fk), file(fk), version(int)
+ separate_pagecounter_id = [
      """
-     Splits the data in pagecounter _id field of form action:guid_id:file_id:version into
-     four new columns: action(char), guid(fk), file(fk), version(int)
+     UPDATE osf_pagecounter PC
+     SET (action, guid_id, file_id, version) = (split_part(PC._id, ':', 1), G.id, F.id, NULLIF(split_part(PC._id, ':', 4), '')::int)
+     FROM osf_guid G, osf_basefilenode F
+     WHERE PC._id LIKE '%' || G._id || '%'
+     AND PC._id LIKE '%' || F._id || '%';
      """
+ ]
+ 
-     with connection.cursor() as cursor:
-         cursor.execute(
-             """
-             UPDATE osf_pagecounter PC
-             SET (action, guid_id, file_id, version) = (split_part(PC._id, ':', 1), G.id, F.id, NULLIF(split_part(PC._id, ':', 4), '')::int)
-             FROM osf_guid G, osf_basefilenode F
-             WHERE PC._id LIKE '%' || G._id || '%'
-             AND PC._id LIKE '%' || F._id || '%';
-             """
-         )
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('osf', '0144_pagecounter_index'),
      ]
  
      operations = [
-         migrations.RunPython(
+         migrations.RunSQL(
              separate_pagecounter_id, reverse_func
          ),
      ]
  
Call RunSQL instead of RunPython in pagecounter data migration.
## Code Before:
from __future__ import unicode_literals

from django.db import migrations, connection


def reverse_func(state, schema):
    with connection.cursor() as cursor:
        cursor.execute(
            """
            UPDATE osf_pagecounter
            SET (action, guid_id, file_id, version) = ('download', NULL, NULL, NULL);
            """
        )

def separate_pagecounter_id(state, schema):
    """
    Splits the data in pagecounter _id field of form action:guid_id:file_id:version into
    four new columns: action(char), guid(fk), file(fk), version(int)
    """
    with connection.cursor() as cursor:
        cursor.execute(
            """
            UPDATE osf_pagecounter PC
            SET (action, guid_id, file_id, version) = (split_part(PC._id, ':', 1), G.id, F.id, NULLIF(split_part(PC._id, ':', 4), '')::int)
            FROM osf_guid G, osf_basefilenode F
            WHERE PC._id LIKE '%' || G._id || '%'
            AND PC._id LIKE '%' || F._id || '%';
            """
        )

class Migration(migrations.Migration):

    dependencies = [
        ('osf', '0144_pagecounter_index'),
    ]

    operations = [
        migrations.RunPython(
            separate_pagecounter_id, reverse_func
        ),
    ]

## Instruction:
Call RunSQL instead of RunPython in pagecounter data migration.
## Code After:
from __future__ import unicode_literals
from django.db import migrations


reverse_func = [
    """
    UPDATE osf_pagecounter
    SET (action, guid_id, file_id, version) = ('download', NULL, NULL, NULL);
    """
]

# Splits the data in pagecounter _id field of form action:guid_id:file_id:version into
# four new columns: action(char), guid(fk), file(fk), version(int)
separate_pagecounter_id = [
    """
    UPDATE osf_pagecounter PC
    SET (action, guid_id, file_id, version) = (split_part(PC._id, ':', 1), G.id, F.id, NULLIF(split_part(PC._id, ':', 4), '')::int)
    FROM osf_guid G, osf_basefilenode F
    WHERE PC._id LIKE '%' || G._id || '%'
    AND PC._id LIKE '%' || F._id || '%';
    """
]


class Migration(migrations.Migration):

    dependencies = [
        ('osf', '0144_pagecounter_index'),
    ]

    operations = [
        migrations.RunSQL(
            separate_pagecounter_id, reverse_func
        ),
    ]
