diff options
author | Dan McClain <git@danseaver.com> | 2012-08-19 18:02:34 -0400 |
---|---|---|
committer | Dan McClain <git@danseaver.com> | 2012-09-14 08:43:47 -0400 |
commit | 4544d2bc90bea93c38bb21d912dba00f51cf620f (patch) | |
tree | 44a38fe8ba8c2d8ca1c62839a973557fe2278c0d /activerecord/lib/active_record/connection_adapters/abstract | |
parent | 84ba499b1645230dd90f46fa63e5d071ada49f37 (diff) | |
download | rails-4544d2bc90bea93c38bb21d912dba00f51cf620f.tar.gz rails-4544d2bc90bea93c38bb21d912dba00f51cf620f.tar.bz2 rails-4544d2bc90bea93c38bb21d912dba00f51cf620f.zip |
Moves column dump specific code to a module included in AbstractAdapter
Having column related schema dumper code in the AbstractAdapter. The
code remains the same, but by placing it in the AbstractAdapter, we can
then overwrite it with Adapter specific methods that will help with
Adapter specific data types.
The goal of moving this code here is to create a new migration key for
PostgreSQL's array type. Since any datatype can be an array, the goal is
to have ':array => true' as a migration option, turning the datatype
into an array. I've implemented this in postgres_ext, the syntax is
shown here: https://github.com/dockyard/postgres_ext#arrays
Adds array migration support
Adds array_test.rb outlining the test cases for array data type
Adds pg_array_parser to Gemfile for testing
Adds pg_array_parser to postgresql_adapter (unused in this commit)
Adds schema dump support for arrays
Adds postgres array type casting support
Updates changelog, adds note for inet and cidr support, which I forgot to add before
Removing debugger, Adds pg_array_parser to JRuby platform
Removes pg_array_parser requirement, creates ArrayParser module used by
PostgreSQLAdapter
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/abstract')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb new file mode 100644 index 0000000000..9d6111b51e --- /dev/null +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb @@ -0,0 +1,56 @@ +module ActiveRecord + module ConnectionAdapters # :nodoc: + # The goal of this module is to move Adapter specific column + # definitions to the Adapter instead of having it in the schema + # dumper itself. This code represents the normal case. + # We can then redefine how certain data types may be handled in the schema dumper on the + # Adapter level by over-writing this code inside the database spececific adapters + module ColumnDumper + def column_spec(column, types) + spec = prepare_column_options(column, types) + (spec.keys - [:name, :type]).each{ |k| spec[k].insert(0, "#{k.to_s}: ")} + spec + end + + # This can be overridden on a Adapter level basis to support other + # extended datatypes (Example: Adding an array option in the + # PostgreSQLAdapter) + def prepare_column_options(column, types) + spec = {} + spec[:name] = column.name.inspect + + # AR has an optimization which handles zero-scale decimals as integers. This + # code ensures that the dumper still dumps the column as a decimal. + spec[:type] = if column.type == :integer && /^(numeric|decimal)/ =~ column.sql_type + 'decimal' + else + column.type.to_s + end + spec[:limit] = column.limit.inspect if column.limit != types[column.type][:limit] && spec[:type] != 'decimal' + spec[:precision] = column.precision.inspect if column.precision + spec[:scale] = column.scale.inspect if column.scale + spec[:null] = 'false' unless column.null + spec[:default] = default_string(column.default) if column.has_default? + spec + end + + # Lists the valid migration options + def migration_keys + [:name, :limit, :precision, :scale, :default, :null] + end + + private + + def default_string(value) + case value + when BigDecimal + value.to_s + when Date, DateTime, Time + "'#{value.to_s(:db)}'" + else + value.inspect + end + end + end + end +end |