Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

fix sqlite3 column default containing newline character #7088

Closed
wants to merge 1 commit into from

4 participants

@mdominiak

Due to incorrect parsing of sqlite3 string column default containing newline character, any string column default containing newline character, e.g. "\n" or "\nRegards\nMatt" is parsed incorrectly by sqlite3 adapter and as a result contains enclosing quote characters ('), e.g. "'\n'" or "'\nRegards\nMatt'".

The regular expressions stripping column defaults out of enclosing quote/double quote characters did not take into consideration column defaults containing newline characters. More specifically /^'(.*)'$/ regular expression would not match "'\n'" since dot (.) in this expression is intended to match any character except for newline character and hence column default was not stripped correctly out of enclosing quotes returned by database when querying for table structure.

@mdominiak mdominiak fix sqlite3 column default containing newline
Due to incorrect parsing of text/string column default containing
newline character, string column default "\n" ended up being
"'\n'" default.
60548b0
@rafaelfranca rafaelfranca commented on the diff
...d/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb
@@ -272,6 +272,19 @@ def test_columns_with_default
}
assert_equal 10, column.default
end
+
+ def test_column_with_newline_default
+ @conn.execute <<-eosql
@rafaelfranca Owner

This will change the state of the database in all the tests. I think we should drop the table after this test using a ensure block

In setup there is new in-memory sqlite3 database created before each test so created tables are not visible to other tests:
https://github.com/rails/rails/blob/master/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb#L14

I followed the convention in sqlite3_adapter_test.rb. There are many other tables created in similar fashion there and not dropped afterwards. For example, that is the other test for default value:
https://github.com/rails/rails/blob/master/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb#L263

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@steveklabnik
Collaborator

This will need a rebase.

@tenderlove
Owner

I think this was fixed in 3e8ab91

@mdominiak

Yes, this is the same fix as in 3e8ab91. I'm closing this issue.

@mdominiak mdominiak closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 18, 2012
  1. @mdominiak

    fix sqlite3 column default containing newline

    mdominiak authored
    Due to incorrect parsing of text/string column default containing
    newline character, string column default "\n" ended up being
    "'\n'" default.
This page is out of date. Refresh to see the latest.
View
4 activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
@@ -380,9 +380,9 @@ def columns(table_name) #:nodoc:
case field["dflt_value"]
when /^null$/i
field["dflt_value"] = nil
- when /^'(.*)'$/
+ when /\A'(.*)'\z/m
field["dflt_value"] = $1.gsub("''", "'")
- when /^"(.*)"$/
+ when /\A"(.*)"\z/m
field["dflt_value"] = $1.gsub('""', '"')
end
View
13 activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb
@@ -272,6 +272,19 @@ def test_columns_with_default
}
assert_equal 10, column.default
end
+
+ def test_column_with_newline_default
+ @conn.execute <<-eosql
@rafaelfranca Owner

This will change the state of the database in all the tests. I think we should drop the table after this test using a ensure block

In setup there is new in-memory sqlite3 database created before each test so created tables are not visible to other tests:
https://github.com/rails/rails/blob/master/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb#L14

I followed the convention in sqlite3_adapter_test.rb. There are many other tables created in similar fashion there and not dropped afterwards. For example, that is the other test for default value:
https://github.com/rails/rails/blob/master/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb#L263

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ CREATE TABLE column_with_newline_default (
+ id integer PRIMARY KEY AUTOINCREMENT,
+ body string default '\nRegards\nMatt'
+ )
+ eosql
+ column = @conn.columns('column_with_newline_default').find { |x|
+ x.name == 'body'
+ }
+ assert_equal "\nRegards\nMatt", column.default
+ end
def test_columns_with_not_null
@conn.execute <<-eosql
Something went wrong with that request. Please try again.