{"size": 1886, "ext": "js", "max_stars_repo_path": "test/testDB.js", "max_stars_repo_name": "flitz-be/ale", "max_stars_repo_stars_event_min_datetime": "2017-12-02T15:51:14.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-16T06:40:42.000Z", "max_issues_repo_path": "test/testDB.js", "max_issues_repo_name": "flitz-be/ale", "max_issues_repo_issues_event_min_datetime": "2018-06-18T19:09:58.000Z", "max_issues_repo_issues_event_max_datetime": "2019-06-30T13:30:16.000Z", "max_forks_repo_path": "test/testDB.js", "max_forks_repo_name": "flitz-be/ale", "max_forks_repo_forks_event_min_datetime": "2017-10-08T17:54:14.000Z", "max_forks_repo_forks_event_max_datetime": "2022-01-11T19:28:26.000Z"}
{"max_stars_count": 55, "max_issues_count": 7, "max_forks_count": 30, "avg_line_length": 33.0877192982, "max_line_length": 118, "alphanum_fraction": 0.6686108165}
/*
 * @license
 * Copyright 2017 Cayle Sharrock
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under the License.
 */

const { Client } = require('pg');
const async = require('async');
const Transaction = require('../models/transaction');
const Book = require('../models/book');
const JournalEntry = require('../models/journal');

process.env.ALE_CONNECTION = process.env.ALE_CONNECTION || 'postgres://postgres@localhost/ale-test';
const masterUri = process.env.MASTER_ALE_CONNECTION || 'postgres://postgres@localhost/postgres';

let sequelize = require('../models/connection');

module.exports.create = (done) => {
    create(masterUri, 'ale-test', done);
};

module.exports.destroy =  () => {
    return sequelize.drop().then(() => {
        return sequelize.close();
    });
};

module.exports.clear = () => {
    return Transaction.truncate().then(() => {
        return JournalEntry.truncate({ cascade: true });
    }).then(() => {
        return Book.truncate({ cascade: true });
    })
};

function create(master_uri, db_name, cb) {
    const queries = [
        `DROP DATABASE IF EXISTS "${db_name}";`,
        `CREATE DATABASE "${db_name}";`,
        `ALTER DATABASE "${db_name}" SET TIMEZONE TO 'UTC';`
    ];
    const client = new Client({ connectionString: master_uri });
    client.connect();
    async.eachSeries(queries, function(sql, done) {
        client.query(sql, done);
    }, cb);
}

