Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add postgres support with tests #44

Closed

Conversation

spencerldixon
Copy link

Building on top of Zach's PR and attempting to get this gem working with tests so we can merge postgresql support. Not entirely sure what I'm doing here so I would appreciate any input!

@dblock
Copy link
Collaborator

dblock commented Feb 14, 2017

Start by rebasing this code to fix the conflicts.

Copy link
Collaborator

@dblock dblock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a bunch of comments.

CHANGELOG.md Outdated
@@ -4,6 +4,7 @@

* [#28](https://github.com/slack-ruby/slack-ruby-bot-server/pull/28): Use slack-ruby-danger gem - [@dblock](https://github.com/dblock).
* [#31](https://github.com/slack-ruby/slack-ruby-bot-server/pull/31): Adds MONGODB_URI as environment variable for MongoDB - [@corprew](https://github.com/corprew).
* [#38](https://github.com/slack-ruby/slack-ruby-bot-server/pull/38): Add Postgres support, reference #7. - [@zachfeldman](https://github.com/zachfeldman).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an extra period in here, and you can remove "reference #7." altogether.

README.md Outdated
@@ -31,6 +31,8 @@ You can use the [sample application](sample_app) to bootstrap your project and s

Install [MongoDB](https://www.mongodb.org/downloads), required to store teams. We would like your help with [supporting other databases](https://github.com/slack-ruby/slack-ruby-bot-server/issues/12).

You can also use PostgreSQL. See the PostgreSQL example - ensure you've filled out postgresql.yml and have setup your database correctly (user with the right name and privileges according to the file).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets make this two sections, maybe ### MongoDB and ### PostgreSQL with instructions on how to use each. You can remove "We would like your help with supporting other databases."

database.yml Outdated
@@ -0,0 +1,18 @@
default: &default
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file seems misplaced. I'd expect it to be in something like config folder.

database.yml Outdated
development:
<<: *default
database: slack_ruby_bot_server_development
username: slack_ruby_bot_server
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is how PostgreSQL default config works, we want this code to be able to run PostgreSQL tests without any changes.


require 'slack-ruby-bot-server/ext'
require 'slack-ruby-bot-server/version'
require 'slack-ruby-bot-server/info'
require 'slack-ruby-bot-server/models'

if SlackRubyBotServer::Config.mongo?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create two files, maybe config/databases/mongo.rb and config/databases/postgresql.rb and move all this logic there. The require should be something like require 'config/databases/SlackRubyBotServer::Config.database_type.

@@ -1,8 +1,12 @@
source 'https://rubygems.org'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mongo => mongoid

spec/models.rb Outdated
@@ -0,0 +1,2 @@
class Team < ActiveRecord::Base
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be needed.

spec/schema.rb Outdated
@@ -0,0 +1,15 @@
ActiveRecord::Schema.define do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in a pg-related subfolder.

files = Dir[File.join(__dir__, 'support', '**/*.rb')]
mongo_files = ["#{File.dirname(__FILE__)}/support/mongoid.rb"]

if defined? Mongo
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spearate into subfolders and load, eg. "spec/support/databases/mongoid.rb" and "pg.rb"

@@ -7,7 +7,7 @@
end

config.after :suite do
Mongoid.purge!
Mongoid.purge! if defined?(Mongo)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move into the support folder's per-database file.

@dblock
Copy link
Collaborator

dblock commented Feb 14, 2017

Most importantly we need a build here that keys of DATABASE_ADAPTER=mongoid or DATABASE_ADAPTER=pgsql or similar in .travis.yml so that we can make sure all tests pass against one and the other.

@dblock
Copy link
Collaborator

dblock commented Feb 14, 2017

Also please see comments in #38, a lot of the changes I'd like are still relevant here.

@spencerldixon
Copy link
Author

Thanks for the feedback @dblock, I'll get on it. Still not entirely sure how everything works but it's really interesting figuring it out, so please bare with me. One more thing, I'd like to use this in a Rails app I'm working on, would that be possible? Perhaps we could include some better instructions for doing this too

@dblock
Copy link
Collaborator

dblock commented Feb 17, 2017

Lets table the whole Rails thing for later, it's a whole other ballgame. Generally I would just use slack-ruby-bot in a Rails app.

@spencerldixon
Copy link
Author

Brilliant, I'm keen to work on rails integration too, I'm trying to create a bot app with rails that will need to support a personal assistant bot for individual members across multiple teams. Perhaps we can open an issue to start discussing this too, I know scale will also be an issue. Anyway, I've made a few changes, let me know your thoughts so far and I'll continue working on this

Copy link
Collaborator

@dblock dblock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good! Your build with PostgreSQL is passing, so it's encouraging. Now you just need to re-add mongodb in the build matrix and get the whole thing green.

I made some naming suggestions, I think it's important to get names right early, but none of them are blockers to get this merged, the build however is.

.travis.yml Outdated
@@ -21,3 +21,6 @@ matrix:
before_install:
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"

env:
- DB=postgres
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there's a matrix above, you should modify the matrix to include 1 of each, something like

matrix:
   include:
     - rvm: 2.3.1
       script:
         - bundle exec danger
     - rvm: 2.3.1
        env:
          - DATABASE_ADAPTER=pg
     - rvm: 2.3.1
        env:
          - DATABASE_ADAPTER=mongoid

For now feel free to just remove ruby-head and jruby-head.

README.md Outdated
Install [MongoDB](https://www.mongodb.org/downloads), required to store teams. We would like your help with [supporting other databases](https://github.com/slack-ruby/slack-ruby-bot-server/issues/12).
### MongoDB

Install [MongoDB](https://www.mongodb.org/downloads), required to store teams.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a note to use mongoid.

README.md Outdated

### PostgreSQL

You can also use PostgreSQL. See the PostgreSQL example - ensure you've filled out postgresql.yml and have setup your database correctly (user with the right name and privileges according to the file).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just remove "You can also use PostgreSQL" and make it similar to the mongoid instructions in every way.

@@ -0,0 +1,3 @@
require 'slack-ruby-bot-server/models/team/mongo.rb'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for consistency sake it's easier if the ENV variable keys the names, if DATABASE_ADAPTER=mongoid then this file should be called database_adapters/mongoid.rb.

…ods to reflect the adapters, from postgres to pg, and mongo to mongoid
@spencerldixon
Copy link
Author

Ok getting closer, I've moved everything to database_adapters folders and renamed the main config methods from postgresql to pg and mongo to mongoid to better reflect this. Let me know if I've missed anything though. Not sure how to go about hooking up the DATABASE_ADAPTER env in .travis.yml though to get this to run against the tests?

@@ -11,20 +11,20 @@ def reset!

def database_adapter
database_adapter = if defined?(::Mongoid)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be @database_adapter ||= ....

elsif SlackRubyBotServer::Config.mongo?
require 'spec/support/databases/mongo'
if SlackRubyBotServer::Config.pg?
require 'spec/support/database_adapters/pg/*.rb'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know you could do a require with *.rb, learning every day :)

This isn't a big deal, but it makes the order of requires unpredictable inside that folder. I would require .../pg.rb and inside pg.rb require anything that it may need.

@dblock
Copy link
Collaborator

dblock commented Feb 21, 2017

Getting really close! For rubocop, run rubocop -a, then rubocop --auto-gen-config.

@@ -2,7 +2,6 @@ module SlackRubyBotServer
class App
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should break this up and out into database-specific classes. For example you could have an Adapter class with a common interface, and SlackRubyBotServeR::Config.database_adapter would no longer be an enum, but an actual instance of that thing that you can call methods like check! and prepare!.


def reset!
self.server_class = SlackRubyBotServer::Server
end

def database_adapter
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could move this into an initializer. We call reset!, so maybe just set it there?

@dblock
Copy link
Collaborator

dblock commented Mar 6, 2017

Made some progress from here in #48, closing this one. Still some test failures remaining.

@dblock dblock closed this Mar 6, 2017
@dblock
Copy link
Collaborator

dblock commented Mar 12, 2017

Released AR support in 0.6.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants