Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

conditionally enable crypto during install based on PG version #43

Merged
merged 2 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/generators/hoardable/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/generators/hoardable/templates/install.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand Down