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

Freehub on Postgres #44

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ jobs: # a collection of steps
BUNDLE_RETRY: 3
BUNDLE_PATH: vendor/bundle
RAILS_ENV: test
- image: mysql:5.6 # database image
environment: # environment variables for database
MYSQL_ROOT_PASSWORD=password
- image: postgres:9.4.11 # database image
steps: # a collection of executable commands
- checkout # special step to check out source code to working directory

Expand All @@ -37,16 +35,16 @@ jobs: # a collection of steps
paths:
- vendor/bundle

# Our primary container isn't MYSQL so wait for it
# Our primary container isn't Postgres so wait for it
- run:
name: Install dockerize
command: wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
environment:
DOCKERIZE_VERSION: v0.6.1

- run:
name: Waiting for MySQL to be ready
command: dockerize -wait tcp://localhost:3306 -timeout 1m
name: Waiting for Postgres to be ready
command: dockerize -wait tcp://localhost:5432 -timeout 1m

- run:
name: Database setup
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
FROM ruby:1.9.3
MAINTAINER Alon Salant <[email protected]>

RUN apt-get update && apt-get install -y mysql-client
RUN apt-get update && apt-get install -y mysql-client postgresql-client pgloader

# Configure the main working directory. This is the base
# directory used in any further RUN, COPY, and ENTRYPOINT
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source 'https://rubygems.org'

gem "rails", "2.3.17"
gem "mysql"
gem "pg", "0.18.4"
gem "authorization", github: "asalant/rails-authorization-plugin"
gem 'json', '1.7.7' # (CVE-2013-026) Can remove once rails depends on > 1.7.6
gem 'haml', "3.0.25"
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ GEM
builder
json (1.7.7)
kgio (2.11.2)
mysql (2.9.1)
newrelic_rpm (3.7.3.204)
pg (0.18.4)
power_assert (0.3.0)
rack (1.1.6)
rails (2.3.17)
Expand Down Expand Up @@ -65,8 +65,8 @@ DEPENDENCIES
haml (= 3.0.25)
hoptoad_notifier
json (= 1.7.7)
mysql
newrelic_rpm
pg (= 0.18.4)
rails (= 2.3.17)
rdoc
test-unit
Expand Down
3 changes: 2 additions & 1 deletion app/models/note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def self.for_person_sql(person, options={})
OR (notes.notable_type = 'Visit' AND notes.notable_id IN (SELECT visits.id FROM visits WHERE visits.person_id = #{person.id}))"

sql += " ORDER BY #{options[:order]}" if options[:order]
sql += " LIMIT #{options[:offset]},#{options[:limit]}" if options[:limit]
sql += " LIMIT #{options[:limit]}" if options[:limit]
sql += " OFFSET #{options[:offset]} "
sql
end

Expand Down
3 changes: 2 additions & 1 deletion app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def tags
unless @tags
tags = Set.new ActsAsTaggableOn::Tag.find(:all,
:select => 'tags.id, tags.name',
:joins => "left join (taggings, people) on (tags.id = taggings.tag_id and taggings.taggable_type = 'Person' and taggings.context = 'tags' and taggings.taggable_id = people.id)",
:joins => ["left join taggings on (tags.id = taggings.tag_id and taggings.taggable_type = 'Person' and taggings.context = 'tags')",
"left join people on (taggings.taggable_id = people.id)"],
:conditions => ["people.organization_id = ?", self])
@tags = tags.sort_by {|tag| tag.name.downcase}
end
Expand Down
4 changes: 2 additions & 2 deletions app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ def on(service_type, time)
} }

named_scope :after, lambda { |date| {
:conditions => [ "convert_tz(people.created_at,'+00:00','#{Time.zone.formatted_offset}') >= ?", date.to_date.to_time.utc ]
:conditions => [ "timezone('#{Time.zone.formatted_offset}', people.created_at) >= ?", date.to_date.to_time.utc ]

} }

named_scope :before, lambda { |date| {
:conditions => [ "convert_tz(people.created_at,'+00:00','#{Time.zone.formatted_offset}') < ?", date.to_date.to_time.utc ]
:conditions => [ "timezone('#{Time.zone.formatted_offset}', people.created_at) < ?", date.to_date.to_time.utc ]
} }

named_scope :matching_name, lambda { |name| {
Expand Down
4 changes: 2 additions & 2 deletions app/models/visit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ class Visit < ActiveRecord::Base
} }

named_scope :after, lambda { |date| {
:conditions => [ "convert_tz(visits.arrived_at,'+00:00','#{Time.zone.formatted_offset}') >= ?", date.to_date.to_time.utc ]
:conditions => [ "timezone('#{Time.zone.formatted_offset}', visits.arrived_at) >= ?", date.to_date.to_time.utc ]
} }

named_scope :before, lambda { |date| {
:conditions => [ "convert_tz(visits.arrived_at,'+00:00','#{Time.zone.formatted_offset}') < ?", date.to_date.to_time.utc ]
:conditions => [ "timezone('#{Time.zone.formatted_offset}', visits.arrived_at) < ?", date.to_date.to_time.utc ]
} }

def initialize(params={})
Expand Down
18 changes: 11 additions & 7 deletions app/models/visits_summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ def summarize_days
date_condition += "and visits.arrived_at > '#{criteria[:from].to_date.to_time.utc.to_s(:db)}' " if criteria[:from]
date_condition += "and visits.arrived_at < '#{criteria[:to].to_date.to_time.utc.to_s(:db)}' " if criteria[:to]
visits_result = ActiveRecord::Base.connection.select_all(<<-END
select date(convert_tz(visits.arrived_at,'+00:00','#{Time.zone.formatted_offset}')) as date, visits.staff, visits.member, visits.volunteer, count(*) as count
select date(timezone('#{Time.zone.formatted_offset}', visits.arrived_at)) as date, visits.staff, visits.member, visits.volunteer, count(*) as count
from visits
left join people on visits.person_id = people.id
where people.organization_id = #{criteria[:organization_id]}
#{date_condition}
group by date(visits.arrived_at), visits.staff, visits.member, visits.volunteer
order by visits.arrived_at asc
group by date(timezone('#{Time.zone.formatted_offset}', visits.arrived_at)), visits.staff, visits.member, visits.volunteer
order by date(timezone('#{Time.zone.formatted_offset}', visits.arrived_at)) asc
END
)
visit_days, day = [], nil
Expand Down Expand Up @@ -80,16 +80,20 @@ def initialize(date)
@staff, @member, @volunteer, @patron = 0, 0, 0, 0
end

def to_bool(value)
ActiveRecord::ConnectionAdapters::Column.value_to_boolean(value)
end

def add_row(row)
if row['staff'] == '1'
if row['volunteer'] == '1'
if to_bool(row['staff'])
if to_bool(row['volunteer'])
@staff += row['count'].to_i
else
@member += row['count'].to_i # count non-volunteering staff as members
end
elsif row['volunteer'] == '1'
elsif to_bool(row['volunteer'])
@volunteer = row['count'].to_i
elsif row['member'] == '1'
elsif to_bool(row['member'])
@member = row['count'].to_i
else
@patron = row['count'].to_i
Expand Down
17 changes: 9 additions & 8 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
development:
adapter: mysql
encoding: utf8
adapter: postgresql
encoding: unicode
database: freehub_for_all_development
username: root
password: password
username: postgres
host: <%= ENV['DATABASE_HOST'] %>
port: 5432

# Warning: The database defined as 'test' will be erased and
# re-generated from your development database when you run 'rake'.
# Do not set this db to the same as development or production.
test:
adapter: mysql
encoding: utf8
adapter: postgresql
encoding: unicode
database: freehub_for_all_test
username: root
password: password
username: postgres
host: <%= ENV['DATABASE_HOST'] %>
port: 5432

14 changes: 0 additions & 14 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
t.datetime "updated_at"
end

add_index "notes", ["created_by_id"], :name => "fk_notes_created_by"
add_index "notes", ["notable_type", "notable_id"], :name => "index_notes_on_notable_type_and_notable_id"
add_index "notes", ["updated_by_id"], :name => "fk_notes_updated_by"

create_table "organizations", :force => true do |t|
t.string "name"
Expand Down Expand Up @@ -58,10 +56,6 @@
t.integer "yob"
end

add_index "people", ["created_by_id"], :name => "fk_people_created_by"
add_index "people", ["organization_id"], :name => "fk_people_organization"
add_index "people", ["updated_by_id"], :name => "fk_people_updated_by"

create_table "roles", :force => true do |t|
t.string "name", :limit => 40
t.string "authorizable_type", :limit => 40
Expand Down Expand Up @@ -90,10 +84,6 @@
t.integer "updated_by_id"
end

add_index "services", ["created_by_id"], :name => "fk_services_created_by"
add_index "services", ["person_id"], :name => "fk_services_person"
add_index "services", ["updated_by_id"], :name => "fk_services_updated_by"

create_table "taggings", :force => true do |t|
t.integer "tag_id"
t.integer "taggable_id"
Expand Down Expand Up @@ -138,8 +128,4 @@
t.boolean "member"
end

add_index "visits", ["created_by_id"], :name => "fk_visits_created_by"
add_index "visits", ["person_id"], :name => "fk_visits_person"
add_index "visits", ["updated_by_id"], :name => "fk_visits_updated_by"

end
7 changes: 6 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@
app:
build: .
environment:
- DATABASE_HOST=mysql
- DATABASE_HOST=postgres
command: /app/script/server
volumes:
- .:/app
ports:
- "3000:3000"
links:
- postgres
- mysql
mysql:
image: mysql:5.6
environment:
- MYSQL_ROOT_PASSWORD=password
ports:
- "3306"
postgres:
image: postgres:9.4
ports:
- "5432"
14 changes: 14 additions & 0 deletions pgloader.load
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- See https://github.com/dimitri/pgloader/blob/master/pgloader.1.md for
-- connection string options.

LOAD DATABASE
FROM mysql://root:password@mysql/freehub_for_all_development
INTO postgresql://postgres@postgres/freehub_for_all_development

-- data only: We don't need pgloader to touch the schema as Rails does a better
-- job using rake db:schema:load.
-- truncate: Ensure all tables are empty first (especially schema_migrations).
-- WARNING: THIS WILL SMOKE YOUR DATABASE!

WITH data only, truncate
EXCLUDING table names matching 'schema_info';