Git Product home page Git Product logo

hubot-mysql-brain's People

Contributors

akhy avatar dnrce avatar halkeye avatar the-smooth-operator avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

hubot-mysql-brain's Issues

`ON DUPLICATE KEY UPDATE` doesn't work

I was struggling with this problem for a long time but I noticed that this SQL statement doesn't work with MySQL 5.7 - the row won't be updated as the data on the right is the data from the conflicting row, not the new version.

There is also another problem when using Hubot with Slack, there be a lot informations in brain related to users, some of them in UTF8mb4 and MySQL will fail to store those data (which is hard to notice as there is no logging).

To overcome these problems I have prepared my own version of the hubot-mysql-brain:

tag = 'hubot-mysql-brain'
rowId = 0
mysql = require 'mysql'

module.exports = (robot) ->
  url = process.env.MYSQL_URL
  table = process.env.MYSQL_TABLE or 'brain'

  robot.logger.info("#{tag}: Using #{url} to connect to brain")

  conn = mysql.createConnection(url)

  load_data = () ->
    conn.query "SELECT `data` FROM `#{table}` WHERE `id`= #{rowId}", (err, rows) ->
      if err or rows.length == 0
        if err
          robot.logger.error err
        if rows.length == 0
          robot.logger.info "#{tag}: No data in brain yet"
        robot.brain.mergeData {}
      else
        robot.logger.info "Mering data into brain: #{rows[rowId].data}"
        data = JSON.parse rows[0].data
        robot.brain.mergeData data

  conn.connect (err) ->
    if err?
      robot.logger.error "#{tag}: Error\n#{err}"
    else
      robot.logger.info "#{tag}: Connected to MySQL brain (table: #{table})"
      load_data()

  robot.brain.on 'save', (data = {}) ->
    brain =
      _private: data['_private']

    vals = { 'id': 0, 'data': JSON.stringify brain }
    conn.query "INSERT INTO `#{table}` SET ? ON DUPLICATE KEY UPDATE `data` = '#{JSON.stringify brain}'", vals, (err, _) ->
      if err
        robot.logger.error err
      return

First I store only _private in the brain, then I use slightly modified INSERT to fix the problem with updating the data, plus I have added a bunch of logging. WDYT?

GitHub not up to date

  • 0.1.3 is the latest version on npm
  • 0.1.1 is the latest version in the commit log on GitHub
  • 0.1.0 is the latest tagged version on GitHub

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.