<commit_before>define(module, (exports, require, make) => {

  var qp = require('qp-utility');

  make({

    ns: 'qp-library/postgres/schema',

    connection: null,

    create_table: function(data, done) {
      this.execute({
        text: [
          'CREATE TABLE', data.table.name, '(',
            qp.map(data.columns, column => {
              var def = column.name;
              if (column.primary_key) {
                def += ' SERIAL PRIMARY KEY';
              } else {
                def += ' ' + column.type;
                if (column.is_array) def += '[]';
              }
              return def;
            }).join(', '),
          ')'
        ],
        done: done
      });
    },

    drop_table: function(data, done) {
      this.execute({
        text: [ 'DROP TABLE IF EXISTS', data.table.name ],
        done: done
      });
    },

    rename_table: function(data, done) {
      this.execute({
        text: [ 'ALTER TABLE', data.table.name, 'RENAME TO', data.table.new_name ],
        done: done
      });
    },

    add_column: function() {
      this.execute({
        text: [ 'ALTER TABLE', data.table.name, 'ADD COLUMN', data.column.name, data.column.type ],
        done: done
      });
    },

    remove_column: function(data, done) {
      this.execute({
        text: [ 'ALTER TABLE', data.table.name, 'DROP COLUMN', data.column.name ],
        done: done
      });
    },

    change_column: function(data, done) {
      this.execute({
        text: [ 'ALTER TABLE', data.table.name, 'ALTER COLUMN', data.column.name, 'TYPE', data.column.type ],
        done: done
      });
    },

    rename_column: function(data, done) {
      this.execute({
        text: [ 'ALTER TABLE', data.table.name, 'RENAME COLUMN', data.column.name, 'TO', data.column.new_name ],
        done: done
      });
    },

    add_index: function() { },
    remove_index: function() { },
    add_constraint: function() { },
    remove_constraint: function() { },

    execute: function(config) {
      var done = config.done || qp.noop;
      var cmd = this.prepare(config);
      this.connection.query(cmd.pg, (error, pg_result) => {
        if (error) { done(error); } else {
          var result = { cmd: cmd };
          done(null, result);
        }
      });<commit_msg>postgres/schema - db schema changes<commit_after>define(module, (exports, require, make) => {

  var qp = require('qp-utility');
  var log = require('qp-library/log');

  function quote(text) { return '"' + text + '"'; }

  make({

    ns: 'qp-library/postgres/schema',

    connection: null,

    create_table: function(data, done) {
      this.execute({
        text: [
          'CREATE TABLE', quote(data.table.name), '(',
            qp.map(data.columns, column => {
              var def = column.name;
              if (column.primary) {
                def += ' SERIAL PRIMARY KEY';
              } else {
                def += ' ' + column.type;
                if (column.is_array) def += '[]';
              }
              return def;
            }).join(', '),
          ')'
        ],
        done: done
      });
    },

    drop_table: function(data, done) {
      this.execute({
        text: [ 'DROP TABLE IF EXISTS', quote(data.table.name) ],
        done: done
      });
    },

    rename_table: function(data, done) {
      this.execute({
        text: [ 'ALTER TABLE', data.table.name, 'RENAME TO', data.table.new_name ],
        done: done
      });
    },

    add_column: function() {
      this.execute({
        text: [ 'ALTER TABLE', data.table.name, 'ADD COLUMN', data.column.name, data.column.type ],
        done: done
      });
    },

    remove_column: function(data, done) {
      this.execute({
        text: [ 'ALTER TABLE', data.table.name, 'DROP COLUMN', data.column.name ],
        done: done
      });
    },

    change_column: function(data, done) {
      this.execute({
        text: [ 'ALTER TABLE', data.table.name, 'ALTER COLUMN', data.column.name, 'TYPE', data.column.type ],
        done: done
      });
    },

    rename_column: function(data, done) {
      this.execute({
        text: [ 'ALTER TABLE', data.table.name, 'RENAME COLUMN', data.column.name, 'TO', data.column.new_name ],
        done: done
      });
    },

    add_index: function() { },
    remove_index: function() { },
    add_constraint: function() { },
    remove_constraint: function() { },

    execute: function(config) {
      var done = config.done || qp.noop;
      var cmd = this.prepare(config);
      // log(cmd.pg.text)
      this.connection.query(cmd.pg, (error, pg_result) => {
        if (error) { done(error); } else {
          var result = { cmd: cmd };
          done(null, result);
        }
      });
{'fraction_non_alphanumeric': 0.16026182432432431, 'fraction_numerical': 0.0, 'mean_word_length': 1.8501805054151625, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '722235', 'n_tokens_mistral': 1497, 'n_tokens_neox': 1463, 'n_words': 317}