<commit_before>var fs = require('fs');
var path = require('path');
var Schema = railway.orm.Schema;

var existsSync = fs.existsSync || path.existsSync;

railway.orm._schemas = [];

module.exports = function init(root) {

var confFile = (root || app.root) + '/config/database.json';
var config;

if (existsSync(confFile)) {
    try {
        config = JSON.parse(fs.readFileSync(confFile, 'utf-8'))[app.set('env')];
        // when driver name started with point - look for driver in app root (relative path)
        if (config.driver && config.driver.match(/^\./)) {
            config.driver = path.join(app.root, config.driver);
        }
    } catch (e) {
        console.log('Could not parse config/database.json');
        throw e;
    }
} else {
    config = {};
}

var schema = new Schema(config && config.driver || 'memory', config);
schema.log = log;
railway.orm._schemas.push(schema);

context = prepareContext(schema);

// run schema first
var schemaFile = (root || app.root) + '/db/schema.';
if (existsSync(schemaFile + 'js')) {
    schemaFile += 'js';
} else if (existsSync(schemaFile + 'coffee')) {
    schemaFile += 'coffee';
} else {
    schemaFile = false;
}

if (schemaFile) {
    railway.utils.runCode(schemaFile, context);
}

// and freeze schemas
railway.orm._schemas.forEach(function (schema) {
    schema.freeze();
});

// check validations and display warning

var displayWarning = false;
Object.keys(app.models).forEach(function (model) {
    var Model = app.models[model];
    if (Model._validations) {
        displayWarning = true;
    }
});

if (displayWarning) {<commit_msg>when using cradle if we don't wait for the schema to be connected, the models fails to load correctly.<commit_after>var fs = require('fs');
var path = require('path');
var Schema = railway.orm.Schema;

var existsSync = fs.existsSync || path.existsSync;

railway.orm._schemas = [];

module.exports = function init(root) {

var confFile = (root || app.root) + '/config/database.json';
var config;

if (existsSync(confFile)) {
    try {
        config = JSON.parse(fs.readFileSync(confFile, 'utf-8'))[app.set('env')];
        // when driver name started with point - look for driver in app root (relative path)
        if (config.driver && config.driver.match(/^\./)) {
            config.driver = path.join(app.root, config.driver);
        }
    } catch (e) {
        console.log('Could not parse config/database.json');
        throw e;
    }
} else {
    config = {};
}

var schema = new Schema(config && config.driver || 'memory', config);
schema.log = log;

// when using cradle if we dont wait for the schema to be connected, the models fails to load correctly.
schema.on('connected', function() {
    railway.orm._schemas.push(schema);

    context = prepareContext(schema);

// run schema first
    var schemaFile = (root || app.root) + '/db/schema.';
    if (existsSync(schemaFile + 'js')) {
        schemaFile += 'js';
    } else if (existsSync(schemaFile + 'coffee')) {
        schemaFile += 'coffee';
    } else {
        schemaFile = false;
    }

    if (schemaFile) {
        railway.utils.runCode(schemaFile, context);
    }

// and freeze schemas
    railway.orm._schemas.forEach(function (schema) {
        schema.freeze();
    });
});

// check validations and display warning

var displayWarning = false;
Object.keys(app.models).forEach(function (model) {
    var Model = app.models[model];
    if (Model._validations) {
        displayWarning = true;
    }
});

if (displayWarning) {
{'fraction_non_alphanumeric': 0.150641940085592, 'fraction_numerical': 0.0005706134094151213, 'mean_word_length': 3.0910151691948657, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3045658', 'n_tokens_mistral': 1089, 'n_tokens_neox': 1073, 'n_words': 285}