Tidy up modules and on_load points
This commit is contained in:
@@ -17,8 +17,6 @@ module SQLiteFTS5SchemaDumperFix
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
ActiveSupport.on_load(:active_record) do
|
ActiveSupport.on_load(:active_record_sqlite3adapter) do
|
||||||
if defined?(ActiveRecord::ConnectionAdapters::SQLite3::SchemaDumper)
|
ActiveRecord::ConnectionAdapters::SQLite3::SchemaDumper.prepend(SQLiteFTS5SchemaDumperFix)
|
||||||
ActiveRecord::ConnectionAdapters::SQLite3::SchemaDumper.prepend(SQLiteFTS5SchemaDumperFix)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -39,10 +39,6 @@ module TableDefinitionColumnLimits
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
ActiveSupport.on_load(:active_record) do
|
|
||||||
ActiveRecord::ConnectionAdapters::TableDefinition.prepend(TableDefinitionColumnLimits)
|
|
||||||
end
|
|
||||||
|
|
||||||
# For SQLite: append inline CHECK constraints to enforce string/text length limits.
|
# For SQLite: append inline CHECK constraints to enforce string/text length limits.
|
||||||
# since SQLite doesn't natively enforce VARCHAR/TEXT length limits.
|
# since SQLite doesn't natively enforce VARCHAR/TEXT length limits.
|
||||||
module SQLiteColumnLimitCheckConstraints
|
module SQLiteColumnLimitCheckConstraints
|
||||||
@@ -65,6 +61,10 @@ module SQLiteColumnLimitCheckConstraints
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
ActiveSupport.on_load(:active_record) do
|
||||||
|
ActiveRecord::ConnectionAdapters::TableDefinition.prepend(TableDefinitionColumnLimits)
|
||||||
|
end
|
||||||
|
|
||||||
ActiveSupport.on_load(:active_record_sqlite3adapter) do
|
ActiveSupport.on_load(:active_record_sqlite3adapter) do
|
||||||
ActiveRecord::ConnectionAdapters::SQLite3::SchemaCreation.prepend(SQLiteColumnLimitCheckConstraints)
|
ActiveRecord::ConnectionAdapters::SQLite3::SchemaCreation.prepend(SQLiteColumnLimitCheckConstraints)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,95 +1,83 @@
|
|||||||
# Automatically use UUID type for all binary(16) columns
|
# Automatically use UUID type for all binary(16) columns
|
||||||
|
|
||||||
ActiveSupport.on_load(:active_record) do
|
module MysqlUuidAdapter
|
||||||
module MysqlUuidAdapter
|
extend ActiveSupport::Concern
|
||||||
# Add UUID to MySQL's native database types
|
|
||||||
|
# Override lookup_cast_type to recognize binary(16) as UUID type
|
||||||
|
def lookup_cast_type(sql_type)
|
||||||
|
if sql_type == "binary(16)"
|
||||||
|
ActiveRecord::Type.lookup(:uuid, adapter: :trilogy)
|
||||||
|
else
|
||||||
|
super
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class_methods do
|
||||||
def native_database_types
|
def native_database_types
|
||||||
@native_database_types_with_uuid ||= super.merge(uuid: { name: "binary", limit: 16 })
|
@native_database_types_with_uuid ||= super.merge(uuid: { name: "binary", limit: 16 })
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Override lookup_cast_type to recognize binary(16) as UUID type
|
module SqliteUuidAdapter
|
||||||
def lookup_cast_type(sql_type)
|
extend ActiveSupport::Concern
|
||||||
if sql_type == "binary(16)"
|
|
||||||
ActiveRecord::Type.lookup(:uuid, adapter: :trilogy)
|
# Override lookup_cast_type to recognize BLOB as UUID type
|
||||||
else
|
def lookup_cast_type(sql_type)
|
||||||
super
|
if sql_type == "blob(16)"
|
||||||
end
|
ActiveRecord::Type.lookup(:uuid, adapter: :sqlite3)
|
||||||
|
else
|
||||||
|
super
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if defined?(ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter)
|
# Override fetch_type_metadata to preserve UUID type and limit
|
||||||
ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter.prepend(MysqlUuidAdapter)
|
def fetch_type_metadata(sql_type)
|
||||||
|
if sql_type == "blob(16)"
|
||||||
|
ActiveRecord::ConnectionAdapters::SqlTypeMetadata.new(
|
||||||
|
sql_type: sql_type,
|
||||||
|
type: :uuid,
|
||||||
|
limit: 16
|
||||||
|
)
|
||||||
|
else
|
||||||
|
super
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module SqliteUuidAdapter
|
class_methods do
|
||||||
def native_database_types
|
def native_database_types
|
||||||
@native_database_types_with_uuid ||= super.merge(uuid: { name: "blob", limit: 16 })
|
@native_database_types_with_uuid ||= super.merge(uuid: { name: "blob", limit: 16 })
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Override lookup_cast_type to recognize BLOB as UUID type
|
module SchemaDumperUuidType
|
||||||
def lookup_cast_type(sql_type)
|
# Map binary(16) and blob(16) columns to :uuid type in schema.rb
|
||||||
if sql_type == "blob(16)"
|
def schema_type(column)
|
||||||
ActiveRecord::Type.lookup(:uuid, adapter: :sqlite3)
|
if column.sql_type == "binary(16)" || column.sql_type == "blob(16)"
|
||||||
else
|
:uuid
|
||||||
super
|
else
|
||||||
end
|
super
|
||||||
end
|
|
||||||
|
|
||||||
# Override fetch_type_metadata to preserve UUID type and limit
|
|
||||||
def fetch_type_metadata(sql_type)
|
|
||||||
if sql_type == "blob(16)"
|
|
||||||
ActiveRecord::ConnectionAdapters::SqlTypeMetadata.new(
|
|
||||||
sql_type: sql_type,
|
|
||||||
type: :uuid,
|
|
||||||
limit: 16
|
|
||||||
)
|
|
||||||
else
|
|
||||||
super
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if defined?(ActiveRecord::ConnectionAdapters::SQLite3Adapter)
|
module TableDefinitionUuidSupport
|
||||||
ActiveRecord::ConnectionAdapters::SQLite3Adapter.prepend(SqliteUuidAdapter)
|
def uuid(name, **options)
|
||||||
|
column(name, :uuid, **options)
|
||||||
# Also add UUID to class-level native_database_types
|
|
||||||
ActiveRecord::ConnectionAdapters::SQLite3Adapter.class_eval do
|
|
||||||
@native_database_types = nil # Clear cache
|
|
||||||
|
|
||||||
class << self
|
|
||||||
alias_method :original_native_database_types, :native_database_types
|
|
||||||
|
|
||||||
def native_database_types
|
|
||||||
@native_database_types_with_uuid ||= original_native_database_types.merge(uuid: { name: "blob", limit: 16 })
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
module SchemaDumperUuidType
|
|
||||||
# Map binary(16) and blob(16) columns to :uuid type in schema.rb
|
|
||||||
def schema_type(column)
|
|
||||||
if column.sql_type == "binary(16)" || column.sql_type == "blob(16)"
|
|
||||||
:uuid
|
|
||||||
else
|
|
||||||
super
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if defined?(ActiveRecord::ConnectionAdapters::MySQL::SchemaDumper)
|
|
||||||
ActiveRecord::ConnectionAdapters::MySQL::SchemaDumper.prepend(SchemaDumperUuidType)
|
|
||||||
end
|
|
||||||
|
|
||||||
if defined?(ActiveRecord::ConnectionAdapters::SQLite3::SchemaDumper)
|
|
||||||
ActiveRecord::ConnectionAdapters::SQLite3::SchemaDumper.prepend(SchemaDumperUuidType)
|
|
||||||
end
|
|
||||||
|
|
||||||
module TableDefinitionUuidSupport
|
|
||||||
def uuid(name, **options)
|
|
||||||
column(name, :uuid, **options)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
ActiveSupport.on_load(:active_record) do
|
||||||
ActiveRecord::ConnectionAdapters::TableDefinition.prepend(TableDefinitionUuidSupport)
|
ActiveRecord::ConnectionAdapters::TableDefinition.prepend(TableDefinitionUuidSupport)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
ActiveSupport.on_load(:active_record_trilogyadapter) do
|
||||||
|
ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter.prepend(MysqlUuidAdapter)
|
||||||
|
ActiveRecord::ConnectionAdapters::MySQL::SchemaDumper.prepend(SchemaDumperUuidType)
|
||||||
|
end
|
||||||
|
|
||||||
|
ActiveSupport.on_load(:active_record_sqlite3adapter) do
|
||||||
|
ActiveRecord::ConnectionAdapters::SQLite3Adapter.prepend(SqliteUuidAdapter)
|
||||||
|
ActiveRecord::ConnectionAdapters::SQLite3::SchemaDumper.prepend(SchemaDumperUuidType)
|
||||||
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user