From 11af7fcf8672f9bdc09e99a8c7e710a7f4892243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Couton?= Date: Fri, 17 Jun 2022 15:51:52 +0200 Subject: [PATCH 01/43] test migration to actionmodel7 + rails7 + couchbase-ruby-client --- couchbase-orm.gemspec | 3 +- lib/couchbase-orm/base.rb | 31 ++++++- lib/couchbase-orm/connection.rb | 12 ++- lib/couchbase-orm/persistence.rb | 13 ++- lib/couchbase-orm/proxies/bucket_proxy.rb | 4 +- lib/couchbase-orm/railtie.rb | 11 ++- rails7-ruby-client/.gitattributes | 7 ++ rails7-ruby-client/.gitignore | 35 +++++++ rails7-ruby-client/.ruby-version | 1 + rails7-ruby-client/Gemfile | 58 ++++++++++++ rails7-ruby-client/README.md | 24 +++++ rails7-ruby-client/Rakefile | 6 ++ .../app/assets/config/manifest.js | 2 + rails7-ruby-client/app/assets/images/.keep | 0 .../app/assets/stylesheets/application.css | 15 +++ .../app/channels/application_cable/channel.rb | 4 + .../channels/application_cable/connection.rb | 4 + .../app/controllers/application_controller.rb | 2 + .../app/controllers/concerns/.keep | 0 .../app/helpers/application_helper.rb | 2 + .../app/jobs/application_job.rb | 7 ++ .../app/mailers/application_mailer.rb | 4 + .../app/models/application_record.rb | 3 + rails7-ruby-client/app/models/concerns/.keep | 0 .../app/views/layouts/application.html.erb | 15 +++ .../app/views/layouts/mailer.html.erb | 13 +++ .../app/views/layouts/mailer.text.erb | 1 + rails7-ruby-client/config.ru | 6 ++ rails7-ruby-client/config/application.rb | 37 ++++++++ rails7-ruby-client/config/boot.rb | 4 + rails7-ruby-client/config/cable.yml | 10 ++ rails7-ruby-client/config/couchbase.yml | 12 +++ rails7-ruby-client/config/credentials.yml.enc | 1 + rails7-ruby-client/config/database.yml | 25 +++++ rails7-ruby-client/config/environment.rb | 5 + .../config/environments/development.rb | 70 ++++++++++++++ .../config/environments/production.rb | 93 +++++++++++++++++++ .../config/environments/test.rb | 60 ++++++++++++ .../config/initializers/assets.rb | 12 +++ .../initializers/content_security_policy.rb | 25 +++++ .../initializers/filter_parameter_logging.rb | 8 ++ .../config/initializers/inflections.rb | 16 ++++ .../config/initializers/permissions_policy.rb | 11 +++ rails7-ruby-client/config/locales/en.yml | 33 +++++++ rails7-ruby-client/config/puma.rb | 43 +++++++++ rails7-ruby-client/config/routes.rb | 6 ++ rails7-ruby-client/config/storage.yml | 34 +++++++ rails7-ruby-client/db/seeds.rb | 7 ++ rails7-ruby-client/lib/assets/.keep | 0 rails7-ruby-client/lib/tasks/.keep | 0 rails7-ruby-client/log/.keep | 0 rails7-ruby-client/public/404.html | 67 +++++++++++++ rails7-ruby-client/public/422.html | 67 +++++++++++++ rails7-ruby-client/public/500.html | 66 +++++++++++++ .../public/apple-touch-icon-precomposed.png | 0 .../public/apple-touch-icon.png | 0 rails7-ruby-client/public/favicon.ico | 0 rails7-ruby-client/public/robots.txt | 1 + rails7-ruby-client/storage/.keep | 0 rails7-ruby-client/test/models/first_test.rb | 30 ++++++ 60 files changed, 1009 insertions(+), 17 deletions(-) create mode 100644 rails7-ruby-client/.gitattributes create mode 100644 rails7-ruby-client/.gitignore create mode 100644 rails7-ruby-client/.ruby-version create mode 100644 rails7-ruby-client/Gemfile create mode 100644 rails7-ruby-client/README.md create mode 100644 rails7-ruby-client/Rakefile create mode 100644 rails7-ruby-client/app/assets/config/manifest.js create mode 100644 rails7-ruby-client/app/assets/images/.keep create mode 100644 rails7-ruby-client/app/assets/stylesheets/application.css create mode 100644 rails7-ruby-client/app/channels/application_cable/channel.rb create mode 100644 rails7-ruby-client/app/channels/application_cable/connection.rb create mode 100644 rails7-ruby-client/app/controllers/application_controller.rb create mode 100644 rails7-ruby-client/app/controllers/concerns/.keep create mode 100644 rails7-ruby-client/app/helpers/application_helper.rb create mode 100644 rails7-ruby-client/app/jobs/application_job.rb create mode 100644 rails7-ruby-client/app/mailers/application_mailer.rb create mode 100644 rails7-ruby-client/app/models/application_record.rb create mode 100644 rails7-ruby-client/app/models/concerns/.keep create mode 100644 rails7-ruby-client/app/views/layouts/application.html.erb create mode 100644 rails7-ruby-client/app/views/layouts/mailer.html.erb create mode 100644 rails7-ruby-client/app/views/layouts/mailer.text.erb create mode 100644 rails7-ruby-client/config.ru create mode 100644 rails7-ruby-client/config/application.rb create mode 100644 rails7-ruby-client/config/boot.rb create mode 100644 rails7-ruby-client/config/cable.yml create mode 100644 rails7-ruby-client/config/couchbase.yml create mode 100644 rails7-ruby-client/config/credentials.yml.enc create mode 100644 rails7-ruby-client/config/database.yml create mode 100644 rails7-ruby-client/config/environment.rb create mode 100644 rails7-ruby-client/config/environments/development.rb create mode 100644 rails7-ruby-client/config/environments/production.rb create mode 100644 rails7-ruby-client/config/environments/test.rb create mode 100644 rails7-ruby-client/config/initializers/assets.rb create mode 100644 rails7-ruby-client/config/initializers/content_security_policy.rb create mode 100644 rails7-ruby-client/config/initializers/filter_parameter_logging.rb create mode 100644 rails7-ruby-client/config/initializers/inflections.rb create mode 100644 rails7-ruby-client/config/initializers/permissions_policy.rb create mode 100644 rails7-ruby-client/config/locales/en.yml create mode 100644 rails7-ruby-client/config/puma.rb create mode 100644 rails7-ruby-client/config/routes.rb create mode 100644 rails7-ruby-client/config/storage.yml create mode 100644 rails7-ruby-client/db/seeds.rb create mode 100644 rails7-ruby-client/lib/assets/.keep create mode 100644 rails7-ruby-client/lib/tasks/.keep create mode 100644 rails7-ruby-client/log/.keep create mode 100644 rails7-ruby-client/public/404.html create mode 100644 rails7-ruby-client/public/422.html create mode 100644 rails7-ruby-client/public/500.html create mode 100644 rails7-ruby-client/public/apple-touch-icon-precomposed.png create mode 100644 rails7-ruby-client/public/apple-touch-icon.png create mode 100644 rails7-ruby-client/public/favicon.ico create mode 100644 rails7-ruby-client/public/robots.txt create mode 100644 rails7-ruby-client/storage/.keep create mode 100644 rails7-ruby-client/test/models/first_test.rb diff --git a/couchbase-orm.gemspec b/couchbase-orm.gemspec index 325e3d68..67cdd2c8 100644 --- a/couchbase-orm.gemspec +++ b/couchbase-orm.gemspec @@ -14,7 +14,8 @@ Gem::Specification.new do |gem| gem.require_paths = ["lib"] gem.add_runtime_dependency 'mt-libcouchbase', '~> 1.2' - gem.add_runtime_dependency 'activemodel', '>= 4.0', '< 6.0' + gem.add_runtime_dependency 'activemodel', '>= 7.0' + gem.add_runtime_dependency 'couchbase' gem.add_runtime_dependency 'radix', '~> 2.2' # converting numbers to and from any base gem.add_development_dependency 'rake', '~> 12.2' diff --git a/lib/couchbase-orm/base.rb b/lib/couchbase-orm/base.rb index 4eebbdef..b3703b3c 100644 --- a/lib/couchbase-orm/base.rb +++ b/lib/couchbase-orm/base.rb @@ -100,12 +100,15 @@ def find(*ids, **options) raise MTLibcouchbase::Error::EmptyKey, 'no id(s) provided' end - CouchbaseOrm.logger.debug "Data - Get #{ids}" - record = bucket.get(*ids, **options) + puts "Data - Get #{ids}" + + record = bucket.default_collection.get(*ids) + puts "After Data - Get #{record}" records = record.is_a?(Array) ? record : [record] records.map! { |record| if record - self.new(record) + puts "content.delete(:type)" + self.new(record, id: ids[0]) else false end @@ -130,6 +133,7 @@ def exists?(id) # Add support for libcouchbase response objects def initialize(model = nil, ignore_doc_type: false, **attributes) + puts "attributes : #{attributes}" @__metadata__ = Metadata.new # Assign default values @@ -143,9 +147,28 @@ def initialize(model = nil, ignore_doc_type: false, **attributes) end end + puts "model : #{model}" if model case model + when Couchbase::Collection::GetResult + puts "Couchbase::Collection::GetResult" + doc = model.content || raise('empty response provided') + type = doc.delete('type') + doc.delete(:id) + + if type && !ignore_doc_type && type.to_s != self.class.design_document + raise "document type mismatch, #{type} != #{self.class.design_document}" + end + + @__metadata__.key = attributes[:id] + @__metadata__.cas = model.cas + + puts "@__metadata__ : #{@__metadata__}" + + # This ensures that defaults are applied + @__attributes__.merge! doc when ::MTLibcouchbase::Response + puts "MTLibcouchbase::Response" doc = model.value || raise('empty response provided') type = doc.delete(:type) doc.delete(:id) @@ -161,9 +184,11 @@ def initialize(model = nil, ignore_doc_type: false, **attributes) @__attributes__.merge! doc clear_changes_information when CouchbaseOrm::Base + puts "CouchbaseOrm::Base" clear_changes_information attributes = model.attributes attributes.delete(:id) + attributes.delete('type') super(attributes) else clear_changes_information diff --git a/lib/couchbase-orm/connection.rb b/lib/couchbase-orm/connection.rb index 33235649..f1b0f0da 100644 --- a/lib/couchbase-orm/connection.rb +++ b/lib/couchbase-orm/connection.rb @@ -1,6 +1,6 @@ -# frozen_string_literal: true, encoding: ASCII-8BIT require 'mt-libcouchbase' +require 'couchbase' module CouchbaseOrm class Connection @@ -10,7 +10,13 @@ class << self end def self.bucket - @bucket ||= ::MTLibcouchbase::Bucket.new(**@options) + if @bucket.nil? + options = Couchbase::Cluster::ClusterOptions.new + options.authenticate("cb_admin", "cb_admin_pwd") + + cluster = Couchbase::Cluster.connect('couchbase://127.0.0.1', options) + @bucket = cluster.bucket("billeo-db-bucket") + end end end -end +end \ No newline at end of file diff --git a/lib/couchbase-orm/persistence.rb b/lib/couchbase-orm/persistence.rb index 187a0285..fe5f8665 100644 --- a/lib/couchbase-orm/persistence.rb +++ b/lib/couchbase-orm/persistence.rb @@ -236,10 +236,10 @@ def _update_record(with_cas: false, **options) _id = @__metadata__.key options[:cas] = @__metadata__.cas if with_cas CouchbaseOrm.logger.debug { "Data - Replace #{_id} #{@__attributes__.to_s.truncate(200)}" } - resp = self.class.bucket.replace(_id, @__attributes__, **options) + resp = self.class.bucket.default_collection.upsert(_id, @__attributes__, **options) # Ensure the model is up to date - @__metadata__.key = resp.key + @__metadata__.key = _id @__metadata__.cas = resp.cas changes_applied @@ -247,7 +247,6 @@ def _update_record(with_cas: false, **options) end end end - def _create_record(**options) return false unless perform_validations(:create, options) @@ -259,10 +258,14 @@ def _create_record(**options) _id = @id || self.class.uuid_generator.next(self) CouchbaseOrm.logger.debug { "Data - Insert #{_id} #{@__attributes__.to_s.truncate(200)}" } - resp = self.class.bucket.add(_id, @__attributes__, **options) + #resp = self.class.bucket.add(_id, @__attributes__, **options) + + puts "ici" + puts @__attributes__ + resp = self.class.bucket.default_collection.upsert(_id, @__attributes__, **options) # Ensure the model is up to date - @__metadata__.key = resp.key + @__metadata__.key = _id @__metadata__.cas = resp.cas changes_applied diff --git a/lib/couchbase-orm/proxies/bucket_proxy.rb b/lib/couchbase-orm/proxies/bucket_proxy.rb index d96620bc..07be88d8 100644 --- a/lib/couchbase-orm/proxies/bucket_proxy.rb +++ b/lib/couchbase-orm/proxies/bucket_proxy.rb @@ -27,8 +27,8 @@ def initialize(proxyfied) proxyfied.public_methods.each do |method| next if self.public_methods.include?(method) - self.class.define_method(method) do |*params, &block| - @proxyfied.send(method, *params, &block) + self.class.define_method(method) do |*params, **options, &block| + @proxyfied.send(method, *params, **options, &block) end end end diff --git a/lib/couchbase-orm/railtie.rb b/lib/couchbase-orm/railtie.rb index 7f5861a4..561e4b34 100644 --- a/lib/couchbase-orm/railtie.rb +++ b/lib/couchbase-orm/railtie.rb @@ -24,8 +24,10 @@ module Rails #:nodoc: module Couchbase #:nodoc: class Railtie < Rails::Railtie #:nodoc: - config.couchbase = ActiveSupport::OrderedOptions.new - config.couchbase.ensure_design_documents = true + puts "Railtie < Rails::Railtie" + config.couchbase2 = ActiveSupport::OrderedOptions.new + config.couchbase2.ensure_design_documents = true + puts "config.couchbase.ensure_design_documents : #{config.couchbase2.ensure_design_documents}" # Maping of rescued exceptions to HTTP responses # @@ -51,9 +53,12 @@ def self.rescue_responses # Initialize Couchbase Mode. This will look for a couchbase.yml in the # config directory and configure Couchbase connection appropriately. initializer 'couchbase.setup_connection' do + puts "ici!!!" + puts Rails.env config_file = Rails.root.join('config', 'couchbase.yml') if config_file.file? && config = YAML.load(ERB.new(File.read(config_file)).result)[Rails.env] + puts config ::CouchbaseOrm::Connection.options = config.deep_symbolize_keys end end @@ -83,7 +88,7 @@ def self.rescue_responses # Check (and upgrade if needed) all design documents config.after_initialize do |app| - if config.couchbase.ensure_design_documents + if config.couchbase2.ensure_design_documents begin ::CouchbaseOrm::Base.descendants.each do |model| model.ensure_design_document! diff --git a/rails7-ruby-client/.gitattributes b/rails7-ruby-client/.gitattributes new file mode 100644 index 00000000..31eeee0b --- /dev/null +++ b/rails7-ruby-client/.gitattributes @@ -0,0 +1,7 @@ +# See https://git-scm.com/docs/gitattributes for more about git attribute files. + +# Mark the database schema as having been generated. +db/schema.rb linguist-generated + +# Mark any vendored files as having been vendored. +vendor/* linguist-vendored diff --git a/rails7-ruby-client/.gitignore b/rails7-ruby-client/.gitignore new file mode 100644 index 00000000..886f714b --- /dev/null +++ b/rails7-ruby-client/.gitignore @@ -0,0 +1,35 @@ +# See https://help.github.com/articles/ignoring-files for more about ignoring files. +# +# If you find yourself ignoring temporary files generated by your text editor +# or operating system, you probably want to add a global ignore instead: +# git config --global core.excludesfile '~/.gitignore_global' + +# Ignore bundler config. +/.bundle + +# Ignore the default SQLite database. +/db/*.sqlite3 +/db/*.sqlite3-* + +# Ignore all logfiles and tempfiles. +/log/* +/tmp/* +!/log/.keep +!/tmp/.keep + +# Ignore pidfiles, but keep the directory. +/tmp/pids/* +!/tmp/pids/ +!/tmp/pids/.keep + +# Ignore uploaded files in development. +/storage/* +!/storage/.keep +/tmp/storage/* +!/tmp/storage/ +!/tmp/storage/.keep + +/public/assets + +# Ignore master key for decrypting credentials and more. +/config/master.key diff --git a/rails7-ruby-client/.ruby-version b/rails7-ruby-client/.ruby-version new file mode 100644 index 00000000..ef538c28 --- /dev/null +++ b/rails7-ruby-client/.ruby-version @@ -0,0 +1 @@ +3.1.2 diff --git a/rails7-ruby-client/Gemfile b/rails7-ruby-client/Gemfile new file mode 100644 index 00000000..1dedb9b0 --- /dev/null +++ b/rails7-ruby-client/Gemfile @@ -0,0 +1,58 @@ +source "https://rubygems.org" +git_source(:github) { |repo| "https://github.com/#{repo}.git" } + +ruby "3.1.2" + +# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" +gem "rails", "~> 7.0.3" + +# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails] +gem "sprockets-rails" + +# Use sqlite3 as the database for Active Record +gem "sqlite3", "~> 1.4" + +gem "couchbase-orm", path: ".." + +# Use the Puma web server [https://github.com/puma/puma] +gem "puma", "~> 5.0" + +# Build JSON APIs with ease [https://github.com/rails/jbuilder] +gem "jbuilder" + +# Use Redis adapter to run Action Cable in production +# gem "redis", "~> 4.0" + +# Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis] +# gem "kredis" + +# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword] +# gem "bcrypt", "~> 3.1.7" + +# Windows does not include zoneinfo files, so bundle the tzinfo-data gem +gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ] + +# Reduces boot times through caching; required in config/boot.rb +gem "bootsnap", require: false + +# Use Sass to process CSS +# gem "sassc-rails" + +# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images] +# gem "image_processing", "~> 1.2" + +group :development, :test do + # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem + gem "debug", platforms: %i[ mri mingw x64_mingw ] +end + +group :development do + # Use console on exceptions pages [https://github.com/rails/web-console] + gem "web-console" + + # Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler] + # gem "rack-mini-profiler" + + # Speed up commands on slow machines / big apps [https://github.com/rails/spring] + # gem "spring" +end diff --git a/rails7-ruby-client/README.md b/rails7-ruby-client/README.md new file mode 100644 index 00000000..7db80e4c --- /dev/null +++ b/rails7-ruby-client/README.md @@ -0,0 +1,24 @@ +# README + +This README would normally document whatever steps are necessary to get the +application up and running. + +Things you may want to cover: + +* Ruby version + +* System dependencies + +* Configuration + +* Database creation + +* Database initialization + +* How to run the test suite + +* Services (job queues, cache servers, search engines, etc.) + +* Deployment instructions + +* ... diff --git a/rails7-ruby-client/Rakefile b/rails7-ruby-client/Rakefile new file mode 100644 index 00000000..9a5ea738 --- /dev/null +++ b/rails7-ruby-client/Rakefile @@ -0,0 +1,6 @@ +# Add your own tasks in files placed in lib/tasks ending in .rake, +# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. + +require_relative "config/application" + +Rails.application.load_tasks diff --git a/rails7-ruby-client/app/assets/config/manifest.js b/rails7-ruby-client/app/assets/config/manifest.js new file mode 100644 index 00000000..59181933 --- /dev/null +++ b/rails7-ruby-client/app/assets/config/manifest.js @@ -0,0 +1,2 @@ +//= link_tree ../images +//= link_directory ../stylesheets .css diff --git a/rails7-ruby-client/app/assets/images/.keep b/rails7-ruby-client/app/assets/images/.keep new file mode 100644 index 00000000..e69de29b diff --git a/rails7-ruby-client/app/assets/stylesheets/application.css b/rails7-ruby-client/app/assets/stylesheets/application.css new file mode 100644 index 00000000..288b9ab7 --- /dev/null +++ b/rails7-ruby-client/app/assets/stylesheets/application.css @@ -0,0 +1,15 @@ +/* + * This is a manifest file that'll be compiled into application.css, which will include all the files + * listed below. + * + * Any CSS (and SCSS, if configured) file within this directory, lib/assets/stylesheets, or any plugin's + * vendor/assets/stylesheets directory can be referenced here using a relative path. + * + * You're free to add application-wide styles to this file and they'll appear at the bottom of the + * compiled file so the styles you add here take precedence over styles defined in any other CSS + * files in this directory. Styles in this file should be added after the last require_* statement. + * It is generally better to create a new file per style scope. + * + *= require_tree . + *= require_self + */ diff --git a/rails7-ruby-client/app/channels/application_cable/channel.rb b/rails7-ruby-client/app/channels/application_cable/channel.rb new file mode 100644 index 00000000..d6726972 --- /dev/null +++ b/rails7-ruby-client/app/channels/application_cable/channel.rb @@ -0,0 +1,4 @@ +module ApplicationCable + class Channel < ActionCable::Channel::Base + end +end diff --git a/rails7-ruby-client/app/channels/application_cable/connection.rb b/rails7-ruby-client/app/channels/application_cable/connection.rb new file mode 100644 index 00000000..0ff5442f --- /dev/null +++ b/rails7-ruby-client/app/channels/application_cable/connection.rb @@ -0,0 +1,4 @@ +module ApplicationCable + class Connection < ActionCable::Connection::Base + end +end diff --git a/rails7-ruby-client/app/controllers/application_controller.rb b/rails7-ruby-client/app/controllers/application_controller.rb new file mode 100644 index 00000000..09705d12 --- /dev/null +++ b/rails7-ruby-client/app/controllers/application_controller.rb @@ -0,0 +1,2 @@ +class ApplicationController < ActionController::Base +end diff --git a/rails7-ruby-client/app/controllers/concerns/.keep b/rails7-ruby-client/app/controllers/concerns/.keep new file mode 100644 index 00000000..e69de29b diff --git a/rails7-ruby-client/app/helpers/application_helper.rb b/rails7-ruby-client/app/helpers/application_helper.rb new file mode 100644 index 00000000..de6be794 --- /dev/null +++ b/rails7-ruby-client/app/helpers/application_helper.rb @@ -0,0 +1,2 @@ +module ApplicationHelper +end diff --git a/rails7-ruby-client/app/jobs/application_job.rb b/rails7-ruby-client/app/jobs/application_job.rb new file mode 100644 index 00000000..d394c3d1 --- /dev/null +++ b/rails7-ruby-client/app/jobs/application_job.rb @@ -0,0 +1,7 @@ +class ApplicationJob < ActiveJob::Base + # Automatically retry jobs that encountered a deadlock + # retry_on ActiveRecord::Deadlocked + + # Most jobs are safe to ignore if the underlying records are no longer available + # discard_on ActiveJob::DeserializationError +end diff --git a/rails7-ruby-client/app/mailers/application_mailer.rb b/rails7-ruby-client/app/mailers/application_mailer.rb new file mode 100644 index 00000000..3c34c814 --- /dev/null +++ b/rails7-ruby-client/app/mailers/application_mailer.rb @@ -0,0 +1,4 @@ +class ApplicationMailer < ActionMailer::Base + default from: "from@example.com" + layout "mailer" +end diff --git a/rails7-ruby-client/app/models/application_record.rb b/rails7-ruby-client/app/models/application_record.rb new file mode 100644 index 00000000..b63caeb8 --- /dev/null +++ b/rails7-ruby-client/app/models/application_record.rb @@ -0,0 +1,3 @@ +class ApplicationRecord < ActiveRecord::Base + primary_abstract_class +end diff --git a/rails7-ruby-client/app/models/concerns/.keep b/rails7-ruby-client/app/models/concerns/.keep new file mode 100644 index 00000000..e69de29b diff --git a/rails7-ruby-client/app/views/layouts/application.html.erb b/rails7-ruby-client/app/views/layouts/application.html.erb new file mode 100644 index 00000000..73782d98 --- /dev/null +++ b/rails7-ruby-client/app/views/layouts/application.html.erb @@ -0,0 +1,15 @@ + + + + CbormRails7b + + <%= csrf_meta_tags %> + <%= csp_meta_tag %> + + <%= stylesheet_link_tag "application" %> + + + + <%= yield %> + + diff --git a/rails7-ruby-client/app/views/layouts/mailer.html.erb b/rails7-ruby-client/app/views/layouts/mailer.html.erb new file mode 100644 index 00000000..cbd34d2e --- /dev/null +++ b/rails7-ruby-client/app/views/layouts/mailer.html.erb @@ -0,0 +1,13 @@ + + + + + + + + + <%= yield %> + + diff --git a/rails7-ruby-client/app/views/layouts/mailer.text.erb b/rails7-ruby-client/app/views/layouts/mailer.text.erb new file mode 100644 index 00000000..37f0bddb --- /dev/null +++ b/rails7-ruby-client/app/views/layouts/mailer.text.erb @@ -0,0 +1 @@ +<%= yield %> diff --git a/rails7-ruby-client/config.ru b/rails7-ruby-client/config.ru new file mode 100644 index 00000000..4a3c09a6 --- /dev/null +++ b/rails7-ruby-client/config.ru @@ -0,0 +1,6 @@ +# This file is used by Rack-based servers to start the application. + +require_relative "config/environment" + +run Rails.application +Rails.application.load_server diff --git a/rails7-ruby-client/config/application.rb b/rails7-ruby-client/config/application.rb new file mode 100644 index 00000000..a6559f5a --- /dev/null +++ b/rails7-ruby-client/config/application.rb @@ -0,0 +1,37 @@ +require_relative "boot" + +require "rails" +# Pick the frameworks you want: +require "active_model/railtie" +require "active_job/railtie" +require "active_record/railtie" +require "active_storage/engine" +require "action_controller/railtie" +require "action_mailer/railtie" +require "action_mailbox/engine" +require "action_text/engine" +require "action_view/railtie" +require "action_cable/engine" +# require "rails/test_unit/railtie" + +# Require the gems listed in Gemfile, including any gems +# you've limited to :test, :development, or :production. +Bundler.require(*Rails.groups) + +module CbormRails7b + class Application < Rails::Application + # Initialize configuration defaults for originally generated Rails version. + config.load_defaults 7.0 + + # Configuration for the application, engines, and railties goes here. + # + # These settings can be overridden in specific environments using the files + # in config/environments, which are processed later. + # + # config.time_zone = "Central Time (US & Canada)" + # config.eager_load_paths << Rails.root.join("extras") + + # Don't generate system test files. + config.generators.system_tests = nil + end +end diff --git a/rails7-ruby-client/config/boot.rb b/rails7-ruby-client/config/boot.rb new file mode 100644 index 00000000..988a5ddc --- /dev/null +++ b/rails7-ruby-client/config/boot.rb @@ -0,0 +1,4 @@ +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) + +require "bundler/setup" # Set up gems listed in the Gemfile. +require "bootsnap/setup" # Speed up boot time by caching expensive operations. diff --git a/rails7-ruby-client/config/cable.yml b/rails7-ruby-client/config/cable.yml new file mode 100644 index 00000000..f042ce82 --- /dev/null +++ b/rails7-ruby-client/config/cable.yml @@ -0,0 +1,10 @@ +development: + adapter: async + +test: + adapter: test + +production: + adapter: redis + url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %> + channel_prefix: cborm_rails7b_production diff --git a/rails7-ruby-client/config/couchbase.yml b/rails7-ruby-client/config/couchbase.yml new file mode 100644 index 00000000..4552cf77 --- /dev/null +++ b/rails7-ruby-client/config/couchbase.yml @@ -0,0 +1,12 @@ + +development: + hosts: localhost + username: cb_admin + password: cb_admin_pwd + bucket: billeo-db-bucket + +test: + hosts: localhost + username: cb_admin + password: cb_admin_pwd + bucket: billeo-db-bucket diff --git a/rails7-ruby-client/config/credentials.yml.enc b/rails7-ruby-client/config/credentials.yml.enc new file mode 100644 index 00000000..2d0ab2aa --- /dev/null +++ b/rails7-ruby-client/config/credentials.yml.enc @@ -0,0 +1 @@ +t11OzhiHElgqsYHwVWT9t4ihqrO6047ZX2GyMzRBcCKUvP2jsGRhyjAVTyAtjKUwLx+32gPFqZPRNLtoB2CWzZxOWrohYjPC+Imu5mOEKEz8Q/AkGZA3Bn73r1VQLLwOoFDoz3pJKHAgCKJKD3LpeB8vWF+fIgYUdovEwiccdrVyth0yaCAVQGtrg7AqCvnfcRxCi3Fk/mMWFmYLHuYFIBiUOrDAozdV6lKaZfhZ+MxwvCn/0/rUzJB6TtozSZMA+RfZOk2TuSYPX4hD3vPtURZf/javb9Rvu4GHwKVSbTcFM6SUDyirvbPDdycPnl6pOzRHM650cCViwecqfJg/8tGUdP1GLWFnHpO1fM0z1+EdyFuMgguAuBr+WFQIRa2uuzL9obMGP8GJKrPZfolxrF2ZI8FezTfPzl+Y--KyMGe+OGMFrzyrnZ--nDGbvqi5U98FfQ9FGwT3/Q== \ No newline at end of file diff --git a/rails7-ruby-client/config/database.yml b/rails7-ruby-client/config/database.yml new file mode 100644 index 00000000..fcba57f1 --- /dev/null +++ b/rails7-ruby-client/config/database.yml @@ -0,0 +1,25 @@ +# SQLite. Versions 3.8.0 and up are supported. +# gem install sqlite3 +# +# Ensure the SQLite 3 gem is defined in your Gemfile +# gem "sqlite3" +# +default: &default + adapter: sqlite3 + pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> + timeout: 5000 + +development: + <<: *default + database: db/development.sqlite3 + +# 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: + <<: *default + database: db/test.sqlite3 + +production: + <<: *default + database: db/production.sqlite3 diff --git a/rails7-ruby-client/config/environment.rb b/rails7-ruby-client/config/environment.rb new file mode 100644 index 00000000..cac53157 --- /dev/null +++ b/rails7-ruby-client/config/environment.rb @@ -0,0 +1,5 @@ +# Load the Rails application. +require_relative "application" + +# Initialize the Rails application. +Rails.application.initialize! diff --git a/rails7-ruby-client/config/environments/development.rb b/rails7-ruby-client/config/environments/development.rb new file mode 100644 index 00000000..8500f459 --- /dev/null +++ b/rails7-ruby-client/config/environments/development.rb @@ -0,0 +1,70 @@ +require "active_support/core_ext/integer/time" + +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # In the development environment your application's code is reloaded any time + # it changes. This slows down response time but is perfect for development + # since you don't have to restart the web server when you make code changes. + config.cache_classes = false + + # Do not eager load code on boot. + config.eager_load = false + + # Show full error reports. + config.consider_all_requests_local = true + + # Enable server timing + config.server_timing = true + + # Enable/disable caching. By default caching is disabled. + # Run rails dev:cache to toggle caching. + if Rails.root.join("tmp/caching-dev.txt").exist? + config.action_controller.perform_caching = true + config.action_controller.enable_fragment_cache_logging = true + + config.cache_store = :memory_store + config.public_file_server.headers = { + "Cache-Control" => "public, max-age=#{2.days.to_i}" + } + else + config.action_controller.perform_caching = false + + config.cache_store = :null_store + end + + # Store uploaded files on the local file system (see config/storage.yml for options). + config.active_storage.service = :local + + # Don't care if the mailer can't send. + config.action_mailer.raise_delivery_errors = false + + config.action_mailer.perform_caching = false + + # Print deprecation notices to the Rails logger. + config.active_support.deprecation = :log + + # Raise exceptions for disallowed deprecations. + config.active_support.disallowed_deprecation = :raise + + # Tell Active Support which deprecation messages to disallow. + config.active_support.disallowed_deprecation_warnings = [] + + # Raise an error on page load if there are pending migrations. + config.active_record.migration_error = :page_load + + # Highlight code that triggered database queries in logs. + config.active_record.verbose_query_logs = true + + # Suppress logger output for asset requests. + config.assets.quiet = true + + # Raises error for missing translations. + # config.i18n.raise_on_missing_translations = true + + # Annotate rendered view with file names. + # config.action_view.annotate_rendered_view_with_filenames = true + + # Uncomment if you wish to allow Action Cable access from any origin. + # config.action_cable.disable_request_forgery_protection = true +end diff --git a/rails7-ruby-client/config/environments/production.rb b/rails7-ruby-client/config/environments/production.rb new file mode 100644 index 00000000..b12048b1 --- /dev/null +++ b/rails7-ruby-client/config/environments/production.rb @@ -0,0 +1,93 @@ +require "active_support/core_ext/integer/time" + +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # Code is not reloaded between requests. + config.cache_classes = true + + # Eager load code on boot. This eager loads most of Rails and + # your application in memory, allowing both threaded web servers + # and those relying on copy on write to perform better. + # Rake tasks automatically ignore this option for performance. + config.eager_load = true + + # Full error reports are disabled and caching is turned on. + config.consider_all_requests_local = false + config.action_controller.perform_caching = true + + # Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"] + # or in config/master.key. This key is used to decrypt credentials (and other encrypted files). + # config.require_master_key = true + + # Disable serving static files from the `/public` folder by default since + # Apache or NGINX already handles this. + config.public_file_server.enabled = ENV["RAILS_SERVE_STATIC_FILES"].present? + + # Compress CSS using a preprocessor. + # config.assets.css_compressor = :sass + + # Do not fallback to assets pipeline if a precompiled asset is missed. + config.assets.compile = false + + # Enable serving of images, stylesheets, and JavaScripts from an asset server. + # config.asset_host = "http://assets.example.com" + + # Specifies the header that your server uses for sending files. + # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for Apache + # config.action_dispatch.x_sendfile_header = "X-Accel-Redirect" # for NGINX + + # Store uploaded files on the local file system (see config/storage.yml for options). + config.active_storage.service = :local + + # Mount Action Cable outside main process or domain. + # config.action_cable.mount_path = nil + # config.action_cable.url = "wss://example.com/cable" + # config.action_cable.allowed_request_origins = [ "http://example.com", /http:\/\/example.*/ ] + + # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. + # config.force_ssl = true + + # Include generic and useful information about system operation, but avoid logging too much + # information to avoid inadvertent exposure of personally identifiable information (PII). + config.log_level = :info + + # Prepend all log lines with the following tags. + config.log_tags = [ :request_id ] + + # Use a different cache store in production. + # config.cache_store = :mem_cache_store + + # Use a real queuing backend for Active Job (and separate queues per environment). + # config.active_job.queue_adapter = :resque + # config.active_job.queue_name_prefix = "cborm_rails7b_production" + + config.action_mailer.perform_caching = false + + # Ignore bad email addresses and do not raise email delivery errors. + # Set this to true and configure the email server for immediate delivery to raise delivery errors. + # config.action_mailer.raise_delivery_errors = false + + # Enable locale fallbacks for I18n (makes lookups for any locale fall back to + # the I18n.default_locale when a translation cannot be found). + config.i18n.fallbacks = true + + # Don't log any deprecations. + config.active_support.report_deprecations = false + + # Use default logging formatter so that PID and timestamp are not suppressed. + config.log_formatter = ::Logger::Formatter.new + + # Use a different logger for distributed setups. + # require "syslog/logger" + # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new "app-name") + + if ENV["RAILS_LOG_TO_STDOUT"].present? + logger = ActiveSupport::Logger.new(STDOUT) + logger.formatter = config.log_formatter + config.logger = ActiveSupport::TaggedLogging.new(logger) + end + + # Do not dump schema after migrations. + config.active_record.dump_schema_after_migration = false +end diff --git a/rails7-ruby-client/config/environments/test.rb b/rails7-ruby-client/config/environments/test.rb new file mode 100644 index 00000000..6ea4d1e7 --- /dev/null +++ b/rails7-ruby-client/config/environments/test.rb @@ -0,0 +1,60 @@ +require "active_support/core_ext/integer/time" + +# The test environment is used exclusively to run your application's +# test suite. You never need to work with it otherwise. Remember that +# your test database is "scratch space" for the test suite and is wiped +# and recreated between test runs. Don't rely on the data there! + +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # Turn false under Spring and add config.action_view.cache_template_loading = true. + config.cache_classes = true + + # Eager loading loads your whole application. When running a single test locally, + # this probably isn't necessary. It's a good idea to do in a continuous integration + # system, or in some way before deploying your code. + config.eager_load = ENV["CI"].present? + + # Configure public file server for tests with Cache-Control for performance. + config.public_file_server.enabled = true + config.public_file_server.headers = { + "Cache-Control" => "public, max-age=#{1.hour.to_i}" + } + + # Show full error reports and disable caching. + config.consider_all_requests_local = true + config.action_controller.perform_caching = false + config.cache_store = :null_store + + # Raise exceptions instead of rendering exception templates. + config.action_dispatch.show_exceptions = false + + # Disable request forgery protection in test environment. + config.action_controller.allow_forgery_protection = false + + # Store uploaded files on the local file system in a temporary directory. + config.active_storage.service = :test + + config.action_mailer.perform_caching = false + + # Tell Action Mailer not to deliver emails to the real world. + # The :test delivery method accumulates sent emails in the + # ActionMailer::Base.deliveries array. + config.action_mailer.delivery_method = :test + + # Print deprecation notices to the stderr. + config.active_support.deprecation = :stderr + + # Raise exceptions for disallowed deprecations. + config.active_support.disallowed_deprecation = :raise + + # Tell Active Support which deprecation messages to disallow. + config.active_support.disallowed_deprecation_warnings = [] + + # Raises error for missing translations. + # config.i18n.raise_on_missing_translations = true + + # Annotate rendered view with file names. + # config.action_view.annotate_rendered_view_with_filenames = true +end diff --git a/rails7-ruby-client/config/initializers/assets.rb b/rails7-ruby-client/config/initializers/assets.rb new file mode 100644 index 00000000..2eeef966 --- /dev/null +++ b/rails7-ruby-client/config/initializers/assets.rb @@ -0,0 +1,12 @@ +# Be sure to restart your server when you modify this file. + +# Version of your assets, change this if you want to expire all your assets. +Rails.application.config.assets.version = "1.0" + +# Add additional assets to the asset load path. +# Rails.application.config.assets.paths << Emoji.images_path + +# Precompile additional assets. +# application.js, application.css, and all non-JS/CSS in the app/assets +# folder are already added. +# Rails.application.config.assets.precompile += %w( admin.js admin.css ) diff --git a/rails7-ruby-client/config/initializers/content_security_policy.rb b/rails7-ruby-client/config/initializers/content_security_policy.rb new file mode 100644 index 00000000..54f47cf1 --- /dev/null +++ b/rails7-ruby-client/config/initializers/content_security_policy.rb @@ -0,0 +1,25 @@ +# Be sure to restart your server when you modify this file. + +# Define an application-wide content security policy. +# See the Securing Rails Applications Guide for more information: +# https://guides.rubyonrails.org/security.html#content-security-policy-header + +# Rails.application.configure do +# config.content_security_policy do |policy| +# policy.default_src :self, :https +# policy.font_src :self, :https, :data +# policy.img_src :self, :https, :data +# policy.object_src :none +# policy.script_src :self, :https +# policy.style_src :self, :https +# # Specify URI for violation reports +# # policy.report_uri "/csp-violation-report-endpoint" +# end +# +# # Generate session nonces for permitted importmap and inline scripts +# config.content_security_policy_nonce_generator = ->(request) { request.session.id.to_s } +# config.content_security_policy_nonce_directives = %w(script-src) +# +# # Report violations without enforcing the policy. +# # config.content_security_policy_report_only = true +# end diff --git a/rails7-ruby-client/config/initializers/filter_parameter_logging.rb b/rails7-ruby-client/config/initializers/filter_parameter_logging.rb new file mode 100644 index 00000000..adc6568c --- /dev/null +++ b/rails7-ruby-client/config/initializers/filter_parameter_logging.rb @@ -0,0 +1,8 @@ +# Be sure to restart your server when you modify this file. + +# Configure parameters to be filtered from the log file. Use this to limit dissemination of +# sensitive information. See the ActiveSupport::ParameterFilter documentation for supported +# notations and behaviors. +Rails.application.config.filter_parameters += [ + :passw, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn +] diff --git a/rails7-ruby-client/config/initializers/inflections.rb b/rails7-ruby-client/config/initializers/inflections.rb new file mode 100644 index 00000000..3860f659 --- /dev/null +++ b/rails7-ruby-client/config/initializers/inflections.rb @@ -0,0 +1,16 @@ +# Be sure to restart your server when you modify this file. + +# Add new inflection rules using the following format. Inflections +# are locale specific, and you may define rules for as many different +# locales as you wish. All of these examples are active by default: +# ActiveSupport::Inflector.inflections(:en) do |inflect| +# inflect.plural /^(ox)$/i, "\\1en" +# inflect.singular /^(ox)en/i, "\\1" +# inflect.irregular "person", "people" +# inflect.uncountable %w( fish sheep ) +# end + +# These inflection rules are supported but not enabled by default: +# ActiveSupport::Inflector.inflections(:en) do |inflect| +# inflect.acronym "RESTful" +# end diff --git a/rails7-ruby-client/config/initializers/permissions_policy.rb b/rails7-ruby-client/config/initializers/permissions_policy.rb new file mode 100644 index 00000000..00f64d71 --- /dev/null +++ b/rails7-ruby-client/config/initializers/permissions_policy.rb @@ -0,0 +1,11 @@ +# Define an application-wide HTTP permissions policy. For further +# information see https://developers.google.com/web/updates/2018/06/feature-policy +# +# Rails.application.config.permissions_policy do |f| +# f.camera :none +# f.gyroscope :none +# f.microphone :none +# f.usb :none +# f.fullscreen :self +# f.payment :self, "https://secure.example.com" +# end diff --git a/rails7-ruby-client/config/locales/en.yml b/rails7-ruby-client/config/locales/en.yml new file mode 100644 index 00000000..8ca56fc7 --- /dev/null +++ b/rails7-ruby-client/config/locales/en.yml @@ -0,0 +1,33 @@ +# Files in the config/locales directory are used for internationalization +# and are automatically loaded by Rails. If you want to use locales other +# than English, add the necessary files in this directory. +# +# To use the locales, use `I18n.t`: +# +# I18n.t "hello" +# +# In views, this is aliased to just `t`: +# +# <%= t("hello") %> +# +# To use a different locale, set it with `I18n.locale`: +# +# I18n.locale = :es +# +# This would use the information in config/locales/es.yml. +# +# The following keys must be escaped otherwise they will not be retrieved by +# the default I18n backend: +# +# true, false, on, off, yes, no +# +# Instead, surround them with single quotes. +# +# en: +# "true": "foo" +# +# To learn more, please read the Rails Internationalization guide +# available at https://guides.rubyonrails.org/i18n.html. + +en: + hello: "Hello world" diff --git a/rails7-ruby-client/config/puma.rb b/rails7-ruby-client/config/puma.rb new file mode 100644 index 00000000..daaf0369 --- /dev/null +++ b/rails7-ruby-client/config/puma.rb @@ -0,0 +1,43 @@ +# Puma can serve each request in a thread from an internal thread pool. +# The `threads` method setting takes two numbers: a minimum and maximum. +# Any libraries that use thread pools should be configured to match +# the maximum value specified for Puma. Default is set to 5 threads for minimum +# and maximum; this matches the default thread size of Active Record. +# +max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 } +min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count } +threads min_threads_count, max_threads_count + +# Specifies the `worker_timeout` threshold that Puma will use to wait before +# terminating a worker in development environments. +# +worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development" + +# Specifies the `port` that Puma will listen on to receive requests; default is 3000. +# +port ENV.fetch("PORT") { 3000 } + +# Specifies the `environment` that Puma will run in. +# +environment ENV.fetch("RAILS_ENV") { "development" } + +# Specifies the `pidfile` that Puma will use. +pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" } + +# Specifies the number of `workers` to boot in clustered mode. +# Workers are forked web server processes. If using threads and workers together +# the concurrency of the application would be max `threads` * `workers`. +# Workers do not work on JRuby or Windows (both of which do not support +# processes). +# +# workers ENV.fetch("WEB_CONCURRENCY") { 2 } + +# Use the `preload_app!` method when specifying a `workers` number. +# This directive tells Puma to first boot the application and load code +# before forking the application. This takes advantage of Copy On Write +# process behavior so workers use less memory. +# +# preload_app! + +# Allow puma to be restarted by `bin/rails restart` command. +plugin :tmp_restart diff --git a/rails7-ruby-client/config/routes.rb b/rails7-ruby-client/config/routes.rb new file mode 100644 index 00000000..262ffd54 --- /dev/null +++ b/rails7-ruby-client/config/routes.rb @@ -0,0 +1,6 @@ +Rails.application.routes.draw do + # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html + + # Defines the root path route ("/") + # root "articles#index" +end diff --git a/rails7-ruby-client/config/storage.yml b/rails7-ruby-client/config/storage.yml new file mode 100644 index 00000000..4942ab66 --- /dev/null +++ b/rails7-ruby-client/config/storage.yml @@ -0,0 +1,34 @@ +test: + service: Disk + root: <%= Rails.root.join("tmp/storage") %> + +local: + service: Disk + root: <%= Rails.root.join("storage") %> + +# Use bin/rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key) +# amazon: +# service: S3 +# access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %> +# secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %> +# region: us-east-1 +# bucket: your_own_bucket-<%= Rails.env %> + +# Remember not to checkin your GCS keyfile to a repository +# google: +# service: GCS +# project: your_project +# credentials: <%= Rails.root.join("path/to/gcs.keyfile") %> +# bucket: your_own_bucket-<%= Rails.env %> + +# Use bin/rails credentials:edit to set the Azure Storage secret (as azure_storage:storage_access_key) +# microsoft: +# service: AzureStorage +# storage_account_name: your_account_name +# storage_access_key: <%= Rails.application.credentials.dig(:azure_storage, :storage_access_key) %> +# container: your_container_name-<%= Rails.env %> + +# mirror: +# service: Mirror +# primary: local +# mirrors: [ amazon, google, microsoft ] diff --git a/rails7-ruby-client/db/seeds.rb b/rails7-ruby-client/db/seeds.rb new file mode 100644 index 00000000..bc25fce3 --- /dev/null +++ b/rails7-ruby-client/db/seeds.rb @@ -0,0 +1,7 @@ +# This file should contain all the record creation needed to seed the database with its default values. +# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup). +# +# Examples: +# +# movies = Movie.create([{ name: "Star Wars" }, { name: "Lord of the Rings" }]) +# Character.create(name: "Luke", movie: movies.first) diff --git a/rails7-ruby-client/lib/assets/.keep b/rails7-ruby-client/lib/assets/.keep new file mode 100644 index 00000000..e69de29b diff --git a/rails7-ruby-client/lib/tasks/.keep b/rails7-ruby-client/lib/tasks/.keep new file mode 100644 index 00000000..e69de29b diff --git a/rails7-ruby-client/log/.keep b/rails7-ruby-client/log/.keep new file mode 100644 index 00000000..e69de29b diff --git a/rails7-ruby-client/public/404.html b/rails7-ruby-client/public/404.html new file mode 100644 index 00000000..2be3af26 --- /dev/null +++ b/rails7-ruby-client/public/404.html @@ -0,0 +1,67 @@ + + + + The page you were looking for doesn't exist (404) + + + + + + +
+
+

The page you were looking for doesn't exist.

+

You may have mistyped the address or the page may have moved.

+
+

If you are the application owner check the logs for more information.

+
+ + diff --git a/rails7-ruby-client/public/422.html b/rails7-ruby-client/public/422.html new file mode 100644 index 00000000..c08eac0d --- /dev/null +++ b/rails7-ruby-client/public/422.html @@ -0,0 +1,67 @@ + + + + The change you wanted was rejected (422) + + + + + + +
+
+

The change you wanted was rejected.

+

Maybe you tried to change something you didn't have access to.

+
+

If you are the application owner check the logs for more information.

+
+ + diff --git a/rails7-ruby-client/public/500.html b/rails7-ruby-client/public/500.html new file mode 100644 index 00000000..78a030af --- /dev/null +++ b/rails7-ruby-client/public/500.html @@ -0,0 +1,66 @@ + + + + We're sorry, but something went wrong (500) + + + + + + +
+
+

We're sorry, but something went wrong.

+
+

If you are the application owner check the logs for more information.

+
+ + diff --git a/rails7-ruby-client/public/apple-touch-icon-precomposed.png b/rails7-ruby-client/public/apple-touch-icon-precomposed.png new file mode 100644 index 00000000..e69de29b diff --git a/rails7-ruby-client/public/apple-touch-icon.png b/rails7-ruby-client/public/apple-touch-icon.png new file mode 100644 index 00000000..e69de29b diff --git a/rails7-ruby-client/public/favicon.ico b/rails7-ruby-client/public/favicon.ico new file mode 100644 index 00000000..e69de29b diff --git a/rails7-ruby-client/public/robots.txt b/rails7-ruby-client/public/robots.txt new file mode 100644 index 00000000..c19f78ab --- /dev/null +++ b/rails7-ruby-client/public/robots.txt @@ -0,0 +1 @@ +# See https://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file diff --git a/rails7-ruby-client/storage/.keep b/rails7-ruby-client/storage/.keep new file mode 100644 index 00000000..e69de29b diff --git a/rails7-ruby-client/test/models/first_test.rb b/rails7-ruby-client/test/models/first_test.rb new file mode 100644 index 00000000..27820ba7 --- /dev/null +++ b/rails7-ruby-client/test/models/first_test.rb @@ -0,0 +1,30 @@ +ENV['RAILS_ENV'] ||= 'test' +require File.expand_path('../../../config/environment', __FILE__) +require 'rails/test_help' +require 'couchbase-orm' + +CouchbaseOrm.logger = Logger.new(STDOUT) + +class Post < CouchbaseOrm::Base + n1ql :all # => emits :id and will return all comments + attribute :title, type: String + attribute :body, type: String + attribute :draft, type: Boolean +end + +class FirstTest < ActiveSupport::TestCase + test "the truth" do + id = "newid_#{DateTime.now.strftime('%s')}" + p = Post.new(id: id, + title: 'Hello world', + draft: true) + p.save + retrieved_p = Post.find(id) + assert_equal retrieved_p.title, p.title + retrieved_p.body = 'Once upon the times....' + retrieved_p.save + + retrieved_p2 = Post.find(id) + assert_equal retrieved_p2.body, retrieved_p.body + end +end \ No newline at end of file From 5758961760e3cb182573197560e80b843b36db57 Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Wed, 22 Jun 2022 15:12:13 +0200 Subject: [PATCH 02/43] Fix base_spec.rb --- lib/couchbase-orm.rb | 51 +++++++++++++---------- lib/couchbase-orm/base.rb | 12 ++++-- lib/couchbase-orm/connection.rb | 6 +-- lib/couchbase-orm/error.rb | 1 + lib/couchbase-orm/persistence.rb | 20 +++++---- lib/couchbase-orm/proxies/bucket_proxy.rb | 19 ++++----- spec/base_spec.rb | 8 ++-- 7 files changed, 66 insertions(+), 51 deletions(-) diff --git a/lib/couchbase-orm.rb b/lib/couchbase-orm.rb index 71a1d1af..c2991844 100644 --- a/lib/couchbase-orm.rb +++ b/lib/couchbase-orm.rb @@ -10,37 +10,46 @@ module CouchbaseOrm autoload :Base, 'couchbase-orm/base' autoload :HasMany, 'couchbase-orm/utilities/has_many' + def self.logger + @@logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDOUT) + end + + def self.logger=(logger) + @@logger = logger + end + def self.try_load(id) result = nil was_array = id.is_a?(Array) if was_array && id.length == 1 - id = id.first - end - result = id.respond_to?(:cas) ? id : CouchbaseOrm::Base.bucket.get(id, quiet: true, extended: true) - if was_array - result = Array.wrap(result) - end - if result && result.is_a?(Array) - return result.map { |r| self.try_load(r) }.compact + query_id = id.first + else + query_id = id end - if result && result.value.is_a?(Hash) && result.value[:type] - ddoc = result.value[:type] - ::CouchbaseOrm::Base.descendants.each do |model| - if model.design_document == ddoc - return model.new(result) - end - end + result = query_id.is_a?(Array) ? CouchbaseOrm::Base.bucket.default_collection.get_multi(query_id) : CouchbaseOrm::Base.bucket.default_collection.get(query_id) + + result = Array.wrap(result) if was_array + + if result&.is_a?(Array) + return result.zip(id).map { |r, id| try_load_create_model(r, id) }.compact end - nil - end - def self.logger - @@logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDOUT) + return try_load_create_model(result, id) end - def self.logger=(logger) - @@logger = logger + private + + def self.try_load_create_model(result, id) + puts "try_load_create_model : #{result.class} #{result.inspect} #{id.inspect}" + ddoc = result&.content["type"] + return nil unless ddoc + ::CouchbaseOrm::Base.descendants.each do |model| + if model.design_document == ddoc + return model.new(result, id: id) + end + end + nil end end diff --git a/lib/couchbase-orm/base.rb b/lib/couchbase-orm/base.rb index b3703b3c..c60fa734 100644 --- a/lib/couchbase-orm/base.rb +++ b/lib/couchbase-orm/base.rb @@ -3,6 +3,7 @@ require 'active_model' require 'active_support/hash_with_indifferent_access' +require 'couchbase' require 'couchbase-orm/error' require 'couchbase-orm/views' require 'couchbase-orm/n1ql' @@ -55,6 +56,10 @@ def bucket @bucket ||= BucketProxy.new(Connection.bucket) end + def collection + bucket.default_collection + end + def uuid_generator @uuid_generator ||= IdGenerator end @@ -102,7 +107,7 @@ def find(*ids, **options) puts "Data - Get #{ids}" - record = bucket.default_collection.get(*ids) + record = collection.get(*ids) puts "After Data - Get #{record}" records = record.is_a?(Array) ? record : [record] records.map! { |record| @@ -130,10 +135,10 @@ def exists?(id) alias_method :has_key?, :exists? end + class MismatchTypeError < RuntimeError; end # Add support for libcouchbase response objects def initialize(model = nil, ignore_doc_type: false, **attributes) - puts "attributes : #{attributes}" @__metadata__ = Metadata.new # Assign default values @@ -147,7 +152,6 @@ def initialize(model = nil, ignore_doc_type: false, **attributes) end end - puts "model : #{model}" if model case model when Couchbase::Collection::GetResult @@ -157,7 +161,7 @@ def initialize(model = nil, ignore_doc_type: false, **attributes) doc.delete(:id) if type && !ignore_doc_type && type.to_s != self.class.design_document - raise "document type mismatch, #{type} != #{self.class.design_document}" + raise CouchbaseOrm::Error::TypeMismatchError.new("document type mismatch, #{type} != #{self.class.design_document}", self) end @__metadata__.key = attributes[:id] diff --git a/lib/couchbase-orm/connection.rb b/lib/couchbase-orm/connection.rb index f1b0f0da..59230e18 100644 --- a/lib/couchbase-orm/connection.rb +++ b/lib/couchbase-orm/connection.rb @@ -10,13 +10,13 @@ class << self end def self.bucket - if @bucket.nil? + @bucket ||= begin options = Couchbase::Cluster::ClusterOptions.new options.authenticate("cb_admin", "cb_admin_pwd") cluster = Couchbase::Cluster.connect('couchbase://127.0.0.1', options) - @bucket = cluster.bucket("billeo-db-bucket") + cluster.bucket("billeo-db-bucket") end end end -end \ No newline at end of file +end diff --git a/lib/couchbase-orm/error.rb b/lib/couchbase-orm/error.rb index 8add65c3..1e63587c 100644 --- a/lib/couchbase-orm/error.rb +++ b/lib/couchbase-orm/error.rb @@ -22,6 +22,7 @@ def initialize(message = nil, record = nil) super(message, record) end end + class TypeMismatchError < Error; end class RecordExists < Error; end end end diff --git a/lib/couchbase-orm/persistence.rb b/lib/couchbase-orm/persistence.rb index 7acf2876..7d77455a 100644 --- a/lib/couchbase-orm/persistence.rb +++ b/lib/couchbase-orm/persistence.rb @@ -100,7 +100,7 @@ def save!(**options) def delete(with_cas: false, **options) options[:cas] = @__metadata__.cas if with_cas CouchbaseOrm.logger.debug "Data - Delete #{@__metadata__.key}" - self.class.bucket.delete(@__metadata__.key, **options) + self.class.collection.remove(@__metadata__.key, **options) @__metadata__.key = nil @id = nil @@ -110,6 +110,8 @@ def delete(with_cas: false, **options) self end + alias :remove :delete + # Deletes the record in the database and freezes this instance to reflect # that no changes should be made (since they can't be persisted). # @@ -123,7 +125,7 @@ def destroy(with_cas: false, **options) options[:cas] = @__metadata__.cas if with_cas CouchbaseOrm.logger.debug "Data - Delete #{@__metadata__.key}" - self.class.bucket.delete(@__metadata__.key, **options) + self.class.collection.remove(@__metadata__.key, **options) @__metadata__.key = nil @id = nil @@ -172,7 +174,7 @@ def update_columns(with_cas: false, **hash) # There is a limit of 16 subdoc operations per request resp = if hash.length <= 16 - subdoc = self.class.bucket.subdoc(_id) + subdoc = self.class.collection.subdoc(_id) hash.each do |key, value| subdoc.dict_upsert(key, value) end @@ -182,7 +184,7 @@ def update_columns(with_cas: false, **hash) @__attributes__[:type] = self.class.design_document @__attributes__.delete(:id) CouchbaseOrm.logger.debug { "Data - Replace #{_id} #{@__attributes__.to_s.truncate(200)}" } - self.class.bucket.replace(_id, @__attributes__, **options) + self.class.collection.replace(_id, @__attributes__, **options) end # Ensure the model is up to date @@ -201,7 +203,7 @@ def reload raise "unable to reload, model not persisted" unless key CouchbaseOrm.logger.debug "Data - Get #{key}" - resp = self.class.bucket.get(key, quiet: false, extended: true) + resp = self.class.collection.get(key, quiet: false, extended: true) @__attributes__ = ::ActiveSupport::HashWithIndifferentAccess.new(resp.value) @__metadata__.key = resp.key @__metadata__.cas = resp.cas @@ -214,7 +216,7 @@ def reload # Updates the TTL of the document def touch(**options) CouchbaseOrm.logger.debug "Data - Touch #{@__metadata__.key}" - res = self.class.bucket.touch(@__metadata__.key, async: false, **options) + res = self.class.collection.touch(@__metadata__.key, async: false, **options) @__metadata__.cas = resp.cas self end @@ -236,7 +238,7 @@ def _update_record(with_cas: false, **options) _id = @__metadata__.key options[:cas] = @__metadata__.cas if with_cas CouchbaseOrm.logger.debug { "Data - Replace #{_id} #{@__attributes__.to_s.truncate(200)}" } - resp = self.class.bucket.default_collection.upsert(_id, @__attributes__, **options) + resp = self.class.collection.upsert(_id, @__attributes__, **options) # Ensure the model is up to date @__metadata__.key = _id @@ -258,11 +260,11 @@ def _create_record(**options) _id = @id || self.class.uuid_generator.next(self) CouchbaseOrm.logger.debug { "Data - Insert #{_id} #{@__attributes__.to_s.truncate(200)}" } - #resp = self.class.bucket.add(_id, @__attributes__, **options) + #resp = self.class.collection.add(_id, @__attributes__, **options) puts "ici" puts @__attributes__ - resp = self.class.bucket.default_collection.upsert(_id, @__attributes__, **options) + resp = self.class.collection.upsert(_id, @__attributes__, **options) # Ensure the model is up to date @__metadata__.key = _id diff --git a/lib/couchbase-orm/proxies/bucket_proxy.rb b/lib/couchbase-orm/proxies/bucket_proxy.rb index 810a5c80..89ede22b 100644 --- a/lib/couchbase-orm/proxies/bucket_proxy.rb +++ b/lib/couchbase-orm/proxies/bucket_proxy.rb @@ -5,6 +5,8 @@ module CouchbaseOrm class BucketProxy def initialize(proxyfied) + raise "Must proxy a non nil object" if proxyfied.nil? + @proxyfied = proxyfied self.class.define_method(:name) do @@ -24,16 +26,13 @@ def initialize(proxyfied) @results = ResultsProxy.new(@proxyfied.send(:view, design, view, **opts, &block)) end - proxyfied.public_methods.each do |method| - next if self.public_methods.include?(method) - if RUBY_VERSION.to_i >= 3 - self.class.define_method(method) do |*params, **options, &block| - @proxyfied.send(method, *params, **options, &block) - end - else - self.class.define_method(method) do |*params, &block| - @proxyfied.send(method, *params, &block) - end + if RUBY_VERSION.to_i >= 3 + def method_missing(name, *args, **options, &block) + @proxyfied.public_send(name, *args, **options, &block) + end + else + def method_missing(name, *args, &block) + @proxyfied.public_send(name, *args, &block) end end end diff --git a/spec/base_spec.rb b/spec/base_spec.rb index c061a45b..0a898c39 100644 --- a/spec/base_spec.rb +++ b/spec/base_spec.rb @@ -35,11 +35,11 @@ class CompareTest < CouchbaseOrm::Base it "should load database responses" do base = BaseTest.create!(name: 'joe') - resp = BaseTest.bucket.get(base.id, extended: true) + resp = BaseTest.bucket.default_collection.get(base.id) - expect(resp.key).to eq(base.id) + base_loaded = BaseTest.new(resp, id: base.id) - base_loaded = BaseTest.new(resp) + expect(base_loaded.id).to eq(base.id) expect(base_loaded).to eq(base) expect(base_loaded).not_to be(base) @@ -49,7 +49,7 @@ class CompareTest < CouchbaseOrm::Base it "should not load objects if there is a type mismatch" do base = BaseTest.create!(name: 'joe') - expect { CompareTest.find_by_id(base.id) }.to raise_error(RuntimeError) + expect { CompareTest.find_by_id(base.id) }.to raise_error(CouchbaseOrm::Error::TypeMismatchError) base.destroy end From 62674142289afd24182a1bde5feb7f99c5b79d42 Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Wed, 22 Jun 2022 15:21:44 +0200 Subject: [PATCH 03/43] remove puts --- lib/couchbase-orm.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/couchbase-orm.rb b/lib/couchbase-orm.rb index c2991844..e047eceb 100644 --- a/lib/couchbase-orm.rb +++ b/lib/couchbase-orm.rb @@ -41,7 +41,6 @@ def self.try_load(id) private def self.try_load_create_model(result, id) - puts "try_load_create_model : #{result.class} #{result.inspect} #{id.inspect}" ddoc = result&.content["type"] return nil unless ddoc ::CouchbaseOrm::Base.descendants.each do |model| From 7c745da5a04e65664e9cf9185f2c5827ac604feb Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Wed, 22 Jun 2022 15:38:53 +0200 Subject: [PATCH 04/43] Fix pesistennce_spec --- lib/couchbase-orm/persistence.rb | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/couchbase-orm/persistence.rb b/lib/couchbase-orm/persistence.rb index 7d77455a..f2a10d64 100644 --- a/lib/couchbase-orm/persistence.rb +++ b/lib/couchbase-orm/persistence.rb @@ -162,7 +162,10 @@ def update!(hash) end alias_method :update_attributes!, :update! - # Updates the record without validating or running callbacks + # Updates the record without validating or running callbacks. + # Updates only the attributes that are passed in as parameters + # except if there is more than 16 attributes, in which case + # the whole record is saved. def update_columns(with_cas: false, **hash) _id = @__metadata__.key raise "unable to update columns, model not persisted" unless _id @@ -174,11 +177,10 @@ def update_columns(with_cas: false, **hash) # There is a limit of 16 subdoc operations per request resp = if hash.length <= 16 - subdoc = self.class.collection.subdoc(_id) - hash.each do |key, value| - subdoc.dict_upsert(key, value) - end - subdoc.execute!(**options) + self.class.collection.mutate_in( + _id, + hash.map { |k, v| Couchbase::MutateInSpec.replace(k.to_s, v) } + ) else # Fallback to writing the whole document @__attributes__[:type] = self.class.design_document @@ -188,7 +190,6 @@ def update_columns(with_cas: false, **hash) end # Ensure the model is up to date - @__metadata__.key = resp.key @__metadata__.cas = resp.cas changes_applied @@ -203,9 +204,9 @@ def reload raise "unable to reload, model not persisted" unless key CouchbaseOrm.logger.debug "Data - Get #{key}" - resp = self.class.collection.get(key, quiet: false, extended: true) - @__attributes__ = ::ActiveSupport::HashWithIndifferentAccess.new(resp.value) - @__metadata__.key = resp.key + resp = self.class.collection.get(key) + @__attributes__ = ::ActiveSupport::HashWithIndifferentAccess.new(resp.content) + @__metadata__.key = key @__metadata__.cas = resp.cas reset_associations From a724cd5b5393665ba6ae26a9ed660b04686bf364 Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Thu, 23 Jun 2022 09:50:27 +0200 Subject: [PATCH 05/43] Fix n1ql_spec.rb --- couchbase-orm.gemspec | 1 - lib/couchbase-orm/base.rb | 5 ++++ lib/couchbase-orm/connection.rb | 11 +++++--- lib/couchbase-orm/n1ql.rb | 31 ++++++++++------------- lib/couchbase-orm/persistence.rb | 2 -- lib/couchbase-orm/proxies/bucket_proxy.rb | 4 --- lib/couchbase-orm/proxies/n1ql_proxy.rb | 5 +++- lib/couchbase-orm/railtie.rb | 2 -- spec/support.rb | 11 +++----- 9 files changed, 34 insertions(+), 38 deletions(-) diff --git a/couchbase-orm.gemspec b/couchbase-orm.gemspec index 5f21c3d9..76508b60 100644 --- a/couchbase-orm.gemspec +++ b/couchbase-orm.gemspec @@ -13,7 +13,6 @@ Gem::Specification.new do |gem| gem.required_ruby_version = '>= 2.1.0' gem.require_paths = ["lib"] - gem.add_runtime_dependency 'mt-libcouchbase', '~> 1.2' gem.add_runtime_dependency 'activemodel', ENV["ACTIVE_MODEL_VERSION"] || '>= 5.0' gem.add_runtime_dependency 'couchbase' gem.add_runtime_dependency 'radix', '~> 2.2' # converting numbers to and from any base diff --git a/lib/couchbase-orm/base.rb b/lib/couchbase-orm/base.rb index c60fa734..302626ec 100644 --- a/lib/couchbase-orm/base.rb +++ b/lib/couchbase-orm/base.rb @@ -56,6 +56,10 @@ def bucket @bucket ||= BucketProxy.new(Connection.bucket) end + def cluster + Connection.cluster + end + def collection bucket.default_collection end @@ -113,6 +117,7 @@ def find(*ids, **options) records.map! { |record| if record puts "content.delete(:type)" + raise "NOT YET IMPLEMENTED" if ids.length > 1 self.new(record, id: ids[0]) else false diff --git a/lib/couchbase-orm/connection.rb b/lib/couchbase-orm/connection.rb index 59230e18..daf3435d 100644 --- a/lib/couchbase-orm/connection.rb +++ b/lib/couchbase-orm/connection.rb @@ -9,14 +9,17 @@ class << self attr_accessor :options end - def self.bucket - @bucket ||= begin + def self.cluster + @cluster ||= begin options = Couchbase::Cluster::ClusterOptions.new options.authenticate("cb_admin", "cb_admin_pwd") - cluster = Couchbase::Cluster.connect('couchbase://127.0.0.1', options) - cluster.bucket("billeo-db-bucket") + cluster = Couchbase::Cluster.connect('couchbase://127.0.0.1', options) end end + + def self.bucket + @bucket ||= cluster.bucket("billeo-db-bucket") + end end end diff --git a/lib/couchbase-orm/n1ql.rb b/lib/couchbase-orm/n1ql.rb index de30d5ef..79ebb6b8 100644 --- a/lib/couchbase-orm/n1ql.rb +++ b/lib/couchbase-orm/n1ql.rb @@ -48,10 +48,9 @@ def n1ql(name, query: nil, emit_key: [], **options) @indexes[name] = method_opts singleton_class.__send__(:define_method, name) do |**opts, &result_modifier| - opts = options.merge(opts) - - values = convert_values(opts[:key]) - current_query = build_query(method_opts[:emit_key], values, query, **opts) + opts = options.merge(opts).reverse_merge(scan_consistency: :request_plus) + values = convert_values(opts.delete(:key)) + current_query = run_query(method_opts[:emit_key], values, query, **opts.except(:include_docs)) if result_modifier opts[:include_docs] = true @@ -110,24 +109,22 @@ def build_order(keys, descending) "#{keys.dup.push("meta().id").map { |k| "#{k} #{descending ? "desc" : "asc" }" }.join(",")}" end - def build_query(keys, values, query, descending: false, limit: nil, **options) + def build_limit(limit) + limit ? "limit #{limit}" : "" + end + + def run_query(keys, values, query, descending: false, limit: nil, **options) if query query.call(bucket, values) else - bucket_name = bucket.bucket + bucket_name = bucket.name where = build_where(keys, values) order = build_order(keys, descending) - query = bucket.n1ql - .select("raw meta().id") - .from("`#{bucket_name}`") - .where(where) - if order - query = query.order_by(order) - end - if limit - query = query.limit(limit) - end - query + limit = build_limit(limit) + n1ql_query = "select raw meta().id from `#{bucket_name}` where #{where} order by #{order} #{limit}" + result = cluster.query(n1ql_query, Couchbase::Options::Query.new(**options)) + CouchbaseOrm.logger.debug "N1QL query: #{n1ql_query} return #{result.rows.to_a.length} rows" + N1qlProxy.new(result) end end end diff --git a/lib/couchbase-orm/persistence.rb b/lib/couchbase-orm/persistence.rb index f2a10d64..b394522f 100644 --- a/lib/couchbase-orm/persistence.rb +++ b/lib/couchbase-orm/persistence.rb @@ -263,8 +263,6 @@ def _create_record(**options) CouchbaseOrm.logger.debug { "Data - Insert #{_id} #{@__attributes__.to_s.truncate(200)}" } #resp = self.class.collection.add(_id, @__attributes__, **options) - puts "ici" - puts @__attributes__ resp = self.class.collection.upsert(_id, @__attributes__, **options) # Ensure the model is up to date diff --git a/lib/couchbase-orm/proxies/bucket_proxy.rb b/lib/couchbase-orm/proxies/bucket_proxy.rb index 89ede22b..7129b7d2 100644 --- a/lib/couchbase-orm/proxies/bucket_proxy.rb +++ b/lib/couchbase-orm/proxies/bucket_proxy.rb @@ -9,10 +9,6 @@ def initialize(proxyfied) @proxyfied = proxyfied - self.class.define_method(:name) do - @proxyfied.bucket - end - self.class.define_method(:n1ql) do N1qlProxy.new(@proxyfied.n1ql) end diff --git a/lib/couchbase-orm/proxies/n1ql_proxy.rb b/lib/couchbase-orm/proxies/n1ql_proxy.rb index 688e29ba..62a8ab3b 100644 --- a/lib/couchbase-orm/proxies/n1ql_proxy.rb +++ b/lib/couchbase-orm/proxies/n1ql_proxy.rb @@ -13,7 +13,10 @@ def initialize(proxyfied) return @results if @results CouchbaseOrm.logger.debug 'Query - ' + self.to_s - @results = ResultsProxy.new(@proxyfied.results(*params, &block)) + + results = @proxyfied.rows + results = results.map { |r| block.call(r) } if block + @results = ResultsProxy.new(results.to_a) end self.class.define_method(:to_s) do diff --git a/lib/couchbase-orm/railtie.rb b/lib/couchbase-orm/railtie.rb index 561e4b34..e3ff6ec2 100644 --- a/lib/couchbase-orm/railtie.rb +++ b/lib/couchbase-orm/railtie.rb @@ -53,8 +53,6 @@ def self.rescue_responses # Initialize Couchbase Mode. This will look for a couchbase.yml in the # config directory and configure Couchbase connection appropriately. initializer 'couchbase.setup_connection' do - puts "ici!!!" - puts Rails.env config_file = Rails.root.join('config', 'couchbase.yml') if config_file.file? && config = YAML.load(ERB.new(File.read(config_file)).result)[Rails.env] diff --git a/spec/support.rb b/spec/support.rb index 5c26d42a..e486a2f3 100644 --- a/spec/support.rb +++ b/spec/support.rb @@ -1,12 +1,5 @@ # frozen_string_literal: true, encoding: ASCII-8BIT - -if ENV['TRAVIS_TEST'] - require 'mt-libcouchbase' - MTLibcouchbase::Defaults.username = 'tester' - MTLibcouchbase::Defaults.password = 'password123' -end - require 'couchbase-orm' require 'minitest/assertions' require 'active_model/lint' @@ -32,3 +25,7 @@ def assertions=(val) @model = subject end end + +if ENV["COUCHBASE_ORM_DEBUG"] + CouchbaseOrm.logger.level = Logger::DEBUG +end From b4f6638f22f0df3812041a9ccda4926244477f80 Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Thu, 23 Jun 2022 14:17:04 +0200 Subject: [PATCH 06/43] Fix index_spec.rb --- lib/couchbase-orm/base.rb | 7 +++--- lib/couchbase-orm/proxies/bucket_proxy.rb | 18 +++++++-------- lib/couchbase-orm/utilities/index.rb | 27 +++++++++-------------- spec/index_spec.rb | 4 ++-- 4 files changed, 26 insertions(+), 30 deletions(-) diff --git a/lib/couchbase-orm/base.rb b/lib/couchbase-orm/base.rb index 302626ec..9b8fb77c 100644 --- a/lib/couchbase-orm/base.rb +++ b/lib/couchbase-orm/base.rb @@ -10,6 +10,7 @@ require 'couchbase-orm/persistence' require 'couchbase-orm/associations' require 'couchbase-orm/proxies/bucket_proxy' +require 'couchbase-orm/proxies/collection_proxy' require 'couchbase-orm/utilities/join' require 'couchbase-orm/utilities/enum' require 'couchbase-orm/utilities/index' @@ -61,7 +62,7 @@ def cluster end def collection - bucket.default_collection + CollectionProxy.new(bucket.default_collection) end def uuid_generator @@ -109,7 +110,7 @@ def find(*ids, **options) raise MTLibcouchbase::Error::EmptyKey, 'no id(s) provided' end - puts "Data - Get #{ids}" + puts "Data - Get(#{ids.length}) #{ids}" record = collection.get(*ids) puts "After Data - Get #{record}" @@ -117,7 +118,7 @@ def find(*ids, **options) records.map! { |record| if record puts "content.delete(:type)" - raise "NOT YET IMPLEMENTED" if ids.length > 1 + raise "FIXME: NOT YET IMPLEMENTED" if ids.length > 1 self.new(record, id: ids[0]) else false diff --git a/lib/couchbase-orm/proxies/bucket_proxy.rb b/lib/couchbase-orm/proxies/bucket_proxy.rb index 7129b7d2..106f8c82 100644 --- a/lib/couchbase-orm/proxies/bucket_proxy.rb +++ b/lib/couchbase-orm/proxies/bucket_proxy.rb @@ -21,15 +21,15 @@ def initialize(proxyfied) CouchbaseOrm.logger.debug "View - #{design} #{view}" @results = ResultsProxy.new(@proxyfied.send(:view, design, view, **opts, &block)) end - - if RUBY_VERSION.to_i >= 3 - def method_missing(name, *args, **options, &block) - @proxyfied.public_send(name, *args, **options, &block) - end - else - def method_missing(name, *args, &block) - @proxyfied.public_send(name, *args, &block) - end + end + + if RUBY_VERSION.to_i >= 3 + def method_missing(name, *args, **options, &block) + @proxyfied.public_send(name, *args, **options, &block) + end + else + def method_missing(name, *args, &block) + @proxyfied.public_send(name, *args, &block) end end end diff --git a/lib/couchbase-orm/utilities/index.rb b/lib/couchbase-orm/utilities/index.rb index ec3b5afe..f12d0ecd 100644 --- a/lib/couchbase-orm/utilities/index.rb +++ b/lib/couchbase-orm/utilities/index.rb @@ -50,13 +50,13 @@ def index(attrs, name = nil, presence: true, &processor) # use the bucket key as an index - lookup records by attr values define_singleton_method(find_by_method) do |*values| key = self.send(class_bucket_key_method, *values) - id = self.bucket.get(key, quiet: true) + id = self.collection.get(key)&.content if id mod = self.find_by_id(id) return mod if mod # Clean up record if the id doesn't exist - self.bucket.delete(key, quiet: true) + self.collection.remove(key) end nil @@ -100,18 +100,17 @@ def index(attrs, name = nil, presence: true, &processor) original_key = instance_variable_get(original_bucket_key_var) if original_key - check_ref_id = record.class.bucket.get(original_key, extended: true, quiet: true) - if check_ref_id && check_ref_id.value == record.id - begin - record.class.bucket.delete(original_key, cas: check_ref_id.cas) - rescue ::MTLibcouchbase::Error::KeyExists - # Errors here can be ignored. Just means the key was updated elswhere + begin + check_ref_id = record.class.collection.get(original_key) + if check_ref_id && check_ref_id.content == record.id + CouchbaseOrm.logger.debug "Removing old key #{original_key}" + record.class.collection.remove(original_key, cas: check_ref_id.cas) end end end unless presence == false && attrs.length == 1 && record[attrs[0]].nil? - record.class.bucket.set(record.send(bucket_key_method), record.id, plain: true) + record.class.collection.upsert(record.send(bucket_key_method), record.id) end instance_variable_set(original_bucket_key_var, nil) end @@ -119,13 +118,9 @@ def index(attrs, name = nil, presence: true, &processor) # cleanup by removing the bucket key before the record is deleted # TODO: handle unpersisted, modified component values before_destroy do |record| - check_ref_id = record.class.bucket.get(record.send(bucket_key_method), extended: true, quiet: true) - if check_ref_id && check_ref_id.value == record.id - begin - record.class.bucket.delete(record.send(bucket_key_method), cas: check_ref_id.cas) - rescue ::MTLibcouchbase::Error::KeyExists - # Errors here can be ignored. Just means the key was updated elswhere - end + check_ref_id = record.class.collection.get(record.send(bucket_key_method)) + if check_ref_id && check_ref_id.content == record.id + record.class.collection.remove(record.send(bucket_key_method), cas: check_ref_id.cas) end true end diff --git a/spec/index_spec.rb b/spec/index_spec.rb index a4cb443a..7c6c7576 100644 --- a/spec/index_spec.rb +++ b/spec/index_spec.rb @@ -4,6 +4,7 @@ class IndexTest < CouchbaseOrm::Base + n1ql :all attribute :email, type: String attribute :name, type: String, default: :joe ensure_unique :email, presence: false @@ -23,8 +24,7 @@ class EnumTest < CouchbaseOrm::Base describe CouchbaseOrm::Index do after :each do - IndexTest.bucket.delete('index_testemail-joe@aca.com') - IndexTest.bucket.delete('index_testemail-') + IndexTest.all.map(&:destroy) end it "should prevent models being created if they should have unique keys" do From c849592792045e28dbd296d464630dc7fcd9b59e Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Thu, 23 Jun 2022 14:17:31 +0200 Subject: [PATCH 07/43] Missing files --- lib/couchbase-orm/proxies/collection_proxy.rb | 39 +++++++++++++++++++ spec/collection_proxy_spec.rb | 18 +++++++++ 2 files changed, 57 insertions(+) create mode 100644 lib/couchbase-orm/proxies/collection_proxy.rb create mode 100644 spec/collection_proxy_spec.rb diff --git a/lib/couchbase-orm/proxies/collection_proxy.rb b/lib/couchbase-orm/proxies/collection_proxy.rb new file mode 100644 index 00000000..35fa4663 --- /dev/null +++ b/lib/couchbase-orm/proxies/collection_proxy.rb @@ -0,0 +1,39 @@ +module CouchbaseOrm + class CollectionProxy + + def get!(id, **options) + @proxyfied.get(id, Couchbase::Options::Get.new(**options)) + end + + def get(id, **options) + @proxyfied.get(id, Couchbase::Options::Get.new(**options)) + rescue Couchbase::Error::DocumentNotFound + nil + end + + def remove!(id, **options) + @proxyfied.remove(id, Couchbase::Options::Remove.new(**options)) + end + + def remove(id, **options) + @proxyfied.remove(id, Couchbase::Options::Remove.new(**options)) + rescue Couchbase::Error::DocumentNotFound + nil + end + + def initialize(proxyfied) + raise "Must proxy a non nil object" if proxyfied.nil? + @proxyfied = proxyfied + end + + if RUBY_VERSION.to_i >= 3 + def method_missing(name, *args, **options, &block) + @proxyfied.public_send(name, *args, **options, &block) + end + else + def method_missing(name, *args, &block) + @proxyfied.public_send(name, *args, &block) + end + end + end +end diff --git a/spec/collection_proxy_spec.rb b/spec/collection_proxy_spec.rb new file mode 100644 index 00000000..4c110b31 --- /dev/null +++ b/spec/collection_proxy_spec.rb @@ -0,0 +1,18 @@ +require File.expand_path("../support", __FILE__) +require File.expand_path("../../lib/couchbase-orm/proxies/collection_proxy", __FILE__) + +class Proxyfied + def get(key, options = nil) + raise Couchbase::Error::DocumentNotFound + end +end + +describe CouchbaseOrm::CollectionProxy do + it "should raise an error when get is called with bang version" do + expect { CouchbaseOrm::CollectionProxy.new(Proxyfied.new).get!('key') }.to raise_error(Couchbase::Error::DocumentNotFound) + end + + it "should not raise an error when get is called with non bang version" do + expect { CouchbaseOrm::CollectionProxy.new(Proxyfied.new).get('key') }.to_not raise_error + end +end From 4d0100dca9db77dc26e4c367f7b1c4af19efbf20 Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Thu, 23 Jun 2022 17:27:29 +0200 Subject: [PATCH 08/43] Fix associations_spec.rb --- lib/couchbase-orm/base.rb | 24 +++++++------------ lib/couchbase-orm/persistence.rb | 8 +++---- lib/couchbase-orm/proxies/collection_proxy.rb | 12 ++++++++++ spec/associations_spec.rb | 11 +++++---- spec/support.rb | 4 ---- 5 files changed, 30 insertions(+), 29 deletions(-) diff --git a/lib/couchbase-orm/base.rb b/lib/couchbase-orm/base.rb index 9b8fb77c..8171854d 100644 --- a/lib/couchbase-orm/base.rb +++ b/lib/couchbase-orm/base.rb @@ -101,6 +101,7 @@ def attributes end def find(*ids, **options) + CouchbaseOrm.logger.debug { "Base.find(l##{ids.length}) #{ids}" } options[:extended] = true options[:quiet] ||= false @@ -110,21 +111,12 @@ def find(*ids, **options) raise MTLibcouchbase::Error::EmptyKey, 'no id(s) provided' end - puts "Data - Get(#{ids.length}) #{ids}" - - record = collection.get(*ids) - puts "After Data - Get #{record}" - records = record.is_a?(Array) ? record : [record] - records.map! { |record| - if record - puts "content.delete(:type)" - raise "FIXME: NOT YET IMPLEMENTED" if ids.length > 1 - self.new(record, id: ids[0]) - else - false - end + records = options[:quiet] ? collection.get_multi(ids) : collection.get_multi!(ids) + CouchbaseOrm.logger.debug { "Base.find found(#{records})" } + records = records.zip(ids).map { |record, id| + self.new(record, id: id) if record } - records.select! { |rec| rec } + records.compact! ids.length > 1 ? records : records[0] end @@ -135,8 +127,8 @@ def find_by_id(*ids, **options) alias_method :[], :find_by_id def exists?(id) - CouchbaseOrm.logger.debug "Data - Get #{id}" - !bucket.get(id, quiet: true).nil? + CouchbaseOrm.logger.debug "Data - Exists? #{id}" + collection.exists(id).exists end alias_method :has_key?, :exists? end diff --git a/lib/couchbase-orm/persistence.rb b/lib/couchbase-orm/persistence.rb index b394522f..0b4bde00 100644 --- a/lib/couchbase-orm/persistence.rb +++ b/lib/couchbase-orm/persistence.rb @@ -204,7 +204,7 @@ def reload raise "unable to reload, model not persisted" unless key CouchbaseOrm.logger.debug "Data - Get #{key}" - resp = self.class.collection.get(key) + resp = self.class.collection.get!(key) @__attributes__ = ::ActiveSupport::HashWithIndifferentAccess.new(resp.content) @__metadata__.key = key @__metadata__.cas = resp.cas @@ -238,8 +238,8 @@ def _update_record(with_cas: false, **options) _id = @__metadata__.key options[:cas] = @__metadata__.cas if with_cas - CouchbaseOrm.logger.debug { "Data - Replace #{_id} #{@__attributes__.to_s.truncate(200)}" } - resp = self.class.collection.upsert(_id, @__attributes__, **options) + CouchbaseOrm.logger.debug { "_update_record - replace #{_id} #{@__attributes__.to_s.truncate(200)}" } + resp = self.class.collection.replace(_id, @__attributes__, **options) # Ensure the model is up to date @__metadata__.key = _id @@ -260,7 +260,7 @@ def _create_record(**options) @__attributes__.delete(:id) _id = @id || self.class.uuid_generator.next(self) - CouchbaseOrm.logger.debug { "Data - Insert #{_id} #{@__attributes__.to_s.truncate(200)}" } + CouchbaseOrm.logger.debug { "_create_record - Upsert #{_id} #{@__attributes__.to_s.truncate(200)}" } #resp = self.class.collection.add(_id, @__attributes__, **options) resp = self.class.collection.upsert(_id, @__attributes__, **options) diff --git a/lib/couchbase-orm/proxies/collection_proxy.rb b/lib/couchbase-orm/proxies/collection_proxy.rb index 35fa4663..9c9cae63 100644 --- a/lib/couchbase-orm/proxies/collection_proxy.rb +++ b/lib/couchbase-orm/proxies/collection_proxy.rb @@ -11,6 +11,18 @@ def get(id, **options) nil end + def get_multi!(*ids, **options) + result = @proxyfied.get_multi(*ids, Couchbase::Options::GetMulti.new(**options)) + first_result_with_error = result.find(&:error) + raise first_result_with_error.error if first_result_with_error + result + end + + def get_multi(*ids, **options) + result = @proxyfied.get_multi(*ids, Couchbase::Options::GetMulti.new(**options)) + result.reject(&:error) + end + def remove!(id, **options) @proxyfied.remove(id, Couchbase::Options::Remove.new(**options)) end diff --git a/spec/associations_spec.rb b/spec/associations_spec.rb index 850d99b8..ff45ed1d 100644 --- a/spec/associations_spec.rb +++ b/spec/associations_spec.rb @@ -39,21 +39,22 @@ class Part < CouchbaseOrm::Base expect(parent.persisted?).to be(true) expect(child.persisted?).to be(true) id = parent.id - + child.destroy expect(child.destroyed?).to be(true) expect(parent.destroyed?).to be(false) # Ensure that parent has been destroyed - expect { Parent.find(id) }.to raise_error(MTLibcouchbase::Error::KeyNotFound) + expect { Parent.find(id) }.to raise_error(Couchbase::Error::DocumentNotFound) + expect(Parent.find_by_id(id)).to be(nil) - expect { parent.reload }.to raise_error(MTLibcouchbase::Error::KeyNotFound) + expect { parent.reload }.to raise_error(Couchbase::Error::DocumentNotFound) # Save will always return true unless the model is changed (won't touch the database) parent.name = 'should fail' - expect { parent.save }.to raise_error(MTLibcouchbase::Error::KeyNotFound) - expect { parent.save! }.to raise_error(MTLibcouchbase::Error::KeyNotFound) + expect { parent.save }.to raise_error(Couchbase::Error::DocumentNotFound) + expect { parent.save! }.to raise_error(Couchbase::Error::DocumentNotFound) end it "should cache associations" do diff --git a/spec/support.rb b/spec/support.rb index e486a2f3..1314cfef 100644 --- a/spec/support.rb +++ b/spec/support.rb @@ -25,7 +25,3 @@ def assertions=(val) @model = subject end end - -if ENV["COUCHBASE_ORM_DEBUG"] - CouchbaseOrm.logger.level = Logger::DEBUG -end From 74f40b1985ef53ba3f0aff70ffc22cc8d9a2c59b Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Thu, 23 Jun 2022 18:37:15 +0200 Subject: [PATCH 09/43] remove rails folder --- rails7-ruby-client/.gitattributes | 7 -- rails7-ruby-client/.gitignore | 35 ------- rails7-ruby-client/.ruby-version | 1 - rails7-ruby-client/Gemfile | 58 ------------ rails7-ruby-client/README.md | 24 ----- rails7-ruby-client/Rakefile | 6 -- .../app/assets/config/manifest.js | 2 - rails7-ruby-client/app/assets/images/.keep | 0 .../app/assets/stylesheets/application.css | 15 --- .../app/channels/application_cable/channel.rb | 4 - .../channels/application_cable/connection.rb | 4 - .../app/controllers/application_controller.rb | 2 - .../app/controllers/concerns/.keep | 0 .../app/helpers/application_helper.rb | 2 - .../app/jobs/application_job.rb | 7 -- .../app/mailers/application_mailer.rb | 4 - .../app/models/application_record.rb | 3 - rails7-ruby-client/app/models/concerns/.keep | 0 .../app/views/layouts/application.html.erb | 15 --- .../app/views/layouts/mailer.html.erb | 13 --- .../app/views/layouts/mailer.text.erb | 1 - rails7-ruby-client/config.ru | 6 -- rails7-ruby-client/config/application.rb | 37 -------- rails7-ruby-client/config/boot.rb | 4 - rails7-ruby-client/config/cable.yml | 10 -- rails7-ruby-client/config/couchbase.yml | 12 --- rails7-ruby-client/config/credentials.yml.enc | 1 - rails7-ruby-client/config/database.yml | 25 ----- rails7-ruby-client/config/environment.rb | 5 - .../config/environments/development.rb | 70 -------------- .../config/environments/production.rb | 93 ------------------- .../config/environments/test.rb | 60 ------------ .../config/initializers/assets.rb | 12 --- .../initializers/content_security_policy.rb | 25 ----- .../initializers/filter_parameter_logging.rb | 8 -- .../config/initializers/inflections.rb | 16 ---- .../config/initializers/permissions_policy.rb | 11 --- rails7-ruby-client/config/locales/en.yml | 33 ------- rails7-ruby-client/config/puma.rb | 43 --------- rails7-ruby-client/config/routes.rb | 6 -- rails7-ruby-client/config/storage.yml | 34 ------- rails7-ruby-client/db/seeds.rb | 7 -- rails7-ruby-client/lib/assets/.keep | 0 rails7-ruby-client/lib/tasks/.keep | 0 rails7-ruby-client/log/.keep | 0 rails7-ruby-client/public/404.html | 67 ------------- rails7-ruby-client/public/422.html | 67 ------------- rails7-ruby-client/public/500.html | 66 ------------- .../public/apple-touch-icon-precomposed.png | 0 .../public/apple-touch-icon.png | 0 rails7-ruby-client/public/favicon.ico | 0 rails7-ruby-client/public/robots.txt | 1 - rails7-ruby-client/storage/.keep | 0 rails7-ruby-client/test/models/first_test.rb | 30 ------ 54 files changed, 952 deletions(-) delete mode 100644 rails7-ruby-client/.gitattributes delete mode 100644 rails7-ruby-client/.gitignore delete mode 100644 rails7-ruby-client/.ruby-version delete mode 100644 rails7-ruby-client/Gemfile delete mode 100644 rails7-ruby-client/README.md delete mode 100644 rails7-ruby-client/Rakefile delete mode 100644 rails7-ruby-client/app/assets/config/manifest.js delete mode 100644 rails7-ruby-client/app/assets/images/.keep delete mode 100644 rails7-ruby-client/app/assets/stylesheets/application.css delete mode 100644 rails7-ruby-client/app/channels/application_cable/channel.rb delete mode 100644 rails7-ruby-client/app/channels/application_cable/connection.rb delete mode 100644 rails7-ruby-client/app/controllers/application_controller.rb delete mode 100644 rails7-ruby-client/app/controllers/concerns/.keep delete mode 100644 rails7-ruby-client/app/helpers/application_helper.rb delete mode 100644 rails7-ruby-client/app/jobs/application_job.rb delete mode 100644 rails7-ruby-client/app/mailers/application_mailer.rb delete mode 100644 rails7-ruby-client/app/models/application_record.rb delete mode 100644 rails7-ruby-client/app/models/concerns/.keep delete mode 100644 rails7-ruby-client/app/views/layouts/application.html.erb delete mode 100644 rails7-ruby-client/app/views/layouts/mailer.html.erb delete mode 100644 rails7-ruby-client/app/views/layouts/mailer.text.erb delete mode 100644 rails7-ruby-client/config.ru delete mode 100644 rails7-ruby-client/config/application.rb delete mode 100644 rails7-ruby-client/config/boot.rb delete mode 100644 rails7-ruby-client/config/cable.yml delete mode 100644 rails7-ruby-client/config/couchbase.yml delete mode 100644 rails7-ruby-client/config/credentials.yml.enc delete mode 100644 rails7-ruby-client/config/database.yml delete mode 100644 rails7-ruby-client/config/environment.rb delete mode 100644 rails7-ruby-client/config/environments/development.rb delete mode 100644 rails7-ruby-client/config/environments/production.rb delete mode 100644 rails7-ruby-client/config/environments/test.rb delete mode 100644 rails7-ruby-client/config/initializers/assets.rb delete mode 100644 rails7-ruby-client/config/initializers/content_security_policy.rb delete mode 100644 rails7-ruby-client/config/initializers/filter_parameter_logging.rb delete mode 100644 rails7-ruby-client/config/initializers/inflections.rb delete mode 100644 rails7-ruby-client/config/initializers/permissions_policy.rb delete mode 100644 rails7-ruby-client/config/locales/en.yml delete mode 100644 rails7-ruby-client/config/puma.rb delete mode 100644 rails7-ruby-client/config/routes.rb delete mode 100644 rails7-ruby-client/config/storage.yml delete mode 100644 rails7-ruby-client/db/seeds.rb delete mode 100644 rails7-ruby-client/lib/assets/.keep delete mode 100644 rails7-ruby-client/lib/tasks/.keep delete mode 100644 rails7-ruby-client/log/.keep delete mode 100644 rails7-ruby-client/public/404.html delete mode 100644 rails7-ruby-client/public/422.html delete mode 100644 rails7-ruby-client/public/500.html delete mode 100644 rails7-ruby-client/public/apple-touch-icon-precomposed.png delete mode 100644 rails7-ruby-client/public/apple-touch-icon.png delete mode 100644 rails7-ruby-client/public/favicon.ico delete mode 100644 rails7-ruby-client/public/robots.txt delete mode 100644 rails7-ruby-client/storage/.keep delete mode 100644 rails7-ruby-client/test/models/first_test.rb diff --git a/rails7-ruby-client/.gitattributes b/rails7-ruby-client/.gitattributes deleted file mode 100644 index 31eeee0b..00000000 --- a/rails7-ruby-client/.gitattributes +++ /dev/null @@ -1,7 +0,0 @@ -# See https://git-scm.com/docs/gitattributes for more about git attribute files. - -# Mark the database schema as having been generated. -db/schema.rb linguist-generated - -# Mark any vendored files as having been vendored. -vendor/* linguist-vendored diff --git a/rails7-ruby-client/.gitignore b/rails7-ruby-client/.gitignore deleted file mode 100644 index 886f714b..00000000 --- a/rails7-ruby-client/.gitignore +++ /dev/null @@ -1,35 +0,0 @@ -# See https://help.github.com/articles/ignoring-files for more about ignoring files. -# -# If you find yourself ignoring temporary files generated by your text editor -# or operating system, you probably want to add a global ignore instead: -# git config --global core.excludesfile '~/.gitignore_global' - -# Ignore bundler config. -/.bundle - -# Ignore the default SQLite database. -/db/*.sqlite3 -/db/*.sqlite3-* - -# Ignore all logfiles and tempfiles. -/log/* -/tmp/* -!/log/.keep -!/tmp/.keep - -# Ignore pidfiles, but keep the directory. -/tmp/pids/* -!/tmp/pids/ -!/tmp/pids/.keep - -# Ignore uploaded files in development. -/storage/* -!/storage/.keep -/tmp/storage/* -!/tmp/storage/ -!/tmp/storage/.keep - -/public/assets - -# Ignore master key for decrypting credentials and more. -/config/master.key diff --git a/rails7-ruby-client/.ruby-version b/rails7-ruby-client/.ruby-version deleted file mode 100644 index ef538c28..00000000 --- a/rails7-ruby-client/.ruby-version +++ /dev/null @@ -1 +0,0 @@ -3.1.2 diff --git a/rails7-ruby-client/Gemfile b/rails7-ruby-client/Gemfile deleted file mode 100644 index 1dedb9b0..00000000 --- a/rails7-ruby-client/Gemfile +++ /dev/null @@ -1,58 +0,0 @@ -source "https://rubygems.org" -git_source(:github) { |repo| "https://github.com/#{repo}.git" } - -ruby "3.1.2" - -# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" -gem "rails", "~> 7.0.3" - -# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails] -gem "sprockets-rails" - -# Use sqlite3 as the database for Active Record -gem "sqlite3", "~> 1.4" - -gem "couchbase-orm", path: ".." - -# Use the Puma web server [https://github.com/puma/puma] -gem "puma", "~> 5.0" - -# Build JSON APIs with ease [https://github.com/rails/jbuilder] -gem "jbuilder" - -# Use Redis adapter to run Action Cable in production -# gem "redis", "~> 4.0" - -# Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis] -# gem "kredis" - -# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword] -# gem "bcrypt", "~> 3.1.7" - -# Windows does not include zoneinfo files, so bundle the tzinfo-data gem -gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ] - -# Reduces boot times through caching; required in config/boot.rb -gem "bootsnap", require: false - -# Use Sass to process CSS -# gem "sassc-rails" - -# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images] -# gem "image_processing", "~> 1.2" - -group :development, :test do - # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem - gem "debug", platforms: %i[ mri mingw x64_mingw ] -end - -group :development do - # Use console on exceptions pages [https://github.com/rails/web-console] - gem "web-console" - - # Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler] - # gem "rack-mini-profiler" - - # Speed up commands on slow machines / big apps [https://github.com/rails/spring] - # gem "spring" -end diff --git a/rails7-ruby-client/README.md b/rails7-ruby-client/README.md deleted file mode 100644 index 7db80e4c..00000000 --- a/rails7-ruby-client/README.md +++ /dev/null @@ -1,24 +0,0 @@ -# README - -This README would normally document whatever steps are necessary to get the -application up and running. - -Things you may want to cover: - -* Ruby version - -* System dependencies - -* Configuration - -* Database creation - -* Database initialization - -* How to run the test suite - -* Services (job queues, cache servers, search engines, etc.) - -* Deployment instructions - -* ... diff --git a/rails7-ruby-client/Rakefile b/rails7-ruby-client/Rakefile deleted file mode 100644 index 9a5ea738..00000000 --- a/rails7-ruby-client/Rakefile +++ /dev/null @@ -1,6 +0,0 @@ -# Add your own tasks in files placed in lib/tasks ending in .rake, -# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. - -require_relative "config/application" - -Rails.application.load_tasks diff --git a/rails7-ruby-client/app/assets/config/manifest.js b/rails7-ruby-client/app/assets/config/manifest.js deleted file mode 100644 index 59181933..00000000 --- a/rails7-ruby-client/app/assets/config/manifest.js +++ /dev/null @@ -1,2 +0,0 @@ -//= link_tree ../images -//= link_directory ../stylesheets .css diff --git a/rails7-ruby-client/app/assets/images/.keep b/rails7-ruby-client/app/assets/images/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/rails7-ruby-client/app/assets/stylesheets/application.css b/rails7-ruby-client/app/assets/stylesheets/application.css deleted file mode 100644 index 288b9ab7..00000000 --- a/rails7-ruby-client/app/assets/stylesheets/application.css +++ /dev/null @@ -1,15 +0,0 @@ -/* - * This is a manifest file that'll be compiled into application.css, which will include all the files - * listed below. - * - * Any CSS (and SCSS, if configured) file within this directory, lib/assets/stylesheets, or any plugin's - * vendor/assets/stylesheets directory can be referenced here using a relative path. - * - * You're free to add application-wide styles to this file and they'll appear at the bottom of the - * compiled file so the styles you add here take precedence over styles defined in any other CSS - * files in this directory. Styles in this file should be added after the last require_* statement. - * It is generally better to create a new file per style scope. - * - *= require_tree . - *= require_self - */ diff --git a/rails7-ruby-client/app/channels/application_cable/channel.rb b/rails7-ruby-client/app/channels/application_cable/channel.rb deleted file mode 100644 index d6726972..00000000 --- a/rails7-ruby-client/app/channels/application_cable/channel.rb +++ /dev/null @@ -1,4 +0,0 @@ -module ApplicationCable - class Channel < ActionCable::Channel::Base - end -end diff --git a/rails7-ruby-client/app/channels/application_cable/connection.rb b/rails7-ruby-client/app/channels/application_cable/connection.rb deleted file mode 100644 index 0ff5442f..00000000 --- a/rails7-ruby-client/app/channels/application_cable/connection.rb +++ /dev/null @@ -1,4 +0,0 @@ -module ApplicationCable - class Connection < ActionCable::Connection::Base - end -end diff --git a/rails7-ruby-client/app/controllers/application_controller.rb b/rails7-ruby-client/app/controllers/application_controller.rb deleted file mode 100644 index 09705d12..00000000 --- a/rails7-ruby-client/app/controllers/application_controller.rb +++ /dev/null @@ -1,2 +0,0 @@ -class ApplicationController < ActionController::Base -end diff --git a/rails7-ruby-client/app/controllers/concerns/.keep b/rails7-ruby-client/app/controllers/concerns/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/rails7-ruby-client/app/helpers/application_helper.rb b/rails7-ruby-client/app/helpers/application_helper.rb deleted file mode 100644 index de6be794..00000000 --- a/rails7-ruby-client/app/helpers/application_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module ApplicationHelper -end diff --git a/rails7-ruby-client/app/jobs/application_job.rb b/rails7-ruby-client/app/jobs/application_job.rb deleted file mode 100644 index d394c3d1..00000000 --- a/rails7-ruby-client/app/jobs/application_job.rb +++ /dev/null @@ -1,7 +0,0 @@ -class ApplicationJob < ActiveJob::Base - # Automatically retry jobs that encountered a deadlock - # retry_on ActiveRecord::Deadlocked - - # Most jobs are safe to ignore if the underlying records are no longer available - # discard_on ActiveJob::DeserializationError -end diff --git a/rails7-ruby-client/app/mailers/application_mailer.rb b/rails7-ruby-client/app/mailers/application_mailer.rb deleted file mode 100644 index 3c34c814..00000000 --- a/rails7-ruby-client/app/mailers/application_mailer.rb +++ /dev/null @@ -1,4 +0,0 @@ -class ApplicationMailer < ActionMailer::Base - default from: "from@example.com" - layout "mailer" -end diff --git a/rails7-ruby-client/app/models/application_record.rb b/rails7-ruby-client/app/models/application_record.rb deleted file mode 100644 index b63caeb8..00000000 --- a/rails7-ruby-client/app/models/application_record.rb +++ /dev/null @@ -1,3 +0,0 @@ -class ApplicationRecord < ActiveRecord::Base - primary_abstract_class -end diff --git a/rails7-ruby-client/app/models/concerns/.keep b/rails7-ruby-client/app/models/concerns/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/rails7-ruby-client/app/views/layouts/application.html.erb b/rails7-ruby-client/app/views/layouts/application.html.erb deleted file mode 100644 index 73782d98..00000000 --- a/rails7-ruby-client/app/views/layouts/application.html.erb +++ /dev/null @@ -1,15 +0,0 @@ - - - - CbormRails7b - - <%= csrf_meta_tags %> - <%= csp_meta_tag %> - - <%= stylesheet_link_tag "application" %> - - - - <%= yield %> - - diff --git a/rails7-ruby-client/app/views/layouts/mailer.html.erb b/rails7-ruby-client/app/views/layouts/mailer.html.erb deleted file mode 100644 index cbd34d2e..00000000 --- a/rails7-ruby-client/app/views/layouts/mailer.html.erb +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - <%= yield %> - - diff --git a/rails7-ruby-client/app/views/layouts/mailer.text.erb b/rails7-ruby-client/app/views/layouts/mailer.text.erb deleted file mode 100644 index 37f0bddb..00000000 --- a/rails7-ruby-client/app/views/layouts/mailer.text.erb +++ /dev/null @@ -1 +0,0 @@ -<%= yield %> diff --git a/rails7-ruby-client/config.ru b/rails7-ruby-client/config.ru deleted file mode 100644 index 4a3c09a6..00000000 --- a/rails7-ruby-client/config.ru +++ /dev/null @@ -1,6 +0,0 @@ -# This file is used by Rack-based servers to start the application. - -require_relative "config/environment" - -run Rails.application -Rails.application.load_server diff --git a/rails7-ruby-client/config/application.rb b/rails7-ruby-client/config/application.rb deleted file mode 100644 index a6559f5a..00000000 --- a/rails7-ruby-client/config/application.rb +++ /dev/null @@ -1,37 +0,0 @@ -require_relative "boot" - -require "rails" -# Pick the frameworks you want: -require "active_model/railtie" -require "active_job/railtie" -require "active_record/railtie" -require "active_storage/engine" -require "action_controller/railtie" -require "action_mailer/railtie" -require "action_mailbox/engine" -require "action_text/engine" -require "action_view/railtie" -require "action_cable/engine" -# require "rails/test_unit/railtie" - -# Require the gems listed in Gemfile, including any gems -# you've limited to :test, :development, or :production. -Bundler.require(*Rails.groups) - -module CbormRails7b - class Application < Rails::Application - # Initialize configuration defaults for originally generated Rails version. - config.load_defaults 7.0 - - # Configuration for the application, engines, and railties goes here. - # - # These settings can be overridden in specific environments using the files - # in config/environments, which are processed later. - # - # config.time_zone = "Central Time (US & Canada)" - # config.eager_load_paths << Rails.root.join("extras") - - # Don't generate system test files. - config.generators.system_tests = nil - end -end diff --git a/rails7-ruby-client/config/boot.rb b/rails7-ruby-client/config/boot.rb deleted file mode 100644 index 988a5ddc..00000000 --- a/rails7-ruby-client/config/boot.rb +++ /dev/null @@ -1,4 +0,0 @@ -ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) - -require "bundler/setup" # Set up gems listed in the Gemfile. -require "bootsnap/setup" # Speed up boot time by caching expensive operations. diff --git a/rails7-ruby-client/config/cable.yml b/rails7-ruby-client/config/cable.yml deleted file mode 100644 index f042ce82..00000000 --- a/rails7-ruby-client/config/cable.yml +++ /dev/null @@ -1,10 +0,0 @@ -development: - adapter: async - -test: - adapter: test - -production: - adapter: redis - url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %> - channel_prefix: cborm_rails7b_production diff --git a/rails7-ruby-client/config/couchbase.yml b/rails7-ruby-client/config/couchbase.yml deleted file mode 100644 index 4552cf77..00000000 --- a/rails7-ruby-client/config/couchbase.yml +++ /dev/null @@ -1,12 +0,0 @@ - -development: - hosts: localhost - username: cb_admin - password: cb_admin_pwd - bucket: billeo-db-bucket - -test: - hosts: localhost - username: cb_admin - password: cb_admin_pwd - bucket: billeo-db-bucket diff --git a/rails7-ruby-client/config/credentials.yml.enc b/rails7-ruby-client/config/credentials.yml.enc deleted file mode 100644 index 2d0ab2aa..00000000 --- a/rails7-ruby-client/config/credentials.yml.enc +++ /dev/null @@ -1 +0,0 @@ -t11OzhiHElgqsYHwVWT9t4ihqrO6047ZX2GyMzRBcCKUvP2jsGRhyjAVTyAtjKUwLx+32gPFqZPRNLtoB2CWzZxOWrohYjPC+Imu5mOEKEz8Q/AkGZA3Bn73r1VQLLwOoFDoz3pJKHAgCKJKD3LpeB8vWF+fIgYUdovEwiccdrVyth0yaCAVQGtrg7AqCvnfcRxCi3Fk/mMWFmYLHuYFIBiUOrDAozdV6lKaZfhZ+MxwvCn/0/rUzJB6TtozSZMA+RfZOk2TuSYPX4hD3vPtURZf/javb9Rvu4GHwKVSbTcFM6SUDyirvbPDdycPnl6pOzRHM650cCViwecqfJg/8tGUdP1GLWFnHpO1fM0z1+EdyFuMgguAuBr+WFQIRa2uuzL9obMGP8GJKrPZfolxrF2ZI8FezTfPzl+Y--KyMGe+OGMFrzyrnZ--nDGbvqi5U98FfQ9FGwT3/Q== \ No newline at end of file diff --git a/rails7-ruby-client/config/database.yml b/rails7-ruby-client/config/database.yml deleted file mode 100644 index fcba57f1..00000000 --- a/rails7-ruby-client/config/database.yml +++ /dev/null @@ -1,25 +0,0 @@ -# SQLite. Versions 3.8.0 and up are supported. -# gem install sqlite3 -# -# Ensure the SQLite 3 gem is defined in your Gemfile -# gem "sqlite3" -# -default: &default - adapter: sqlite3 - pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> - timeout: 5000 - -development: - <<: *default - database: db/development.sqlite3 - -# 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: - <<: *default - database: db/test.sqlite3 - -production: - <<: *default - database: db/production.sqlite3 diff --git a/rails7-ruby-client/config/environment.rb b/rails7-ruby-client/config/environment.rb deleted file mode 100644 index cac53157..00000000 --- a/rails7-ruby-client/config/environment.rb +++ /dev/null @@ -1,5 +0,0 @@ -# Load the Rails application. -require_relative "application" - -# Initialize the Rails application. -Rails.application.initialize! diff --git a/rails7-ruby-client/config/environments/development.rb b/rails7-ruby-client/config/environments/development.rb deleted file mode 100644 index 8500f459..00000000 --- a/rails7-ruby-client/config/environments/development.rb +++ /dev/null @@ -1,70 +0,0 @@ -require "active_support/core_ext/integer/time" - -Rails.application.configure do - # Settings specified here will take precedence over those in config/application.rb. - - # In the development environment your application's code is reloaded any time - # it changes. This slows down response time but is perfect for development - # since you don't have to restart the web server when you make code changes. - config.cache_classes = false - - # Do not eager load code on boot. - config.eager_load = false - - # Show full error reports. - config.consider_all_requests_local = true - - # Enable server timing - config.server_timing = true - - # Enable/disable caching. By default caching is disabled. - # Run rails dev:cache to toggle caching. - if Rails.root.join("tmp/caching-dev.txt").exist? - config.action_controller.perform_caching = true - config.action_controller.enable_fragment_cache_logging = true - - config.cache_store = :memory_store - config.public_file_server.headers = { - "Cache-Control" => "public, max-age=#{2.days.to_i}" - } - else - config.action_controller.perform_caching = false - - config.cache_store = :null_store - end - - # Store uploaded files on the local file system (see config/storage.yml for options). - config.active_storage.service = :local - - # Don't care if the mailer can't send. - config.action_mailer.raise_delivery_errors = false - - config.action_mailer.perform_caching = false - - # Print deprecation notices to the Rails logger. - config.active_support.deprecation = :log - - # Raise exceptions for disallowed deprecations. - config.active_support.disallowed_deprecation = :raise - - # Tell Active Support which deprecation messages to disallow. - config.active_support.disallowed_deprecation_warnings = [] - - # Raise an error on page load if there are pending migrations. - config.active_record.migration_error = :page_load - - # Highlight code that triggered database queries in logs. - config.active_record.verbose_query_logs = true - - # Suppress logger output for asset requests. - config.assets.quiet = true - - # Raises error for missing translations. - # config.i18n.raise_on_missing_translations = true - - # Annotate rendered view with file names. - # config.action_view.annotate_rendered_view_with_filenames = true - - # Uncomment if you wish to allow Action Cable access from any origin. - # config.action_cable.disable_request_forgery_protection = true -end diff --git a/rails7-ruby-client/config/environments/production.rb b/rails7-ruby-client/config/environments/production.rb deleted file mode 100644 index b12048b1..00000000 --- a/rails7-ruby-client/config/environments/production.rb +++ /dev/null @@ -1,93 +0,0 @@ -require "active_support/core_ext/integer/time" - -Rails.application.configure do - # Settings specified here will take precedence over those in config/application.rb. - - # Code is not reloaded between requests. - config.cache_classes = true - - # Eager load code on boot. This eager loads most of Rails and - # your application in memory, allowing both threaded web servers - # and those relying on copy on write to perform better. - # Rake tasks automatically ignore this option for performance. - config.eager_load = true - - # Full error reports are disabled and caching is turned on. - config.consider_all_requests_local = false - config.action_controller.perform_caching = true - - # Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"] - # or in config/master.key. This key is used to decrypt credentials (and other encrypted files). - # config.require_master_key = true - - # Disable serving static files from the `/public` folder by default since - # Apache or NGINX already handles this. - config.public_file_server.enabled = ENV["RAILS_SERVE_STATIC_FILES"].present? - - # Compress CSS using a preprocessor. - # config.assets.css_compressor = :sass - - # Do not fallback to assets pipeline if a precompiled asset is missed. - config.assets.compile = false - - # Enable serving of images, stylesheets, and JavaScripts from an asset server. - # config.asset_host = "http://assets.example.com" - - # Specifies the header that your server uses for sending files. - # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for Apache - # config.action_dispatch.x_sendfile_header = "X-Accel-Redirect" # for NGINX - - # Store uploaded files on the local file system (see config/storage.yml for options). - config.active_storage.service = :local - - # Mount Action Cable outside main process or domain. - # config.action_cable.mount_path = nil - # config.action_cable.url = "wss://example.com/cable" - # config.action_cable.allowed_request_origins = [ "http://example.com", /http:\/\/example.*/ ] - - # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. - # config.force_ssl = true - - # Include generic and useful information about system operation, but avoid logging too much - # information to avoid inadvertent exposure of personally identifiable information (PII). - config.log_level = :info - - # Prepend all log lines with the following tags. - config.log_tags = [ :request_id ] - - # Use a different cache store in production. - # config.cache_store = :mem_cache_store - - # Use a real queuing backend for Active Job (and separate queues per environment). - # config.active_job.queue_adapter = :resque - # config.active_job.queue_name_prefix = "cborm_rails7b_production" - - config.action_mailer.perform_caching = false - - # Ignore bad email addresses and do not raise email delivery errors. - # Set this to true and configure the email server for immediate delivery to raise delivery errors. - # config.action_mailer.raise_delivery_errors = false - - # Enable locale fallbacks for I18n (makes lookups for any locale fall back to - # the I18n.default_locale when a translation cannot be found). - config.i18n.fallbacks = true - - # Don't log any deprecations. - config.active_support.report_deprecations = false - - # Use default logging formatter so that PID and timestamp are not suppressed. - config.log_formatter = ::Logger::Formatter.new - - # Use a different logger for distributed setups. - # require "syslog/logger" - # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new "app-name") - - if ENV["RAILS_LOG_TO_STDOUT"].present? - logger = ActiveSupport::Logger.new(STDOUT) - logger.formatter = config.log_formatter - config.logger = ActiveSupport::TaggedLogging.new(logger) - end - - # Do not dump schema after migrations. - config.active_record.dump_schema_after_migration = false -end diff --git a/rails7-ruby-client/config/environments/test.rb b/rails7-ruby-client/config/environments/test.rb deleted file mode 100644 index 6ea4d1e7..00000000 --- a/rails7-ruby-client/config/environments/test.rb +++ /dev/null @@ -1,60 +0,0 @@ -require "active_support/core_ext/integer/time" - -# The test environment is used exclusively to run your application's -# test suite. You never need to work with it otherwise. Remember that -# your test database is "scratch space" for the test suite and is wiped -# and recreated between test runs. Don't rely on the data there! - -Rails.application.configure do - # Settings specified here will take precedence over those in config/application.rb. - - # Turn false under Spring and add config.action_view.cache_template_loading = true. - config.cache_classes = true - - # Eager loading loads your whole application. When running a single test locally, - # this probably isn't necessary. It's a good idea to do in a continuous integration - # system, or in some way before deploying your code. - config.eager_load = ENV["CI"].present? - - # Configure public file server for tests with Cache-Control for performance. - config.public_file_server.enabled = true - config.public_file_server.headers = { - "Cache-Control" => "public, max-age=#{1.hour.to_i}" - } - - # Show full error reports and disable caching. - config.consider_all_requests_local = true - config.action_controller.perform_caching = false - config.cache_store = :null_store - - # Raise exceptions instead of rendering exception templates. - config.action_dispatch.show_exceptions = false - - # Disable request forgery protection in test environment. - config.action_controller.allow_forgery_protection = false - - # Store uploaded files on the local file system in a temporary directory. - config.active_storage.service = :test - - config.action_mailer.perform_caching = false - - # Tell Action Mailer not to deliver emails to the real world. - # The :test delivery method accumulates sent emails in the - # ActionMailer::Base.deliveries array. - config.action_mailer.delivery_method = :test - - # Print deprecation notices to the stderr. - config.active_support.deprecation = :stderr - - # Raise exceptions for disallowed deprecations. - config.active_support.disallowed_deprecation = :raise - - # Tell Active Support which deprecation messages to disallow. - config.active_support.disallowed_deprecation_warnings = [] - - # Raises error for missing translations. - # config.i18n.raise_on_missing_translations = true - - # Annotate rendered view with file names. - # config.action_view.annotate_rendered_view_with_filenames = true -end diff --git a/rails7-ruby-client/config/initializers/assets.rb b/rails7-ruby-client/config/initializers/assets.rb deleted file mode 100644 index 2eeef966..00000000 --- a/rails7-ruby-client/config/initializers/assets.rb +++ /dev/null @@ -1,12 +0,0 @@ -# Be sure to restart your server when you modify this file. - -# Version of your assets, change this if you want to expire all your assets. -Rails.application.config.assets.version = "1.0" - -# Add additional assets to the asset load path. -# Rails.application.config.assets.paths << Emoji.images_path - -# Precompile additional assets. -# application.js, application.css, and all non-JS/CSS in the app/assets -# folder are already added. -# Rails.application.config.assets.precompile += %w( admin.js admin.css ) diff --git a/rails7-ruby-client/config/initializers/content_security_policy.rb b/rails7-ruby-client/config/initializers/content_security_policy.rb deleted file mode 100644 index 54f47cf1..00000000 --- a/rails7-ruby-client/config/initializers/content_security_policy.rb +++ /dev/null @@ -1,25 +0,0 @@ -# Be sure to restart your server when you modify this file. - -# Define an application-wide content security policy. -# See the Securing Rails Applications Guide for more information: -# https://guides.rubyonrails.org/security.html#content-security-policy-header - -# Rails.application.configure do -# config.content_security_policy do |policy| -# policy.default_src :self, :https -# policy.font_src :self, :https, :data -# policy.img_src :self, :https, :data -# policy.object_src :none -# policy.script_src :self, :https -# policy.style_src :self, :https -# # Specify URI for violation reports -# # policy.report_uri "/csp-violation-report-endpoint" -# end -# -# # Generate session nonces for permitted importmap and inline scripts -# config.content_security_policy_nonce_generator = ->(request) { request.session.id.to_s } -# config.content_security_policy_nonce_directives = %w(script-src) -# -# # Report violations without enforcing the policy. -# # config.content_security_policy_report_only = true -# end diff --git a/rails7-ruby-client/config/initializers/filter_parameter_logging.rb b/rails7-ruby-client/config/initializers/filter_parameter_logging.rb deleted file mode 100644 index adc6568c..00000000 --- a/rails7-ruby-client/config/initializers/filter_parameter_logging.rb +++ /dev/null @@ -1,8 +0,0 @@ -# Be sure to restart your server when you modify this file. - -# Configure parameters to be filtered from the log file. Use this to limit dissemination of -# sensitive information. See the ActiveSupport::ParameterFilter documentation for supported -# notations and behaviors. -Rails.application.config.filter_parameters += [ - :passw, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn -] diff --git a/rails7-ruby-client/config/initializers/inflections.rb b/rails7-ruby-client/config/initializers/inflections.rb deleted file mode 100644 index 3860f659..00000000 --- a/rails7-ruby-client/config/initializers/inflections.rb +++ /dev/null @@ -1,16 +0,0 @@ -# Be sure to restart your server when you modify this file. - -# Add new inflection rules using the following format. Inflections -# are locale specific, and you may define rules for as many different -# locales as you wish. All of these examples are active by default: -# ActiveSupport::Inflector.inflections(:en) do |inflect| -# inflect.plural /^(ox)$/i, "\\1en" -# inflect.singular /^(ox)en/i, "\\1" -# inflect.irregular "person", "people" -# inflect.uncountable %w( fish sheep ) -# end - -# These inflection rules are supported but not enabled by default: -# ActiveSupport::Inflector.inflections(:en) do |inflect| -# inflect.acronym "RESTful" -# end diff --git a/rails7-ruby-client/config/initializers/permissions_policy.rb b/rails7-ruby-client/config/initializers/permissions_policy.rb deleted file mode 100644 index 00f64d71..00000000 --- a/rails7-ruby-client/config/initializers/permissions_policy.rb +++ /dev/null @@ -1,11 +0,0 @@ -# Define an application-wide HTTP permissions policy. For further -# information see https://developers.google.com/web/updates/2018/06/feature-policy -# -# Rails.application.config.permissions_policy do |f| -# f.camera :none -# f.gyroscope :none -# f.microphone :none -# f.usb :none -# f.fullscreen :self -# f.payment :self, "https://secure.example.com" -# end diff --git a/rails7-ruby-client/config/locales/en.yml b/rails7-ruby-client/config/locales/en.yml deleted file mode 100644 index 8ca56fc7..00000000 --- a/rails7-ruby-client/config/locales/en.yml +++ /dev/null @@ -1,33 +0,0 @@ -# Files in the config/locales directory are used for internationalization -# and are automatically loaded by Rails. If you want to use locales other -# than English, add the necessary files in this directory. -# -# To use the locales, use `I18n.t`: -# -# I18n.t "hello" -# -# In views, this is aliased to just `t`: -# -# <%= t("hello") %> -# -# To use a different locale, set it with `I18n.locale`: -# -# I18n.locale = :es -# -# This would use the information in config/locales/es.yml. -# -# The following keys must be escaped otherwise they will not be retrieved by -# the default I18n backend: -# -# true, false, on, off, yes, no -# -# Instead, surround them with single quotes. -# -# en: -# "true": "foo" -# -# To learn more, please read the Rails Internationalization guide -# available at https://guides.rubyonrails.org/i18n.html. - -en: - hello: "Hello world" diff --git a/rails7-ruby-client/config/puma.rb b/rails7-ruby-client/config/puma.rb deleted file mode 100644 index daaf0369..00000000 --- a/rails7-ruby-client/config/puma.rb +++ /dev/null @@ -1,43 +0,0 @@ -# Puma can serve each request in a thread from an internal thread pool. -# The `threads` method setting takes two numbers: a minimum and maximum. -# Any libraries that use thread pools should be configured to match -# the maximum value specified for Puma. Default is set to 5 threads for minimum -# and maximum; this matches the default thread size of Active Record. -# -max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 } -min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count } -threads min_threads_count, max_threads_count - -# Specifies the `worker_timeout` threshold that Puma will use to wait before -# terminating a worker in development environments. -# -worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development" - -# Specifies the `port` that Puma will listen on to receive requests; default is 3000. -# -port ENV.fetch("PORT") { 3000 } - -# Specifies the `environment` that Puma will run in. -# -environment ENV.fetch("RAILS_ENV") { "development" } - -# Specifies the `pidfile` that Puma will use. -pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" } - -# Specifies the number of `workers` to boot in clustered mode. -# Workers are forked web server processes. If using threads and workers together -# the concurrency of the application would be max `threads` * `workers`. -# Workers do not work on JRuby or Windows (both of which do not support -# processes). -# -# workers ENV.fetch("WEB_CONCURRENCY") { 2 } - -# Use the `preload_app!` method when specifying a `workers` number. -# This directive tells Puma to first boot the application and load code -# before forking the application. This takes advantage of Copy On Write -# process behavior so workers use less memory. -# -# preload_app! - -# Allow puma to be restarted by `bin/rails restart` command. -plugin :tmp_restart diff --git a/rails7-ruby-client/config/routes.rb b/rails7-ruby-client/config/routes.rb deleted file mode 100644 index 262ffd54..00000000 --- a/rails7-ruby-client/config/routes.rb +++ /dev/null @@ -1,6 +0,0 @@ -Rails.application.routes.draw do - # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html - - # Defines the root path route ("/") - # root "articles#index" -end diff --git a/rails7-ruby-client/config/storage.yml b/rails7-ruby-client/config/storage.yml deleted file mode 100644 index 4942ab66..00000000 --- a/rails7-ruby-client/config/storage.yml +++ /dev/null @@ -1,34 +0,0 @@ -test: - service: Disk - root: <%= Rails.root.join("tmp/storage") %> - -local: - service: Disk - root: <%= Rails.root.join("storage") %> - -# Use bin/rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key) -# amazon: -# service: S3 -# access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %> -# secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %> -# region: us-east-1 -# bucket: your_own_bucket-<%= Rails.env %> - -# Remember not to checkin your GCS keyfile to a repository -# google: -# service: GCS -# project: your_project -# credentials: <%= Rails.root.join("path/to/gcs.keyfile") %> -# bucket: your_own_bucket-<%= Rails.env %> - -# Use bin/rails credentials:edit to set the Azure Storage secret (as azure_storage:storage_access_key) -# microsoft: -# service: AzureStorage -# storage_account_name: your_account_name -# storage_access_key: <%= Rails.application.credentials.dig(:azure_storage, :storage_access_key) %> -# container: your_container_name-<%= Rails.env %> - -# mirror: -# service: Mirror -# primary: local -# mirrors: [ amazon, google, microsoft ] diff --git a/rails7-ruby-client/db/seeds.rb b/rails7-ruby-client/db/seeds.rb deleted file mode 100644 index bc25fce3..00000000 --- a/rails7-ruby-client/db/seeds.rb +++ /dev/null @@ -1,7 +0,0 @@ -# This file should contain all the record creation needed to seed the database with its default values. -# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup). -# -# Examples: -# -# movies = Movie.create([{ name: "Star Wars" }, { name: "Lord of the Rings" }]) -# Character.create(name: "Luke", movie: movies.first) diff --git a/rails7-ruby-client/lib/assets/.keep b/rails7-ruby-client/lib/assets/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/rails7-ruby-client/lib/tasks/.keep b/rails7-ruby-client/lib/tasks/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/rails7-ruby-client/log/.keep b/rails7-ruby-client/log/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/rails7-ruby-client/public/404.html b/rails7-ruby-client/public/404.html deleted file mode 100644 index 2be3af26..00000000 --- a/rails7-ruby-client/public/404.html +++ /dev/null @@ -1,67 +0,0 @@ - - - - The page you were looking for doesn't exist (404) - - - - - - -
-
-

The page you were looking for doesn't exist.

-

You may have mistyped the address or the page may have moved.

-
-

If you are the application owner check the logs for more information.

-
- - diff --git a/rails7-ruby-client/public/422.html b/rails7-ruby-client/public/422.html deleted file mode 100644 index c08eac0d..00000000 --- a/rails7-ruby-client/public/422.html +++ /dev/null @@ -1,67 +0,0 @@ - - - - The change you wanted was rejected (422) - - - - - - -
-
-

The change you wanted was rejected.

-

Maybe you tried to change something you didn't have access to.

-
-

If you are the application owner check the logs for more information.

-
- - diff --git a/rails7-ruby-client/public/500.html b/rails7-ruby-client/public/500.html deleted file mode 100644 index 78a030af..00000000 --- a/rails7-ruby-client/public/500.html +++ /dev/null @@ -1,66 +0,0 @@ - - - - We're sorry, but something went wrong (500) - - - - - - -
-
-

We're sorry, but something went wrong.

-
-

If you are the application owner check the logs for more information.

-
- - diff --git a/rails7-ruby-client/public/apple-touch-icon-precomposed.png b/rails7-ruby-client/public/apple-touch-icon-precomposed.png deleted file mode 100644 index e69de29b..00000000 diff --git a/rails7-ruby-client/public/apple-touch-icon.png b/rails7-ruby-client/public/apple-touch-icon.png deleted file mode 100644 index e69de29b..00000000 diff --git a/rails7-ruby-client/public/favicon.ico b/rails7-ruby-client/public/favicon.ico deleted file mode 100644 index e69de29b..00000000 diff --git a/rails7-ruby-client/public/robots.txt b/rails7-ruby-client/public/robots.txt deleted file mode 100644 index c19f78ab..00000000 --- a/rails7-ruby-client/public/robots.txt +++ /dev/null @@ -1 +0,0 @@ -# See https://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file diff --git a/rails7-ruby-client/storage/.keep b/rails7-ruby-client/storage/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/rails7-ruby-client/test/models/first_test.rb b/rails7-ruby-client/test/models/first_test.rb deleted file mode 100644 index 27820ba7..00000000 --- a/rails7-ruby-client/test/models/first_test.rb +++ /dev/null @@ -1,30 +0,0 @@ -ENV['RAILS_ENV'] ||= 'test' -require File.expand_path('../../../config/environment', __FILE__) -require 'rails/test_help' -require 'couchbase-orm' - -CouchbaseOrm.logger = Logger.new(STDOUT) - -class Post < CouchbaseOrm::Base - n1ql :all # => emits :id and will return all comments - attribute :title, type: String - attribute :body, type: String - attribute :draft, type: Boolean -end - -class FirstTest < ActiveSupport::TestCase - test "the truth" do - id = "newid_#{DateTime.now.strftime('%s')}" - p = Post.new(id: id, - title: 'Hello world', - draft: true) - p.save - retrieved_p = Post.find(id) - assert_equal retrieved_p.title, p.title - retrieved_p.body = 'Once upon the times....' - retrieved_p.save - - retrieved_p2 = Post.find(id) - assert_equal retrieved_p2.body, retrieved_p.body - end -end \ No newline at end of file From 1f07d6896c59e83c72e0f48eb8218a56ef613ee7 Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Thu, 23 Jun 2022 22:59:55 +0200 Subject: [PATCH 10/43] Fix has_many_spec.rb --- lib/couchbase-orm/associations.rb | 2 +- lib/couchbase-orm/n1ql.rb | 30 ++++++------- lib/couchbase-orm/utilities/has_many.rb | 9 +--- lib/couchbase-orm/views.rb | 57 ++++++++++++++----------- spec/has_many_spec.rb | 13 +++--- spec/views_spec.rb | 16 +++---- 6 files changed, 66 insertions(+), 61 deletions(-) diff --git a/lib/couchbase-orm/associations.rb b/lib/couchbase-orm/associations.rb index c0507f20..b893a484 100644 --- a/lib/couchbase-orm/associations.rb +++ b/lib/couchbase-orm/associations.rb @@ -181,7 +181,7 @@ def destroy_associations! when :destroy, :delete if model.respond_to?(:stream) model.stream { |mod| mod.__send__(dependent) } - elsif model.is_a?(Array) + elsif model.is_a?(Array) || model.is_a?(CouchbaseOrm::ResultsProxy) model.each { |m| m.__send__(dependent) } else model.__send__(dependent) diff --git a/lib/couchbase-orm/n1ql.rb b/lib/couchbase-orm/n1ql.rb index 79ebb6b8..7c157aec 100644 --- a/lib/couchbase-orm/n1ql.rb +++ b/lib/couchbase-orm/n1ql.rb @@ -31,11 +31,11 @@ module ClassMethods # n1ql :by_rating, emit_key: :rating # end # - # Post.by_rating.stream do |response| + # Post.by_rating do |response| # # ... # end # TODO: add range keys [:startkey, :endkey] - def n1ql(name, query: nil, emit_key: [], **options) + def n1ql(name, select: nil, emit_key: [], **options) emit_key = Array.wrap(emit_key) emit_key.each do |key| raise "unknown emit_key attribute for n1ql :#{name}, emit_key: :#{key}" if key && @attributes[key].nil? @@ -50,7 +50,7 @@ def n1ql(name, query: nil, emit_key: [], **options) singleton_class.__send__(:define_method, name) do |**opts, &result_modifier| opts = options.merge(opts).reverse_merge(scan_consistency: :request_plus) values = convert_values(opts.delete(:key)) - current_query = run_query(method_opts[:emit_key], values, query, **opts.except(:include_docs)) + current_query = run_query(method_opts[:emit_key], values, select: select, **opts.except(:include_docs)) if result_modifier opts[:include_docs] = true @@ -113,19 +113,17 @@ def build_limit(limit) limit ? "limit #{limit}" : "" end - def run_query(keys, values, query, descending: false, limit: nil, **options) - if query - query.call(bucket, values) - else - bucket_name = bucket.name - where = build_where(keys, values) - order = build_order(keys, descending) - limit = build_limit(limit) - n1ql_query = "select raw meta().id from `#{bucket_name}` where #{where} order by #{order} #{limit}" - result = cluster.query(n1ql_query, Couchbase::Options::Query.new(**options)) - CouchbaseOrm.logger.debug "N1QL query: #{n1ql_query} return #{result.rows.to_a.length} rows" - N1qlProxy.new(result) - end + def run_query(keys, values, select: nil, descending: false, limit: nil, **options) + bucket_name = bucket.name + where = build_where(keys, values) + order = build_order(keys, descending) + limit = build_limit(limit) + select ||= "raw meta().id" + raise "select must be a string" unless select.is_a?(String) + n1ql_query = "select #{select} from `#{bucket_name}` where #{where} order by #{order} #{limit}" + result = cluster.query(n1ql_query, Couchbase::Options::Query.new(**options)) + CouchbaseOrm.logger.debug "N1QL query: #{n1ql_query} return #{result.rows.to_a.length} rows" + N1qlProxy.new(result) end end end diff --git a/lib/couchbase-orm/utilities/has_many.rb b/lib/couchbase-orm/utilities/has_many.rb index 632c1ef7..3138e035 100644 --- a/lib/couchbase-orm/utilities/has_many.rb +++ b/lib/couchbase-orm/utilities/has_many.rb @@ -42,7 +42,7 @@ class #{class_name} < CouchbaseOrm::Base when :n1ql remote_klass.find(row) when :view - remote_klass.find(row.value[through_key]) + remote_klass.find(row[through_key]) else raise 'type is unknown' end @@ -93,12 +93,7 @@ def build_index_view(klass, remote_class, remote_method, through_key, foreign_ke def build_index_n1ql(klass, remote_class, remote_method, through_key, foreign_key) if remote_class klass.class_eval do - n1ql remote_method, query: proc { |bucket, values| - bucket_name = bucket.bucket - bucket.n1ql.select("raw #{through_key}") - .from("`#{bucket_name}`") - .where("type=\"#{design_document}\" and #{foreign_key} = #{values[0]}") - } + n1ql remote_method, select: "raw #{through_key}" end else klass.class_eval do diff --git a/lib/couchbase-orm/views.rb b/lib/couchbase-orm/views.rb index ad8ef33e..530554cf 100644 --- a/lib/couchbase-orm/views.rb +++ b/lib/couchbase-orm/views.rb @@ -19,7 +19,7 @@ module ClassMethods # view :by_rating, emit_key: :rating # end # - # Post.by_rating.stream do |response| + # Post.by_rating do |response| # # ... # end def view(name, map: nil, emit_key: nil, reduce: nil, **options) @@ -78,17 +78,14 @@ def view(name, map: nil, emit_key: nil, reduce: nil, **options) @views[name] = method_opts singleton_class.__send__(:define_method, name) do |**opts, &result_modifier| - opts = options.merge(opts) - + opts = options.merge(opts).reverse_merge(scan_consistency: :request_plus) + CouchbaseOrm.logger.debug("View [#{@design_document}, #{name.inspect}] options: #{opts.inspect}") if result_modifier - opts[:include_docs] = true - bucket.view(@design_document, name, **opts, &result_modifier) + include_docs(bucket.view_query(@design_document, name.to_s, Couchbase::Options::View.new(**opts.except(:include_docs)))).map(&result_modifier) elsif opts[:include_docs] - bucket.view(@design_document, name, **opts) { |row| - self.new(row) - } + include_docs(bucket.view_query(@design_document, name.to_s, Couchbase::Options::View.new(**opts.except(:include_docs)))) else - bucket.view(@design_document, name, **opts) + bucket.view_query(@design_document, name.to_s, Couchbase::Options::View.new(**opts.except(:include_docs))) end end end @@ -116,26 +113,28 @@ def ensure_design_document! update_required = false # Grab the existing view details - ddoc = bucket.design_docs[@design_document] - existing = ddoc.view_config if ddoc - + begin + ddoc = bucket.view_indexes.get_design_document(@design_document, :production) + rescue Couchbase::Error::DesignDocumentNotFound + end + existing = ddoc.views if ddoc views_actual = {} # Fill in the design documents @views.each do |name, document| - doc = document.dup - views_actual[name] = doc - doc[:map] = doc[:map].gsub('{{design_document}}', @design_document) if doc[:map] - doc[:reduce] = doc[:reduce].gsub('{{design_document}}', @design_document) if doc[:reduce] + views_actual[name.to_s] = Couchbase::Management::View.new( + document[:map]&.gsub('{{design_document}}', @design_document), + document[:reduce]&.gsub('{{design_document}}', @design_document) + ) end # Check there are no changes we need to apply views_actual.each do |name, desired| check = existing[name] if check - cmap = (check[:map] || '').gsub(/\s+/, '') - creduce = (check[:reduce] || '').gsub(/\s+/, '') - dmap = (desired[:map] || '').gsub(/\s+/, '') - dreduce = (desired[:reduce] || '').gsub(/\s+/, '') + cmap = (check.map || '').gsub(/\s+/, '') + creduce = (check.reduce || '').gsub(/\s+/, '') + dmap = (desired.map || '').gsub(/\s+/, '') + dreduce = (desired.reduce || '').gsub(/\s+/, '') unless cmap == dmap && creduce == dreduce update_required = true @@ -149,16 +148,26 @@ def ensure_design_document! # Updated the design document if update_required - bucket.save_design_doc({ - views: views_actual - }, @design_document) + document = Couchbase::Management::DesignDocument.new + document.views = views_actual + document.name = @design_document + bucket.view_indexes.upsert_design_document(document, :production) - puts "Couchbase views updated for #{self.name}, design doc: #{@design_document}" true else false end end + + def include_docs(view_result) + if view_result.rows.length > 1 + self.find(view_result.rows.map(&:id)) + elsif view_result.rows.length == 1 + [self.find(view_result.rows.first.id)] + else + [] + end + end end end end diff --git a/spec/has_many_spec.rb b/spec/has_many_spec.rb index 1e713305..b4069507 100644 --- a/spec/has_many_spec.rb +++ b/spec/has_many_spec.rb @@ -5,7 +5,6 @@ shared_examples "has_many example" do |parameter| before :all do @context = parameter[:context].to_s - @rating_test_class = Kernel.const_get("Rating#{@context.camelize}Test".classify) @object_test_class = Kernel.const_get("Object#{@context.camelize}Test".classify) @object_rating_test_class = Kernel.const_get("ObjectRating#{@context.camelize}Test".classify) @@ -13,12 +12,16 @@ @rating_test_class.ensure_design_document! @object_test_class.ensure_design_document! @object_rating_test_class.ensure_design_document! + + @rating_test_class.all.each(&:destroy) + @object_test_class.all.each(&:destroy) + @object_rating_test_class.all.each(&:destroy) end after :each do - @rating_test_class.all.stream(&:delete) - @object_test_class.all.stream(&:delete) - @object_rating_test_class.all.stream(&:delete) + @rating_test_class.all.each(&:destroy) + @object_test_class.all.each(&:destroy) + @object_rating_test_class.all.each(&:destroy) end it "should return matching results" do @@ -38,7 +41,7 @@ expect(docs).to match_array([1, 2]) first.destroy - expect { @rating_test_class.find rate.id }.to raise_error(::MTLibcouchbase::Error::KeyNotFound) + expect { @rating_test_class.find rate.id }.to raise_error(Couchbase::Error::DocumentNotFound) expect(@rating_test_class.all.count).to be(1) end diff --git a/spec/views_spec.rb b/spec/views_spec.rb index 22ed7bb7..337ad188 100644 --- a/spec/views_spec.rb +++ b/spec/views_spec.rb @@ -18,11 +18,14 @@ class ViewTest < CouchbaseOrm::Base describe CouchbaseOrm::Views do + before(:each) do + ViewTest.all.each(&:destroy) + end + after(:each) do + ViewTest.all.each(&:destroy) + end it "should save a new design document" do - begin - ViewTest.bucket.delete_design_doc(ViewTest.design_document) - rescue MTLibcouchbase::Error::HttpResponseError - end + ViewTest.bucket.view_indexes.drop_design_document(ViewTest.design_document, :production) expect(ViewTest.ensure_design_document!).to be(true) end @@ -47,7 +50,7 @@ class ViewTest < CouchbaseOrm::Base ViewTest.create! name: :jane, rating: :awesome ViewTest.create! name: :greg, rating: :bad - docs = ViewTest.by_rating(descending: :true).collect { |ob| + docs = ViewTest.by_rating(order: :descending).collect { |ob| ob.destroy ob.name } @@ -64,9 +67,6 @@ class ViewTest < CouchbaseOrm::Base docs = ViewTest.find_by_rating(1).collect { |ob| ob.name } - ViewTest.all.stream { |ob| - ob.destroy - } expect(Set.new(docs)).to eq(Set.new(['bob', 'jane'])) end From 797c018b72f33a5f0826a3cd6f3bb536a58ddf92 Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Fri, 24 Jun 2022 10:28:00 +0200 Subject: [PATCH 11/43] Add coverage report --- couchbase-orm.gemspec | 3 ++- spec/persistence_spec.rb | 6 +----- spec/support.rb | 9 ++++++++- spec/views_spec.rb | 8 +++++++- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/couchbase-orm.gemspec b/couchbase-orm.gemspec index 76508b60..79eba3a5 100644 --- a/couchbase-orm.gemspec +++ b/couchbase-orm.gemspec @@ -20,7 +20,8 @@ Gem::Specification.new do |gem| gem.add_development_dependency 'rake', '~> 12.2' gem.add_development_dependency 'rspec', '~> 3.7' gem.add_development_dependency 'yard', '~> 0.9' - gem.add_development_dependency 'minitest', '~> 5.10' + gem.add_development_dependency 'pry' + gem.add_development_dependency 'simplecov' gem.files = `git ls-files`.split("\n") gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") diff --git a/spec/persistence_spec.rb b/spec/persistence_spec.rb index 033b1972..71052bc3 100644 --- a/spec/persistence_spec.rb +++ b/spec/persistence_spec.rb @@ -205,11 +205,7 @@ class ModelWithValidations < CouchbaseOrm::Base expect(model.save!).to be(model) # coercion will fail here - begin - model.age = 'a23' - expect(false).to be(true) - rescue ArgumentError => e - end + expect{ model.age = "a23" }.to raise_error(ArgumentError) model.destroy end diff --git a/spec/support.rb b/spec/support.rb index 1314cfef..b8d89104 100644 --- a/spec/support.rb +++ b/spec/support.rb @@ -1,9 +1,16 @@ # frozen_string_literal: true, encoding: ASCII-8BIT - +require 'simplecov' require 'couchbase-orm' require 'minitest/assertions' require 'active_model/lint' +SimpleCov.start do + add_group 'Core', [/lib\/couchbase-orm\/(?!(proxies|utilities))/, 'lib/couchbase-orm.rb'] + add_group 'Proxies', 'lib/couchbase-orm/proxies' + add_group 'Utilities', 'lib/couchbase-orm/utilities' + add_group 'Specs', 'spec' + minimum_coverage 93 +end shared_examples_for "ActiveModel" do include Minitest::Assertions diff --git a/spec/views_spec.rb b/spec/views_spec.rb index 337ad188..1db0205d 100644 --- a/spec/views_spec.rb +++ b/spec/views_spec.rb @@ -21,11 +21,17 @@ class ViewTest < CouchbaseOrm::Base before(:each) do ViewTest.all.each(&:destroy) end + after(:each) do ViewTest.all.each(&:destroy) end + it "should save a new design document" do - ViewTest.bucket.view_indexes.drop_design_document(ViewTest.design_document, :production) + begin + ViewTest.bucket.view_indexes.drop_design_document(ViewTest.design_document, :production) + rescue Couchbase::Error::InternalServerFailure + # ignore if design document does not exist + end expect(ViewTest.ensure_design_document!).to be(true) end From 9308902f8a84108cc4da595c8ae3423bd149d0a3 Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Fri, 24 Jun 2022 10:35:53 +0200 Subject: [PATCH 12/43] Try to update to ubuntu 20.04 and couchbase 7.1 --- .github/workflows/test.yml | 2 +- ci/run_couchbase.sh | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index fd8c3834..5de174cf 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -20,7 +20,7 @@ jobs: - ruby: '2.6' gemfile: '7.0.0' fail-fast: false - runs-on: ubuntu-18.04 + runs-on: ubuntu-20.04 name: ${{ matrix.ruby }} ${{ matrix.database }} rails-${{ matrix.gemfile }} steps: - uses: actions/checkout@v2 diff --git a/ci/run_couchbase.sh b/ci/run_couchbase.sh index 8100453a..791737bd 100755 --- a/ci/run_couchbase.sh +++ b/ci/run_couchbase.sh @@ -1,8 +1,8 @@ set -x set -e apt-get install libev-dev python-httplib2 libssl1.0.0 -wget https://packages.couchbase.com/releases/5.1.0/couchbase-server-enterprise_5.1.0-ubuntu14.04_amd64.deb -dpkg -i couchbase-server-enterprise_5.1.0-ubuntu14.04_amd64.deb +wget https://packages.couchbase.com/releases/7.1.0/couchbase-server-enterprise_7.1.0-ubuntu20.04_amd64.deb +dpkg -i couchbase-server-enterprise_7.1.0-ubuntu20.04_amd64.deb sleep 8 sudo service couchbase-server status /opt/couchbase/bin/couchbase-cli cluster-init -c 127.0.0.1:8091 --cluster-username=admin --cluster-password=password --cluster-ramsize=320 --cluster-index-ramsize=256 --cluster-fts-ramsize=256 --services=data,index,query,fts From 73c320e2afa7b0709e5d068eda87637001b18f03 Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Fri, 24 Jun 2022 10:37:28 +0200 Subject: [PATCH 13/43] without old ssl --- ci/run_couchbase.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/run_couchbase.sh b/ci/run_couchbase.sh index 791737bd..f7181859 100755 --- a/ci/run_couchbase.sh +++ b/ci/run_couchbase.sh @@ -1,6 +1,6 @@ set -x set -e -apt-get install libev-dev python-httplib2 libssl1.0.0 +apt-get install libev-dev python-httplib2 wget https://packages.couchbase.com/releases/7.1.0/couchbase-server-enterprise_7.1.0-ubuntu20.04_amd64.deb dpkg -i couchbase-server-enterprise_7.1.0-ubuntu20.04_amd64.deb sleep 8 From 857535050cfff88ce0e9f70660c0e792151d7ecf Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Fri, 24 Jun 2022 10:45:18 +0200 Subject: [PATCH 14/43] regroup apt operations --- .github/workflows/test.yml | 2 +- ci/run_couchbase.sh | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5de174cf..4c309aa9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -24,7 +24,7 @@ jobs: name: ${{ matrix.ruby }} ${{ matrix.database }} rails-${{ matrix.gemfile }} steps: - uses: actions/checkout@v2 - - run: sudo apt-get update && sudo apt-get install libevent-dev + - run: sudo apt-get update && sudo apt-get install libevent-dev libev-dev python-httplib2 - uses: ruby/setup-ruby@v1 with: ruby-version: ${{ matrix.ruby }} diff --git a/ci/run_couchbase.sh b/ci/run_couchbase.sh index f7181859..b4f783c4 100755 --- a/ci/run_couchbase.sh +++ b/ci/run_couchbase.sh @@ -1,6 +1,5 @@ set -x set -e -apt-get install libev-dev python-httplib2 wget https://packages.couchbase.com/releases/7.1.0/couchbase-server-enterprise_7.1.0-ubuntu20.04_amd64.deb dpkg -i couchbase-server-enterprise_7.1.0-ubuntu20.04_amd64.deb sleep 8 From 076c04dc86b26cbd5ee4d5f9069e678bf4e2a972 Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Fri, 24 Jun 2022 11:07:32 +0200 Subject: [PATCH 15/43] Remove breaking refs to MTlibcouchbase --- lib/couchbase-orm.rb | 3 --- lib/couchbase-orm/base.rb | 16 ---------------- lib/couchbase-orm/connection.rb | 1 - 3 files changed, 20 deletions(-) diff --git a/lib/couchbase-orm.rb b/lib/couchbase-orm.rb index e047eceb..4c6257a6 100644 --- a/lib/couchbase-orm.rb +++ b/lib/couchbase-orm.rb @@ -1,8 +1,5 @@ # frozen_string_literal: true, encoding: ASCII-8BIT -require 'mt-libcouchbase' -MTLibcouchbase.autoload(:QueryN1QL, 'ext/query_n1ql') - module CouchbaseOrm autoload :Error, 'couchbase-orm/error' autoload :Connection, 'couchbase-orm/connection' diff --git a/lib/couchbase-orm/base.rb b/lib/couchbase-orm/base.rb index 8171854d..21253b35 100644 --- a/lib/couchbase-orm/base.rb +++ b/lib/couchbase-orm/base.rb @@ -169,22 +169,6 @@ def initialize(model = nil, ignore_doc_type: false, **attributes) # This ensures that defaults are applied @__attributes__.merge! doc - when ::MTLibcouchbase::Response - puts "MTLibcouchbase::Response" - doc = model.value || raise('empty response provided') - type = doc.delete(:type) - doc.delete(:id) - - if type && !ignore_doc_type && type.to_s != self.class.design_document - raise "document type mismatch, #{type} != #{self.class.design_document}" - end - - @__metadata__.key = model.key - @__metadata__.cas = model.cas - - # This ensures that defaults are applied - @__attributes__.merge! doc - clear_changes_information when CouchbaseOrm::Base puts "CouchbaseOrm::Base" clear_changes_information diff --git a/lib/couchbase-orm/connection.rb b/lib/couchbase-orm/connection.rb index daf3435d..7fa120ef 100644 --- a/lib/couchbase-orm/connection.rb +++ b/lib/couchbase-orm/connection.rb @@ -1,5 +1,4 @@ -require 'mt-libcouchbase' require 'couchbase' module CouchbaseOrm From 6d44b855c707050c4c4e3d0305b8e7a63c133535 Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Fri, 24 Jun 2022 11:11:04 +0200 Subject: [PATCH 16/43] handle ci auth --- lib/couchbase-orm/connection.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/couchbase-orm/connection.rb b/lib/couchbase-orm/connection.rb index 7fa120ef..5196c0ff 100644 --- a/lib/couchbase-orm/connection.rb +++ b/lib/couchbase-orm/connection.rb @@ -11,7 +11,11 @@ class << self def self.cluster @cluster ||= begin options = Couchbase::Cluster::ClusterOptions.new - options.authenticate("cb_admin", "cb_admin_pwd") + if ENV["TRAVIS_TEST"] + options.authenticate("tester", "password123") + else + options.authenticate("cb_admin", "cb_admin_pwd") + end cluster = Couchbase::Cluster.connect('couchbase://127.0.0.1', options) end From 662706087e829db905b45d5e3777a16bbde1fd5d Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Fri, 24 Jun 2022 11:16:45 +0200 Subject: [PATCH 17/43] Fix flaky --- spec/support.rb | 2 +- spec/views_spec.rb | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/support.rb b/spec/support.rb index b8d89104..68cec06a 100644 --- a/spec/support.rb +++ b/spec/support.rb @@ -9,7 +9,7 @@ add_group 'Proxies', 'lib/couchbase-orm/proxies' add_group 'Utilities', 'lib/couchbase-orm/utilities' add_group 'Specs', 'spec' - minimum_coverage 93 + minimum_coverage 94 end shared_examples_for "ActiveModel" do diff --git a/spec/views_spec.rb b/spec/views_spec.rb index 1db0205d..e2044f5a 100644 --- a/spec/views_spec.rb +++ b/spec/views_spec.rb @@ -24,6 +24,8 @@ class ViewTest < CouchbaseOrm::Base after(:each) do ViewTest.all.each(&:destroy) + rescue Couchbase::Error::InternalServerFailure + # ignore end it "should save a new design document" do From cf5c88cf566e7c9d5935bffdf59dd77d085f4cf9 Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Fri, 24 Jun 2022 11:18:39 +0200 Subject: [PATCH 18/43] Use default bucket --- lib/couchbase-orm/connection.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/couchbase-orm/connection.rb b/lib/couchbase-orm/connection.rb index 5196c0ff..5ea9d9c8 100644 --- a/lib/couchbase-orm/connection.rb +++ b/lib/couchbase-orm/connection.rb @@ -22,7 +22,7 @@ def self.cluster end def self.bucket - @bucket ||= cluster.bucket("billeo-db-bucket") + @bucket ||= cluster.bucket("default") end end end From 5bfb1b0b08ffa1d54890218b6e5ade8e9a6b7268 Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Fri, 24 Jun 2022 11:24:34 +0200 Subject: [PATCH 19/43] Ignore more errors on cleanup --- spec/views_spec.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/views_spec.rb b/spec/views_spec.rb index e2044f5a..3b520204 100644 --- a/spec/views_spec.rb +++ b/spec/views_spec.rb @@ -20,12 +20,14 @@ class ViewTest < CouchbaseOrm::Base describe CouchbaseOrm::Views do before(:each) do ViewTest.all.each(&:destroy) + rescue Couchbase::Error::DesignDocumentNotFound + # ignore (FIXME: check before merge) end after(:each) do ViewTest.all.each(&:destroy) rescue Couchbase::Error::InternalServerFailure - # ignore + # ignore (FIXME: check before merge) end it "should save a new design document" do From a82197edb3373ade67a16d1773e5f8ccbc1965c1 Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Fri, 24 Jun 2022 11:51:37 +0200 Subject: [PATCH 20/43] more rescue --- spec/views_spec.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/spec/views_spec.rb b/spec/views_spec.rb index 3b520204..57af27a2 100644 --- a/spec/views_spec.rb +++ b/spec/views_spec.rb @@ -21,13 +21,15 @@ class ViewTest < CouchbaseOrm::Base before(:each) do ViewTest.all.each(&:destroy) rescue Couchbase::Error::DesignDocumentNotFound - # ignore (FIXME: check before merge) + # ignore (FIXME: check before merge) mainly because if there is nothing in all we should not have an error end after(:each) do ViewTest.all.each(&:destroy) rescue Couchbase::Error::InternalServerFailure # ignore (FIXME: check before merge) + rescue Couchbase::Error::DesignDocumentNotFound + # ignore (FIXME: check before merge) (7.1) end it "should save a new design document" do @@ -35,6 +37,8 @@ class ViewTest < CouchbaseOrm::Base ViewTest.bucket.view_indexes.drop_design_document(ViewTest.design_document, :production) rescue Couchbase::Error::InternalServerFailure # ignore if design document does not exist + rescue Couchbase::Error::DesignDocumentNotFound + # ignore if design document does not exist (7.1) end expect(ViewTest.ensure_design_document!).to be(true) end @@ -42,6 +46,10 @@ class ViewTest < CouchbaseOrm::Base it "should not re-save a design doc if nothing has changed" do expect(ViewTest.ensure_design_document!).to be(false) end + + it "should return an empty array when there is no objects" do + expect(ViewTest.all).to eq([]) + end it "should perform a map-reduce and return the view" do ViewTest.ensure_design_document! From 75b1e2a4d21d43045ed05d0b72f6ce79dd3eac2d Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Fri, 24 Jun 2022 14:29:46 +0200 Subject: [PATCH 21/43] Fix for ruby 2.6 --- lib/couchbase-orm/persistence.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/couchbase-orm/persistence.rb b/lib/couchbase-orm/persistence.rb index 0b4bde00..6e30e820 100644 --- a/lib/couchbase-orm/persistence.rb +++ b/lib/couchbase-orm/persistence.rb @@ -239,7 +239,7 @@ def _update_record(with_cas: false, **options) _id = @__metadata__.key options[:cas] = @__metadata__.cas if with_cas CouchbaseOrm.logger.debug { "_update_record - replace #{_id} #{@__attributes__.to_s.truncate(200)}" } - resp = self.class.collection.replace(_id, @__attributes__, **options) + resp = self.class.collection.replace(_id, @__attributes__, Couchbase::Options::Replace.new(**options)) # Ensure the model is up to date @__metadata__.key = _id @@ -263,7 +263,7 @@ def _create_record(**options) CouchbaseOrm.logger.debug { "_create_record - Upsert #{_id} #{@__attributes__.to_s.truncate(200)}" } #resp = self.class.collection.add(_id, @__attributes__, **options) - resp = self.class.collection.upsert(_id, @__attributes__, **options) + resp = self.class.collection.upsert(_id, @__attributes__, Couchbase::Options::Upsert.new(**options)) # Ensure the model is up to date @__metadata__.key = _id From 6c42d4d2dde91f953b6aabbcc2a0cd2afbfe6b40 Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Fri, 24 Jun 2022 15:08:45 +0200 Subject: [PATCH 22/43] Remove puts except in railtie --- lib/couchbase-orm/base.rb | 7 +++---- lib/couchbase-orm/utilities/has_many.rb | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/couchbase-orm/base.rb b/lib/couchbase-orm/base.rb index 21253b35..4b0e20d7 100644 --- a/lib/couchbase-orm/base.rb +++ b/lib/couchbase-orm/base.rb @@ -153,7 +153,7 @@ def initialize(model = nil, ignore_doc_type: false, **attributes) if model case model when Couchbase::Collection::GetResult - puts "Couchbase::Collection::GetResult" + CouchbaseORM.logger.debug "Initialize with Couchbase::Collection::GetResult" doc = model.content || raise('empty response provided') type = doc.delete('type') doc.delete(:id) @@ -165,12 +165,11 @@ def initialize(model = nil, ignore_doc_type: false, **attributes) @__metadata__.key = attributes[:id] @__metadata__.cas = model.cas - puts "@__metadata__ : #{@__metadata__}" - # This ensures that defaults are applied @__attributes__.merge! doc when CouchbaseOrm::Base - puts "CouchbaseOrm::Base" + CouchbaseORM.logger.debug "Initialize with CouchbaseOrm::Base" + clear_changes_information attributes = model.attributes attributes.delete(:id) diff --git a/lib/couchbase-orm/utilities/has_many.rb b/lib/couchbase-orm/utilities/has_many.rb index 3138e035..9773599c 100644 --- a/lib/couchbase-orm/utilities/has_many.rb +++ b/lib/couchbase-orm/utilities/has_many.rb @@ -17,8 +17,8 @@ def has_many(model, class_name: nil, foreign_key: nil, through: nil, through_cla klass = begin class_name.constantize - rescue NameError => e - puts "WARNING: #{class_name} referenced in #{self.name} before it was aded" + rescue NameError + CouchbaseORM.logger.warn "WARNING: #{class_name} referenced in #{self.name} before it was aded" # Open the class early - load order will have to be changed to prevent this. # Warning notice required as a misspelling will not raise an error From 8175236fb8bb9ec5969979fad7dadfb923505b17 Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Fri, 24 Jun 2022 15:10:58 +0200 Subject: [PATCH 23/43] Fix typo --- lib/couchbase-orm/base.rb | 4 ++-- lib/couchbase-orm/utilities/has_many.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/couchbase-orm/base.rb b/lib/couchbase-orm/base.rb index 4b0e20d7..af334f1f 100644 --- a/lib/couchbase-orm/base.rb +++ b/lib/couchbase-orm/base.rb @@ -153,7 +153,7 @@ def initialize(model = nil, ignore_doc_type: false, **attributes) if model case model when Couchbase::Collection::GetResult - CouchbaseORM.logger.debug "Initialize with Couchbase::Collection::GetResult" + CouchbaseOrm.logger.debug "Initialize with Couchbase::Collection::GetResult" doc = model.content || raise('empty response provided') type = doc.delete('type') doc.delete(:id) @@ -168,7 +168,7 @@ def initialize(model = nil, ignore_doc_type: false, **attributes) # This ensures that defaults are applied @__attributes__.merge! doc when CouchbaseOrm::Base - CouchbaseORM.logger.debug "Initialize with CouchbaseOrm::Base" + CouchbaseOrm.logger.debug "Initialize with CouchbaseOrm::Base" clear_changes_information attributes = model.attributes diff --git a/lib/couchbase-orm/utilities/has_many.rb b/lib/couchbase-orm/utilities/has_many.rb index 9773599c..1bead6ff 100644 --- a/lib/couchbase-orm/utilities/has_many.rb +++ b/lib/couchbase-orm/utilities/has_many.rb @@ -18,7 +18,7 @@ def has_many(model, class_name: nil, foreign_key: nil, through: nil, through_cla klass = begin class_name.constantize rescue NameError - CouchbaseORM.logger.warn "WARNING: #{class_name} referenced in #{self.name} before it was aded" + warn "WARNING: #{class_name} referenced in #{self.name} before it was aded" # Open the class early - load order will have to be changed to prevent this. # Warning notice required as a misspelling will not raise an error From 60e28660605fcc87d79587622ad0555d1f4c56a1 Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Fri, 24 Jun 2022 16:28:30 +0200 Subject: [PATCH 24/43] Handle empty find --- lib/couchbase-orm/associations.rb | 9 ++++++--- lib/couchbase-orm/base.rb | 9 +++------ lib/couchbase-orm/error.rb | 1 + 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/couchbase-orm/associations.rb b/lib/couchbase-orm/associations.rb index b893a484..0d21f94c 100644 --- a/lib/couchbase-orm/associations.rb +++ b/lib/couchbase-orm/associations.rb @@ -75,12 +75,15 @@ def has_and_belongs_to_many(name, **options) # Define reader define_method(name) do return instance_variable_get(instance_var) if instance_variable_defined?(instance_var) + ref_value = self.send(ref) + ref_value = nil if ref_value.respond_to?(:empty?) && ref_value.empty? + val = if options[:polymorphic] - ::CouchbaseOrm.try_load(self.send(ref)) + ::CouchbaseOrm.try_load(ref_value) if ref_value else - assoc.constantize.find(self.send(ref), quiet: true) + assoc.constantize.find(ref_value) if ref_value end - val = Array.wrap(val) + val = Array.wrap(val || []) instance_variable_set(instance_var, val) val end diff --git a/lib/couchbase-orm/base.rb b/lib/couchbase-orm/base.rb index af334f1f..185d7ad6 100644 --- a/lib/couchbase-orm/base.rb +++ b/lib/couchbase-orm/base.rb @@ -100,18 +100,15 @@ def attributes @attributes ||= {} end - def find(*ids, **options) + def find(*ids, quiet: false) CouchbaseOrm.logger.debug { "Base.find(l##{ids.length}) #{ids}" } - options[:extended] = true - options[:quiet] ||= false ids = ids.flatten.select { |id| id.present? } if ids.empty? - return nil if options[:quiet] - raise MTLibcouchbase::Error::EmptyKey, 'no id(s) provided' + raise CouchbaseOrm::Error::EmptyNotAllowed, 'no id(s) provided' end - records = options[:quiet] ? collection.get_multi(ids) : collection.get_multi!(ids) + records = quiet ? collection.get_multi(ids) : collection.get_multi!(ids) CouchbaseOrm.logger.debug { "Base.find found(#{records})" } records = records.zip(ids).map { |record, id| self.new(record, id: id) if record diff --git a/lib/couchbase-orm/error.rb b/lib/couchbase-orm/error.rb index 1e63587c..6aa7ebcc 100644 --- a/lib/couchbase-orm/error.rb +++ b/lib/couchbase-orm/error.rb @@ -24,5 +24,6 @@ def initialize(message = nil, record = nil) end class TypeMismatchError < Error; end class RecordExists < Error; end + class CouchbaseOrm::Error::EmptyNotAllowed < Error; end end end From ef3ef32ee363d47d7db6c506cb94d90bc56ca8cc Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Fri, 24 Jun 2022 16:58:18 +0200 Subject: [PATCH 25/43] 100% coverage on utilities/enum --- spec/index_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/index_spec.rb b/spec/index_spec.rb index 7c6c7576..cf4436e3 100644 --- a/spec/index_spec.rb +++ b/spec/index_spec.rb @@ -19,6 +19,7 @@ class NoUniqueIndexTest < CouchbaseOrm::Base class EnumTest < CouchbaseOrm::Base enum visibility: [:group, :authority, :public], default: :authority + enum color: [:red, :green, :blue] end @@ -98,6 +99,10 @@ class EnumTest < CouchbaseOrm::Base enum = EnumTest.create! expect(enum.visibility).to eq(2) enum.destroy + + # Test default default + enum = EnumTest.create! + expect(enum.color).to eq(1) end it "should not overwrite index's that do not belong to the current model" do From 8add1c092914306ad249d487802a02214d2e682e Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Mon, 27 Jun 2022 13:02:41 +0200 Subject: [PATCH 26/43] More coverage on collection proxy --- lib/couchbase-orm/proxies/collection_proxy.rb | 6 ++++-- spec/collection_proxy_spec.rb | 11 +++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/couchbase-orm/proxies/collection_proxy.rb b/lib/couchbase-orm/proxies/collection_proxy.rb index 9c9cae63..9ddf54c6 100644 --- a/lib/couchbase-orm/proxies/collection_proxy.rb +++ b/lib/couchbase-orm/proxies/collection_proxy.rb @@ -1,3 +1,5 @@ +require "couchbase" + module CouchbaseOrm class CollectionProxy @@ -42,8 +44,8 @@ def initialize(proxyfied) def method_missing(name, *args, **options, &block) @proxyfied.public_send(name, *args, **options, &block) end - else - def method_missing(name, *args, &block) + else # :nocov: + def method_missing(name, *args, &block) @proxyfied.public_send(name, *args, &block) end end diff --git a/spec/collection_proxy_spec.rb b/spec/collection_proxy_spec.rb index 4c110b31..c6030968 100644 --- a/spec/collection_proxy_spec.rb +++ b/spec/collection_proxy_spec.rb @@ -5,6 +5,9 @@ class Proxyfied def get(key, options = nil) raise Couchbase::Error::DocumentNotFound end + def remove(key, options = nil) + raise Couchbase::Error::DocumentNotFound + end end describe CouchbaseOrm::CollectionProxy do @@ -15,4 +18,12 @@ def get(key, options = nil) it "should not raise an error when get is called with non bang version" do expect { CouchbaseOrm::CollectionProxy.new(Proxyfied.new).get('key') }.to_not raise_error end + + it "should raise an error when remove is called with bang version" do + expect { CouchbaseOrm::CollectionProxy.new(Proxyfied.new).remove!('key') }.to raise_error(Couchbase::Error::DocumentNotFound) + end + + it "should not raise an error when remove is called with non bang version" do + expect { CouchbaseOrm::CollectionProxy.new(Proxyfied.new).remove('key') }.to_not raise_error + end end From fa065bd9165965c9826061000bc6fbd7b31bc3a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Couton?= Date: Fri, 1 Jul 2022 12:09:07 +0200 Subject: [PATCH 27/43] remove MTLibcouchbase link + add temp env var for couchbase connection --- lib/couchbase-orm/connection.rb | 4 ++-- lib/couchbase-orm/railtie.rb | 4 ---- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/couchbase-orm/connection.rb b/lib/couchbase-orm/connection.rb index 5ea9d9c8..8f649a42 100644 --- a/lib/couchbase-orm/connection.rb +++ b/lib/couchbase-orm/connection.rb @@ -14,7 +14,7 @@ def self.cluster if ENV["TRAVIS_TEST"] options.authenticate("tester", "password123") else - options.authenticate("cb_admin", "cb_admin_pwd") + options.authenticate(ENV["COUCHBASE_USER"], ENV["COUCHBASE_PASSWORD"]) end cluster = Couchbase::Cluster.connect('couchbase://127.0.0.1', options) @@ -22,7 +22,7 @@ def self.cluster end def self.bucket - @bucket ||= cluster.bucket("default") + @bucket ||= cluster.bucket(ENV["COUCHBASE_BUCKET"]) end end end diff --git a/lib/couchbase-orm/railtie.rb b/lib/couchbase-orm/railtie.rb index e3ff6ec2..c1071aef 100644 --- a/lib/couchbase-orm/railtie.rb +++ b/lib/couchbase-orm/railtie.rb @@ -37,10 +37,6 @@ class Railtie < Rails::Railtie #:nodoc: # @return [Hash] rescued responses def self.rescue_responses { - 'MTLibcouchbase::Error::KeyNotFound' => :not_found, - 'MTLibcouchbase::Error::NotStored' => :unprocessable_entity, - MTLibcouchbase::Error::KeyNotFound => :not_found, - MTLibcouchbase::Error::NotStored => :unprocessable_entity } end From 2f254ebe0c2e3bf02d1ddd9b4c451a000b950a38 Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Thu, 7 Jul 2022 14:41:31 +0200 Subject: [PATCH 28/43] CI Fix, use env vars to setup test base and tests --- ci/run_couchbase.sh | 6 +++--- lib/couchbase-orm/connection.rb | 7 +------ 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/ci/run_couchbase.sh b/ci/run_couchbase.sh index b4f783c4..6776623a 100755 --- a/ci/run_couchbase.sh +++ b/ci/run_couchbase.sh @@ -7,7 +7,7 @@ sudo service couchbase-server status /opt/couchbase/bin/couchbase-cli cluster-init -c 127.0.0.1:8091 --cluster-username=admin --cluster-password=password --cluster-ramsize=320 --cluster-index-ramsize=256 --cluster-fts-ramsize=256 --services=data,index,query,fts sleep 5 /opt/couchbase/bin/couchbase-cli server-info -c 127.0.0.1:8091 -u admin -p password -/opt/couchbase/bin/couchbase-cli bucket-create -c 127.0.0.1:8091 -u admin -p password --bucket=default --bucket-type=couchbase --bucket-ramsize=160 --bucket-replica=0 --wait +/opt/couchbase/bin/couchbase-cli bucket-create -c 127.0.0.1:8091 -u admin -p password --bucket=${COUCHBASE_BUCKET} --bucket-type=couchbase --bucket-ramsize=160 --bucket-replica=0 --wait sleep 1 -/opt/couchbase/bin/couchbase-cli user-manage -c 127.0.0.1:8091 -u admin -p password --set --rbac-username tester --rbac-password password123 --rbac-name "Auto Tester" --roles admin --auth-domain local -curl http://admin:password@localhost:8093/query/service -d 'statement=CREATE INDEX `default_type` ON `default`(`type`)' +/opt/couchbase/bin/couchbase-cli user-manage -c 127.0.0.1:8091 -u admin -p password --set --rbac-username ${COUCHBASE_USER} --rbac-password ${COUCHBASE_PASSWORD} --rbac-name "Auto Tester" --roles admin --auth-domain local +curl http://admin:password@localhost:8093/query/service -d "statement=CREATE INDEX `default_type` ON `${COUCHBASE_BUCKET}`(`type`)" diff --git a/lib/couchbase-orm/connection.rb b/lib/couchbase-orm/connection.rb index 8f649a42..9886c640 100644 --- a/lib/couchbase-orm/connection.rb +++ b/lib/couchbase-orm/connection.rb @@ -11,12 +11,7 @@ class << self def self.cluster @cluster ||= begin options = Couchbase::Cluster::ClusterOptions.new - if ENV["TRAVIS_TEST"] - options.authenticate("tester", "password123") - else - options.authenticate(ENV["COUCHBASE_USER"], ENV["COUCHBASE_PASSWORD"]) - end - + options.authenticate(ENV["COUCHBASE_USER"], ENV["COUCHBASE_PASSWORD"]) cluster = Couchbase::Cluster.connect('couchbase://127.0.0.1', options) end end From 2c52505b05fed4cd90432ba73882e0ae3e3fda16 Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Thu, 7 Jul 2022 14:46:02 +0200 Subject: [PATCH 29/43] Add env vars in CI --- .github/workflows/test.yml | 5 +++-- ci/run_couchbase.sh | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4c309aa9..2c374941 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -35,5 +35,6 @@ jobs: ACTIVE_MODEL_VERSION: ${{ matrix.gemfile }} BUNDLE_JOBS: 4 BUNDLE_PATH: vendor/bundle - TRAVIS_TEST: true - RAILS_ENV: test + COUCHBASE_BUCKET: default + COUCHBASE_USER: tester + COUCHBASE_PASSWORD: password123 diff --git a/ci/run_couchbase.sh b/ci/run_couchbase.sh index 6776623a..40275007 100755 --- a/ci/run_couchbase.sh +++ b/ci/run_couchbase.sh @@ -7,7 +7,7 @@ sudo service couchbase-server status /opt/couchbase/bin/couchbase-cli cluster-init -c 127.0.0.1:8091 --cluster-username=admin --cluster-password=password --cluster-ramsize=320 --cluster-index-ramsize=256 --cluster-fts-ramsize=256 --services=data,index,query,fts sleep 5 /opt/couchbase/bin/couchbase-cli server-info -c 127.0.0.1:8091 -u admin -p password -/opt/couchbase/bin/couchbase-cli bucket-create -c 127.0.0.1:8091 -u admin -p password --bucket=${COUCHBASE_BUCKET} --bucket-type=couchbase --bucket-ramsize=160 --bucket-replica=0 --wait +/opt/couchbase/bin/couchbase-cli bucket-create -c 127.0.0.1:8091 -u admin -p password --bucket=$COUCHBASE_BUCKET --bucket-type=couchbase --bucket-ramsize=160 --bucket-replica=0 --wait sleep 1 -/opt/couchbase/bin/couchbase-cli user-manage -c 127.0.0.1:8091 -u admin -p password --set --rbac-username ${COUCHBASE_USER} --rbac-password ${COUCHBASE_PASSWORD} --rbac-name "Auto Tester" --roles admin --auth-domain local -curl http://admin:password@localhost:8093/query/service -d "statement=CREATE INDEX `default_type` ON `${COUCHBASE_BUCKET}`(`type`)" +/opt/couchbase/bin/couchbase-cli user-manage -c 127.0.0.1:8091 -u admin -p password --set --rbac-username $COUCHBASE_USER --rbac-password ${COUCHBASE_PASSWORD} --rbac-name "Auto Tester" --roles admin --auth-domain local +curl http://admin:password@localhost:8093/query/service -d "statement=CREATE INDEX `default_type` ON `$COUCHBASE_BUCKET`(`type`)" From 58a2dd9043e7b8f301c223ee1476409135925836 Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Thu, 7 Jul 2022 14:52:51 +0200 Subject: [PATCH 30/43] preserve env in sudo --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2c374941..10a31d6c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -29,7 +29,7 @@ jobs: with: ruby-version: ${{ matrix.ruby }} bundler-cache: true - - run: sudo ./ci/run_couchbase.sh + - run: sudo -E ./ci/run_couchbase.sh - run: bundle exec rspec env: ACTIVE_MODEL_VERSION: ${{ matrix.gemfile }} From a3e10f966a7238418ddccc18efb775e8ed2b6fb1 Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Thu, 7 Jul 2022 14:59:36 +0200 Subject: [PATCH 31/43] matrix couchbase version --- .github/workflows/test.yml | 16 ++++++++++++---- ci/run_couchbase.sh | 15 +++++++++++++-- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 10a31d6c..546bc286 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -12,13 +12,20 @@ jobs: matrix: gemfile: [ '7.0.0', '5.1.7' ] ruby: [ '3.0', '2.7', '2.6'] - exclude: + couchbase: ['6.6', '7.1'] + include: - ruby: '3.0' - gemfile: '5.1.7' + gemfile: '7.0.0' + couchbase: '6.6' + - ruby: '3.0' + gemfile: '7.0.0' + couchbase: '7.1' - ruby: '2.7' - gemfile: '5.1.7' - - ruby: '2.6' gemfile: '7.0.0' + couchbase: '7.1' + - ruby: '2.6' + gemfile: '5.1.7' + couchbase: '7.1' fail-fast: false runs-on: ubuntu-20.04 name: ${{ matrix.ruby }} ${{ matrix.database }} rails-${{ matrix.gemfile }} @@ -38,3 +45,4 @@ jobs: COUCHBASE_BUCKET: default COUCHBASE_USER: tester COUCHBASE_PASSWORD: password123 + COUCHBASE_VERSION: ${{ matrix.couchbase }} diff --git a/ci/run_couchbase.sh b/ci/run_couchbase.sh index 40275007..f3cec9a5 100755 --- a/ci/run_couchbase.sh +++ b/ci/run_couchbase.sh @@ -1,7 +1,18 @@ set -x set -e -wget https://packages.couchbase.com/releases/7.1.0/couchbase-server-enterprise_7.1.0-ubuntu20.04_amd64.deb -dpkg -i couchbase-server-enterprise_7.1.0-ubuntu20.04_amd64.deb + +if [ -z "$COUCHBASE_VERSION" ] + echo "missing COUCHBASE_VERSION" + exit +fi + +if [ -z "$COUCHBASE_USER" ] + echo "missing COUCHBASE_USER" + exit +fi + +wget https://packages.couchbase.com/releases/$COUCHBASE_VERSION.0/couchbase-server-enterprise_$COUCHBASE_VERSION.0-ubuntu20.04_amd64.deb +dpkg -i couchbase-server-enterprise_$COUCHBASE_VERSION.0-ubuntu20.04_amd64.deb sleep 8 sudo service couchbase-server status /opt/couchbase/bin/couchbase-cli cluster-init -c 127.0.0.1:8091 --cluster-username=admin --cluster-password=password --cluster-ramsize=320 --cluster-index-ramsize=256 --cluster-fts-ramsize=256 --services=data,index,query,fts From 735b293baa0d52025279cecc81d6728cdb0c7782 Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Thu, 7 Jul 2022 15:05:33 +0200 Subject: [PATCH 32/43] reduce number of jobs --- .github/workflows/test.yml | 7 ++----- ci/run_couchbase.sh | 22 +++++++++------------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 546bc286..c943623f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,9 +10,6 @@ jobs: test: strategy: matrix: - gemfile: [ '7.0.0', '5.1.7' ] - ruby: [ '3.0', '2.7', '2.6'] - couchbase: ['6.6', '7.1'] include: - ruby: '3.0' gemfile: '7.0.0' @@ -28,7 +25,7 @@ jobs: couchbase: '7.1' fail-fast: false runs-on: ubuntu-20.04 - name: ${{ matrix.ruby }} ${{ matrix.database }} rails-${{ matrix.gemfile }} + name: ${{ matrix.ruby }} rails-${{ matrix.gemfile }} couchbase-${{ matrix.database }} steps: - uses: actions/checkout@v2 - run: sudo apt-get update && sudo apt-get install libevent-dev libev-dev python-httplib2 @@ -36,7 +33,7 @@ jobs: with: ruby-version: ${{ matrix.ruby }} bundler-cache: true - - run: sudo -E ./ci/run_couchbase.sh + - run: sudo ./ci/run_couchbase.sh $COUCHBASE_VERSION $COUCHBASE_BUCKET $COUCHBASE_USER $COUCHBASE_PASSWORD - run: bundle exec rspec env: ACTIVE_MODEL_VERSION: ${{ matrix.gemfile }} diff --git a/ci/run_couchbase.sh b/ci/run_couchbase.sh index f3cec9a5..e16788e3 100755 --- a/ci/run_couchbase.sh +++ b/ci/run_couchbase.sh @@ -1,24 +1,20 @@ set -x set -e -if [ -z "$COUCHBASE_VERSION" ] - echo "missing COUCHBASE_VERSION" - exit -fi +VERSION=$1 +BUCKET=$2 +USER=$3 +PASSWORD=$4 -if [ -z "$COUCHBASE_USER" ] - echo "missing COUCHBASE_USER" - exit -fi -wget https://packages.couchbase.com/releases/$COUCHBASE_VERSION.0/couchbase-server-enterprise_$COUCHBASE_VERSION.0-ubuntu20.04_amd64.deb -dpkg -i couchbase-server-enterprise_$COUCHBASE_VERSION.0-ubuntu20.04_amd64.deb +wget https://packages.couchbase.com/releases/$VERSION.0/couchbase-server-enterprise_$VERSION.0-ubuntu20.04_amd64.deb +dpkg -i couchbase-server-enterprise_$VERSION.0-ubuntu20.04_amd64.deb sleep 8 sudo service couchbase-server status /opt/couchbase/bin/couchbase-cli cluster-init -c 127.0.0.1:8091 --cluster-username=admin --cluster-password=password --cluster-ramsize=320 --cluster-index-ramsize=256 --cluster-fts-ramsize=256 --services=data,index,query,fts sleep 5 /opt/couchbase/bin/couchbase-cli server-info -c 127.0.0.1:8091 -u admin -p password -/opt/couchbase/bin/couchbase-cli bucket-create -c 127.0.0.1:8091 -u admin -p password --bucket=$COUCHBASE_BUCKET --bucket-type=couchbase --bucket-ramsize=160 --bucket-replica=0 --wait +/opt/couchbase/bin/couchbase-cli bucket-create -c 127.0.0.1:8091 -u admin -p password --bucket=$BUCKET --bucket-type=couchbase --bucket-ramsize=160 --bucket-replica=0 --wait sleep 1 -/opt/couchbase/bin/couchbase-cli user-manage -c 127.0.0.1:8091 -u admin -p password --set --rbac-username $COUCHBASE_USER --rbac-password ${COUCHBASE_PASSWORD} --rbac-name "Auto Tester" --roles admin --auth-domain local -curl http://admin:password@localhost:8093/query/service -d "statement=CREATE INDEX `default_type` ON `$COUCHBASE_BUCKET`(`type`)" +/opt/couchbase/bin/couchbase-cli user-manage -c 127.0.0.1:8091 -u admin -p password --set --rbac-username $USER --rbac-password $PASSWORD --rbac-name "Auto Tester" --roles admin --auth-domain local +curl http://admin:password@localhost:8093/query/service -d "statement=CREATE INDEX `default_type` ON `$BUCKET`(`type`)" From 3f7de4c8ba738dc5820d6699e5edd2ee953b01e2 Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Thu, 7 Jul 2022 15:08:36 +0200 Subject: [PATCH 33/43] fix couchbase version --- .github/workflows/test.yml | 10 +++++----- ci/run_couchbase.sh | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c943623f..bbaff0ef 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -13,19 +13,19 @@ jobs: include: - ruby: '3.0' gemfile: '7.0.0' - couchbase: '6.6' + couchbase: '6.6.5' - ruby: '3.0' gemfile: '7.0.0' - couchbase: '7.1' + couchbase: '7.1.0' - ruby: '2.7' gemfile: '7.0.0' - couchbase: '7.1' + couchbase: '7.1.0' - ruby: '2.6' gemfile: '5.1.7' - couchbase: '7.1' + couchbase: '7.1.0' fail-fast: false runs-on: ubuntu-20.04 - name: ${{ matrix.ruby }} rails-${{ matrix.gemfile }} couchbase-${{ matrix.database }} + name: ${{ matrix.ruby }} rails-${{ matrix.gemfile }} couchbase-${{ matrix.couchbase }} steps: - uses: actions/checkout@v2 - run: sudo apt-get update && sudo apt-get install libevent-dev libev-dev python-httplib2 diff --git a/ci/run_couchbase.sh b/ci/run_couchbase.sh index e16788e3..fa5c5269 100755 --- a/ci/run_couchbase.sh +++ b/ci/run_couchbase.sh @@ -7,8 +7,8 @@ USER=$3 PASSWORD=$4 -wget https://packages.couchbase.com/releases/$VERSION.0/couchbase-server-enterprise_$VERSION.0-ubuntu20.04_amd64.deb -dpkg -i couchbase-server-enterprise_$VERSION.0-ubuntu20.04_amd64.deb +wget https://packages.couchbase.com/releases/$VERSION/couchbase-server-enterprise_$VERSION-ubuntu20.04_amd64.deb +dpkg -i couchbase-server-enterprise_$VERSION-ubuntu20.04_amd64.deb sleep 8 sudo service couchbase-server status /opt/couchbase/bin/couchbase-cli cluster-init -c 127.0.0.1:8091 --cluster-username=admin --cluster-password=password --cluster-ramsize=320 --cluster-index-ramsize=256 --cluster-fts-ramsize=256 --services=data,index,query,fts From 6f9c7670cf7617db90b6c39cd87df82ef4a585e1 Mon Sep 17 00:00:00 2001 From: Gauthier Monserand Date: Thu, 7 Jul 2022 15:11:13 +0200 Subject: [PATCH 34/43] Add escaping --- ci/run_couchbase.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/run_couchbase.sh b/ci/run_couchbase.sh index fa5c5269..912938bf 100755 --- a/ci/run_couchbase.sh +++ b/ci/run_couchbase.sh @@ -17,4 +17,4 @@ sleep 5 /opt/couchbase/bin/couchbase-cli bucket-create -c 127.0.0.1:8091 -u admin -p password --bucket=$BUCKET --bucket-type=couchbase --bucket-ramsize=160 --bucket-replica=0 --wait sleep 1 /opt/couchbase/bin/couchbase-cli user-manage -c 127.0.0.1:8091 -u admin -p password --set --rbac-username $USER --rbac-password $PASSWORD --rbac-name "Auto Tester" --roles admin --auth-domain local -curl http://admin:password@localhost:8093/query/service -d "statement=CREATE INDEX `default_type` ON `$BUCKET`(`type`)" +curl http://admin:password@localhost:8093/query/service -d "statement=CREATE INDEX \`default_type\` ON \`$BUCKET\`(\`type\`)" From b0cc946978aef69a06bb4a149be88001ee42650c Mon Sep 17 00:00:00 2001 From: Damien Voreiter Date: Thu, 18 Aug 2022 15:48:00 +0200 Subject: [PATCH 35/43] remove puts --- lib/couchbase-orm/railtie.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/couchbase-orm/railtie.rb b/lib/couchbase-orm/railtie.rb index c1071aef..fefad17a 100644 --- a/lib/couchbase-orm/railtie.rb +++ b/lib/couchbase-orm/railtie.rb @@ -52,7 +52,6 @@ def self.rescue_responses config_file = Rails.root.join('config', 'couchbase.yml') if config_file.file? && config = YAML.load(ERB.new(File.read(config_file)).result)[Rails.env] - puts config ::CouchbaseOrm::Connection.options = config.deep_symbolize_keys end end From c7f87d8a30d2902658757ac8573ccc6ff1f6f80c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Couton?= Date: Thu, 18 Aug 2022 17:48:53 +0200 Subject: [PATCH 36/43] reinsert query usage and rename it to query_fn --- lib/couchbase-orm/n1ql.rb | 28 +++++++++++++++------------- spec/n1ql_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/lib/couchbase-orm/n1ql.rb b/lib/couchbase-orm/n1ql.rb index 7c157aec..2f4562f7 100644 --- a/lib/couchbase-orm/n1ql.rb +++ b/lib/couchbase-orm/n1ql.rb @@ -35,7 +35,7 @@ module ClassMethods # # ... # end # TODO: add range keys [:startkey, :endkey] - def n1ql(name, select: nil, emit_key: [], **options) + def n1ql(name, query_fn: nil, emit_key: [], **options) emit_key = Array.wrap(emit_key) emit_key.each do |key| raise "unknown emit_key attribute for n1ql :#{name}, emit_key: :#{key}" if key && @attributes[key].nil? @@ -50,7 +50,7 @@ def n1ql(name, select: nil, emit_key: [], **options) singleton_class.__send__(:define_method, name) do |**opts, &result_modifier| opts = options.merge(opts).reverse_merge(scan_consistency: :request_plus) values = convert_values(opts.delete(:key)) - current_query = run_query(method_opts[:emit_key], values, select: select, **opts.except(:include_docs)) + current_query = run_query(method_opts[:emit_key], values, query_fn, **opts.except(:include_docs)) if result_modifier opts[:include_docs] = true @@ -113,17 +113,19 @@ def build_limit(limit) limit ? "limit #{limit}" : "" end - def run_query(keys, values, select: nil, descending: false, limit: nil, **options) - bucket_name = bucket.name - where = build_where(keys, values) - order = build_order(keys, descending) - limit = build_limit(limit) - select ||= "raw meta().id" - raise "select must be a string" unless select.is_a?(String) - n1ql_query = "select #{select} from `#{bucket_name}` where #{where} order by #{order} #{limit}" - result = cluster.query(n1ql_query, Couchbase::Options::Query.new(**options)) - CouchbaseOrm.logger.debug "N1QL query: #{n1ql_query} return #{result.rows.to_a.length} rows" - N1qlProxy.new(result) + def run_query(keys, values, query_fn, descending: false, limit: nil, **options) + if query_fn + N1qlProxy.new(query_fn.call(bucket, values, Couchbase::Options::Query.new(**options))) + else + bucket_name = bucket.name + where = build_where(keys, values) + order = build_order(keys, descending) + limit = build_limit(limit) + n1ql_query = "select raw meta().id from `#{bucket_name}` where #{where} order by #{order} #{limit}" + result = cluster.query(n1ql_query, Couchbase::Options::Query.new(**options)) + CouchbaseOrm.logger.debug "N1QL query: #{n1ql_query} return #{result.rows.to_a.length} rows" + N1qlProxy.new(result) + end end end end diff --git a/spec/n1ql_spec.rb b/spec/n1ql_spec.rb index 66da5f8a..fd75180b 100644 --- a/spec/n1ql_spec.rb +++ b/spec/n1ql_spec.rb @@ -9,6 +9,12 @@ class N1QLTest < CouchbaseOrm::Base n1ql :all n1ql :by_name, emit_key: :name n1ql :by_rating, emit_key: :rating + n1ql :by_custom_rating, query_fn: proc { |bucket, _values, options| + cluster.query("SELECT raw meta().id FROM `#{bucket.name}` where type = 'n1_ql_test' AND rating IN [1,2] ORDER BY name ASC", options) + } + n1ql :by_custom_rating_values, emit_key: [:rating], query_fn: proc { |bucket, values, options| + cluster.query("SELECT raw meta().id FROM `#{bucket.name}` where type = 'n1_ql_test' AND rating IN #{values[0]} ORDER BY name ASC", options) + } # This generates both: # view :by_rating, emit_key: :rating # same as above @@ -58,6 +64,26 @@ class N1QLTest < CouchbaseOrm::Base expect(Set.new(docs)).to eq(Set.new(%w[bob jane])) end + it "should return matching results with custom n1ql query" do + N1QLTest.create! name: :bob, rating: :awesome + N1QLTest.create! name: :jane, rating: :awesome + N1QLTest.create! name: :greg, rating: :bad + N1QLTest.create! name: :mel, rating: :good + + + docs = N1QLTest.by_custom_rating().collect { |ob| + ob.name + } + + expect(Set.new(docs)).to eq(Set.new(%w[bob jane mel])) + + docs = N1QLTest.by_custom_rating_values(key: [[1, 2]]).collect { |ob| + ob.name + } + + expect(Set.new(docs)).to eq(Set.new(%w[bob jane mel])) + end + after(:all) do N1QLTest.all.to_a.each(&:destroy) end From 3788688214df503ca7695a4911d0b9cb8148d4d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Couton?= Date: Fri, 19 Aug 2022 11:10:55 +0200 Subject: [PATCH 37/43] query_fn instead of query --- lib/couchbase-orm/utilities/has_many.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/couchbase-orm/utilities/has_many.rb b/lib/couchbase-orm/utilities/has_many.rb index 1bead6ff..d03458b0 100644 --- a/lib/couchbase-orm/utilities/has_many.rb +++ b/lib/couchbase-orm/utilities/has_many.rb @@ -93,7 +93,9 @@ def build_index_view(klass, remote_class, remote_method, through_key, foreign_ke def build_index_n1ql(klass, remote_class, remote_method, through_key, foreign_key) if remote_class klass.class_eval do - n1ql remote_method, select: "raw #{through_key}" + n1ql remote_method, query_fn: proc { |bucket, values, options| + cluster.query("SELECT raw #{through_key} FROM `#{bucket.name}` where type = \"#{design_document}\" and #{foreign_key} = #{values[0]}", options) + } end else klass.class_eval do From ff80f7804016321abe6368167bd96db349ec39db Mon Sep 17 00:00:00 2001 From: Damien Voreiter <78789202+DamienVoreiter@users.noreply.github.com> Date: Thu, 8 Sep 2022 11:02:40 +0200 Subject: [PATCH 38/43] Update lib/couchbase-orm/railtie.rb --- lib/couchbase-orm/railtie.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/couchbase-orm/railtie.rb b/lib/couchbase-orm/railtie.rb index fefad17a..70a0cbe1 100644 --- a/lib/couchbase-orm/railtie.rb +++ b/lib/couchbase-orm/railtie.rb @@ -24,10 +24,8 @@ module Rails #:nodoc: module Couchbase #:nodoc: class Railtie < Rails::Railtie #:nodoc: - puts "Railtie < Rails::Railtie" config.couchbase2 = ActiveSupport::OrderedOptions.new config.couchbase2.ensure_design_documents = true - puts "config.couchbase.ensure_design_documents : #{config.couchbase2.ensure_design_documents}" # Maping of rescued exceptions to HTTP responses # From de0ef2ef14efcbc15d16350358c81e6014726b1a Mon Sep 17 00:00:00 2001 From: Damien Voreiter <78789202+DamienVoreiter@users.noreply.github.com> Date: Thu, 8 Sep 2022 14:17:25 +0200 Subject: [PATCH 39/43] Update lib/couchbase-orm/proxies/bucket_proxy.rb --- lib/couchbase-orm/proxies/bucket_proxy.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/couchbase-orm/proxies/bucket_proxy.rb b/lib/couchbase-orm/proxies/bucket_proxy.rb index 106f8c82..259ecd1e 100644 --- a/lib/couchbase-orm/proxies/bucket_proxy.rb +++ b/lib/couchbase-orm/proxies/bucket_proxy.rb @@ -5,7 +5,7 @@ module CouchbaseOrm class BucketProxy def initialize(proxyfied) - raise "Must proxy a non nil object" if proxyfied.nil? + raise ArgumentError, "Must proxy a non nil object" if proxyfied.nil? @proxyfied = proxyfied From eed7f40ea9272fb60e473ecd54b2ec33d26dfc7b Mon Sep 17 00:00:00 2001 From: Damien Voreiter Date: Thu, 8 Sep 2022 15:05:51 +0200 Subject: [PATCH 40/43] unhardcode host config & use yml file --- README.md | 24 ++++++++++++---- lib/couchbase-orm/connection.rb | 28 ++++++++++++------- lib/couchbase-orm/railtie.rb | 16 ++++------- .../config/templates/couchbase.yml | 5 ++-- 4 files changed, 45 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 8f471af7..2d40914d 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,9 @@ To generate config you can use `rails generate couchbase_orm:config`: It will generate this `config/couchbase.yml` for you: +```yaml common: &common - hosts: localhost + connection_string: couchbase://localhost username: dev_user password: dev_password @@ -26,10 +27,23 @@ It will generate this `config/couchbase.yml` for you: # set these environment variables on your production server production: - hosts: <%= ENV['COUCHBASE_HOST'] || ENV['COUCHBASE_HOSTS'] %> - bucket: <%= ENV['COUCHBASE_BUCKET'] %> - username: <%= ENV['COUCHBASE_USER'] %> - password: <%= ENV['COUCHBASE_PASSWORD'] %> + connection_string: <%= ENV['COUCHBASE_CONNECTION_STRING'] %> + bucket: <%= ENV['COUCHBASE_BUCKET'] %> + username: <%= ENV['COUCHBASE_USER'] %> + password: <%= ENV['COUCHBASE_PASSWORD'] %> +``` + +## Setup without Rails +If you are not using Rails, you can configure couchbase-orm with an initializer: +```ruby +# config/initializers/couchbase_orm.rb +CouchbaseOrm::Connection.config = { + connection_string: "couchbase://localhost" + username: "dev_user" + password: "dev_password" + bucket: "dev_bucket" +} +``` Views are generated on application load if they don't exist or mismatch. This works fine in production however by default in development models are lazy loaded. diff --git a/lib/couchbase-orm/connection.rb b/lib/couchbase-orm/connection.rb index 9886c640..c959393e 100644 --- a/lib/couchbase-orm/connection.rb +++ b/lib/couchbase-orm/connection.rb @@ -1,23 +1,31 @@ - require 'couchbase' module CouchbaseOrm class Connection - @options = {} - class << self - attr_accessor :options + + def self.config + @@config || raise('Missing CouchbaseOrm connection config') + end + + def self.config=(config) + @@config = config end def self.cluster @cluster ||= begin - options = Couchbase::Cluster::ClusterOptions.new - options.authenticate(ENV["COUCHBASE_USER"], ENV["COUCHBASE_PASSWORD"]) - cluster = Couchbase::Cluster.connect('couchbase://127.0.0.1', options) - end + cb_config = Couchbase::Configuration.new + cb_config.connection_string = config[:connection_string] || raise('Missing CouchbaseOrm connection string') + cb_config.username = config[:username] || raise('Missing CouchbaseOrm username') + cb_config.password = config[:password] || raise('Missing CouchbaseOrm password') + Couchbase::Cluster.connect(cb_config) + end end def self.bucket - @bucket ||= cluster.bucket(ENV["COUCHBASE_BUCKET"]) + @bucket ||= begin + bucket_name = config[:bucket] || raise('Missing CouchbaseOrm bucket name') + cluster.bucket(bucket_name) + end end end -end +end \ No newline at end of file diff --git a/lib/couchbase-orm/railtie.rb b/lib/couchbase-orm/railtie.rb index 70a0cbe1..391512e9 100644 --- a/lib/couchbase-orm/railtie.rb +++ b/lib/couchbase-orm/railtie.rb @@ -24,8 +24,8 @@ module Rails #:nodoc: module Couchbase #:nodoc: class Railtie < Rails::Railtie #:nodoc: - config.couchbase2 = ActiveSupport::OrderedOptions.new - config.couchbase2.ensure_design_documents = true + config.couchbase_orm = ActiveSupport::OrderedOptions.new + config.couchbase_orm.ensure_design_documents = true # Maping of rescued exceptions to HTTP responses # @@ -44,14 +44,8 @@ def self.rescue_responses config.action_dispatch.rescue_responses.merge!(rescue_responses) end - # Initialize Couchbase Mode. This will look for a couchbase.yml in the - # config directory and configure Couchbase connection appropriately. - initializer 'couchbase.setup_connection' do - config_file = Rails.root.join('config', 'couchbase.yml') - if config_file.file? && - config = YAML.load(ERB.new(File.read(config_file)).result)[Rails.env] - ::CouchbaseOrm::Connection.options = config.deep_symbolize_keys - end + initializer 'couchbase_orm.setup_connection_config' do + CouchbaseOrm::Connection.config = Rails.application.config_for(:couchbase) end # After initialization we will warn the user if we can't find a couchbase.yml and @@ -79,7 +73,7 @@ def self.rescue_responses # Check (and upgrade if needed) all design documents config.after_initialize do |app| - if config.couchbase2.ensure_design_documents + if config.couchbase_orm.ensure_design_documents begin ::CouchbaseOrm::Base.descendants.each do |model| model.ensure_design_document! diff --git a/lib/rails/generators/couchbase_orm/config/templates/couchbase.yml b/lib/rails/generators/couchbase_orm/config/templates/couchbase.yml index fefc7479..52821e1c 100644 --- a/lib/rails/generators/couchbase_orm/config/templates/couchbase.yml +++ b/lib/rails/generators/couchbase_orm/config/templates/couchbase.yml @@ -1,5 +1,6 @@ common: &common - hosts: localhost + connection_string: couchbase://localhost + bucket: <%= bucket_name || app_name %> username: <%= username || bucket_name || app_name %> password: <%= password %> @@ -13,7 +14,7 @@ test: # set these environment variables on your production server production: - hosts: <%%= ENV['COUCHBASE_HOST'] || ENV['COUCHBASE_HOSTS'] %> + connection_string: <%%= ENV['COUCHBASE_CONNECTION_STRING'] %> bucket: <%%= ENV['COUCHBASE_BUCKET'] %> username: <%%= ENV['COUCHBASE_USER'] %> password: <%%= ENV['COUCHBASE_PASSWORD'] %> From 922dbb828629e1c628f3136158ca2b546be7c10b Mon Sep 17 00:00:00 2001 From: Damien Voreiter Date: Thu, 8 Sep 2022 15:09:31 +0200 Subject: [PATCH 41/43] raise CouchbaseOrm::Error --- lib/couchbase-orm/connection.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/couchbase-orm/connection.rb b/lib/couchbase-orm/connection.rb index c959393e..a50e4c4b 100644 --- a/lib/couchbase-orm/connection.rb +++ b/lib/couchbase-orm/connection.rb @@ -4,7 +4,7 @@ module CouchbaseOrm class Connection def self.config - @@config || raise('Missing CouchbaseOrm connection config') + @@config || raise(CouchbaseOrm::Error, 'Missing CouchbaseOrm connection config') end def self.config=(config) @@ -14,18 +14,18 @@ def self.config=(config) def self.cluster @cluster ||= begin cb_config = Couchbase::Configuration.new - cb_config.connection_string = config[:connection_string] || raise('Missing CouchbaseOrm connection string') - cb_config.username = config[:username] || raise('Missing CouchbaseOrm username') - cb_config.password = config[:password] || raise('Missing CouchbaseOrm password') + cb_config.connection_string = config[:connection_string] || raise(CouchbaseOrm::Error, 'Missing CouchbaseOrm connection string') + cb_config.username = config[:username] || raise(CouchbaseOrm::Error, 'Missing CouchbaseOrm username') + cb_config.password = config[:password] || raise(CouchbaseOrm::Error, 'Missing CouchbaseOrm password') Couchbase::Cluster.connect(cb_config) end end def self.bucket @bucket ||= begin - bucket_name = config[:bucket] || raise('Missing CouchbaseOrm bucket name') - cluster.bucket(bucket_name) - end + bucket_name = config[:bucket] || raise(CouchbaseOrm::Error, 'Missing CouchbaseOrm bucket name') + cluster.bucket(bucket_name) + end end end end \ No newline at end of file From b5701f6ab9108c8dd00057216d933deb2347cb33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Couton?= Date: Thu, 8 Sep 2022 22:48:21 +0200 Subject: [PATCH 42/43] try fix ci --- lib/couchbase-orm/connection.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/couchbase-orm/connection.rb b/lib/couchbase-orm/connection.rb index a50e4c4b..f2b34fa5 100644 --- a/lib/couchbase-orm/connection.rb +++ b/lib/couchbase-orm/connection.rb @@ -2,9 +2,14 @@ module CouchbaseOrm class Connection - + @@config = nil def self.config - @@config || raise(CouchbaseOrm::Error, 'Missing CouchbaseOrm connection config') + @@config || { + :connection_string => "couchbase://#{ENV['COUCHBASE_HOST'] || 'localhost'}", + :username => ENV['COUCHBASE_USER'], + :password => ENV['COUCHBASE_PASSWORD'], + :bucket => ENV['COUCHBASE_BUCKET'] + } end def self.config=(config) From 684f1f95c589d6a6d7a4dcf8d965b7d784e37bfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Couton?= Date: Thu, 8 Sep 2022 22:52:01 +0200 Subject: [PATCH 43/43] use ip not localhost --- lib/couchbase-orm/connection.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/couchbase-orm/connection.rb b/lib/couchbase-orm/connection.rb index f2b34fa5..cda9dc76 100644 --- a/lib/couchbase-orm/connection.rb +++ b/lib/couchbase-orm/connection.rb @@ -5,7 +5,7 @@ class Connection @@config = nil def self.config @@config || { - :connection_string => "couchbase://#{ENV['COUCHBASE_HOST'] || 'localhost'}", + :connection_string => "couchbase://#{ENV['COUCHBASE_HOST'] || '127.0.0.1'}", :username => ENV['COUCHBASE_USER'], :password => ENV['COUCHBASE_PASSWORD'], :bucket => ENV['COUCHBASE_BUCKET']