<commit_before>var fs = require('fs');
var path = require('path');
var Schema = require('./schema').Schema;

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

if (global.railway) {
    railway.orm._schemas = [];
}

module.exports = function init(root) {
    var railway, app, models;

    if (typeof root !== 'object' || root.constructor.name !== 'Compound') {
        railway = global.railway;
        app = global.app;
        models = app.models;
    } else {
        railway = root;
        app = railway.app;
        root = railway.root;
        models = railway.models;
    }

    railway.orm._schemas = [];

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

    try {
        var cf = require(confFile);
        if (cf instanceof Array) cf = cf[0];
        config = cf[app.set('env')];
    } catch (e) {
        console.log('Could not load config/database.{js|json|yml}');
        throw e;
    }

    // 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);
    }

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

    if (!schema.adapter) throw new Error('Adapter is not defined');

    if (schema.waitForConnect) {
        schema.on('connected', function() {
            loadSchema(schema, railway, app, models);
        });
    } else {
        loadSchema(schema, railway, app, models);
    }

// check validations and display warning

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

if (displayWarning) {
    var $ = railway.utils.stylize.$;
    // require('util').puts($('WARNING:').bold.red + ' ' + $('I can see that you\'ve added validation to db/schema.js. However schema.js file is only used to describe database schema. Therefore validations configured in db/schema.js will be ignored.\nFor business logic (incl. validations) please create models as separate .js files here: app/models/*.js').yellow);
}

function loadSchema(schema, railway, app, models) {
    railway.orm._schemas.push(schema);

    var context = prepareContext(models, railway, app, 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) {
        var code = fs.readFileSync(schemaFile).toString();
        if (schemaFile.match(/\.coffee$/)) {
            code = require('coffee-script').compile(code);
        }
        var fn = new Function('context', 'require', 'with(context){(function(){' + code + '})()}');
        fn(context, require);
    }

    // autoupdate if set app.enable('autoupdate')  or freeze schemas by default
    railway.orm._schemas.forEach(function (schema) {
        if(app.enabled('autoupdate')){
            schema.autoupdate();
        } esle {
            schema.freeze();
        }
    });
}

function log(str, startTime) {
    var $ = railway.utils.stylize.$;
    var m = Date.now() - startTime;
    railway.utils.debug(str + $(' [' + (m < 10 ? m : $(m).red) + ' ms]').bold);
    app.emit('app-event', {
        type: 'query',<commit_msg>fix typo :)<commit_after>var fs = require('fs');
var path = require('path');
var Schema = require('./schema').Schema;

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

if (global.railway) {
    railway.orm._schemas = [];
}

module.exports = function init(root) {
    var railway, app, models;

    if (typeof root !== 'object' || root.constructor.name !== 'Compound') {
        railway = global.railway;
        app = global.app;
        models = app.models;
    } else {
        railway = root;
        app = railway.app;
        root = railway.root;
        models = railway.models;
    }

    railway.orm._schemas = [];

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

    try {
        var cf = require(confFile);
        if (cf instanceof Array) cf = cf[0];
        config = cf[app.set('env')];
    } catch (e) {
        console.log('Could not load config/database.{js|json|yml}');
        throw e;
    }

    // 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);
    }

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

    if (!schema.adapter) throw new Error('Adapter is not defined');

    if (schema.waitForConnect) {
        schema.on('connected', function() {
            loadSchema(schema, railway, app, models);
        });
    } else {
        loadSchema(schema, railway, app, models);
    }

// check validations and display warning

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

if (displayWarning) {
    var $ = railway.utils.stylize.$;
    // require('util').puts($('WARNING:').bold.red + ' ' + $('I can see that you\'ve added validation to db/schema.js. However schema.js file is only used to describe database schema. Therefore validations configured in db/schema.js will be ignored.\nFor business logic (incl. validations) please create models as separate .js files here: app/models/*.js').yellow);
}

function loadSchema(schema, railway, app, models) {
    railway.orm._schemas.push(schema);

    var context = prepareContext(models, railway, app, 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) {
        var code = fs.readFileSync(schemaFile).toString();
        if (schemaFile.match(/\.coffee$/)) {
            code = require('coffee-script').compile(code);
        }
        var fn = new Function('context', 'require', 'with(context){(function(){' + code + '})()}');
        fn(context, require);
    }

    // autoupdate if set app.enable('autoupdate')  or freeze schemas by default
    railway.orm._schemas.forEach(function (schema) {
        if(app.enabled('autoupdate')){
            schema.autoupdate();
        } else {
            schema.freeze();
        }
    });
}

function log(str, startTime) {
    var $ = railway.utils.stylize.$;
    var m = Date.now() - startTime;
    railway.utils.debug(str + $(' [' + (m < 10 ? m : $(m).red) + ' ms]').bold);
    app.emit('app-event', {
        type: 'query',
{'fraction_non_alphanumeric': 0.15675519630484988, 'fraction_numerical': 0.0008660508083140878, 'mean_word_length': 2.5661348430262483, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1402850', 'n_tokens_mistral': 2117, 'n_tokens_neox': 2076, 'n_words': 539}