'--fake-initial only works if all tables created in the initial
migration of an app exists. Database routers must be obeyed when doing
that check.'

@override_settings(MIGRATION_MODULES={'migrations': 'migrations.test_migrations'}, DATABASE_ROUTERS=['migrations.routers.TestRouter'])
def test_migrate_fake_initial(self):

    for db in connections:
        self.assertTableNotExists('migrations_author', using=db)
        self.assertTableNotExists('migrations_tribble', using=db)
    call_command('migrate', 'migrations', '0001', verbosity=0)
    call_command('migrate', 'migrations', '0001', verbosity=0, database='other')
    self.assertTableExists('migrations_author')
    self.assertTableNotExists('migrations_tribble')
    self.assertTableNotExists('migrations_author', using='other')
    self.assertTableExists('migrations_tribble', using='other')
    call_command('migrate', 'migrations', 'zero', fake=True, verbosity=0)
    call_command('migrate', 'migrations', 'zero', fake=True, verbosity=0, database='other')
    self.assertTableExists('migrations_author')
    self.assertTableExists('migrations_tribble', using='other')
    with self.assertRaises(DatabaseError):
        call_command('migrate', 'migrations', '0001', verbosity=0)
    out = io.StringIO()
    with mock.patch('django.core.management.color.supports_color', (lambda *args: False)):
        call_command('migrate', 'migrations', '0001', fake_initial=True, stdout=out, verbosity=1)
        call_command('migrate', 'migrations', '0001', fake_initial=True, verbosity=0, database='other')
    self.assertIn('migrations.0001_initial...    faked', out.getvalue().lower())
    call_command('migrate', verbosity=0)
    call_command('migrate', verbosity=0, database='other')
    self.assertTableExists('migrations_author')
    self.assertTableNotExists('migrations_tribble')
    self.assertTableExists('migrations_book')
    self.assertTableNotExists('migrations_author', using='other')
    self.assertTableNotExists('migrations_tribble', using='other')
    self.assertTableNotExists('migrations_book', using='other')
    call_command('migrate', 'migrations', 'zero', fake=True, verbosity=0)
    call_command('migrate', 'migrations', 'zero', fake=True, verbosity=0, database='other')
    self.assertTableExists('migrations_author')
    self.assertTableNotExists('migrations_tribble')
    self.assertTableExists('migrations_book')
    with self.assertRaises(DatabaseError):
        call_command('migrate', 'migrations', verbosity=0)
    with self.assertRaises(DatabaseError):
        call_command('migrate', 'migrations', fake_initial=True, verbosity=0)
    call_command('migrate', 'migrations', fake=True, verbosity=0)
    call_command('migrate', 'migrations', fake=True, verbosity=0, database='other')
    call_command('migrate', 'migrations', 'zero', verbosity=0)
    call_command('migrate', 'migrations', 'zero', verbosity=0, database='other')
    for db in connections:
        self.assertTableNotExists('migrations_author', using=db)
        self.assertTableNotExists('migrations_tribble', using=db)
        self.assertTableNotExists('migrations_book', using=db)
