From 2880e39d01d4d13f78509b5a6289969c054e372b Mon Sep 17 00:00:00 2001 From: megli2 Date: Mon, 3 Jul 2023 13:59:44 +0200 Subject: [PATCH 01/14] add pg_search configuration --- Gemfile | 1 + Gemfile.lock | 4 ++++ app/models/encryptable.rb | 3 +++ app/models/folder.rb | 3 +++ app/models/team.rb | 3 +++ ...20230703095213_create_pg_search_documents.rb | 17 +++++++++++++++++ db/schema.rb | 11 ++++++++++- 7 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20230703095213_create_pg_search_documents.rb diff --git a/Gemfile b/Gemfile index a1c774548..8ac724e4d 100644 --- a/Gemfile +++ b/Gemfile @@ -21,6 +21,7 @@ gem 'maxmind-db' gem 'openid_connect' gem 'password_strength' gem 'pg' +gem 'pg_search' gem 'pundit' gem 'rails-i18n' gem 'seed-fu' diff --git a/Gemfile.lock b/Gemfile.lock index e2dedf172..600cac540 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -198,6 +198,9 @@ GEM password_strength (1.1.4) activemodel pg (1.5.3) + pg_search (2.3.6) + activerecord (>= 5.2) + activesupport (>= 5.2) pry (0.14.2) coderay (~> 1.1) method_source (~> 1.0) @@ -390,6 +393,7 @@ DEPENDENCIES openid_connect password_strength pg + pg_search pry pry-byebug pry-stack_explorer diff --git a/app/models/encryptable.rb b/app/models/encryptable.rb index c7cfc8f39..b1334e7d2 100644 --- a/app/models/encryptable.rb +++ b/app/models/encryptable.rb @@ -28,6 +28,9 @@ class Encryptable < ApplicationRecord validates :name, presence: true validates :description, length: { maximum: 4000 } + include PgSearch::Model + multisearchable against: [:name] + def encrypt(team_password, encryption_algorithm = nil) used_encrypted_attrs.each do |a| encrypt_attr(a, team_password, encryption_algorithm) diff --git a/app/models/folder.rb b/app/models/folder.rb index 6854f9c27..58ca1965c 100644 --- a/app/models/folder.rb +++ b/app/models/folder.rb @@ -25,6 +25,9 @@ class Folder < ApplicationRecord validates :description, length: { maximum: 300 } validates :personal_inbox, uniqueness: { scope: :team, if: :personal_inbox? } + include PgSearch::Model + multisearchable against: [:name] + def label name end diff --git a/app/models/team.rb b/app/models/team.rb index bf25c0ea7..9c2d813ac 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -34,6 +34,9 @@ class Team < ApplicationRecord in_progress: 2 }, _prefix: :recrypt + include PgSearch::Model + multisearchable against: [:name] + def label name end diff --git a/db/migrate/20230703095213_create_pg_search_documents.rb b/db/migrate/20230703095213_create_pg_search_documents.rb new file mode 100644 index 000000000..d16e38142 --- /dev/null +++ b/db/migrate/20230703095213_create_pg_search_documents.rb @@ -0,0 +1,17 @@ +class CreatePgSearchDocuments < ActiveRecord::Migration[7.0] + def up + say_with_time("Creating table for pg_search multisearch") do + create_table :pg_search_documents do |t| + t.text :content + t.belongs_to :searchable, polymorphic: true, index: true + t.timestamps null: false + end + end + end + + def down + say_with_time("Dropping table for pg_search multisearch") do + drop_table :pg_search_documents + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 40e3c98f2..3e895b0b9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2022_07_19_120958) do +ActiveRecord::Schema[7.0].define(version: 2023_07_03_095213) do create_table "encryptables", force: :cascade do |t| t.string "name", limit: 255, default: "", null: false t.integer "folder_id" @@ -41,6 +41,15 @@ t.index ["name"], name: "index_folders_on_name" end + create_table "pg_search_documents", force: :cascade do |t| + t.text "content" + t.string "searchable_type" + t.integer "searchable_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["searchable_type", "searchable_id"], name: "index_pg_search_documents_on_searchable" + end + create_table "settings", force: :cascade do |t| t.string "key", null: false t.string "value" From a45bc5b1ae7796f92897346a8b7080cd2ca9aedf Mon Sep 17 00:00:00 2001 From: megli2 Date: Tue, 4 Jul 2023 08:19:55 +0200 Subject: [PATCH 02/14] add postgres models to load them dynamically if application was started with postgres database --- app/models/folder.rb | 3 --- app/models/postgres/encryptable.rb | 6 ++++++ app/models/postgres/folder.rb | 6 ++++++ app/models/postgres/team.rb | 6 ++++++ app/models/team.rb | 3 --- 5 files changed, 18 insertions(+), 6 deletions(-) create mode 100644 app/models/postgres/encryptable.rb create mode 100644 app/models/postgres/folder.rb create mode 100644 app/models/postgres/team.rb diff --git a/app/models/folder.rb b/app/models/folder.rb index 58ca1965c..6854f9c27 100644 --- a/app/models/folder.rb +++ b/app/models/folder.rb @@ -25,9 +25,6 @@ class Folder < ApplicationRecord validates :description, length: { maximum: 300 } validates :personal_inbox, uniqueness: { scope: :team, if: :personal_inbox? } - include PgSearch::Model - multisearchable against: [:name] - def label name end diff --git a/app/models/postgres/encryptable.rb b/app/models/postgres/encryptable.rb new file mode 100644 index 000000000..50755e70d --- /dev/null +++ b/app/models/postgres/encryptable.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true +class Encryptable < Encryptable + include PgSearch::Model + multisearchable against: [:name] +end + diff --git a/app/models/postgres/folder.rb b/app/models/postgres/folder.rb new file mode 100644 index 000000000..33c61c864 --- /dev/null +++ b/app/models/postgres/folder.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +class Folder < Folder + include PgSearch::Model + multisearchable against: [:name] +end \ No newline at end of file diff --git a/app/models/postgres/team.rb b/app/models/postgres/team.rb new file mode 100644 index 000000000..e7b084402 --- /dev/null +++ b/app/models/postgres/team.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true +class Team < Team + include PgSearch::Model + multisearchable against: [:name] +end + diff --git a/app/models/team.rb b/app/models/team.rb index 9c2d813ac..bf25c0ea7 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -34,9 +34,6 @@ class Team < ApplicationRecord in_progress: 2 }, _prefix: :recrypt - include PgSearch::Model - multisearchable against: [:name] - def label name end From 9b818f71235d4d91631640329792c13e44a2255a Mon Sep 17 00:00:00 2001 From: megli2 Date: Tue, 4 Jul 2023 10:37:41 +0200 Subject: [PATCH 03/14] change configuration to work with postgres database --- config/application.rb | 8 +++++--- config/database.yml | 29 +++++++++++++++++++++-------- db/schema.rb | 13 ++++++++----- docker-compose.yml | 11 +++++++++++ 4 files changed, 45 insertions(+), 16 deletions(-) diff --git a/config/application.rb b/config/application.rb index 744034189..896c97d36 100644 --- a/config/application.rb +++ b/config/application.rb @@ -7,7 +7,6 @@ require_relative 'boot' - # See https://github.com/rails/rails/blob/v6.0.2.1/railties/lib/rails/all.rb for the list # and https://stackoverflow.com/questions/59593542/omit-action-mailbox-activestorage-and-conductor-routes-from-bin-rails-routes-i # of what is being included here @@ -63,7 +62,6 @@ class Application < Rails::Application # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] # config.i18n.default_locale = :de - # Configure the default encoding used in templates for Ruby 1.9. config.encoding = "utf-8" @@ -92,12 +90,16 @@ class Application < Rails::Application # config.active_record.schema_format = :sql config.generators do |g| - g.test_framework :minitest, fixture_replacement: :fabrication + g.test_framework :minitest, fixture_replacement: :fabrication g.fixture_replacement :fabrication, dir: "test/fabricators" end config.time_zone = ENV['TIME_ZONE'] || 'Bern' config.paths['config/routes.rb'].concat Dir[Rails.root.join('config/routes/*.rb')].sort + + if Rails.configuration.database_configuration["development"]["database"].exclude?("postgres") + Dir.glob("#{Rails.root}/app/models/**[^postgres^]/*/").each {|dir| config.autoload_paths << dir } + end end end diff --git a/config/database.yml b/config/database.yml index 72e6e684c..70c886aba 100644 --- a/config/database.yml +++ b/config/database.yml @@ -5,20 +5,31 @@ # PostgreSQL. Versions 8.2 and up are supported. # +#base: &generic +# adapter: <%#= ENV['RAILS_DB_ADAPTER'] || 'sqlite3' %> +# pool: 5 +# timeout: 5000 +# encoding: utf8 +<%# %w(host port username password).each do |option| %> +# <%# value = ENV["RAILS_DB_#{option.upcase}"] %> +# <%#= "#{option}: #{value}" if value.present? %> +<%# end %> + base: &generic - adapter: <%= ENV['RAILS_DB_ADAPTER'] || 'sqlite3' %> + adapter: <%= ENV['RAILS_DB_ADAPTER'] || 'postgresql' %> pool: 5 timeout: 5000 encoding: utf8 -<% %w(host port username password).each do |option| %> - <% value = ENV["RAILS_DB_#{option.upcase}"] %> - <%= "#{option}: #{value}" if value.present? %> -<% end %> + host: postgresql + port: 5432 + username: app + password: postgres development: <<: *generic - database: <%= ENV['RAILS_DB_NAME'] || "#{Rails.root}/db/development.sqlite3" %> + database: <%= ENV['RAILS_DB_NAME'] || "postgres" %> +# database: <%#= ENV['RAILS_DB_NAME'] || "#{Rails.root}/db/development.sqlite3" %> # Connect on a TCP socket. Omitted by default since the client uses a # domain socket that doesn't need configuration. Windows does not have @@ -40,8 +51,10 @@ development: # Do not set this db to the same as development or production. test: <<: *generic - database: <%= ENV['RAILS_DB_NAME'] || "#{Rails.root}/db/test.sqlite3" %> + database: <%= ENV['RAILS_DB_NAME'] || "postgres" %> +# <%#= ENV['RAILS_DB_NAME'] || "#{Rails.root}/db/test.sqlite3" %> production: <<: *generic - database: <%= ENV['RAILS_DB_NAME'] || "#{Rails.root}/db/production.sqlite3" %> + database: <%= ENV['RAILS_DB_NAME'] || "postgres" %> +# <%#= ENV['RAILS_DB_NAME'] || "#{Rails.root}/db/production.sqlite3" %> diff --git a/db/schema.rb b/db/schema.rb index 3e895b0b9..93ddb73a6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,6 +11,9 @@ # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema[7.0].define(version: 2023_07_03_095213) do + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + create_table "encryptables", force: :cascade do |t| t.string "name", limit: 255, default: "", null: false t.integer "folder_id" @@ -18,7 +21,7 @@ t.datetime "created_at", precision: nil, null: false t.datetime "updated_at", precision: nil, null: false t.string "type", default: "Account::Credentials", null: false - t.text "encrypted_data", limit: 16777215 + t.text "encrypted_data" t.integer "credential_id" t.text "content_type" t.string "encrypted_transfer_password" @@ -44,13 +47,13 @@ create_table "pg_search_documents", force: :cascade do |t| t.text "content" t.string "searchable_type" - t.integer "searchable_id" + t.bigint "searchable_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["searchable_type", "searchable_id"], name: "index_pg_search_documents_on_searchable" end - create_table "settings", force: :cascade do |t| + create_table "settings", id: :serial, force: :cascade do |t| t.string "key", null: false t.string "value" t.string "type", null: false @@ -85,7 +88,7 @@ t.datetime "updated_at", null: false end - create_table "users", force: :cascade do |t| + create_table "users", id: :serial, force: :cascade do |t| t.text "public_key", null: false t.binary "private_key", null: false t.binary "password" @@ -104,7 +107,7 @@ t.integer "human_user_id" t.text "options" t.integer "role", default: 0, null: false - t.integer "default_ccli_user_id" + t.bigint "default_ccli_user_id" t.index ["default_ccli_user_id"], name: "index_users_on_default_ccli_user_id" end diff --git a/docker-compose.yml b/docker-compose.yml index 413d9df11..3b42edd07 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,6 +1,15 @@ version: '3.4' services: + postgresql: + image: postgres:14.1-alpine + restart: always + environment: + - POSTGRES_USER=app + - POSTGRES_PASSWORD=postgres + ports: + - '5432:5432' + rails: image: cryptopus-dev/rails user: "${UID:-1000}" @@ -42,3 +51,5 @@ services: volumes: bundler_cache: yarn_cache: + postgresql: + driver: local From 09aa23c4a58724f0cf0ea96a6e12bb2aef5d11c2 Mon Sep 17 00:00:00 2001 From: megli2 Date: Wed, 5 Jul 2023 07:46:21 +0200 Subject: [PATCH 04/14] exclude postgres models from autoload when other database than postgres is used --- app/models/postgres/team.rb | 11 ++++++++--- config/application.rb | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/app/models/postgres/team.rb b/app/models/postgres/team.rb index e7b084402..f0dc4472e 100644 --- a/app/models/postgres/team.rb +++ b/app/models/postgres/team.rb @@ -1,6 +1,11 @@ # frozen_string_literal: true -class Team < Team - include PgSearch::Model - multisearchable against: [:name] + +class Postgres::Team + def initialize() + Team.class_eval do + include PgSearch::Model + multisearchable against: [:name] + end + end end diff --git a/config/application.rb b/config/application.rb index 896c97d36..109e5c6bc 100644 --- a/config/application.rb +++ b/config/application.rb @@ -99,7 +99,7 @@ class Application < Rails::Application config.paths['config/routes.rb'].concat Dir[Rails.root.join('config/routes/*.rb')].sort if Rails.configuration.database_configuration["development"]["database"].exclude?("postgres") - Dir.glob("#{Rails.root}/app/models/**[^postgres^]/*/").each {|dir| config.autoload_paths << dir } + Dir.glob("#{Rails.root}/app/models/**[^postgres.rb^]/*/").each {|dir| config.autoload_paths << dir } end end end From 869349000fa6fdca63f7ab40f25e12f3941bec1f Mon Sep 17 00:00:00 2001 From: megli2 Date: Wed, 5 Jul 2023 07:59:12 +0200 Subject: [PATCH 05/14] delete unnecessary inheritance attempt and code inclusion directly into classes --- app/models/folder.rb | 3 +++ app/models/postgres/encryptable.rb | 6 ------ app/models/postgres/folder.rb | 6 ------ app/models/postgres/team.rb | 11 ----------- app/models/team.rb | 3 +++ 5 files changed, 6 insertions(+), 23 deletions(-) delete mode 100644 app/models/postgres/encryptable.rb delete mode 100644 app/models/postgres/folder.rb delete mode 100644 app/models/postgres/team.rb diff --git a/app/models/folder.rb b/app/models/folder.rb index 6854f9c27..58ca1965c 100644 --- a/app/models/folder.rb +++ b/app/models/folder.rb @@ -25,6 +25,9 @@ class Folder < ApplicationRecord validates :description, length: { maximum: 300 } validates :personal_inbox, uniqueness: { scope: :team, if: :personal_inbox? } + include PgSearch::Model + multisearchable against: [:name] + def label name end diff --git a/app/models/postgres/encryptable.rb b/app/models/postgres/encryptable.rb deleted file mode 100644 index 50755e70d..000000000 --- a/app/models/postgres/encryptable.rb +++ /dev/null @@ -1,6 +0,0 @@ -# frozen_string_literal: true -class Encryptable < Encryptable - include PgSearch::Model - multisearchable against: [:name] -end - diff --git a/app/models/postgres/folder.rb b/app/models/postgres/folder.rb deleted file mode 100644 index 33c61c864..000000000 --- a/app/models/postgres/folder.rb +++ /dev/null @@ -1,6 +0,0 @@ -# frozen_string_literal: true - -class Folder < Folder - include PgSearch::Model - multisearchable against: [:name] -end \ No newline at end of file diff --git a/app/models/postgres/team.rb b/app/models/postgres/team.rb deleted file mode 100644 index f0dc4472e..000000000 --- a/app/models/postgres/team.rb +++ /dev/null @@ -1,11 +0,0 @@ -# frozen_string_literal: true - -class Postgres::Team - def initialize() - Team.class_eval do - include PgSearch::Model - multisearchable against: [:name] - end - end -end - diff --git a/app/models/team.rb b/app/models/team.rb index bf25c0ea7..9c2d813ac 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -34,6 +34,9 @@ class Team < ApplicationRecord in_progress: 2 }, _prefix: :recrypt + include PgSearch::Model + multisearchable against: [:name] + def label name end From 3153ec9fd97373e22e0f57a3589c3a4f0b5102c6 Mon Sep 17 00:00:00 2001 From: megli2 Date: Wed, 5 Jul 2023 09:18:30 +0200 Subject: [PATCH 06/14] delete unnecessary autoload configuration --- config/application.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/config/application.rb b/config/application.rb index 109e5c6bc..6f17947f0 100644 --- a/config/application.rb +++ b/config/application.rb @@ -97,9 +97,5 @@ class Application < Rails::Application config.time_zone = ENV['TIME_ZONE'] || 'Bern' config.paths['config/routes.rb'].concat Dir[Rails.root.join('config/routes/*.rb')].sort - - if Rails.configuration.database_configuration["development"]["database"].exclude?("postgres") - Dir.glob("#{Rails.root}/app/models/**[^postgres.rb^]/*/").each {|dir| config.autoload_paths << dir } - end end end From 66c946f7d518edac874ecb982e6bfbc483d5bfbe Mon Sep 17 00:00:00 2001 From: megli2 Date: Wed, 19 Jul 2023 11:51:35 +0200 Subject: [PATCH 07/14] add initializer to configure postgres to only search for prefixes instead of whole word --- config/initializers/postgres-options.rb | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 config/initializers/postgres-options.rb diff --git a/config/initializers/postgres-options.rb b/config/initializers/postgres-options.rb new file mode 100644 index 000000000..a2f8307d5 --- /dev/null +++ b/config/initializers/postgres-options.rb @@ -0,0 +1,2 @@ +# We have to discuss this, because we don't want to add it, if we don't use postgres +PgSearch.multisearch_options = { :using => { :tsearch => {:prefix => true} } } \ No newline at end of file From 62d84994c95270b8792703eeb4872ba93e7491ed Mon Sep 17 00:00:00 2001 From: megli2 Date: Wed, 19 Jul 2023 15:43:44 +0200 Subject: [PATCH 08/14] use new ruby syntax in postgres initializer --- config/initializers/postgres-options.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/initializers/postgres-options.rb b/config/initializers/postgres-options.rb index a2f8307d5..a3380490e 100644 --- a/config/initializers/postgres-options.rb +++ b/config/initializers/postgres-options.rb @@ -1,2 +1,2 @@ # We have to discuss this, because we don't want to add it, if we don't use postgres -PgSearch.multisearch_options = { :using => { :tsearch => {:prefix => true} } } \ No newline at end of file +PgSearch.multisearch_options = { using: { tsearch: {prefix: true} } } \ No newline at end of file From c0ad0a3f9ab15561bffe97820bf5ecb4e3d2c294 Mon Sep 17 00:00:00 2001 From: megli2 Date: Thu, 20 Jul 2023 08:05:55 +0200 Subject: [PATCH 09/14] add pgSearch in search function --- app/presenters/filtered_list.rb | 4 ++++ app/presenters/teams/filtered_list.rb | 17 +++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/app/presenters/filtered_list.rb b/app/presenters/filtered_list.rb index 92b3a2b62..9852cb7a3 100644 --- a/app/presenters/filtered_list.rb +++ b/app/presenters/filtered_list.rb @@ -15,4 +15,8 @@ def fetch_entries def list_param(key) @params[key].to_a.map(&:to_i) end + + def database_name + Rails.configuration.database_configuration[Rails.env]['database'] + end end diff --git a/app/presenters/teams/filtered_list.rb b/app/presenters/teams/filtered_list.rb index ce555904e..757e9144b 100644 --- a/app/presenters/teams/filtered_list.rb +++ b/app/presenters/teams/filtered_list.rb @@ -60,15 +60,20 @@ def limit end def filter_by_query(teams) - teams.where( - 'lower(encryptables.description) LIKE :query + if database_name.include? 'postgres' + binding.pry + PgSearch.multisearch(query) + else + teams.where( + 'lower(encryptables.description) LIKE :query OR lower(encryptables.name) LIKE :query OR lower(folders.name) LIKE :query OR lower(teams.name) LIKE :query', - query: "%#{query}%" - ) - .references(:folders, - folders: [:encryptables]) + query: "%#{query}%" + ) + .references(:folders, + folders: [:encryptables]) + end end def filter_by_id(filtered_teams) From 2a2fd0b166c1ddc17cf94a939d2620c7f02c4226 Mon Sep 17 00:00:00 2001 From: megli2 Date: Fri, 21 Jul 2023 11:16:16 +0200 Subject: [PATCH 10/14] implement new searchstrategy with postgres --- app/models/encryptable.rb | 6 +++++- app/models/folder.rb | 6 +++++- app/models/team.rb | 6 +++++- app/presenters/teams/filtered_list.rb | 19 +------------------ app/presenters/teams/pg_searching.rb | 23 +++++++++++++++++++++++ app/presenters/teams/search_strategy.rb | 13 +++++++++++++ app/presenters/teams/sql_search.rb | 17 +++++++++++++++++ 7 files changed, 69 insertions(+), 21 deletions(-) create mode 100644 app/presenters/teams/pg_searching.rb create mode 100644 app/presenters/teams/search_strategy.rb create mode 100644 app/presenters/teams/sql_search.rb diff --git a/app/models/encryptable.rb b/app/models/encryptable.rb index b1334e7d2..cc9f1d981 100644 --- a/app/models/encryptable.rb +++ b/app/models/encryptable.rb @@ -29,7 +29,11 @@ class Encryptable < ApplicationRecord validates :description, length: { maximum: 4000 } include PgSearch::Model - multisearchable against: [:name] + pg_search_scope :search_by_name, + against: [:name, :description], + using: { + tsearch: { prefix: true } + } def encrypt(team_password, encryption_algorithm = nil) used_encrypted_attrs.each do |a| diff --git a/app/models/folder.rb b/app/models/folder.rb index 58ca1965c..c2ef76091 100644 --- a/app/models/folder.rb +++ b/app/models/folder.rb @@ -26,7 +26,11 @@ class Folder < ApplicationRecord validates :personal_inbox, uniqueness: { scope: :team, if: :personal_inbox? } include PgSearch::Model - multisearchable against: [:name] + pg_search_scope :search_by_name, + against: [:name, :description], + using: { + tsearch: { prefix: true } + } def label name diff --git a/app/models/team.rb b/app/models/team.rb index 9c2d813ac..3a834802c 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -35,7 +35,11 @@ class Team < ApplicationRecord }, _prefix: :recrypt include PgSearch::Model - multisearchable against: [:name] + pg_search_scope :search_by_name, + against: [:name, :description], + using: { + tsearch: { prefix: true } + } def label name diff --git a/app/presenters/teams/filtered_list.rb b/app/presenters/teams/filtered_list.rb index 757e9144b..9dafbcb95 100644 --- a/app/presenters/teams/filtered_list.rb +++ b/app/presenters/teams/filtered_list.rb @@ -9,7 +9,7 @@ def fetch_entries else filtered_teams = teams - filtered_teams = filter_by_query(filtered_teams) if query_present? + filtered_teams = SearchStrategy.new.search(query, filtered_teams) if query_present? filtered_teams = filter_by_id(filtered_teams) if team_id.present? end @@ -59,23 +59,6 @@ def limit @params[:limit] end - def filter_by_query(teams) - if database_name.include? 'postgres' - binding.pry - PgSearch.multisearch(query) - else - teams.where( - 'lower(encryptables.description) LIKE :query - OR lower(encryptables.name) LIKE :query - OR lower(folders.name) LIKE :query - OR lower(teams.name) LIKE :query', - query: "%#{query}%" - ) - .references(:folders, - folders: [:encryptables]) - end - end - def filter_by_id(filtered_teams) filtered_teams.where(id: team_id) end diff --git a/app/presenters/teams/pg_searching.rb b/app/presenters/teams/pg_searching.rb new file mode 100644 index 000000000..b01616d2f --- /dev/null +++ b/app/presenters/teams/pg_searching.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module ::Teams + class PgSearching < SearchStrategy + def search(query, teams) + # Get allowed entities which user is allowed to read + allowed_folders = Folder.where(team_id: teams) + allowed_encryptables = Encryptable.where(folder_id: allowed_folders) + + # Get matching ids with pg_search + matching_team_ids = teams.search_by_name(query).pluck :id + matching_folder_ids = Folder.where({ id: allowed_folders }).search_by_name(query).pluck :id + matching_encryptable_ids = Encryptable.where({ id: allowed_encryptables }).search_by_name(query).pluck :id + + # Join allowed tables together and return matching ones + binding.pry + teams.where('encryptables.id IN (:encryptable_ids) OR teams.id IN (:team_ids) OR folders.id IN (:folder_ids)', + encryptable_ids: matching_encryptable_ids, team_ids: matching_team_ids, folder_ids: matching_folder_ids) + .references(:folders, + folders: [:encryptables]) + end + end +end \ No newline at end of file diff --git a/app/presenters/teams/search_strategy.rb b/app/presenters/teams/search_strategy.rb new file mode 100644 index 000000000..b03334f54 --- /dev/null +++ b/app/presenters/teams/search_strategy.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module ::Teams + class SearchStrategy + def search(query, teams) + if Rails.configuration.database_configuration[Rails.env]['database'].include?('postgres') + PgSearching.new.search(query, teams) + else + SqlSearch.new.search(query, teams) + end + end + end +end \ No newline at end of file diff --git a/app/presenters/teams/sql_search.rb b/app/presenters/teams/sql_search.rb new file mode 100644 index 000000000..6ffcdc8d2 --- /dev/null +++ b/app/presenters/teams/sql_search.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module ::Teams + class SqlSearch < SearchStrategy + def self.search(query, teams) + teams.where( + 'lower(encryptables.description) LIKE :query + OR lower(encryptables.name) LIKE :query + OR lower(folders.name) LIKE :query + OR lower(teams.name) LIKE :query', + query: "%#{query}%" + ) + .references(:folders, + folders: [:encryptables]) + end + end +end \ No newline at end of file From 263b23bf8bdcbcc163bd68ec580166916d207505 Mon Sep 17 00:00:00 2001 From: megli2 Date: Fri, 21 Jul 2023 12:37:32 +0200 Subject: [PATCH 11/14] finish pg_search by getting configuration option from adapter --- app/presenters/teams/pg_searching.rb | 1 - app/presenters/teams/search_strategy.rb | 6 +++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/presenters/teams/pg_searching.rb b/app/presenters/teams/pg_searching.rb index b01616d2f..e26beef14 100644 --- a/app/presenters/teams/pg_searching.rb +++ b/app/presenters/teams/pg_searching.rb @@ -13,7 +13,6 @@ def search(query, teams) matching_encryptable_ids = Encryptable.where({ id: allowed_encryptables }).search_by_name(query).pluck :id # Join allowed tables together and return matching ones - binding.pry teams.where('encryptables.id IN (:encryptable_ids) OR teams.id IN (:team_ids) OR folders.id IN (:folder_ids)', encryptable_ids: matching_encryptable_ids, team_ids: matching_team_ids, folder_ids: matching_folder_ids) .references(:folders, diff --git a/app/presenters/teams/search_strategy.rb b/app/presenters/teams/search_strategy.rb index b03334f54..d0cccaa39 100644 --- a/app/presenters/teams/search_strategy.rb +++ b/app/presenters/teams/search_strategy.rb @@ -3,11 +3,15 @@ module ::Teams class SearchStrategy def search(query, teams) - if Rails.configuration.database_configuration[Rails.env]['database'].include?('postgres') + if get_database_adapter.include?('postgres') PgSearching.new.search(query, teams) else SqlSearch.new.search(query, teams) end end + + def get_database_adapter + Rails.configuration.database_configuration[Rails.env]['adapter'] + end end end \ No newline at end of file From e5be0dcf66bf0d907a59fb0549453caa96504c34 Mon Sep 17 00:00:00 2001 From: megli2 Date: Fri, 21 Jul 2023 12:52:09 +0200 Subject: [PATCH 12/14] clean up branch and delete configuration for postgres which I inserted earlier --- app/presenters/filtered_list.rb | 4 ---- config/database.yml | 29 ++++++++--------------------- docker-compose.yml | 11 ----------- 3 files changed, 8 insertions(+), 36 deletions(-) diff --git a/app/presenters/filtered_list.rb b/app/presenters/filtered_list.rb index 9852cb7a3..92b3a2b62 100644 --- a/app/presenters/filtered_list.rb +++ b/app/presenters/filtered_list.rb @@ -15,8 +15,4 @@ def fetch_entries def list_param(key) @params[key].to_a.map(&:to_i) end - - def database_name - Rails.configuration.database_configuration[Rails.env]['database'] - end end diff --git a/config/database.yml b/config/database.yml index 70c886aba..72e6e684c 100644 --- a/config/database.yml +++ b/config/database.yml @@ -5,31 +5,20 @@ # PostgreSQL. Versions 8.2 and up are supported. # -#base: &generic -# adapter: <%#= ENV['RAILS_DB_ADAPTER'] || 'sqlite3' %> -# pool: 5 -# timeout: 5000 -# encoding: utf8 -<%# %w(host port username password).each do |option| %> -# <%# value = ENV["RAILS_DB_#{option.upcase}"] %> -# <%#= "#{option}: #{value}" if value.present? %> -<%# end %> - base: &generic - adapter: <%= ENV['RAILS_DB_ADAPTER'] || 'postgresql' %> + adapter: <%= ENV['RAILS_DB_ADAPTER'] || 'sqlite3' %> pool: 5 timeout: 5000 encoding: utf8 - host: postgresql - port: 5432 - username: app - password: postgres +<% %w(host port username password).each do |option| %> + <% value = ENV["RAILS_DB_#{option.upcase}"] %> + <%= "#{option}: #{value}" if value.present? %> +<% end %> development: <<: *generic - database: <%= ENV['RAILS_DB_NAME'] || "postgres" %> -# database: <%#= ENV['RAILS_DB_NAME'] || "#{Rails.root}/db/development.sqlite3" %> + database: <%= ENV['RAILS_DB_NAME'] || "#{Rails.root}/db/development.sqlite3" %> # Connect on a TCP socket. Omitted by default since the client uses a # domain socket that doesn't need configuration. Windows does not have @@ -51,10 +40,8 @@ development: # Do not set this db to the same as development or production. test: <<: *generic - database: <%= ENV['RAILS_DB_NAME'] || "postgres" %> -# <%#= ENV['RAILS_DB_NAME'] || "#{Rails.root}/db/test.sqlite3" %> + database: <%= ENV['RAILS_DB_NAME'] || "#{Rails.root}/db/test.sqlite3" %> production: <<: *generic - database: <%= ENV['RAILS_DB_NAME'] || "postgres" %> -# <%#= ENV['RAILS_DB_NAME'] || "#{Rails.root}/db/production.sqlite3" %> + database: <%= ENV['RAILS_DB_NAME'] || "#{Rails.root}/db/production.sqlite3" %> diff --git a/docker-compose.yml b/docker-compose.yml index 3b42edd07..413d9df11 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,15 +1,6 @@ version: '3.4' services: - postgresql: - image: postgres:14.1-alpine - restart: always - environment: - - POSTGRES_USER=app - - POSTGRES_PASSWORD=postgres - ports: - - '5432:5432' - rails: image: cryptopus-dev/rails user: "${UID:-1000}" @@ -51,5 +42,3 @@ services: volumes: bundler_cache: yarn_cache: - postgresql: - driver: local From 9759b4ff62451e3b5b7daca6ad5fd14a7fcf88f4 Mon Sep 17 00:00:00 2001 From: megli2 Date: Fri, 21 Jul 2023 13:02:31 +0200 Subject: [PATCH 13/14] clean up classes to match rubocop claims --- app/presenters/teams/pg_searching.rb | 11 +++++++---- app/presenters/teams/search_strategy.rb | 2 +- app/presenters/teams/sql_search.rb | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/app/presenters/teams/pg_searching.rb b/app/presenters/teams/pg_searching.rb index e26beef14..3ec197a65 100644 --- a/app/presenters/teams/pg_searching.rb +++ b/app/presenters/teams/pg_searching.rb @@ -10,13 +10,16 @@ def search(query, teams) # Get matching ids with pg_search matching_team_ids = teams.search_by_name(query).pluck :id matching_folder_ids = Folder.where({ id: allowed_folders }).search_by_name(query).pluck :id - matching_encryptable_ids = Encryptable.where({ id: allowed_encryptables }).search_by_name(query).pluck :id + matching_encryptable_ids = Encryptable.where({ id: allowed_encryptables }) + .search_by_name(query).pluck :id # Join allowed tables together and return matching ones teams.where('encryptables.id IN (:encryptable_ids) OR teams.id IN (:team_ids) OR folders.id IN (:folder_ids)', - encryptable_ids: matching_encryptable_ids, team_ids: matching_team_ids, folder_ids: matching_folder_ids) - .references(:folders, - folders: [:encryptables]) + encryptable_ids: matching_encryptable_ids, + team_ids: matching_team_ids, + folder_ids: matching_folder_ids) + .references(:folders, + folders: [:encryptables]) end end end \ No newline at end of file diff --git a/app/presenters/teams/search_strategy.rb b/app/presenters/teams/search_strategy.rb index d0cccaa39..bb4868974 100644 --- a/app/presenters/teams/search_strategy.rb +++ b/app/presenters/teams/search_strategy.rb @@ -14,4 +14,4 @@ def get_database_adapter Rails.configuration.database_configuration[Rails.env]['adapter'] end end -end \ No newline at end of file +end diff --git a/app/presenters/teams/sql_search.rb b/app/presenters/teams/sql_search.rb index 6ffcdc8d2..ac9d6ed5b 100644 --- a/app/presenters/teams/sql_search.rb +++ b/app/presenters/teams/sql_search.rb @@ -14,4 +14,4 @@ def self.search(query, teams) folders: [:encryptables]) end end -end \ No newline at end of file +end From 893dfd94706597795d23d3b4b70ec57229778bd4 Mon Sep 17 00:00:00 2001 From: megli2 Date: Fri, 21 Jul 2023 13:09:59 +0200 Subject: [PATCH 14/14] solve problem of method length by disabling rubocop for this method since trim it would only reduce the understandability --- app/presenters/teams/pg_searching.rb | 7 +++++-- app/presenters/teams/search_strategy.rb | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/app/presenters/teams/pg_searching.rb b/app/presenters/teams/pg_searching.rb index 3ec197a65..40111f955 100644 --- a/app/presenters/teams/pg_searching.rb +++ b/app/presenters/teams/pg_searching.rb @@ -2,6 +2,7 @@ module ::Teams class PgSearching < SearchStrategy + # rubocop:disable Metrics/MethodLength def search(query, teams) # Get allowed entities which user is allowed to read allowed_folders = Folder.where(team_id: teams) @@ -14,12 +15,14 @@ def search(query, teams) .search_by_name(query).pluck :id # Join allowed tables together and return matching ones - teams.where('encryptables.id IN (:encryptable_ids) OR teams.id IN (:team_ids) OR folders.id IN (:folder_ids)', + teams.where('encryptables.id IN (:encryptable_ids) OR teams.id IN (:team_ids) OR folders.id +IN (:folder_ids)', encryptable_ids: matching_encryptable_ids, team_ids: matching_team_ids, folder_ids: matching_folder_ids) .references(:folders, folders: [:encryptables]) end + # rubocop:enable Metrics/MethodLength end -end \ No newline at end of file +end diff --git a/app/presenters/teams/search_strategy.rb b/app/presenters/teams/search_strategy.rb index bb4868974..cf5f956fe 100644 --- a/app/presenters/teams/search_strategy.rb +++ b/app/presenters/teams/search_strategy.rb @@ -3,14 +3,14 @@ module ::Teams class SearchStrategy def search(query, teams) - if get_database_adapter.include?('postgres') + if database_adapter.include?('postgres') PgSearching.new.search(query, teams) else SqlSearch.new.search(query, teams) end end - def get_database_adapter + def database_adapter Rails.configuration.database_configuration[Rails.env]['adapter'] end end