var vows = require('vows');
var Promise = require('bluebird');
var assert = require('assert');
var dbmeta = require('db-meta');
var pg = require('pg');
var dataType = require('../../lib/data_type');
var driver = require('../../lib/driver');

var databaseUrl = 'postgres://localhost/db_migrate_test';

var internals = {};
internals.migrationTable = 'migrations';

vows.describe('pg').addBatch({
    'create schema which needs escaping and connect': {
        topic: function() {
            var callback = this.callback;
            var client = new pg.Client(databaseUrl);

            client.connect(function (err) {
                if (err) { return callback(err); }
                client.query('CREATE SCHEMA "test-schema"', function(err) {
                    driver.connect({ driver: 'pg', database: 'db_migrate_test', schema: 'test-schema' }, internals, function(err, db) {
                        callback(err, db, client);
                    });
                });
            });
        },

        'migrations': {
            topic: function(db, client) {
                var callback = this.callback;

                db.createMigrationsTable(function() {
                    client.query("SELECT table_name FROM information_schema.tables WHERE table_schema = 'test-schema' AND table_name = 'migrations'", function(err, result) {
                        callback(err, result);
                    });
                });
            },

            'is in test-schema': function(err, result) {
                assert.isNull(err);
                assert.isNotNull(result);
                assert.equal(result.rowCount, 1);
            }
        },

        teardown: function(db, client) {
            var callback = this.callback;
            client.query('DROP SCHEMA "test-schema" CASCADE', function (err) {
              if (err) { return callback(err); }
              client.end();
              callback();
          });
        }
    }
})
.addBatch({
    'create schema and a public.migrations table and connect': {
        topic: function() {
            var callback = this.callback;
            var client = new pg.Client(databaseUrl);
            var query = Promise.promisify( client.query ).bind(client);

            client.connect(function (err) {
                if (err) { return callback(err) };
                Promise.all([
                  query('CREATE SCHEMA test_schema'),
                  query('CREATE TABLE migrations ()')
                ])
                .then( function() {

                    driver.connect({ driver: 'pg', database: 'db_migrate_test', schema: 'test_schema' }, internals, function(err, db) {
                        callback(err, db, client);
                    });
                })
                .catch( function(err) {
                  callback(err);
                });
            });
        },

        'migrations table': {
            topic: function(db, client) {
                var callback = this.callback;

                db.createMigrationsTable(function() {
                    client.query("SELECT table_name FROM information_schema.tables WHERE table_schema = 'test_schema' AND table_name = 'migrations'", function(err, result) {
                        callback(err, result);
                    });
                });
            },

            'is in test_schema': function(err, result) {
                assert.isNull(err);
                assert.isNotNull(result);
                assert.equal(result.rowCount, 1);
            }
        },

        teardown: function(db, client) {
            var callback = this.callback;
            var query = Promise.promisify(client.query).bind(client);

            Promise.all([
              query('DROP SCHEMA test_schema CASCADE'),
              query('DROP TABLE migrations')
            ])
            .then(function (err) {
              client.end();
              callback();
            })
            .catch(function (err) {
              callback(err);
            });
        }
    }
})
.addBatch({
    'create schema and connect': {
        topic: function() {
            var callback = this.callback;
            var client = new pg.Client(databaseUrl);

            client.connect(function (err) {
                if (err) { return callback(err); }
                client.query('CREATE SCHEMA test_schema', function(err) {
                    driver.connect({ driver: 'pg', database: 'db_migrate_test', schema: 'test_schema' }, internals, function(err, db) {
                        callback(err, db, client);
                    });
                });
            });
        },

        'migrations table': {
            topic: function(db, client) {
                var callback = this.callback;

                db.createMigrationsTable(function() {
                    client.query("SELECT table_name FROM information_schema.tables WHERE table_schema = 'test_schema' AND table_name = 'migrations'", function(err, result) {
                        callback(err, result);
                    });
                });
            },

            'is in test_schema': function(err, result) {
                assert.isNull(err);
                assert.isNotNull(result);
                assert.equal(result.rowCount, 1);
            }
        },

        teardown: function(db, client) {
            var callback = this.callback;
            client.query('DROP SCHEMA test_schema CASCADE', function (err) {
              if (err) { return callback(err); }
              client.end();
              callback();
          });
        }
    }
}).export(module);

@@ -1189,33 +1189,33 @@
    client.query(
-%22
+'
 SELECT table_nam
@@ -1264,24 +1264,25 @@
 le_schema = 
+%5C
 'test-schema
@@ -1277,24 +1277,25 @@
 'test-schema
+%5C
 ' AND table_
@@ -1293,32 +1293,33 @@
 ND table_name = 
+%5C
 'migrations'%22, f
@@ -1305,34 +1305,35 @@
 e = %5C'migrations
+%5C'
 '
-%22
 , function(err, 
@@ -2326,11 +2326,11 @@
 err)
+;
  %7D
-;
 %0A   
@@ -3069,33 +3069,33 @@
    client.query(
-%22
+'
 SELECT table_nam
@@ -3140,32 +3140,33 @@
  table_schema = 
+%5C
 'test_schema' AN
@@ -3153,32 +3153,33 @@
  = %5C'test_schema
+%5C
 ' AND table_name
@@ -3173,32 +3173,33 @@
 ND table_name = 
+%5C
 'migrations'%22, f
@@ -3185,34 +3185,35 @@
 e = %5C'migrations
+%5C'
 '
-%22
 , function(err, 
@@ -4845,17 +4845,17 @@
 t.query(
-%22
+'
 SELECT t
@@ -4916,16 +4916,17 @@
 chema = 
+%5C
 'test_sc
@@ -4929,16 +4929,17 @@
 t_schema
+%5C
 ' AND ta
@@ -4945,24 +4945,25 @@
 able_name = 
+%5C
 'migrations'
@@ -4965,10 +4965,11 @@
 ions
+%5C'
 '
-%22
 , fu

fix some cs issues