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

Postgresql Migration #333

Merged
merged 7 commits into from
Aug 30, 2024
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ on:
default: 'master'
jobs:
wagon_tests:
uses: hitobito/hitobito/.github/workflows/wagon-tests.yml@master
uses: hitobito/hitobito/.github/workflows/wagon-tests.yml@psql-migration
with:
wagon_repository: ${{ github.event.repository.name }}
wagon_dependency_repository: hitobito_youth
Expand Down
2 changes: 0 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,3 @@ load 'rspec/rails/tasks/rspec.rake'
require 'ci/reporter/rake/rspec' unless Rails.env == 'production'

HitobitoPbs::Wagon.load_tasks

task 'test:prepare' => 'db:test:prepare'
2 changes: 1 addition & 1 deletion app/abilities/pbs/event_ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def if_participating_as_leader_role_of_supercamp
end

def if_part_of_krisenteam
user.roles.where(type: CAMP_KRISENTEAM_ROLES, group: kantonalverbaende).exists?
user.roles.where(type: CAMP_KRISENTEAM_ROLES.map(&:to_s), group: kantonalverbaende).exists?
end

def kantonalverbaende
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/event/approvals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Event::ApprovalsController < CrudController
decorates :group, :event, :participation

def index
@approvals = entries.group_by(&:participation)
@approvals = entries.select("event_approvals.*").group_by(&:participation)
end

def new
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/group_health_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class GroupHealthController < ApplicationController
CANTON_JOIN = "LEFT JOIN #{Group.quoted_table_name} AS canton " \
"ON #{Group.quoted_table_name}.lft >= canton.lft " \
"AND #{Group.quoted_table_name}.lft < canton.rgt " \
'AND canton.type = "Group::Kantonalverband"'.freeze
"AND canton.type = 'Group::Kantonalverband'".freeze

# exclude Group::InternesGremium groups
INTERNES_GREMIUM_GROUP_TYPES = [Group::InternesGremium, Group::InternesAbteilungsGremium,
Expand Down Expand Up @@ -77,6 +77,7 @@ def groups
respond(Group.from("((#{bund}) UNION (#{cantons}) UNION (#{abt_and_below})) " \
"AS #{Group.quoted_table_name}")
.where(EXCLUDE_INTERNES_GREMIUM)
.order(:lft)
.page(params[:page]).per(params[:size] || DEFAULT_PAGE_SIZE)
.as_json(only: GROUPS_FIELDS))
end
Expand Down
5 changes: 3 additions & 2 deletions app/controllers/pbs/event/lists_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def all_kantonalverband_camps
.joins(:groups)
.where("groups.lft >= ? AND groups.rgt <= ?", @group.lft, @group.rgt)
.in_year(year)
.list
end

def all_camps_in_canton
Expand All @@ -108,17 +109,17 @@ def all_camps_abroad
base_camp_query
.where(canton: Event::Camp::ABROAD_CANTON)
.in_year(year)
.list
end

def base_camp_query(excluded_states = %w[created canceled])
Event::Camp.where.not(camp_submitted_at: nil)
.where.not(state: excluded_states)
.includes(:groups)
.list
end

def in_next_three_weeks(scope)
timespan = Time.zone.now.midnight + 3.weeks
scope.where("event_dates.start_at <= ? OR event_dates.finish_at <= ?", timespan, timespan)
scope.where("event_dates.start_at <= ? OR event_dates.finish_at <= ?", timespan, timespan).list
end
end
3 changes: 2 additions & 1 deletion app/controllers/population_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def group
end

def load_groups
group.self_and_descendants.without_deleted.order_by_type(group)
group.self_and_descendants.without_deleted.order_by_type
end

def load_people_by_group
Expand All @@ -50,6 +50,7 @@ def load_people(group)
.where(roles: {group_id: group})
.includes(:groups)
.preload_groups
.select("people.*")
.order_by_role
.order_by_name
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/supercamps_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def supercamps_on_group_and_above
def matching_supercamps
Event::Camp.upcoming
.left_joins(:translations)
.where("event_translations.name LIKE ?", "%#{params[:q]}%")
.where("event_translations.name ILIKE ?", "%#{params[:q]}%")
.where(allow_sub_camps: true, state: "created").distinct
end

Expand Down
4 changes: 2 additions & 2 deletions app/domain/pbs/export/tabular/events/bsv_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ def kurs_kind
end

def kantonalverband
entry.groups.where(type: Group::Kantonalverband).join(", ") || nil
entry.groups.where(type: "Group::Kantonalverband").join(", ") || nil
end

def region
entry.groups.where(type: Group::Region).join(", ") || nil
entry.groups.where(type: "Group::Region").join(", ") || nil
end

def language_count_pbs
Expand Down
10 changes: 0 additions & 10 deletions app/indices/group_index.rb

This file was deleted.

10 changes: 0 additions & 10 deletions app/indices/person_index.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/jobs/event/approval_cleanup_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def old_events
.select(:event_id).select("MAX(finish_at) AS finish_at, MAX(start_at) AS start_at")
.joins(:event).where(events: {state: "closed"})
.group(:event_id))
.where("IFNULL(finish_at, start_at) < ?", cutoff_date)
.where("COALESCE(finish_at, start_at) < ?", cutoff_date)
end

def approvals
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/event/camp_reminder_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Event::CampReminderJob < RecurringJob
RUN_AT = 3 # 3 a.m.
SPAN_NATIONAL = 6.weeks
SPAN_ABROAD = 10.weeks
NOTIFIED_ROLES = [Event::Camp::Role::Leader, Event::Camp::Role::Coach].freeze
NOTIFIED_ROLES = ["Event::Camp::Role::Leader", "Event::Camp::Role::Coach"].freeze

run_every 24.hours

Expand Down
1 change: 1 addition & 0 deletions app/jobs/event/canceled_camp_participation_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def camp_leaders
.where(event_roles: {type: Event::Camp::Role::Leader.sti_name})
.distinct
.includes(:additional_emails)
.order(:id)
end

def participation
Expand Down
2 changes: 1 addition & 1 deletion app/mailers/black_list_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def hit(person, target = nil)
def recipients
@recipients ||= Person
.joins(:roles)
.where(roles: {type: BLACK_LIST_ROLES})
.where(roles: {type: BLACK_LIST_ROLES.map(&:sti_name)})
.pluck(:email)
end

Expand Down
6 changes: 3 additions & 3 deletions app/mailers/crisis_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ class CrisisMailer < ApplicationMailer
CONTENT_CRISIS_ACKNOWLEDGED = "content_crisis_acknowledged".freeze
CONTENT_CRISIS_TRIGGERED = "content_crisis_triggered".freeze

BUND_RECIPIENTS = [Group::Bund::VerantwortungKrisenteam,
Group::Bund::LeitungKernaufgabeKommunikation]
BUND_RECIPIENTS = [Group::Bund::VerantwortungKrisenteam.sti_name,
Group::Bund::LeitungKernaufgabeKommunikation.sti_name]

BUND_CREATORS = BUND_RECIPIENTS + [Group::Bund::MitgliedKrisenteam]
BUND_CREATORS = BUND_RECIPIENTS + [Group::Bund::MitgliedKrisenteam.sti_name]

attr_reader :crisis

Expand Down
11 changes: 6 additions & 5 deletions app/models/member_count.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,18 @@ def total_by_regionen(year, state)
end

def total_for_bund(year)
totals_by(year, :year).first
totals_by(year, :year).order("MIN(member_counts.id)").first
end

def total_for_abteilung(year, abteilung)
totals_by(year, :abteilung_id, abteilung_id: abteilung.id).first
totals_by(year, :abteilung_id, abteilung_id: abteilung.id).order("MIN(member_counts.id)")
.first
end

def totals(year)
columns = "kantonalverband_id, " \
"region_id, " \
"abteilung_id, " +
columns = "MIN(kantonalverband_id) AS kantonalverband_id, " \
"MIN(region_id) AS region_id, " \
"MIN(abteilung_id) AS abteilung_id, " +
COUNT_COLUMNS.collect { |c| "SUM(#{c}) AS #{c}" }.join(",")

select(columns).where(year: year)
Expand Down
3 changes: 3 additions & 0 deletions app/models/pbs/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ module Pbs::Group
self.used_attributes += [:website, :bank_account, :pbs_shortname]
self.superior_attributes = [:pbs_shortname]

Group::SEARCHABLE_ATTRS << :pbs_shortname << :description << :website << :bank_account
include PgSearchable

validates :description, length: {allow_nil: true, maximum: 2**16 - 1}
validates :hostname, uniqueness: true, allow_blank: true
has_many :crises
Expand Down
3 changes: 3 additions & 0 deletions app/models/pbs/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ module Pbs::Person
:prefers_digital_correspondence << :kantonalverband_id
Person::ADDRESS_ATTRS << "prefers_digital_correspondence"

Person::SEARCHABLE_ATTRS << :title << :pbs_number
include PgSearchable

alias_method_chain :full_name, :title

i18n_boolean_setter :prefers_digital_correspondence
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,16 @@ def initialize(people_scope)
end

def grouped_households
people = Person.quoted_table_name

@people_scope.
# remove previously added selects, very important to make this query scale
unscope(:select, :includes).
# group by household, but keep NULLs separate
select("IFNULL(#{people}.`household_key`, #{people}.`id`) as `key`").
select("COALESCE(#{people}.\"household_key\", \"people\".\"id\"::text) as \"key\"").
group(:key).
# Primary sorting criterion
select("COUNT(#{people}.`household_key`) as `member_count`").
select("COUNT(#{people}.\"household_key\") as \"member_count\"").
# Secondary, unique sorting criterion
select("MIN(#{people}.`id`) as `id`")
select("MIN(#{people}.\"id\") as \"id\"")
end

def households_in_batches
Expand Down
4 changes: 2 additions & 2 deletions spec/abilities/black_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@

describe 'other roles' do
it 'cannot read BlackList' do
other_roles = Role.where.not(type: [Group::Bund::Geschaeftsleitung,
Group::Bund::LeitungKernaufgabeKommunikation])
other_roles = Role.where.not(type: ["Group::Bund::Geschaeftsleitung",
"Group::Bund::LeitungKernaufgabeKommunikation"])

other_roles.each do |role|
ability = Ability.new(role.person)
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/event/lists_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@
values = rows.second.split(';')
#main labels
expect(values[0..8]).to eq(
["", "", "LPK (Leitpfadikurs)", "Zürich, Bern", '""', "124", "11.11.2015", "12.11.2015", ""]
["", "", "LPK (Leitpfadikurs)", "Bern, Zürich", '""', "124", "11.11.2015", "12.11.2015", ""]
)
#counts -> participant_, canton_ & language_count are incorrectly 0 here
# as person.canton is not easily setable in test here.
Expand Down
8 changes: 4 additions & 4 deletions spec/controllers/population_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@

it do
is_expected.to eq([abteilung,
groups(:sunnewirbu),
groups(:sunnewirbu),
groups(:baereried),
groups(:medusa),
groups(:pegasus),
groups(:poseidon),
groups(:medusa),
groups(:baereried),
groups(:rovers),
groups(:elternrat),
groups(:fussballers)])
Expand All @@ -48,7 +48,7 @@

describe 'people by group' do
subject { assigns(:people_by_group) }

it { expect(subject[abteilung].collect(&:to_s)).to contain_exactly(*[leader, people(:al_schekka), guide].collect(&:to_s)) }
it { expect(subject[groups(:pegasus)].collect(&:to_s)).to contain_exactly(*[group_leader, child, people(:child)].collect(&:to_s)) }
it { expect(subject[groups(:baereried)]).to be_nil } # no people in group - not displayed at all
Expand Down
41 changes: 41 additions & 0 deletions spec/domain/search_strategy/group_search_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright (c) 2024, Pfadibewegung Schweiz. This file is part of
# hitobito_pbs and licensed under the Affero General Public License version 3
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_pbs.
#
require "spec_helper"

describe SearchStrategies::GroupSearch do
before do
groups(:bund).update!(pbs_shortname: "shortname", description: "description", website: "lemurenshop",
bank_account: "account")
end

describe "#search_fulltext" do
let(:user) { people(:root) }

it "finds accessible groups by shortname" do
result = search_class(groups(:bund).pbs_shortname).search_fulltext
expect(result).to include(groups(:bund))
end

it "finds accessible groups by description" do
result = search_class(groups(:bund).description).search_fulltext
expect(result).to include(groups(:bund))
end

it "finds accessible groups by website" do
result = search_class(groups(:bund).website).search_fulltext
expect(result).to include(groups(:bund))
end

it "finds accessible groups by bank account" do
result = search_class(groups(:bund).bank_account).search_fulltext
expect(result).to include(groups(:bund))
end
end

def search_class(term = nil, page = nil)
described_class.new(user, term, page)
end
end
30 changes: 30 additions & 0 deletions spec/domain/search_strategy/person_search_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (c) 2024, Pfadibewegung Schweiz. This file is part of
# hitobito_pbs and licensed under the Affero General Public License version 3
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_pbs.

require "spec_helper"

describe SearchStrategies::PersonSearch do
before do
people(:al_schekka).update!(pbs_number: 4566, title: "Sky")
end

describe "#search_fulltext" do
let(:user) { people(:bulei) }

it "finds accessible person by title" do
result = search_class(people(:al_schekka).title).search_fulltext
expect(result).to include(people(:al_schekka))
end

it "finds accessible person by pbs number" do
result = search_class(people(:al_schekka).pbs_number.to_s).search_fulltext
expect(result).to include(people(:al_schekka))
end
end

def search_class(term = nil, page = nil)
described_class.new(user, term, page)
end
end
Loading
Loading