//mongoDB
// //folder architecture for a project
// var projectSchema = new mongoose.Schema({
//   projectName: String,
//   projectFileTree: String // obj_json_str
// });

//mongoDB
//insert the straight up mongo

//MySQL
var knex = require('knex');
var connection;
// Use A Different Database For Testing
if (process.env.NODE_ENV === 'test') {
  connection = {
    host: process.env.DB_HOST || 'localhost',
    user: 'root',
    password: '',
    database: 'code_friends_test',
  };
} else {
  connection = {
    host: process.env.DB_HOST || 'localhost',
    user: 'root',
    password: '',
    database: 'code_friends',
  };
}

//creates database
var db = knex({
  client: 'mysql',
  connection: connection
});

if (process.env.NODE_ENV === 'test') {
  console.log('Deleting All Tables');
  db.schema
    .dropTable('users')
    .dropTable('projects')
    .dropTable('users_projects');
}

//users schema
db.schema.hasTable('users').then(function (exists) {
  if (!exists) {
    db.schema.createTable('users', function (user) {
        user.increments('id').primary();
        user.string('username', 255);
        user.string('githubId', 255);
        user.string('githubName', 255);
        user.string('githubEmail', 255);
        user.string('githubLocation', 255);
        user.string('githubAccessToken', 255);
        user.string('githubAvatarUrl', 255);
        user.timestamps();
      })
      .then(function () {
        console.log('created table: users');
      })
      .catch(function (error) {
        console.log('error creating users: ', error);
      });
  }
});

//projects schema
db.schema.hasTable('projects').then(function (exists) {
  if (!exists) {
    db.schema.createTable('projects', function (project) {
        project.increments('id').primary();
        project.string('project_name', 255);
        project.timestamps();
      })
      .then(function () {
        console.log('created table: projects');
      })
      .catch(function (error) {
        console.log('error creating projects: ', error);
      });
  }
});

//DO WE NEED THIS?? MODELS SHOULD TAKE CARE OF IT
//creates join table for users and projects

db.schema.hasTable('projects_users').then(function (exists) {
  if (!exists) {
    db.schema.createTable('projects_users', function (projectsUsers) {
        projectsUsers.increments('id').primary();
        projectsUsers.integer('user_id').unsigned().references('id').inTable('users');
        projectsUsers.integer('project_id').unsigned().references('id').inTable('projects');
        projectsUsers.timestamps();
      })
      .then(function () {
        console.log('created table: user_projects');
      })
      .catch(function (error) {
        console.log('error creating user_projects: ', error);
      });
  }
});

var bookshelf = require('bookshelf')(db);
module.exports = bookshelf;

//to create database at beginning of project
//go to terminal
//mysql -u root
//create database 'name of database'

@@ -1,217 +1,38 @@
-//mongoDB%0A// //folder architecture for a project%0A// 
 var 
-projectSchema = new mongoose.Schema(%7B%0A//   projectName: String,%0A//   projectFileTree: String // obj_json_str%0A// %7D);%0A%0A//mongoDB%0A//insert the straight up mongo
+Promise = require('bluebird');
 %0A%0A//
@@ -574,44 +574,26 @@
 %7B%0A  
-console.log('Deleting All Tables');%0A
+Promise.all(%5B%0A    
   db
@@ -599,21 +599,16 @@
 b.schema
-%0A    
 .dropTab
@@ -618,21 +618,33 @@
 'users')
+,
 %0A    
+  db.schema
 .dropTab
@@ -657,21 +657,33 @@
 ojects')
-%0A    
+,%0A      db.schema
 .dropTab
@@ -690,17 +690,16 @@
 le('user
-s
 _project
@@ -701,16 +701,180 @@
 ojects')
+,%0A    %5D)%0A    .then(function () %7B%0A      console.log('Deleting All Tables');%0A    %7D)%0A    .catch(function (err) %7B%0A      console.log('Didnt delete talbes:', err);%0A    %7D)
 ;%0A%7D%0A%0A//u

Fix test data deletion