<commit_before>const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const logger = require('../logging');

const DATABASE_URL = process.env.DATABASE_URL;

const db = {};

const sequelize = new Sequelize(DATABASE_URL, {
  dialect: 'postgres',
  // Use winston for logging.
  // See https://github.com/sequelize/sequelize/issues/7821#issuecomment-311564259
  logging: msg => logger.debug(msg),
  pool: {
    max: 5,
    min: 0,
    acquire: 30000,
    idle: 10000,
  },
});

// Import all models
fs.readdirSync(__dirname)
  .filter(file =>
    // Ignore hidden files
    (file.indexOf('.') !== 0)
      // Ignore this file
      && (file !== path.basename(__filename))
      // Ignore all non-js files
      && (file.slice(-3) === '.js')
      // Ignore files containing only accessor functions (getters and setters)
      // (These requires the model to be imported first)
      && (file.search('.accessors.') === -1)
      && (file !== 'essentials.js'), // Ignore mongodb file
  )
  .forEach(async (file) => {
    try {
      const model = await sequelize.import(path.join(__dirname, file));
      db[model.name] = model;
    } catch (err) {
      logger.error(`import of model from file "${file}" failed`, err);
    }
  });

Object.keys(db).forEach(async (model) => {
  if (db[model].associate) {
    await db[model].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;<commit_msg>Wait for models to be imported before associating<commit_after>const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const logger = require('../logging');

const DATABASE_URL = process.env.DATABASE_URL;

const db = {};

const sequelize = new Sequelize(DATABASE_URL, {
  dialect: 'postgres',
  // Use winston for logging.
  // See https://github.com/sequelize/sequelize/issues/7821#issuecomment-311564259
  logging: msg => logger.debug(msg),
  pool: {
    max: 5,
    min: 0,
    acquire: 30000,
    idle: 10000,
  },
});

// Import all models
Promise.all(fs.readdirSync(__dirname)
  .filter(file =>
    // Ignore hidden files
    (file.indexOf('.') !== 0)
      // Ignore this file
      && (file !== path.basename(__filename))
      // Ignore all non-js files
      && (file.slice(-3) === '.js')
      // Ignore files containing only accessor functions (getters and setters)
      // (These requires the model to be imported first)
      && (file.search('.accessors.') === -1)
      && (file !== 'essentials.js'), // Ignore mongodb file
  )
  .map(async (file) => {
    try {
      const model = await sequelize.import(path.join(__dirname, file));
      db[model.name] = model;
    } catch (err) {
      logger.error(`import of model from file "${file}" failed`, err);
    }
  })).then(async () => {
    logger.debug('Imported all models, starting association.');
    Object.keys(db)
      .filter(key => (key.search(/sequelize/i) === -1))
      .map(async (model) => {
        console.log('associating ', model);
        if (db[model].associate) {
          await db[model].associate(db);
        }
      });
      // console.log('done', db)
  })
  .catch((err) => {
    console.log('Error during association of model relations', err);
  });


db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;
{'fraction_non_alphanumeric': 0.16901831281897328, 'fraction_numerical': 0.017111978384869407, 'mean_word_length': 2.9246171967020023, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, 'https://': 2, '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': '7120943', 'n_tokens_mistral': 1131, 'n_tokens_neox': 1091, 'n_words': 264}