Skip to content

Commit

Permalink
add postgres support
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderadam committed Jan 31, 2016
1 parent 652ac14 commit 3ed7733
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 20 deletions.
15 changes: 12 additions & 3 deletions .travis.yml
Expand Up @@ -3,18 +3,27 @@ cache: bundler
sudo: false
services:
- mysql
- postgresql

rvm:
- 2.1.8
- 2.2.4
- 2.3.0

bundler_args: "--deployment --without development production --with mysql postgresql --jobs 3 --retry 3"

before_script:
- mysql -e 'create database posty_api_test;'
- cp config/database.travis.yml config/database.yml
- sh -c "if [ '$DB' = 'mysql2' ]; then mysql -e 'create database posty_api_test;'; fi"
- sh -c "if [ '$DB' = 'mysql2' ]; then cp config/database.travis.mysql2.yml config/database.yml; fi"
- sh -c "if [ '$DB' = 'postgresql' ]; then psql -c 'create database posty_api_test;' -U postgres; fi"
- sh -c "if [ '$DB' = 'postgresql' ]; then cp config/database.travis.postgresql.yml config/database.yml; fi"
- bundle exec rake db:migrate
- bundle exec rake api_key:generate

script:
- bundle exec rake spec
- bundle exec rubocop
- bundle exec rubocop

env:
- DB=mysql2
- DB=postgresql
28 changes: 17 additions & 11 deletions Gemfile
@@ -1,16 +1,13 @@
source 'http://rubygems.org'

group :default do
gem 'rack', '~> 1.5.2'
gem 'rake', '~> 10.3.2'
gem 'grape', '~> 0.7.0'
gem 'activerecord', '~> 3.2.22', require: 'active_record'
gem 'json'
gem 'grape-swagger'
gem 'rack-cors', require: 'rack/cors'
gem 'mysql2', '~> 0.3.16'
gem 'schema_plus', '~> 1.5.1'
end
gem 'rack', '~> 1.5.2'
gem 'rake', '~> 10.3.2'
gem 'grape', '~> 0.7.0'
gem 'activerecord', '~> 3.2.22', require: 'active_record'
gem 'json'
gem 'grape-swagger'
gem 'rack-cors', require: 'rack/cors'
gem 'schema_plus', '~> 1.5.1'

group :test, :development do
gem 'rspec'
Expand All @@ -19,3 +16,12 @@ group :test, :development do
gem 'racksh'
gem 'rubocop'
end

group :mysql, optional: true do
gem 'mysql2', '~> 0.3.16'
end

group :postgresql, optional: true do
gem 'pg', '~> 0.18.4'
gem 'activerecord-postgresql-adapter'
end
5 changes: 5 additions & 0 deletions Gemfile.lock
Expand Up @@ -9,6 +9,8 @@ GEM
activesupport (= 3.2.22.1)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
activerecord-postgresql-adapter (0.0.1)
pg
activesupport (3.2.22.1)
i18n (~> 0.6, >= 0.6.4)
multi_json (~> 1.0)
Expand Down Expand Up @@ -50,6 +52,7 @@ GEM
mysql2 (0.3.20)
parser (2.3.0.2)
ast (~> 2.2)
pg (0.18.4)
powerpack (0.1.1)
rack (1.5.5)
rack-accept (0.4.5)
Expand Down Expand Up @@ -102,10 +105,12 @@ PLATFORMS

DEPENDENCIES
activerecord (~> 3.2.22)
activerecord-postgresql-adapter
grape (~> 0.7.0)
grape-swagger
json
mysql2 (~> 0.3.16)
pg (~> 0.18.4)
rack (~> 1.5.2)
rack-cors
rack-test
Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -13,8 +13,8 @@ Tested with ruby 2.1.8, 2.2.4, 2.3.0
1. Download the source either using git or from the GitHub page as archive.
2. Extract the archive
3. Change directory to the extracted folder
4. Move config/database_default.yml to config/database.yml and change it to your needs.
5. Run ``bundle install``
4. Copy config/database.mysql2.yml or config/database.postgresql.yml to config/database.yml and change it to your needs.
5. Run ``bundle install --with mysql`` for MySQL or ``bundle install --with postgresql`` for PostgreSQL
6. Run ``rake db:migrate``
7. Run ``rake api_key:generate``
8. Start the application e.g. with ``rackup``
Expand Down
1 change: 1 addition & 0 deletions Rakefile
Expand Up @@ -5,6 +5,7 @@ begin
rescue LoadError
end


task :environment do
require File.expand_path('../config/environment', __FILE__)
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/api_key.rb
Expand Up @@ -5,7 +5,7 @@ class ApiKey < ActiveRecord::Base
validates :expires_at, presence: true
validates :access_token, uniqueness: true

scope :active, -> { where('expires_at > ? AND active = 1', Time.now) }
scope :active, -> { where('expires_at > ? AND active = ?', Time.now, true) }

private

Expand Down
File renamed without changes.
19 changes: 19 additions & 0 deletions config/database.postgresql.yml
@@ -0,0 +1,19 @@
production:
adapter: postgresql
encoding: utf8
reconnect: false
database: database_name
pool: 5
username: database_user_name
password: database_user_password
host: localhost

development:
adapter: postgresql
encoding: utf8
reconnect: false
database: database_name
pool: 5
username: database_user_name
password: database_user_password
host: localhost
Expand Up @@ -2,4 +2,4 @@ test:
adapter: mysql2
database: posty_api_test
username: travis
encoding: utf8
encoding: utf8
4 changes: 4 additions & 0 deletions config/database.travis.postgresql.yml
@@ -0,0 +1,4 @@
test:
adapter: postgresql
database: posty_api_test
username: postgres
2 changes: 0 additions & 2 deletions spec/spec_helper.rb
@@ -1,5 +1,3 @@
require 'rubygems'

ENV['RACK_ENV'] ||= 'test'

require File.expand_path('../../config/environment', __FILE__)
Expand Down

0 comments on commit 3ed7733

Please sign in to comment.