<commit_before>var bookshelf = require('../common/bookshelf_init');
require('./User');
require('./Hashtag');
require('./Country');

// Returns Changeset model
var Changeset = bookshelf.Model.extend({
  tableName: 'changesets',
  users: function () {
    return this.belongsTo('User');
  },
  hashtags: function () {
    return this.belongsToMany('Hashtag');
  },
  countries: function () {
    return this.belongsToMany('Country');
  }
}, {
  createChangesetIfNotExists: function (metrics, transaction) {
    return Changeset.where({id: metrics.id}).fetch({transacting: transaction}).then(function (result) {
      if (!result) {
        return Changeset.forge({
          id: metrics.id,
          road_count_add: metrics.metrics.road_count,
          road_count_mod: metrics.metrics.road_count_mod,
          building_count_add: metrics.metrics.building_count,
          building_count_mod: metrics.metrics.building_count_mod,
          waterway_count_add: metrics.metrics.waterway_count,
          poi_count_add: metrics.metrics.poi_count,
          // GPS trace not yet implemented
          gpstrace_count_add: 0,
          road_km_add: metrics.metrics.road_km,
          road_km_mod: metrics.metrics.road_km_mod,
          waterway_km_add: metrics.metrics.waterway_km,
          // GPS trace not yet implemented
          gpstrace_km_add: 0,
          editor: metrics.editor,
          user_id: metrics.user.id,
          created_at: new Date(metrics.created_at)
        }).save(null, {method: 'insert', transacting: transaction});
      } else {
        throw new Error('Changeset exists');
      }
    });
  }
});

module.exports = bookshelf.model('Changeset', Changeset);<commit_msg>update changeset with new info, fixes #7<commit_after>var bookshelf = require('../common/bookshelf_init');
require('./User');
require('./Hashtag');
require('./Country');

// Returns Changeset model
var Changeset = bookshelf.Model.extend({
  tableName: 'changesets',
  users: function () {
    return this.belongsTo('User');
  },
  hashtags: function () {
    return this.belongsToMany('Hashtag');
  },
  countries: function () {
    return this.belongsToMany('Country');
  }
}, {
  createChangesetIfNotExists: function (metrics, transaction) {
    return Changeset.where({id: metrics.id}).fetch({transacting: transaction}).then(function (result) {
      if (!result) {
        return Changeset.forge({
          id: metrics.id,
          road_count_add: metrics.metrics.road_count,
          road_count_mod: metrics.metrics.road_count_mod,
          building_count_add: metrics.metrics.building_count,
          building_count_mod: metrics.metrics.building_count_mod,
          waterway_count_add: metrics.metrics.waterway_count,
          poi_count_add: metrics.metrics.poi_count,
          // GPS trace not yet implemented
          gpstrace_count_add: 0,
          road_km_add: metrics.metrics.road_km,
          road_km_mod: metrics.metrics.road_km_mod,
          waterway_km_add: metrics.metrics.waterway_km,
          // GPS trace not yet implemented
          gpstrace_km_add: 0,
          editor: metrics.editor,
          user_id: metrics.user.id,
          created_at: new Date(metrics.created_at)
        }).save(null, {method: 'insert', transacting: transaction});
      } else {
        return result.save({
          id: result.get('id'),
          road_count_add: Number(result.get('road_count_add')) + metrics.metrics.road_count,
          road_count_mod: Number(result.get('road_count_mod')) + metrics.metrics.road_count_mod,
          building_count_add: Number(result.get('building_count_add')) + metrics.metrics.building_count,
          building_count_mod: Number(result.get('building_count_mod')) + metrics.metrics.building_count_mod,
          waterway_count_add: Number(result.get('waterway_count_add')) + metrics.metrics.waterway_count,
          poi_count_add: Number(result.get('poi_count_add')) + metrics.metrics.poi_count,
          // GPS trace not yet implemented
          gpstrace_count_add: 0,
          road_km_add: Number(result.get('road_km_add')) + metrics.metrics.road_km,
          road_km_mod: Number(result.get('road_km_mod')) + metrics.metrics.road_km_mod,
          waterway_km_add: Number(result.get('waterway_km_add')) + metrics.metrics.waterway_km,
          // GPS trace not yet implemented
          gpstrace_km_add: 0,
          editor: result.get('editor'),
          user_id: metrics.user.id,
          created_at: result.get('created_at')
        }, {method: 'update', transacting: transaction}); 
      }
    });
  }
});

module.exports = bookshelf.model('Changeset', Changeset);
{'fraction_non_alphanumeric': 0.12302702702702703, 'fraction_numerical': 0.0015135135135135136, 'mean_word_length': 3.0938053097345133, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4857643', 'n_tokens_mistral': 1405, 'n_tokens_neox': 1380, 'n_words': 242}