Skip to content

Commit

Permalink
PostgreSQL and SQLite, remove varchar limit. [Vladimir Sazhin & Toms …
Browse files Browse the repository at this point in the history
…Mikoss & Yves Senn]

There is no reason for the PG adapter to have a default limit of 255 on :string
columns. See this snippet from the PG docs:

    Tip: There is no performance difference among these three types, apart
    from increased storage space when using the blank-padded type, and a
    few extra CPU cycles to check the length when storing into a
    length-constrained column. While character(n) has performance
    advantages in some other database systems, there is no such advantage
    in PostgreSQL; in fact character(n) is usually the slowest of the
    three because of its additional storage costs. In most situations text
    or character varying should be used instead.
  • Loading branch information
senny committed Apr 4, 2014
1 parent 362203e commit f4226c3
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 5 deletions.
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
* PostgreSQL and SQLite string columns no longer have a default limit of 255.

Fixes #13435, #9153.

*Vladimir Sazhin*, *Toms Mikoss*, *Yves Senn*

* Block a few default Class methods as scope name.

For instance, this will raise:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class Table < ActiveRecord::ConnectionAdapters::Table

NATIVE_DATABASE_TYPES = {
primary_key: "serial primary key",
string: { name: "character varying", limit: 255 },
string: { name: "character varying" },
text: { name: "text" },
integer: { name: "integer" },
float: { name: "float" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class SQLite3Adapter < AbstractAdapter

NATIVE_DATABASE_TYPES = {
primary_key: 'INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL',
string: { name: "varchar", limit: 255 },
string: { name: "varchar" },
text: { name: "text" },
integer: { name: "integer" },
float: { name: "float" },
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/adapters/postgresql/array_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def setup

def test_column
assert_equal :string, @column.type
assert_equal "character varying(255)", @column.sql_type
assert_equal "character varying", @column.sql_type
assert @column.array
assert_not @column.text?
assert_not @column.number?
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/reflection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_content_columns

def test_column_string_type_and_limit
assert_equal :string, @first.column_for_attribute("title").type
assert_equal 255, @first.column_for_attribute("title").limit
assert_equal 250, @first.column_for_attribute("title").limit
end

def test_column_null_not_null
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/schema/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ def create_table(*args, &block)
end

create_table :topics, force: true do |t|
t.string :title
t.string :title, limit: 250
t.string :author_name
t.string :author_email_address
if mysql_56?
Expand Down

0 comments on commit f4226c3

Please sign in to comment.