diff --git a/activerecord-ksuid/CHANGELOG.md b/activerecord-ksuid/CHANGELOG.md index 816c8a8..3d9af91 100644 --- a/activerecord-ksuid/CHANGELOG.md +++ b/activerecord-ksuid/CHANGELOG.md @@ -4,7 +4,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). -## [1.0.0](https://github.com/michaelherold/ksuid/compare/v0.5.0...v1.0.0) - 2023-02-25 +## [Unreleased](https://github.com/michaelherold/ksuid-ruby/compare/v1.0.0...main) + +### Fixed + +- Using a `ksuid` or `ksuid_binary` type with `add_column` in migrations will now work correctly without throwing an error about an unknown column type. +- Using a `ksuid` or `ksuid_binary` type with `rails generate [model|resource]` will now work correctly without throwing an error about an unknown column type. + +## [1.0.0](https://github.com/michaelherold/ksuid-ruby/compare/v0.5.0...v1.0.0) - 2023-02-25 ### Added diff --git a/activerecord-ksuid/lib/active_record/ksuid/railtie.rb b/activerecord-ksuid/lib/active_record/ksuid/railtie.rb index 3e95a6b..1688f5b 100644 --- a/activerecord-ksuid/lib/active_record/ksuid/railtie.rb +++ b/activerecord-ksuid/lib/active_record/ksuid/railtie.rb @@ -18,9 +18,31 @@ class Railtie < ::Rails::Railtie ActiveSupport.on_load :active_record do require 'active_record/ksuid/table_definition' - ActiveRecord::ConnectionAdapters::TableDefinition.include( - ActiveRecord::KSUID::TableDefinition - ) + ActiveRecord::ConnectionAdapters::AbstractAdapter.descendants.each do |adapter| + unless (types = adapter::NATIVE_DATABASE_TYPES) + Rails.logger <<~MSG.strip_heredoc + #{adapter.name} was unable to be patched by activerecord-ksuid for + usage in generators. Please raise an issue with details on which + adapter this is. + MSG + + next + end + + if (string_type = types[:string]) + adapter::NATIVE_DATABASE_TYPES[:ksuid] = { name: string_type[:name] } + end + + if (binary_type = types[:binary]) + adapter::NATIVE_DATABASE_TYPES[:ksuid_binary] = { name: binary_type[:name] } + end + end + + ActiveRecord::ConnectionAdapters::TableDefinition.descendants.each do |defn| + next unless defn.name + + defn.prepend(ActiveRecord::KSUID::TableDefinition) + end end end end diff --git a/activerecord-ksuid/lib/active_record/ksuid/table_definition.rb b/activerecord-ksuid/lib/active_record/ksuid/table_definition.rb index fda031c..347ba7e 100644 --- a/activerecord-ksuid/lib/active_record/ksuid/table_definition.rb +++ b/activerecord-ksuid/lib/active_record/ksuid/table_definition.rb @@ -52,6 +52,28 @@ def ksuid(*args, **options) def ksuid_binary(*args, **options) args.each { |name| column(name, :binary, **options.merge(limit: 20)) } end + + # Monkey-patches defining a new column within a table + # + # @api private + # @private + # + # @param name [String, Symbol] the name of the column + # @param type [String, Symbol] the type of the column + # @param options [Hash] options for the definition + # @return [ActiveRecord::ConnectionAdapters::ColumnDefinition] + def new_column_definition(name, type, **options) + case type.to_s + when 'ksuid' + prefix_length = options.delete(:prefix)&.length || 0 + + super(name, :string, **options.merge(limit: 27 + prefix_length)) + when 'ksuid_binary' + super(name, :binary, **options.merge(limit: 20)) + else + super + end + end end end end diff --git a/activerecord-ksuid/spec/active_record/ksuid/railtie_spec.rb b/activerecord-ksuid/spec/active_record/ksuid/railtie_spec.rb index a0f9990..9fe576d 100644 --- a/activerecord-ksuid/spec/active_record/ksuid/railtie_spec.rb +++ b/activerecord-ksuid/spec/active_record/ksuid/railtie_spec.rb @@ -257,6 +257,35 @@ class EventPrefix < ActiveRecord::Base end end + context 'when writing a migration that adds KSUID fields' do + it 'can use the ksuid and ksuid_binary field types' do + connection = ActiveRecord::Base.connection + + connection.create_table :field_tests do |t| + t.string :name + end + + expect do + connection.add_column :field_tests, :id_one, :ksuid + connection.add_column :field_tests, :id_two, :ksuid_binary + end.not_to raise_error + ensure + connection.drop_table :field_tests, if_exists: true + end + end + + context 'when using a generator that adds KSUID fields' do + require 'rails/generators' + require 'rails/generators/generated_attribute' + + it 'can use the ksuid and ksuid_binary field types' do + aggregate_failures do + expect(Rails::Generators::GeneratedAttribute.valid_type?(:ksuid)).to be true + expect(Rails::Generators::GeneratedAttribute.valid_type?(:ksuid_binary)).to be true + end + end + end + matcher :issue_sql_queries do |expected| supports_block_expectations diff --git a/activerecord-ksuid/spec/doctest_helper.rb b/activerecord-ksuid/spec/doctest_helper.rb index e5a4332..2dfa436 100644 --- a/activerecord-ksuid/spec/doctest_helper.rb +++ b/activerecord-ksuid/spec/doctest_helper.rb @@ -6,11 +6,11 @@ require 'active_record/ksuid/railtie' -ActiveRecord::KSUID::Railtie.initializers.each(&:run) -ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Base) - ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') ActiveRecord::Base.logger = Logger.new(IO::NULL) ActiveRecord::Schema.verbose = false +ActiveRecord::KSUID::Railtie.initializers.each(&:run) +ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Base) + ActiveSupport::Deprecation.instance.silenced = true diff --git a/ksuid/CHANGELOG.md b/ksuid/CHANGELOG.md index fd2b94a..9f8fa7b 100644 --- a/ksuid/CHANGELOG.md +++ b/ksuid/CHANGELOG.md @@ -4,13 +4,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). -## [1.0.0](https://github.com/michaelherold/ksuid/compare/v0.5.0...v1.0.0) - 2023-02-25 +## [1.0.0](https://github.com/michaelherold/ksuid-ruby/compare/v0.5.0...v1.0.0) - 2023-02-25 ### Removed - Extracted the ActiveRecord functionality into its own gem, `activerecord-ksuid`. It is API-compatible with v0.5.0 so to restore functionality, you should only need to add the new gem to your application. See [the upgrading notice](./UPGRADING.md) for more information. -## [0.5.0](https://github.com/michaelherold/ksuid/compare/v0.4.0...v0.5.0) - 2022-08-18 +## [0.5.0](https://github.com/michaelherold/ksuid-ruby/compare/v0.4.0...v0.5.0) - 2022-08-18 ### Added @@ -26,7 +26,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a - The compatibility check for the Base62 implementation in the gem is about 10x faster now. The original optimization did not optimize as much due to an error with the benchmark. This change has a tested benchmark that shows a great improvement. Note that this is a micro-optimization and we see no real performance gain in the parsing of KSUID strings. -## [0.4.0](https://github.com/michaelherold/ksuid/compare/v0.3.0...v0.4.0) - 2022-07-29 +## [0.4.0](https://github.com/michaelherold/ksuid-ruby/compare/v0.3.0...v0.4.0) - 2022-07-29 ### Added @@ -37,13 +37,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a - `ActiveRecord::QueryMethods#include` works as expected now due to the fix on the value object semantics of `KSUID::Type`. - Binary KSUID primary and foreign keys work as expected on JRuby. -## [0.3.0](https://github.com/michaelherold/ksuid/compare/v0.2.0...v0.3.0) - 2021-10-07 +## [0.3.0](https://github.com/michaelherold/ksuid-ruby/compare/v0.2.0...v0.3.0) - 2021-10-07 ### Added - A utility function for converting from a hexidecimal-encoded string to a byte string. This is necessary to handle the default encoding of binary fields within PostgreSQL. -## [0.2.0](https://github.com/michaelherold/ksuid/compare/v0.1.0...v0.2.0) - 2020-11-11 +## [0.2.0](https://github.com/michaelherold/ksuid-ruby/compare/v0.1.0...v0.2.0) - 2020-11-11 ### Added @@ -54,7 +54,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a - The `KSUID::Type#inspect` method now makes it much easier to see what you're looking at in the console when you're debugging. -## [0.1.0](https://github.com/michaelherold/ksuid/tree/v0.1.0) - 2017-11-05 +## [0.1.0](https://github.com/michaelherold/ksuid-ruby/tree/v0.1.0) - 2017-11-05 ### Added