I might be wrong but imo Rails 3.2 doesn't touch this code during `db:migrate`.
This is how `schema_migrations` table is created in Rails 3.2

```ruby
def initialize_schema_migrations_table
  sm_table = ActiveRecord::Migrator.schema_migrations_table_name
  
  unless table_exists?(sm_table)
    create_table(sm_table, :id => false) do |schema_migrations_table|
      schema_migrations_table.column :version, :string, :null => false
    end
    add_index sm_table, :version, :unique => true,
      :name => "#{Base.table_name_prefix}unique_schema_migrations#{Base.table_name_suffix}"
    # ....
    # more code
end
```

But Rails 4.0, uses [SchemaMigration](https://github.com/rails/rails/blob/4-0-stable/activerecord/lib/active_record/schema_migration.rb) class to create that table. It inherits from `ActiveRecord::Base`, so it's "sharded" by default (is_sharded? returns true).

```ruby
def initialize_schema_migrations_table
  ActiveRecord::SchemaMigration.create_table
end
```

The bottom line is that we shouldn't switch connection for `ActiveRecord::SchemaMigration`, because it exists in account database and on all shards.

This is the error I got earlier:
```shell
$ be rails c
Loading development environment (Rails 4.0.4)
irb(main):001:0> ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_paths)
   (20.4ms)  CREATE TABLE `schema_migrations` (`version` varchar(255) NOT NULL) ENGINE=InnoDB
   (29.8ms)  CREATE UNIQUE INDEX `unique_schema_migrations` ON `schema_migrations` (`version`)
  ActiveRecord::SchemaMigration Load (0.5ms)  SELECT `schema_migrations`.* FROM `schema_migrations`
  ActiveRecord::SchemaMigration Load (0.4ms)  SELECT `schema_migrations`.* FROM `schema_migrations`
Migrating to CreateAccounts (20130929141909)
== 20130929141909 CreateAccounts: migrating ===================================
-- create_table(:accounts)
   (20.1ms)  CREATE TABLE `accounts` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `name` varchar(255), `created_at` datetime, `updated_at` datetime) ENGINE=InnoDB
   -> 0.0212s
== 20130929141909 CreateAccounts: migrated (0.0213s) ==========================

  ActiveRecord::SchemaMigration Load (0.3ms)  SELECT `schema_migrations`.* FROM `schema_migrations`
Mysql2::Error: Table 'active_record_shards_test0.schema_migrations' doesn't exist: SHOW FULL FIELDS FROM `schema_migrations`
StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: Table 'active_record_shards_test0.schema_migrations' doesn't exist: SHOW FULL FIELDS FROM `schema_migrations`
	from /Users/lukkry/projects/rails/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:287:in `query'
	from /Users/lukkry/projects/rails/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:287:in `block in execute'
	from /Users/lukkry/projects/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:442:in `block in log'
	from /Users/lukkry/projects/rails/activesupport/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
	from /Users/lukkry/projects/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:437:in `log'
	from /Users/lukkry/projects/rails/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:287:in `execute'
	from /Users/lukkry/projects/rails/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb:222:in `execute'
	from /Users/lukkry/projects/rails/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:301:in `execute_and_free'
	from /Users/lukkry/projects/rails/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:462:in `columns'
	from /Users/lukkry/projects/rails/activerecord/lib/active_record/connection_adapters/schema_cache.rb:114:in `block in prepare_default_proc'
	from /Users/lukkry/projects/rails/activerecord/lib/active_record/connection_adapters/schema_cache.rb:56:in `yield'
	from /Users/lukkry/projects/rails/activerecord/lib/active_record/connection_adapters/schema_cache.rb:56:in `columns'
	from /Users/lukkry/projects/rails/activerecord/lib/active_record/model_schema.rb:208:in `columns'
	from /Users/lukkry/projects/zendesk/active_record_shards/lib/active_record_shards/connection_switcher.rb:207:in `block in columns_with_default_shard'
	from /Users/lukkry/projects/zendesk/active_record_shards/lib/active_record_shards/connection_switcher.rb:23:in `block in on_first_shard'
	from /Users/lukkry/projects/zendesk/active_record_shards/lib/active_record_shards/connection_switcher.rb:16:in `on_shard'
... 11 levels...
	from /Users/lukkry/projects/rails/activerecord/lib/active_record/migration.rb:1011:in `ddl_transaction'
	from /Users/lukkry/projects/rails/activerecord/lib/active_record/migration.rb:962:in `execute_migration_in_transaction'
	from /Users/lukkry/projects/rails/activerecord/lib/active_record/migration.rb:924:in `block in migrate'
	from /Users/lukkry/projects/rails/activerecord/lib/active_record/migration.rb:920:in `each'
	from /Users/lukkry/projects/rails/activerecord/lib/active_record/migration.rb:920:in `migrate'
	from /Users/lukkry/projects/rails/activerecord/lib/active_record/migration.rb:768:in `up'
	from /Users/lukkry/projects/zendesk/active_record_shards/lib/active_record_shards/migration.rb:7:in `block (3 levels) in singleton class'
	from /Users/lukkry/projects/zendesk/active_record_shards/lib/active_record_shards/connection_switcher.rb:16:in `on_shard'
	from /Users/lukkry/projects/zendesk/active_record_shards/lib/active_record_shards/migration.rb:6:in `block (2 levels) in singleton class'
	from /Users/lukkry/projects/rails/activerecord/lib/active_record/migration.rb:746:in `migrate'
	from (irb):1
	from /Users/lukkry/projects/rails/railties/lib/rails/commands/console.rb:90:in `start'
	from /Users/lukkry/projects/rails/railties/lib/rails/commands/console.rb:9:in `start'
	from /Users/lukkry/projects/rails/railties/lib/rails/commands.rb:62:in `<top (required)>'
	from bin/rails:4:in `require'
```