{"size": 1338, "ext": "js", "max_stars_repo_path": "core/server/data/migrations/hooks/migrate/after.js", "max_stars_repo_name": "mdno/Ghost", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "core/server/data/migrations/hooks/migrate/after.js", "max_issues_repo_name": "mdno/Ghost", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "core/server/data/migrations/hooks/migrate/after.js", "max_forks_repo_name": "mdno/Ghost", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 43.1612903226, "max_line_length": 110, "alphanum_fraction": 0.5896860987}
var errors = require('../../../../errors'),
    config = require('../../../../config'),
    versioning = require('../../../schema/versioning'),
    database = require('../../../db');

module.exports = function after(options) {
    return versioning.getDatabaseVersion(options)
        .catch(function (err) {
            if (err instanceof errors.DatabaseVersionError && err.code === 'VERSION_DOES_NOT_EXIST') {
                return versioning.setDatabaseVersion(options);
            }

            throw err;
        })
        .finally(function destroyConnection() {
            // do not close database connection in test mode, because all tests are executed one after another
            // this check is not nice, but there is only one other solution i can think of:
            // forward a custom object to knex-migrator, which get's forwarded to the hooks
            if (config.get('env').match(/testing/g)) {
                return;
            }

            // we need to close the database connection
            // the after hook signals the last step of a knex-migrator command
            // Example:
            // Ghost-CLI calls knexMigrator.init and afterwards it starts Ghost, but Ghost-CLI can't shutdown
            // if Ghost keeps a connection alive
            return database.knex.destroy();
        });
};
