from __future__ import unicode_literals

from django import VERSION
from django.db import migrations, models
if VERSION >= (1, 8):
    from django.contrib.postgres.fields import ArrayField
    chapters_field = ArrayField(base_field=models.CharField(max_length=100), default=list, size=None)
else:
    chapters_field = models.Field()  # Dummy field


class PostgresOnlyCreateModel(migrations.CreateModel):
    def database_forwards(self, app_label, schema_editor, from_state, to_state):
        if VERSION >= (1, 8) and schema_editor.connection.vendor.startswith("postgres"):
            super(PostgresOnlyCreateModel, self).database_forwards(app_label, schema_editor, from_state, to_state)

    def database_backwards(self, app_label, schema_editor, from_state, to_state):
        if VERSION >= (1, 8) and schema_editor.connection.vendor.startswith("postgres"):
            super(PostgresOnlyCreateModel, self).database_backwards(app_label, schema_editor, from_state, to_state)


class Migration(migrations.Migration):

    dependencies = [
        ('core', '0003_withfloatfield'),
    ]

    operations = [
        PostgresOnlyCreateModel(
            name='BookWithChapters',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=100, verbose_name='Book name')),
                ('chapters', chapters_field)
            ],
        ),
    ]

@@ -101,16 +101,91 @@
 , models
+%0A%0Acan_use_arrayfield = False%0Achapters_field = models.Field()  # Dummy field
 %0Aif VERS
@@ -199,16 +199,29 @@
 (1, 8):%0A
+    try:%0A    
     from
@@ -270,24 +270,28 @@
 ayField%0A    
+   
+ 
 chapters_fie
@@ -380,694 +380,141 @@
 ne)%0A
-else:%0A    chapters_field = models.Field()  # Dummy field%0A%0A%0Aclass PostgresOnlyCreateModel(migrations.CreateModel):%0A    def database_forwards(self, app_label, schema_editor, from_state, to_state):%0A        if VERSION %3E= (1, 8) and schema_editor.connection.vendor.startswith(%22postgres%22):%0A            super(PostgresOnlyCreateModel, self).database_forwards(app_label, schema_editor, from_state, to_state)%0A%0A    def database_backwards(self, app_label, schema_editor, from_state, to_state):%0A        if VERSION %3E= (1, 8) and schema_editor.connection.vendor.startswith(%22postgres%22):%0A            super(PostgresOnlyCreateModel, self).database_backwards(app_label, schema_editor, from_state, to_state)
+        can_use_arrayfield = True%0A    except ImportError:%0A        # We can't use ArrayField if psycopg2 is not installed%0A        pass
 %0A%0A%0Ac
@@ -643,29 +643,57 @@
  = %5B
+%5D%0A
 %0A    
-    PostgresOnly
+pg_only_operations = %5B%0A        migrations.
 Crea
@@ -1029,16 +1029,336 @@
        ),%0A    %5D%0A
+%0A    def apply(self, project_state, schema_editor, collect_sql=False):%0A        if can_use_arrayfield and schema_editor.connection.vendor.startswith(%22postgres%22):%0A            self.operations = self.operations + self.pg_only_operations%0A        return super(Migration, self).apply(project_state, schema_editor, collect_sql)%0A

Refactor migration script to handle case where Postgres is being used but psycopg2 isn't installed