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)
            ],
        ),
    ]

from __future__ import unicode_literals

from django import VERSION
from django.db import migrations, models

can_use_arrayfield = False
chapters_field = models.Field()  # Dummy field
if VERSION >= (1, 8):
    try:
        from django.contrib.postgres.fields import ArrayField
        chapters_field = ArrayField(base_field=models.CharField(max_length=100), default=list, size=None)
        can_use_arrayfield = True
    except ImportError:
        # We can't use ArrayField if psycopg2 is not installed
        pass


class Migration(migrations.Migration):

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

    operations = []

    pg_only_operations = [
        migrations.CreateModel(
            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)
            ],
        ),
    ]

    def apply(self, project_state, schema_editor, collect_sql=False):
        if can_use_arrayfield and schema_editor.connection.vendor.startswith("postgres"):
            self.operations = self.operations + self.pg_only_operations
        return super(Migration, self).apply(project_state, schema_editor, collect_sql)

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

  from __future__ import unicode_literals
  
  from django import VERSION
  from django.db import migrations, models
+ 
+ can_use_arrayfield = False
+ chapters_field = models.Field()  # Dummy field
  if VERSION >= (1, 8):
+     try:
-     from django.contrib.postgres.fields import ArrayField
+         from django.contrib.postgres.fields import ArrayField
-     chapters_field = ArrayField(base_field=models.CharField(max_length=100), default=list, size=None)
+         chapters_field = ArrayField(base_field=models.CharField(max_length=100), default=list, size=None)
+         can_use_arrayfield = True
+     except ImportError:
+         # We can't use ArrayField if psycopg2 is not installed
+         pass
- 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 = [
+     operations = []
-         PostgresOnlyCreateModel(
+ 
+     pg_only_operations = [
+         migrations.CreateModel(
              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)
              ],
          ),
      ]
  
+     def apply(self, project_state, schema_editor, collect_sql=False):
+         if can_use_arrayfield and schema_editor.connection.vendor.startswith("postgres"):
+             self.operations = self.operations + self.pg_only_operations
+         return super(Migration, self).apply(project_state, schema_editor, collect_sql)
+ 
Refactor migration script to handle case where Postgres is being used but psycopg2 isn't installed
## Code Before:
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)
            ],
        ),
    ]

## Instruction:
Refactor migration script to handle case where Postgres is being used but psycopg2 isn't installed
## Code After:
from __future__ import unicode_literals

from django import VERSION
from django.db import migrations, models

can_use_arrayfield = False
chapters_field = models.Field()  # Dummy field
if VERSION >= (1, 8):
    try:
        from django.contrib.postgres.fields import ArrayField
        chapters_field = ArrayField(base_field=models.CharField(max_length=100), default=list, size=None)
        can_use_arrayfield = True
    except ImportError:
        # We can't use ArrayField if psycopg2 is not installed
        pass


class Migration(migrations.Migration):

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

    operations = []

    pg_only_operations = [
        migrations.CreateModel(
            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)
            ],
        ),
    ]

    def apply(self, project_state, schema_editor, collect_sql=False):
        if can_use_arrayfield and schema_editor.connection.vendor.startswith("postgres"):
            self.operations = self.operations + self.pg_only_operations
        return super(Migration, self).apply(project_state, schema_editor, collect_sql)
