<commit_before>var pg = require('pg');

module.exports = function (grunt) {
  var config = grunt.config.get('pgutils')
    , _ = grunt.util._
    , log = function (args, depth) { console.log(require('util').inspect(args, { colors: true, depth: depth })); };


  grunt.task.registerTask('pgutils:backupSP', 'Dump PostgresSQL stored procedures in separated files.', function () {
    var done = this.async()
      , pgClient = new pg.Client(config.connection)
      // Success count
      , success = 0
      , query;

    // Actually connecting to postgreSQL
    pgClient.connect();

    query = pgClient.query("select pg_get_functiondef(sp.oid) as functiondef, sp.proname as proname, sp.pronargs as pronargs from (select oid, proname, pronargs from pg_proc where proname ~ '" + config.spregex + "') as sp");
    query.on('row', function (row, result) {
      grunt.file.write(config.dest + '/' + row.proname + '_' + row.pronargs + '.sql', row.functiondef);
      success++;
    });

    query.on('error', grunt.fail.fatal);
    query.on('end', function () {
      grunt.log.ok('Correctly backupped ' + success + ' stored procedures.');
      done();
    });
  });

  grunt.task.registerTask('pgutils:restoreSP', 'Restore stored procedures.', function () {
    var done = this.async()
      , async = grunt.util.async
      , pgClient = new pg.Client(config.connection)
      // File listing
      , sqlFiles
      , filesIterator = 0
      // Error and Success count
      , errors = []
      , success = 0
      , query;

    // Actually connecting to postgreSQL
    pgClient.connect();

    sqlFiles = grunt.file.expand([config.dest + '/*.sql']);

    async.whilst(
      function () {
        return filesIterator < sqlFiles.length;
      },
      function (callback) {
        pgClient.query(grunt.file.read(sqlFiles[filesIterator]), function (err, result) {
          if (err) {
            errors.push(err);
            callback();
          } else {
            success++;
            callback();
          }
        });
        filesIterator++;
      },
      function (err) {
        if (errors.length) {
          grunt.log.error(errors.length + ' Errors occurred, listing them:');
          _.forEach(errors, function (value, index) {
            grunt.log.errorlns(value);
          });
        }
        grunt.log.ok('Correctly restored ' + success + ' stored procedures.');
        done();
      }
    );
  });

  // grunt.task.registerTask('pgutils:testSP', 'PLZ LET ME TEST ALONE', function () {
  //   var done = this.async()
  //     , pgClient = new pg.Client(config.connection);

  //   query.on('error', grunt.fail.fatal);
  //   query.on('end', done);
  // });

  grunt.task.registerTask('pgutils:cleanDir', 'Empties "dest" directory', function () {
    grunt.file.delete(config.dest);
  });

};
<commit_msg>pgutils.src added, source directory for restore Task<commit_after>var pg = require('pg');

module.exports = function (grunt) {
  var config = grunt.config.get('pgutils')
    , _ = grunt.util._
    , log = function (args, depth) { console.log(require('util').inspect(args, { colors: true, depth: depth })); };


  grunt.task.registerTask('pgutils:backupSP', 'Dump PostgresSQL stored procedures in separated files.', function () {
    var done = this.async()
      , pgClient = new pg.Client(config.connection)
      // Success count
      , success = 0
      , query;

    // Actually connecting to postgreSQL
    pgClient.connect();

    query = pgClient.query("select pg_get_functiondef(sp.oid) as functiondef, sp.proname as proname, sp.pronargs as pronargs from (select oid, proname, pronargs from pg_proc where proname ~ '" + config.spRegex + "') as sp");
    query.on('row', function (row, result) {
      grunt.file.write(config.dest + '/' + row.proname + '_' + row.pronargs + '.sql', row.functiondef);
      success++;
    });

    query.on('error', grunt.fail.fatal);
    query.on('end', function () {
      grunt.log.ok('Correctly backupped ' + success + ' stored procedures.');
      done();
    });
  });

  grunt.task.registerTask('pgutils:restoreSP', 'Restore stored procedures.', function () {
    var done = this.async()
      , async = grunt.util.async
      , pgClient = new pg.Client(config.connection)
      // File listing
      , sqlFiles
      , filesIterator = 0
      // Error and Success count
      , errors = []
      , success = 0
      , query;

    // Actually connecting to postgreSQL
    pgClient.connect();

    sqlFiles = grunt.file.expand(config.src);

    async.whilst(
      function () {
        return filesIterator < sqlFiles.length;
      },
      function (callback) {
        pgClient.query(grunt.file.read(sqlFiles[filesIterator]), function (err, result) {
          if (err) {
            errors.push(err);
            callback();
          } else {
            success++;
            callback();
          }
        });
        filesIterator++;
      },
      function (err) {
        if (errors.length) {
          grunt.log.error(errors.length + ' Errors occurred, listing them:');
          _.forEach(errors, function (value, index) {
            grunt.log.errorlns(value);
          });
        }
        grunt.log.ok('Correctly restored ' + success + ' stored procedures.');
        done();
      }
    );
  });

  // grunt.task.registerTask('pgutils:testSP', 'PLZ LET ME TEST ALONE', function () {
  //   var done = this.async()
  //     , pgClient = new pg.Client(config.connection);

  //   query.on('error', grunt.fail.fatal);
  //   query.on('end', done);
  // });

  grunt.task.registerTask('pgutils:cleanDir', 'Empties "dest" directory', function () {
    grunt.file.delete(config.dest);
  });

};

{'fraction_non_alphanumeric': 0.15408970976253297, 'fraction_numerical': 0.0010554089709762533, 'mean_word_length': 2.3885578069129916, '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': '3259661', 'n_tokens_mistral': 1750, 'n_tokens_neox': 1715, 'n_words': 371}