From 3ed7733d6f61eb8e7465bb6b612ecd932455dbf9 Mon Sep 17 00:00:00 2001 From: Alexander Adam Date: Sun, 31 Jan 2016 22:41:07 +0100 Subject: [PATCH] add postgres support --- .travis.yml | 15 ++++++++-- Gemfile | 28 +++++++++++-------- Gemfile.lock | 5 ++++ README.md | 4 +-- Rakefile | 1 + app/models/api_key.rb | 2 +- ...tabase_default.yml => database.mysql2.yml} | 0 config/database.postgresql.yml | 19 +++++++++++++ ....travis.yml => database.travis.mysql2.yml} | 2 +- config/database.travis.postgresql.yml | 4 +++ spec/spec_helper.rb | 2 -- 11 files changed, 62 insertions(+), 20 deletions(-) rename config/{database_default.yml => database.mysql2.yml} (100%) create mode 100644 config/database.postgresql.yml rename config/{database.travis.yml => database.travis.mysql2.yml} (80%) create mode 100644 config/database.travis.postgresql.yml diff --git a/.travis.yml b/.travis.yml index 0b7daf3..6a4038a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 \ No newline at end of file + - bundle exec rubocop + +env: + - DB=mysql2 + - DB=postgresql \ No newline at end of file diff --git a/Gemfile b/Gemfile index 5f41263..2237927 100644 --- a/Gemfile +++ b/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' @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index 85b8ca8..60afc48 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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 diff --git a/README.md b/README.md index d47c7e9..5cf36f3 100644 --- a/README.md +++ b/README.md @@ -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`` diff --git a/Rakefile b/Rakefile index feddf88..cdb5376 100644 --- a/Rakefile +++ b/Rakefile @@ -5,6 +5,7 @@ begin rescue LoadError end + task :environment do require File.expand_path('../config/environment', __FILE__) end diff --git a/app/models/api_key.rb b/app/models/api_key.rb index a857b9d..763214e 100644 --- a/app/models/api_key.rb +++ b/app/models/api_key.rb @@ -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 diff --git a/config/database_default.yml b/config/database.mysql2.yml similarity index 100% rename from config/database_default.yml rename to config/database.mysql2.yml diff --git a/config/database.postgresql.yml b/config/database.postgresql.yml new file mode 100644 index 0000000..6d2f840 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/config/database.travis.yml b/config/database.travis.mysql2.yml similarity index 80% rename from config/database.travis.yml rename to config/database.travis.mysql2.yml index fe9f9c9..6af0089 100644 --- a/config/database.travis.yml +++ b/config/database.travis.mysql2.yml @@ -2,4 +2,4 @@ test: adapter: mysql2 database: posty_api_test username: travis - encoding: utf8 \ No newline at end of file + encoding: utf8 diff --git a/config/database.travis.postgresql.yml b/config/database.travis.postgresql.yml new file mode 100644 index 0000000..4878db4 --- /dev/null +++ b/config/database.travis.postgresql.yml @@ -0,0 +1,4 @@ +test: + adapter: postgresql + database: posty_api_test + username: postgres \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5223304..ddfe988 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,5 +1,3 @@ -require 'rubygems' - ENV['RACK_ENV'] ||= 'test' require File.expand_path('../../config/environment', __FILE__)