<commit_before>/* Database configuration */
var mysql = require('mysql');
var Sequelize = require('sequelize');
var sequelize = null;
var Schemas = require('./schemas');

if (process.env.CLEARDB_DATABASE_URL) {
  //initialize db on production server
  sequelize = new Sequelize(process.env.CLEARDB_DATABASE_URL);
} else {
  //initialize db on localhost
  sequelize = new Sequelize('fincensus', 'root', '', {logging: false});
}

var Bank = sequelize.define('Bank', Schemas.Bank);
var State = sequelize.define('State', Schemas.State);
var Population = sequelize.define('Population', Schemas.Population);
var Complaint = sequelize.define('Complaint', Schemas.Complaint);
var Product = sequelize.define('Product', Schemas.Product);
var Type = sequelize.define('Type', Schemas.Type);

/* Define relationships */
// 1:m
Complaint.belongsTo(Bank);
Complaint.belongsTo(Product);
Type.belongsTo(Product);
Population.belongsTo(State);

// m:n
State.belongsToMany(Bank, {through: 'BankState'});
Bank.belongsToMany(State, {through: 'BankState'});

// creates these tables in MySQL if they don't already exist. Pass in {force: true}
// to drop all existing tables and make new ones. ie: sequelize.sync({force: true});
sequelize.sync({force:false});

// paginate results to max 200 per request

var initStatesTable = require('./seed/state.seed').initStatesTable;
var initPopulationsTable = require('./seed/population.seed').initPopulationsTable;
var initBanksTable = require('./seed/bank.seed').initBanksTable;




initStatesTable(State).then(function(res) {
  
  initBanksTable(Bank)
    .then(function(res) {
      console.log('Banks seeded.', res);
    })
    .catch(function(err) {
      console.log('Banks not seeded properly!', err);
    });
// initPopulationsTable(Population)
//     .then(function(res) {
//       console.log('response from population chain:', res);
//     })
//     .catch(function(err) {
//       console.log('error on child population chain:',err)
//     }); 

})
.catch(function(err) {
  console.log('error on states chain:', err);
});
// State.create({capital:'no', name:'no'})
//   .then(function(state) {
//     console.log('is this the state object from the promise?', state.dataValues);
//   });
// State.find({where:{capital:'GA'}}).then(function(state) {
//   Population.create({population: 101, year: 2020}).then(function(pop) {
//     return pop.setState('GA');
//   }); 
// })

exports.Bank = Bank;
exports.State = State;
exports.Population = Population;
exports.Complaint = Complaint;
exports.Product = Product;
exports.Type = Type;
<commit_msg>(refactor) Consolidate entry point for database initialization<commit_after>/* Database configuration */
var worker = require('./worker');
var mysql = require('mysql');
var Sequelize = require('sequelize');
var sequelize = null;
var Schemas = require('./schemas');

if (process.env.CLEARDB_DATABASE_URL) {
  //initialize db on production server
  sequelize = new Sequelize(process.env.CLEARDB_DATABASE_URL);
} else {
  //initialize db on localhost
  sequelize = new Sequelize('fincensus', 'root', '', {logging: false});
}

var Bank = sequelize.define('Bank', Schemas.Bank);
var State = sequelize.define('State', Schemas.State);
var Population = sequelize.define('Population', Schemas.Population);
var Complaint = sequelize.define('Complaint', Schemas.Complaint);
var Product = sequelize.define('Product', Schemas.Product);
var Issue = sequelize.define('Issue', Schemas.Issue);
var Submission = sequelize.define('Submission', Schemas.Submission);

/* Define relationships */
// 1:m
Complaint.belongsTo(Bank);
Complaint.belongsTo(Product);
Complaint.belongsTo(State);
Complaint.belongsTo(Issue);
Complaint.belongsTo(Submission);

Population.belongsTo(State);

// m:n
State.belongsToMany(Bank, {through: 'BankState'});
Bank.belongsToMany(State, {through: 'BankState'});

// creates these tables in MySQL if they don't already exist. Pass in {force: true}
// to drop all existing tables and make new ones. ie: sequelize.sync({force: true});
sequelize.sync({force:false});

// paginate results to max 200 per request

var initStatesTable = require('./seed/state.seed').initStatesTable;
var initBanksTable = require('./seed/bank.seed').initBanksTable;
var initPopulationsTable = require('./seed/population.seed').initPopulationsTable;
var initProductsTable = require('./seed/product.seed').initProductsTable;
var initIssuesTable = require('./seed/product.seed').initIssuesTable;
var initSubmissionsTable = require('./seed/product.seed').initSubmissionsTable;
var initComplaintsTable = require('./seed/product.seed').initComplaintsTable;
/* Build Re-use of init functions to handle creation OR updates */

// ORDER OF INITIALIZATION (foreign key constraints)
// States -> Banks -> Populations -> Products -> Issues -> Submissions -> Complaints
initStatesTable(State)
  .then(function(res) {
    console.log('States seeded.', res);
    return initBanksTable(Bank);
  })
  .then(function(res) {
    console.log('Banks seeded.', res);
    return initPopulationsTable(Population);
  })
  .then(function(res) {
    console.log('Populations seeded.', res);
    return initProductsTable(Product);
  })
  .then(function(res) {
    console.log('Products seeded.', res);
    return initIssuesTable(Issue);
  })
  .then(function(res) {
    console.log('Issues seeded.', res);
    return initSubmissionsTable(Submission);
  })
  .then(function(res) {
    console.log('Submissions seeded.', res);
    return initComplaintsTable(Complaint);
  })
  .then(function(res) {
    console.log('Complaints seeded.', res);
    return Promise.resolve('Database seeded.');
  })
  .catch(function(err) {
    console.log('Seeding failed.', err);
    return Promise.reject('Database not seeded.');
  });
  
exports.Bank = Bank;
exports.State = State;
exports.Population = Population;
exports.Complaint = Complaint;
exports.Product = Product;
exports.Submission = Submission;
exports.Issue = Issue;

{'fraction_non_alphanumeric': 0.15588136164475902, 'fraction_numerical': 0.0025278058645096056, 'mean_word_length': 4.812928501469148, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4974281', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1791, 'n_words': 412}