Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Added support for "auto-sizing" of Text fields (in MySQL)
* Works much the same way as it does for Integer primitives
  • Loading branch information
jpr5 authored and dkubb committed Sep 7, 2009
1 parent e022b85 commit f85d827
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
25 changes: 25 additions & 0 deletions lib/dm-core/migrations.rb
Expand Up @@ -376,6 +376,7 @@ def property_schema_hash(property)
schema = super

if schema[:primitive] == 'TEXT'
schema[:primitive] = text_column_statement(property.length)
schema.delete(:default)
end

Expand Down Expand Up @@ -419,6 +420,30 @@ def show_variable(name)

private

# Return SQL statement for the text column
#
# @param [Integer] length
# the max allowed length
#
# @return [String]
# the statement to create the text column
#
# @api private
def text_column_statement(length)
if length < 2**8 then 'TINYTEXT'
elsif length < 2**16 then 'TEXT'
elsif length < 2**24 then 'MEDIUMTEXT'
elsif length < 2**32 then 'LONGTEXT'

# http://www.postgresql.org/files/documentation/books/aw_pgsql/node90.html
# Implies that PostgreSQL doesn't have a size limit on text
# fields, so this param validation happens here instead of
# DM::Property#initialize.
else
raise ArgumentError, "length of #{length} exceeds maximum size supported"
end
end

# Return SQL statement for the integer column
#
# @param [Range] range
Expand Down
41 changes: 40 additions & 1 deletion spec/public/migrations_spec.rb
Expand Up @@ -190,7 +190,7 @@ class Article
end

it "should create a #{statement} column" do
@output.last.should == "CREATE TABLE \"blog_articles\" (\"id\" #{statement} NOT NULL, PRIMARY KEY(\"id\")) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci"
@output.last.should =~ %r{\ACREATE TABLE "blog_articles" \("id" #{Regexp.escape(statement)} NOT NULL, PRIMARY KEY\("id"\)\) ENGINE = InnoDB CHARACTER SET [a-z\d]+ COLLATE (?:[a-z\d](?:_?[a-z\d]+)*)\z}
end

options.only(:min, :max).each do |key, value|
Expand All @@ -204,6 +204,45 @@ class Article
end
end
end

describe 'Text property' do
before :all do
@model.property(:id, DataMapper::Types::Serial)
end

[
[ 0, 'TINYTEXT' ],
[ 1, 'TINYTEXT' ],
[ 255, 'TINYTEXT' ],
[ 256, 'TEXT' ],
[ 65535, 'TEXT' ],
[ 65536, 'MEDIUMTEXT' ],
[ 16777215, 'MEDIUMTEXT' ],
[ 16777216, 'LONGTEXT' ],
[ 4294967295, 'LONGTEXT' ],

[ nil, 'TEXT' ],
].each do |length, statement|
options = {}
options[:length] = length if length

describe "with a length of #{length}" do
before :all do
@property = @model.property(:body, DataMapper::Types::Text, options)

@response = capture_log(DataObjects::Mysql) { @model.auto_migrate! }
end

it 'should return true' do
@response.should be_true
end

it "should create a #{statement} column" do
@output.last.should =~ %r{\ACREATE TABLE "blog_articles" \("id" INT\(10\) UNSIGNED NOT NULL AUTO_INCREMENT, "body" #{Regexp.escape(statement)}, PRIMARY KEY\("id"\)\) ENGINE = InnoDB CHARACTER SET [a-z\d]+ COLLATE (?:[a-z\d](?:_?[a-z\d]+)*)\z}
end
end
end
end
end
end

Expand Down

0 comments on commit f85d827

Please sign in to comment.