From e729927b770ca5e6ed6bfa33bc4407f96a0831ac Mon Sep 17 00:00:00 2001 From: justin talbott Date: Fri, 12 Jan 2024 00:14:30 -0400 Subject: [PATCH] conditionally enable crypto during install based on PG version (#43) * conditionally enable crypto during install based on PG version * stree --- lib/generators/hoardable/install_generator.rb | 11 +++++++++++ lib/generators/hoardable/templates/install.rb.erb | 3 ++- ...l_generator_test.rb => test_install_generator.rb} | 12 +++--------- 3 files changed, 16 insertions(+), 10 deletions(-) rename test/{install_generator_test.rb => test_install_generator.rb} (59%) diff --git a/lib/generators/hoardable/install_generator.rb b/lib/generators/hoardable/install_generator.rb index b831e86..01d3df9 100644 --- a/lib/generators/hoardable/install_generator.rb +++ b/lib/generators/hoardable/install_generator.rb @@ -32,6 +32,17 @@ def create_functions end end + no_tasks do + def postgres_version + ActiveRecord::Base + .connection + .select_value("SELECT VERSION()") + .match(/[0-9]{1,2}([,.][0-9]{1,2})?/)[ + 0 + ].to_f + end + end + def self.next_migration_number(dir) ::ActiveRecord::Generators::Base.next_migration_number(dir) end diff --git a/lib/generators/hoardable/templates/install.rb.erb b/lib/generators/hoardable/templates/install.rb.erb index ccccd33..3544af7 100644 --- a/lib/generators/hoardable/templates/install.rb.erb +++ b/lib/generators/hoardable/templates/install.rb.erb @@ -2,7 +2,8 @@ class InstallHoardable < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>] def change - create_function :hoardable_prevent_update_id + <% if postgres_version < 13 %>enable_extension :pgcrypto + <% end %>create_function :hoardable_prevent_update_id create_function :hoardable_source_set_id create_function :hoardable_version_prevent_update create_enum :hoardable_operation, %w[update delete insert] diff --git a/test/install_generator_test.rb b/test/test_install_generator.rb similarity index 59% rename from test/install_generator_test.rb rename to test/test_install_generator.rb index 72e96cc..f3dcd99 100644 --- a/test/install_generator_test.rb +++ b/test/test_install_generator.rb @@ -2,7 +2,7 @@ require "helper" -class InstallGeneratorTest < Rails::Generators::TestCase +class TestInstallGenerator < Rails::Generators::TestCase tests Hoardable::InstallGenerator destination tmp_dir setup :prepare_destination @@ -11,14 +11,8 @@ class InstallGeneratorTest < Rails::Generators::TestCase run_generator assert_file "config/initializers/hoardable.rb", /Hoardable.enabled = true/ assert_migration "db/migrate/install_hoardable.rb" do |migration| - if Hoardable::InstallGenerator.supports_schema_enums? - assert_match(/create_enum :hoardable_operation, %w\[update delete insert\]/, migration) - else - assert_match( - /CREATE TYPE hoardable_operation AS ENUM \('update', 'delete', 'insert'\);/, - migration - ) - end + assert_match(/create_enum :hoardable_operation, %w\[update delete insert\]/, migration) + assert_no_match(/enable_extension :pgcrypto/, migration) end assert_file( "db/functions/hoardable_source_set_id_v01.sql",