Created
November 1, 2015 16:50
-
-
Save samsondav/8ba5d425b797650bbf3d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'cases/helper' | |
class HotCompatibilityTest < ActiveRecord::TestCase | |
self.use_transactional_tests = false | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
class ::SolarSystem < ActiveRecord::Base | |
has_many :solid_planets, -> {where(classification: "terrestrial")}, class_name: 'Planet' | |
end | |
class ::Planet < ActiveRecord::Base | |
belongs_to :solar_system | |
end | |
setup do | |
Planet.connection.create_table :planets, force: true do |t| | |
t.string :classification | |
t.string :name | |
t.references :solar_system | |
end | |
SolarSystem.connection.create_table :solar_systems, force: true do |t| | |
t.string :name | |
end | |
end | |
teardown do | |
[:solar_systems, | |
:planets].each do |table| | |
ActiveRecord::Base.connection.drop_table table | |
end | |
end | |
test "select after add_column" do | |
record = SolarSystem.create! name: 'Sol' | |
# do a reload to prepare the reload statement | |
record.reload | |
# add a new column | |
SolarSystem.connection.add_column :solar_systems, :description, :string | |
# we can still reload the object | |
record.reload | |
assert_equal 'Sol', record.name | |
ActiveRecord::Base.connection.execute('DROP TABLE planets;') | |
ActiveRecord::Base.connection.execute('DROP TABLE solar_systems;') | |
ActiveRecord::Base.connection.execute('CREATE TABLE "planets" ("id" serial primary key, "classification" character varying, "name" character varying, "solar_system_id" integer);') | |
ActiveRecord::Base.connection.execute('CREATE TABLE "solar_systems" ("id" serial primary key, "name" character varying);') | |
record = SolarSystem.create! name: 'Sol' | |
# prepare the reload statement in a transaction | |
SolarSystem.transaction do | |
record.reload | |
end | |
# add a new column | |
SolarSystem.connection.add_column :solar_systems, :description, :string | |
# we can still reload the object in a transaction | |
SolarSystem.transaction do | |
record.reload | |
assert_equal 'Sol', record.name | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment