 rmdirp = require('../helpers/rmdirp.coffee')
 GeneralHelper = require("../helpers/general.coffee")
 CustomAssertions = require("../helpers/custom_assertions.coffee")
+SchemaMigration = rek("lib/sails-migrations/schema_migration.coffee")
 
 Migrator = rek("lib/sails-migrations/migrator.coffee")
 AdapterWrapper = rek("lib/sails-migrations/adapter_wrapper.coffee")

  describe 'db:migrate', ->
    it 'should be able to run a migration', (done)->
      @tableName = 'one_migration'
      migrateScope(@adapter, migrationsPath, @tableName, (err)=>
        return done(err) if err
        CustomAssertions.assertTableColumnCount(@AdapterWrapper, @tableName, 3, done)
      )

    it 'should be able to run 2 migrations', (done)->
      @tableName = 'two_migrations'
      migrateScope(@adapter, migrationsPath, @tableName, (err)=>
        return done(err) if err
        CustomAssertions.assertTableColumnCount(@AdapterWrapper, @tableName, 4, done)
      )

    afterEach (done)->
      @AdapterWrapper.drop(@tableName, done)

  describe 'db:rollback', ->
    it 'should rollback one migration', (done)->
      @tableName = 'one_migration'
      rollbackScope(@adapter, migrationsPath, @tableName, (err)=>
        return done(err) if err
        CustomAssertions.assertTableColumnCount(@AdapterWrapper, @tableName, 0, done)
      )

    it 'should rollback once with two migrations', (done)->
      @tableName = 'two_migrations'
      rollbackScope(@adapter, migrationsPath, @tableName, (err)=>
        return done(err) if err
        CustomAssertions.assertTableColumnCount(@AdapterWrapper, @tableName, 3, done)
      )

    afterEach (done)->
      @AdapterWrapper.drop(@tableName, done)

# Runs migration on the migration files that include provided name (scope)
migrateScope = (adapter, migrationsPath, scope, cb)->
  copyFixturesToMigrationsPath(scope)
  Migrator.migrate(adapter, migrationsPath, null, (err)=>
    return cb(err) if err
    SchemaMigration.getAllVersions(adapter, cb)
  )

# Runs rollback on the migration files that include provided name (scope)
rollbackScope = (adapter, migrationsPath, scope, cb)->
  migrateScope(adapter, migrationsPath, scope, (err)=>
    return cb(err) if err
    Migrator.rollback(adapter, migrationsPath, null, (err)=>
      return cb(err) if err
      SchemaMigration.getAllVersions(adapter, cb)
    )
  )

describe 'migration', ->
