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

Fix add_column and generator behavior for missing type #49

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 8 additions & 1 deletion activerecord-ksuid/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 25 additions & 3 deletions activerecord-ksuid/lib/active_record/ksuid/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions activerecord-ksuid/lib/active_record/ksuid/table_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<Symbol, Object>] 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
29 changes: 29 additions & 0 deletions activerecord-ksuid/spec/active_record/ksuid/railtie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions activerecord-ksuid/spec/doctest_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 6 additions & 6 deletions ksuid/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down
Loading