aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/adapters/sqlite3/legacy_migration_test.rb
blob: fcca8d66b5e3ea7e95c9e7f8ba4277bb78fafa61 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require "cases/helper"

class SqliteLegacyMigrationTest < ActiveRecord::SQLite3TestCase
  self.use_transactional_tests = false

  class GenerateTableWithoutBigint < ActiveRecord::Migration[5.0]
    def change
      create_table :legacy_integer_pk do |table|
        table.string :foo
      end

      create_table :override_pk, id: :bigint do |table|
        table.string :bar
      end
    end
  end

  def setup
    super
    @connection = ActiveRecord::Base.connection

    @migration_verbose_old = ActiveRecord::Migration.verbose
    ActiveRecord::Migration.verbose = false

    migrations = [GenerateTableWithoutBigint.new(nil, 1)]

    ActiveRecord::Migrator.new(:up, migrations).migrate
  end

  def teardown
    ActiveRecord::Migration.verbose = @migration_verbose_old
    @connection.drop_table("legacy_integer_pk")
    @connection.drop_table("override_pk")
    ActiveRecord::SchemaMigration.delete_all rescue nil
    super
  end

  def test_create_table_uses_integer_as_pkey_by_default
    col = column(:legacy_integer_pk, :id)
    assert_equal "INTEGER", sql_type_for(col)
    assert primary_key?(:legacy_integer_pk, "id"), "id is not primary key"
  end

  private

    def column(table_name, column_name)
      ActiveRecord::Base.connection
        .columns(table_name.to_s)
        .detect { |c| c.name == column_name.to_s }
    end

    def sql_type_for(col)
      col && col.sql_type
    end

    def primary_key?(table_name, column)
      ActiveRecord::Base.connection.execute("PRAGMA table_info(#{table_name})").find { |col| col["name"] == column }["pk"] == 1
    end
end