-
-
Notifications
You must be signed in to change notification settings - Fork 76
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
Add postgres support with tests #44
Conversation
Start by rebasing this code to fix the conflicts. |
There was a problem hiding this 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). |
There was a problem hiding this comment.
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). |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
lib/slack-ruby-bot-server.rb
Outdated
|
||
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? |
There was a problem hiding this comment.
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
.
sample_apps/sample_app_mongo/Gemfile
Outdated
@@ -1,8 +1,12 @@ | |||
source 'https://rubygems.org' |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
spec/spec_helper.rb
Outdated
files = Dir[File.join(__dir__, 'support', '**/*.rb')] | ||
mongo_files = ["#{File.dirname(__FILE__)}/support/mongoid.rb"] | ||
|
||
if defined? Mongo |
There was a problem hiding this comment.
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"
spec/support/database_cleaner.rb
Outdated
@@ -7,7 +7,7 @@ | |||
end | |||
|
|||
config.after :suite do | |||
Mongoid.purge! | |||
Mongoid.purge! if defined?(Mongo) |
There was a problem hiding this comment.
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.
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. |
Also please see comments in #38, a lot of the changes I'd like are still relevant here. |
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 |
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. |
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 |
There was a problem hiding this 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 |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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). |
There was a problem hiding this comment.
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.
config/databases/mongo.rb
Outdated
@@ -0,0 +1,3 @@ | |||
require 'slack-ruby-bot-server/models/team/mongo.rb' |
There was a problem hiding this comment.
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
Ok getting closer, I've moved everything to |
lib/slack-ruby-bot-server/config.rb
Outdated
@@ -11,20 +11,20 @@ def reset! | |||
|
|||
def database_adapter | |||
database_adapter = if defined?(::Mongoid) |
There was a problem hiding this comment.
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 ||= ...
.
spec/spec_helper.rb
Outdated
elsif SlackRubyBotServer::Config.mongo? | ||
require 'spec/support/databases/mongo' | ||
if SlackRubyBotServer::Config.pg? | ||
require 'spec/support/database_adapters/pg/*.rb' |
There was a problem hiding this comment.
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.
Getting really close! For rubocop, run |
@@ -2,7 +2,6 @@ module SlackRubyBotServer | |||
class App |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
Made some progress from here in #48, closing this one. Still some test failures remaining. |
Released AR support in 0.6.0. |
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!