From 6db829ef89c4e7d242991f82912b519cb5a3bcdc Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Fri, 17 Jul 2020 13:16:54 +0800 Subject: [PATCH 01/15] [#98] new approach --- .github/workflows/changelog.yml | 42 ++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/.github/workflows/changelog.yml b/.github/workflows/changelog.yml index c23e4ea3..ecf5c1bf 100644 --- a/.github/workflows/changelog.yml +++ b/.github/workflows/changelog.yml @@ -33,23 +33,31 @@ jobs: git config --local user.name "$ACTION_USERNAME" git add CHANGELOG.md && git commit -m 'Updated CHANGELOG.md' && echo ::set-env name=push::1 || echo "No changes to CHANGELOG.md" - - name: Push changelog to master - if: env.push == 1 - uses: ad-m/github-push-action@master - with: - github_token: ${{ secrets.CHANGELOG_GITHUB_TOKEN }} - branch: master - - - name: Cherry-pick changelog to development + - name: Push changes if: env.push == 1 env: - ACTION_EMAIL: action@github.com - ACTION_USERNAME: GitHub Action + # CI_USER: ${{ secrets.YOUR_GITHUB_USER }} + CI_TOKEN: ${{ secrets.CHANGELOG_GITHUB_TOKEN }} run: | - git config --local user.email "$ACTION_EMAIL" - git config --local user.name "$ACTION_USERNAME" - commit_hash=`git show HEAD | egrep commit\ .+$ | cut -d' ' -f2` - git checkout development - git pull - git cherry-pick $commit_hash - git push + git push "https://$GITHUB_ACTOR:$CI_TOKEN@github.com/$GITHUB_REPOSITORY.git" HEAD:master + + # - name: Push changelog to master + # if: env.push == 1 + # uses: ad-m/github-push-action@master + # with: + # github_token: ${{ secrets.CHANGELOG_GITHUB_TOKEN }} + # branch: master + + # - name: Cherry-pick changelog to development + # if: env.push == 1 + # env: + # ACTION_EMAIL: action@github.com + # ACTION_USERNAME: GitHub Action + # run: | + # git config --local user.email "$ACTION_EMAIL" + # git config --local user.name "$ACTION_USERNAME" + # commit_hash=`git show HEAD | egrep commit\ .+$ | cut -d' ' -f2` + # git checkout development + # git pull + # git cherry-pick $commit_hash + # git push From 2087f7afaf040d041f672a35e1c452eda59c9aae Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 12 Jul 2020 15:37:58 +0800 Subject: [PATCH 02/15] [#86] ensure listed tenants are created before trying to migrate them or seed them --- lib/apartment/tasks/task_helper.rb | 8 ++++++++ lib/tasks/apartment.rake | 11 ++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/apartment/tasks/task_helper.rb b/lib/apartment/tasks/task_helper.rb index 84257b9a..3940e8cd 100644 --- a/lib/apartment/tasks/task_helper.rb +++ b/lib/apartment/tasks/task_helper.rb @@ -29,5 +29,13 @@ def self.warn_if_tenants_empty Note that your tenants currently haven't been migrated. You'll need to run `db:migrate` to rectify this. WARNING end + + def self.create_tenant(tenant_name) + begin + Apartment::Tenant.create(tenant_name) + rescue Apartment::TenantExists => e + puts 'Tried to create already existing tenant' + end + end end end diff --git a/lib/tasks/apartment.rake b/lib/tasks/apartment.rake index f4583074..1ea98fed 100644 --- a/lib/tasks/apartment.rake +++ b/lib/tasks/apartment.rake @@ -10,12 +10,7 @@ apartment_namespace = namespace :apartment do Apartment::TaskHelper.warn_if_tenants_empty Apartment::TaskHelper.tenants.each do |tenant| - begin - puts("Creating #{tenant} tenant") - Apartment::Tenant.create(tenant) - rescue Apartment::TenantExists => e - puts e.message - end + Apartment::TaskHelper.create_tenant(tenant) end end @@ -36,6 +31,7 @@ apartment_namespace = namespace :apartment do Apartment::TaskHelper.warn_if_tenants_empty Apartment::TaskHelper.each_tenant do |tenant| begin + Apartment::TaskHelper.create_tenant(tenant) puts("Migrating #{tenant} tenant") Apartment::Migrator.migrate tenant rescue Apartment::TenantNotFound => e @@ -45,11 +41,12 @@ apartment_namespace = namespace :apartment do end desc 'Seed all tenants' - task seed: :create do + task :seed do Apartment::TaskHelper.warn_if_tenants_empty Apartment::TaskHelper.each_tenant do |tenant| begin + Apartment::TaskHelper.create_tenant(tenant) puts("Seeding #{tenant} tenant") Apartment::Tenant.switch(tenant) do Apartment::Tenant.seed From c80e57a7eda6163f151e67e00180d91a6fca9032 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Fri, 17 Jul 2020 01:07:58 +0800 Subject: [PATCH 03/15] [#86] addressed rubocop --- lib/apartment/tasks/task_helper.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/apartment/tasks/task_helper.rb b/lib/apartment/tasks/task_helper.rb index 3940e8cd..95d1d236 100644 --- a/lib/apartment/tasks/task_helper.rb +++ b/lib/apartment/tasks/task_helper.rb @@ -31,11 +31,9 @@ def self.warn_if_tenants_empty end def self.create_tenant(tenant_name) - begin - Apartment::Tenant.create(tenant_name) - rescue Apartment::TenantExists => e - puts 'Tried to create already existing tenant' - end + Apartment::Tenant.create(tenant_name) + rescue Apartment::TenantExists => e + puts "Tried to create already existing tenant: #{e}" end end end From fa45a93f7496c23a645654bed445bca066f03b5b Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Sun, 14 Jun 2020 15:40:50 +0800 Subject: [PATCH 04/15] using transaction --- lib/apartment/adapters/postgresql_adapter.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index 17cbb892..c762fc30 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -97,7 +97,11 @@ def tenant_exists?(tenant) end def create_tenant_command(conn, tenant) - conn.execute(%(CREATE SCHEMA "#{tenant}")) + schema = %(BEGIN; + CREATE SCHEMA "#{tenant}"; + COMMIT;) + + conn.execute(schema) end # Generate the final search path to set including persistent_schemas From 6b58e19ed2fe6c902f3c36ad152a43924a23064d Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Mon, 15 Jun 2020 09:15:14 +0800 Subject: [PATCH 05/15] indentation --- lib/apartment/adapters/postgresql_adapter.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index c762fc30..eb660282 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -98,8 +98,8 @@ def tenant_exists?(tenant) def create_tenant_command(conn, tenant) schema = %(BEGIN; - CREATE SCHEMA "#{tenant}"; - COMMIT;) + CREATE SCHEMA "#{tenant}"; + COMMIT;) conn.execute(schema) end From 547ee9e90a7466520108bb00d520b0395d4bc5ad Mon Sep 17 00:00:00 2001 From: Gabo Date: Thu, 8 Oct 2020 18:45:14 -0500 Subject: [PATCH 06/15] Add creating tenant message again --- lib/apartment/tasks/task_helper.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/apartment/tasks/task_helper.rb b/lib/apartment/tasks/task_helper.rb index 95d1d236..6065c953 100644 --- a/lib/apartment/tasks/task_helper.rb +++ b/lib/apartment/tasks/task_helper.rb @@ -31,6 +31,7 @@ def self.warn_if_tenants_empty end def self.create_tenant(tenant_name) + puts("Creating #{tenant_name} tenant") Apartment::Tenant.create(tenant_name) rescue Apartment::TenantExists => e puts "Tried to create already existing tenant: #{e}" From a5d47ad34414f2cf625e5409b9a0d7be7ed1e2b0 Mon Sep 17 00:00:00 2001 From: Gabo Date: Thu, 8 Oct 2020 18:47:39 -0500 Subject: [PATCH 07/15] Add rollback transaction for postgres, and use it when failed to create schema --- lib/apartment/adapters/abstract_adapter.rb | 4 ++++ lib/apartment/adapters/postgresql_adapter.rb | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/lib/apartment/adapters/abstract_adapter.rb b/lib/apartment/adapters/abstract_adapter.rb index 8ca4f308..1cbd5947 100644 --- a/lib/apartment/adapters/abstract_adapter.rb +++ b/lib/apartment/adapters/abstract_adapter.rb @@ -164,6 +164,10 @@ def create_tenant(tenant) create_tenant_command(conn, tenant) end rescue *rescuable_exceptions => e + # rollback if unable to create + with_neutral_connection(tenant) do |conn| + rollback_transaction(conn) + end raise_create_tenant_error!(tenant, e) end diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index eb660282..f657ed99 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -104,6 +104,10 @@ def create_tenant_command(conn, tenant) conn.execute(schema) end + def rollback_transaction(conn) + conn.execute("ROLLBACK;") + end + # Generate the final search path to set including persistent_schemas # def full_search_path From 8612c51be99f003177f82d31df431c55ab6260a9 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Tue, 10 Nov 2020 18:38:47 +0800 Subject: [PATCH 08/15] rollback only on postgresql --- lib/apartment/adapters/abstract_adapter.rb | 4 ---- lib/apartment/adapters/postgresql_adapter.rb | 3 +++ spec/examples/generic_adapter_custom_configuration_example.rb | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/apartment/adapters/abstract_adapter.rb b/lib/apartment/adapters/abstract_adapter.rb index 1cbd5947..8ca4f308 100644 --- a/lib/apartment/adapters/abstract_adapter.rb +++ b/lib/apartment/adapters/abstract_adapter.rb @@ -164,10 +164,6 @@ def create_tenant(tenant) create_tenant_command(conn, tenant) end rescue *rescuable_exceptions => e - # rollback if unable to create - with_neutral_connection(tenant) do |conn| - rollback_transaction(conn) - end raise_create_tenant_error!(tenant, e) end diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index f657ed99..7a223813 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -102,6 +102,9 @@ def create_tenant_command(conn, tenant) COMMIT;) conn.execute(schema) + rescue *rescuable_exceptions => e + rollback_transaction(conn) + raise e end def rollback_transaction(conn) diff --git a/spec/examples/generic_adapter_custom_configuration_example.rb b/spec/examples/generic_adapter_custom_configuration_example.rb index 5abe5029..2ad7ee20 100644 --- a/spec/examples/generic_adapter_custom_configuration_example.rb +++ b/spec/examples/generic_adapter_custom_configuration_example.rb @@ -29,7 +29,7 @@ receive(:establish_connection).with(expected_args).and_call_original ) - # because we dont have another server to connect to it errors + # because we don't have another server to connect to it errors # what matters is establish_connection receives proper args expect { subject.create(custom_tenant_name) }.to raise_error(Apartment::TenantExists) end From a219d58ed7a9a327a01c1561b027ecce1354e446 Mon Sep 17 00:00:00 2001 From: Thorben Starsy Date: Mon, 23 Nov 2020 23:07:47 +0100 Subject: [PATCH 09/15] Relaxes requirements on Rails to make it compatible with Rails 6.1 --- ros-apartment.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ros-apartment.gemspec b/ros-apartment.gemspec index 5c01be69..7c59a6ba 100644 --- a/ros-apartment.gemspec +++ b/ros-apartment.gemspec @@ -27,7 +27,7 @@ Gem::Specification.new do |s| s.homepage = 'https://github.com/rails-on-services/apartment' s.licenses = ['MIT'] - s.add_dependency 'activerecord', '>= 5.0.0', '< 6.1' + s.add_dependency 'activerecord', '>= 5.0.0', '< 6.2' s.add_dependency 'parallel', '< 2.0' s.add_dependency 'public_suffix', '>= 2.0.5', '< 5.0' s.add_dependency 'rack', '>= 1.3.6', '< 3.0' From 9a9308ede4e5aa1b2ffff9397c0feebf55c76465 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Tue, 15 Dec 2020 16:08:50 +0800 Subject: [PATCH 10/15] [#123] check if there is an open transaction and if yes, dont create a new one --- lib/apartment/adapters/postgresql_adapter.rb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/apartment/adapters/postgresql_adapter.rb b/lib/apartment/adapters/postgresql_adapter.rb index 7a223813..fd2ea683 100644 --- a/lib/apartment/adapters/postgresql_adapter.rb +++ b/lib/apartment/adapters/postgresql_adapter.rb @@ -97,11 +97,16 @@ def tenant_exists?(tenant) end def create_tenant_command(conn, tenant) - schema = %(BEGIN; - CREATE SCHEMA "#{tenant}"; - COMMIT;) - - conn.execute(schema) + # NOTE: This was causing some tests to fail because of the database strategy for rspec + if ActiveRecord::Base.connection.open_transactions > 0 + conn.execute(%(CREATE SCHEMA "#{tenant}")) + else + schema = %(BEGIN; + CREATE SCHEMA "#{tenant}"; + COMMIT;) + + conn.execute(schema) + end rescue *rescuable_exceptions => e rollback_transaction(conn) raise e From d6ba126eeb82919ed6c0de8a3142ecc02799c083 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Tue, 15 Dec 2020 17:57:53 +0800 Subject: [PATCH 11/15] copied active record logger to apartment and editing it instead of monkeypatch --- lib/apartment.rb | 2 +- lib/apartment/active_record/log_subscriber.rb | 44 ------ lib/apartment/log_subscriber.rb | 136 ++++++++++++++++++ lib/apartment/railtie.rb | 8 ++ 4 files changed, 145 insertions(+), 45 deletions(-) delete mode 100644 lib/apartment/active_record/log_subscriber.rb create mode 100644 lib/apartment/log_subscriber.rb diff --git a/lib/apartment.rb b/lib/apartment.rb index 6bca1cc8..59a61cd1 100644 --- a/lib/apartment.rb +++ b/lib/apartment.rb @@ -6,7 +6,7 @@ require 'active_record' require 'apartment/tenant' -require_relative 'apartment/active_record/log_subscriber' +require_relative 'apartment/log_subscriber' if ActiveRecord.version.release >= Gem::Version.new('6.0') require_relative 'apartment/active_record/connection_handling' diff --git a/lib/apartment/active_record/log_subscriber.rb b/lib/apartment/active_record/log_subscriber.rb deleted file mode 100644 index 45d3772d..00000000 --- a/lib/apartment/active_record/log_subscriber.rb +++ /dev/null @@ -1,44 +0,0 @@ -# frozen_string_literal: true - -module ActiveRecord - # Supports the logging configuration to prepend the database and schema in the ActiveRecord log - class LogSubscriber < ActiveSupport::LogSubscriber - def apartment_log - return unless Apartment.active_record_log - - database = color("[#{Apartment.connection.current_database}] ", ActiveSupport::LogSubscriber::MAGENTA, true) - schema = nil - unless Apartment.connection.schema_search_path.nil? - schema = color("[#{Apartment.connection.schema_search_path.tr('"', '')}] ", - ActiveSupport::LogSubscriber::YELLOW, true) - end - "#{database}#{schema}" - end - - def payload_binds(binds, type_casted_binds) - return unless (binds || []).empty? - - casted_params = type_casted_binds(type_casted_binds) - ' ' + binds.zip(casted_params).map { |attr, value| render_bind(attr, value) }.inspect - end - - def sql(event) - self.class.runtime += event.duration - return unless logger.debug? - - payload = event.payload - - return if IGNORE_PAYLOAD_NAMES.include?(payload[:name]) - - name = "#{payload[:name]} (#{event.duration.round(1)}ms)" - name = "CACHE #{name}" if payload[:cached] - sql = payload[:sql] - binds = payload_binds(payload[:binds], payload[:type_casted_binds]) - - name = colorize_payload_name(name, payload[:name]) - sql = color(sql, sql_color(sql), true) if colorize_logging - - debug " #{apartment_log}#{name} #{sql}#{binds}" - end - end -end diff --git a/lib/apartment/log_subscriber.rb b/lib/apartment/log_subscriber.rb new file mode 100644 index 00000000..38e3b412 --- /dev/null +++ b/lib/apartment/log_subscriber.rb @@ -0,0 +1,136 @@ +# frozen_string_literal: true + +module Apartment + class LogSubscriber < ActiveSupport::LogSubscriber + IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"] + + class_attribute :backtrace_cleaner, default: ActiveSupport::BacktraceCleaner.new + + def self.runtime=(value) + ActiveRecord::RuntimeRegistry.sql_runtime = value + end + + def self.runtime + ActiveRecord::RuntimeRegistry.sql_runtime ||= 0 + end + + def self.reset_runtime + rt, self.runtime = runtime, 0 + rt + end + + def sql(event) + self.class.runtime += event.duration + return unless logger.debug? + + payload = event.payload + + return if IGNORE_PAYLOAD_NAMES.include?(payload[:name]) + + name = "#{payload[:name]} (#{event.duration.round(1)}ms)" + name = "CACHE #{name}" if payload[:cached] + sql = payload[:sql] + binds = nil + + if payload[:binds]&.any? + casted_params = type_casted_binds(payload[:type_casted_binds]) + + binds = [] + payload[:binds].each_with_index do |attr, i| + binds << render_bind(attr, casted_params[i]) + end + binds = binds.inspect + binds.prepend(" ") + end + + name = colorize_payload_name(name, payload[:name]) + sql = color(sql, sql_color(sql), true) if colorize_logging + + debug " #{apartment_log}#{name} #{sql}#{binds}" + end + + private + + def apartment_log + database = color("[#{Apartment.connection.current_database}] ", ActiveSupport::LogSubscriber::MAGENTA, true) + schema = nil + unless Apartment.connection.schema_search_path.nil? + schema = color("[#{Apartment.connection.schema_search_path.tr('"', '')}] ", + ActiveSupport::LogSubscriber::YELLOW, true) + end + "#{database}#{schema}" + end + + def type_casted_binds(casted_binds) + casted_binds.respond_to?(:call) ? casted_binds.call : casted_binds + end + + def render_bind(attr, value) + case attr + when ActiveModel::Attribute + if attr.type.binary? && attr.value + value = "<#{attr.value_for_database.to_s.bytesize} bytes of binary data>" + end + when Array + attr = attr.first + else + attr = nil + end + + [attr&.name, value] + end + + def colorize_payload_name(name, payload_name) + if payload_name.blank? || payload_name == "SQL" # SQL vs Model Load/Exists + color(name, MAGENTA, true) + else + color(name, CYAN, true) + end + end + + def sql_color(sql) + case sql + when /\A\s*rollback/mi + RED + when /select .*for update/mi, /\A\s*lock/mi + WHITE + when /\A\s*select/i + BLUE + when /\A\s*insert/i + GREEN + when /\A\s*update/i + YELLOW + when /\A\s*delete/i + RED + when /transaction\s*\Z/i + CYAN + else + MAGENTA + end + end + + def logger + ActiveRecord::Base.logger + end + + def debug(progname = nil, &block) + return unless super + + if ActiveRecord::Base.verbose_query_logs + log_query_source + end + end + + def log_query_source + source = extract_query_source_location(caller) + + if source + logger.debug(" ↳ #{source}") + end + end + + def extract_query_source_location(locations) + backtrace_cleaner.clean(locations.lazy).first + end + end +end diff --git a/lib/apartment/railtie.rb b/lib/apartment/railtie.rb index c7d8f6b5..18b47644 100644 --- a/lib/apartment/railtie.rb +++ b/lib/apartment/railtie.rb @@ -45,6 +45,14 @@ class Railtie < Rails::Railtie end end + config.after_initialize do |app| + # NOTE: Load the custom log subscriber if enabled + if Apartment.active_record_log + ActiveSupport::Notifications.unsubscribe 'sql.active_record' + Apartment::LogSubscriber.attach_to :active_record + end + end + # # Ensure rake tasks are loaded # From 4e8c13d5d9aa7e4019e1fbb839680f80c480d907 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Wed, 16 Dec 2020 09:39:00 +0800 Subject: [PATCH 12/15] inheriting and reducing overwritting --- lib/apartment/log_subscriber.rb | 123 ++------------------------------ 1 file changed, 7 insertions(+), 116 deletions(-) diff --git a/lib/apartment/log_subscriber.rb b/lib/apartment/log_subscriber.rb index 38e3b412..cb733f3c 100644 --- a/lib/apartment/log_subscriber.rb +++ b/lib/apartment/log_subscriber.rb @@ -1,56 +1,19 @@ # frozen_string_literal: true module Apartment - class LogSubscriber < ActiveSupport::LogSubscriber - IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"] - - class_attribute :backtrace_cleaner, default: ActiveSupport::BacktraceCleaner.new - - def self.runtime=(value) - ActiveRecord::RuntimeRegistry.sql_runtime = value - end - - def self.runtime - ActiveRecord::RuntimeRegistry.sql_runtime ||= 0 - end - - def self.reset_runtime - rt, self.runtime = runtime, 0 - rt - end - + class LogSubscriber < ActiveRecord::LogSubscriber def sql(event) - self.class.runtime += event.duration - return unless logger.debug? - - payload = event.payload - - return if IGNORE_PAYLOAD_NAMES.include?(payload[:name]) - - name = "#{payload[:name]} (#{event.duration.round(1)}ms)" - name = "CACHE #{name}" if payload[:cached] - sql = payload[:sql] - binds = nil - - if payload[:binds]&.any? - casted_params = type_casted_binds(payload[:type_casted_binds]) + super(event) + end - binds = [] - payload[:binds].each_with_index do |attr, i| - binds << render_bind(attr, casted_params[i]) - end - binds = binds.inspect - binds.prepend(" ") - end + private - name = colorize_payload_name(name, payload[:name]) - sql = color(sql, sql_color(sql), true) if colorize_logging + def debug(progname = nil, &block) + progname = " #{apartment_log}#{progname}" unless progname.nil? - debug " #{apartment_log}#{name} #{sql}#{binds}" + super(progname, &block) end - private - def apartment_log database = color("[#{Apartment.connection.current_database}] ", ActiveSupport::LogSubscriber::MAGENTA, true) schema = nil @@ -60,77 +23,5 @@ def apartment_log end "#{database}#{schema}" end - - def type_casted_binds(casted_binds) - casted_binds.respond_to?(:call) ? casted_binds.call : casted_binds - end - - def render_bind(attr, value) - case attr - when ActiveModel::Attribute - if attr.type.binary? && attr.value - value = "<#{attr.value_for_database.to_s.bytesize} bytes of binary data>" - end - when Array - attr = attr.first - else - attr = nil - end - - [attr&.name, value] - end - - def colorize_payload_name(name, payload_name) - if payload_name.blank? || payload_name == "SQL" # SQL vs Model Load/Exists - color(name, MAGENTA, true) - else - color(name, CYAN, true) - end - end - - def sql_color(sql) - case sql - when /\A\s*rollback/mi - RED - when /select .*for update/mi, /\A\s*lock/mi - WHITE - when /\A\s*select/i - BLUE - when /\A\s*insert/i - GREEN - when /\A\s*update/i - YELLOW - when /\A\s*delete/i - RED - when /transaction\s*\Z/i - CYAN - else - MAGENTA - end - end - - def logger - ActiveRecord::Base.logger - end - - def debug(progname = nil, &block) - return unless super - - if ActiveRecord::Base.verbose_query_logs - log_query_source - end - end - - def log_query_source - source = extract_query_source_location(caller) - - if source - logger.debug(" ↳ #{source}") - end - end - - def extract_query_source_location(locations) - backtrace_cleaner.clean(locations.lazy).first - end end end From cef40ecf271a2cdba32246f87a92bc1e37489eba Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Wed, 16 Dec 2020 10:49:41 +0800 Subject: [PATCH 13/15] added missing information to the change log --- CHANGELOG.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a0b7741..a2d9fc78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,9 +4,19 @@ [Full Changelog](https://github.com/rails-on-services/apartment/compare/v2.7.2...HEAD) +**Implemented enhancements:** + +- Uses a transaction to create a tenant [\#66](https://github.com/rails-on-services/apartment/issues/66) + +**Fixed bugs:** + +- Fix seeding errors [\#86](https://github.com/rails-on-services/apartment/issues/86) +- When tests run in a transaction, new tenants in tests fail to create [\#123](https://github.com/rails-on-services/apartment/issues/123) + **Closed issues:** - Improve changelog automatic generation [\#98](https://github.com/rails-on-services/apartment/issues/98) +- Relaxes dependencies to allow rails 6.1 [\#121](https://github.com/rails-on-services/apartment/issues/121) ## [v2.7.2](https://github.com/rails-on-services/apartment/tree/v2.7.2) (2020-07-17) From 02044bc09cf0b1c66f2620badc4341c56d9b41e8 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Wed, 16 Dec 2020 21:53:20 +0800 Subject: [PATCH 14/15] Updated changelog and readme --- CHANGELOG.md | 6 ++++++ README.md | 7 +++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a2d9fc78..345ae68e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,12 @@ - Fix seeding errors [\#86](https://github.com/rails-on-services/apartment/issues/86) - When tests run in a transaction, new tenants in tests fail to create [\#123](https://github.com/rails-on-services/apartment/issues/123) +- Reverted unsafe initializer - introduces the possibility of disabling the initial connection to the database via +environment variable. Relates to the following tickets/PRs: + - [#113](https://github.com/rails-on-services/apartment/issues/113) + - [#39](https://github.com/rails-on-services/apartment/pull/39) + - [#53](https://github.com/rails-on-services/apartment/pull/53) + - [#118](https://github.com/rails-on-services/apartment/pull/118) **Closed issues:** diff --git a/README.md b/README.md index f296892c..5704b97e 100644 --- a/README.md +++ b/README.md @@ -594,11 +594,10 @@ module Apartment end ``` -## Running rails console without a connection +## Running rails console without a connection to the database -Before this fork, running rails with the gem installed would connect to the database -which is different than the default behavior. To disable this initial -connection, just run with `APARTMENT_DISABLE_INIT` set to something: +By default, once apartment starts, it establishes a connection to the database. It is possible to +disable this initial connection, by running with `APARTMENT_DISABLE_INIT` set to something: ```shell $ APARTMENT_DISABLE_INIT=true DATABASE_URL=postgresql://localhost:1234/buk_development bin/rails runner 'puts 1' From 344b039dc8e17b4b58440c70aef873f45c071059 Mon Sep 17 00:00:00 2001 From: Rui Baltazar Date: Wed, 16 Dec 2020 22:29:00 +0800 Subject: [PATCH 15/15] Version Bump --- lib/apartment/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/apartment/version.rb b/lib/apartment/version.rb index 8191b485..398dd80a 100644 --- a/lib/apartment/version.rb +++ b/lib/apartment/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Apartment - VERSION = '2.7.2' + VERSION = '2.8.0' end