Skip to content
This repository
Browse code

Cleanup SQLite AUTOINCREMENT: exclude sqlite_sequence table, factor o…

…ut feature availability check.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5520 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 50ee332c4aa97aa797e814d3ab6ae63a0f8351e2 1 parent fda4330
Jeremy Kemper jeremy authored
2  activerecord/CHANGELOG
@@ -6,7 +6,7 @@
6 6
7 7 * Don't inspect unloaded associations. #2905 [lmarlow]
8 8
9   -* SQLite: use AUTOINCREMENT primary key in >= 3.1.0. #6588 [careo]
  9 +* SQLite: use AUTOINCREMENT primary key in >= 3.1.0. #6588, #6616 [careo, lukfugl]
10 10
11 11 * Cache inheritance_column. #6592 [Stefan Kaes]
12 12
14 activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
@@ -106,6 +106,10 @@ def supports_count_distinct? #:nodoc:
106 106 sqlite_version >= '3.2.6'
107 107 end
108 108
  109 + def supports_autoincrement? #:nodoc:
  110 + sqlite_version >= '3.1.0'
  111 + end
  112 +
109 113 def native_database_types #:nodoc:
110 114 {
111 115 :primary_key => default_primary_key_type,
@@ -197,7 +201,13 @@ def add_lock!(sql, options) #:nodoc:
197 201 # SCHEMA STATEMENTS ========================================
198 202
199 203 def tables(name = nil) #:nodoc:
200   - execute("SELECT name FROM sqlite_master WHERE type = 'table'", name).map do |row|
  204 + sql = <<-SQL
  205 + SELECT name
  206 + FROM sqlite_master
  207 + WHERE type = 'table' AND NOT name = 'sqlite_sequence'
  208 + SQL
  209 +
  210 + execute(sql, name).map do |row|
201 211 row[0]
202 212 end
203 213 end
@@ -353,7 +363,7 @@ def sqlite_version
353 363 end
354 364
355 365 def default_primary_key_type
356   - if sqlite_version >= '3.1.0'
  366 + if supports_autoincrement?
357 367 'INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL'.freeze
358 368 else
359 369 'INTEGER PRIMARY KEY NOT NULL'.freeze
7 activerecord/test/fixtures/db_definitions/schema.rb
@@ -57,4 +57,11 @@ def create_table(*args, &block)
57 57 create_table :lock_without_defaults_cust, :force => true do |t|
58 58 t.column :custom_lock_version, :integer
59 59 end
  60 +
  61 + # For sqlite 3.1.0+, make a table with a autoincrement column
  62 + if adapter_name == 'SQLite' and supports_autoincrement?
  63 + create_table :table_with_autoincrement, :force => true do |t|
  64 + t.column :name, :string
  65 + end
  66 + end
60 67 end
6 activerecord/test/schema_dumper_test.rb
@@ -8,7 +8,6 @@ class SchemaDumperTest < Test::Unit::TestCase
8 8 def standard_dump
9 9 stream = StringIO.new
10 10 ActiveRecord::SchemaDumper.ignore_tables = []
11   - ActiveRecord::SchemaDumper.ignore_tables << /^sqlite_/ if current_adapter?(:SQLiteAdapter)
12 11 ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
13 12 stream.string
14 13 end
@@ -20,6 +19,11 @@ def test_schema_dump
20 19 assert_no_match %r{create_table "schema_info"}, output
21 20 end
22 21
  22 + def test_schema_dump_excludes_sqlite_sequence
  23 + output = standard_dump
  24 + assert_no_match %r{create_table "sqlite_sequence"}, output
  25 + end
  26 +
23 27 def assert_line_up(lines, pattern, required = false)
24 28 return assert(true) if lines.empty?
25 29 matches = lines.map { |line| line.match(pattern) }

0 comments on commit 50ee332

Please sign in to comment.
Something went wrong with that request. Please try again.