store sis_batch_id as an integer, not a string

test plan:
 * run the migration
 * redo the migration
 * do a batch mode sis import

Change-Id: I0204256f3825ffe0b4d252bbb83a0604d7a58942
Reviewed-on: https://gerrit.instructure.com/7904
Tested-by: Hudson <hudson@instructure.com>
Reviewed-by: Jacob Fugal <jacob@instructure.com>
Reviewed-by: Ben Chobot <bench@instructure.com>
This commit is contained in:
Cody Cutrer 2012-01-05 15:39:13 -07:00
parent 74b9386d9c
commit 7e97621462
4 changed files with 73 additions and 25 deletions

View File

@ -147,19 +147,19 @@ class SisBatch < ActiveRecord::Base
# delete courses that weren't in this batch, in the selected term
scope = Course.active.for_term(self.batch_mode_term).scoped(:conditions => ["courses.root_account_id = ?", self.account.id])
scope.scoped(:conditions => ["sis_batch_id is not null and sis_batch_id <> ?", self.id.to_s]).find_each do |course|
scope.scoped(:conditions => ["sis_batch_id is not null and sis_batch_id <> ?", self.id]).find_each do |course|
course.destroy
end
# delete sections who weren't in this batch, whose course was in the selected term
scope = CourseSection.scoped(:conditions => ["course_sections.workflow_state = ? and course_sections.root_account_id = ? and course_sections.sis_batch_id is not null and course_sections.sis_batch_id <> ?", 'active', self.account.id, self.id.to_s])
scope = CourseSection.scoped(:conditions => ["course_sections.workflow_state = ? and course_sections.root_account_id = ? and course_sections.sis_batch_id is not null and course_sections.sis_batch_id <> ?", 'active', self.account.id, self.id])
scope = scope.scoped(:include => :course, :select => "course_sections.*", :conditions => ["courses.enrollment_term_id = ?", self.batch_mode_term.id])
scope.find_each do |section|
section.destroy
end
# delete enrollments for courses that weren't in this batch, in the selected term
scope = Enrollment.active.scoped(:include => :course, :select => "enrollments.*", :conditions => ["courses.root_account_id = ? and enrollments.sis_batch_id is not null and enrollments.sis_batch_id <> ?", self.account.id, self.id.to_s])
scope = Enrollment.active.scoped(:include => :course, :select => "enrollments.*", :conditions => ["courses.root_account_id = ? and enrollments.sis_batch_id is not null and enrollments.sis_batch_id <> ?", self.account.id, self.id])
scope = scope.scoped(:conditions => ["courses.enrollment_term_id = ?", self.batch_mode_term.id])
scope.find_each do |enrollment|
enrollment.destroy

View File

@ -0,0 +1,43 @@
class ChangeSisBatchIdToInteger < ActiveRecord::Migration
def self.up
if connection.adapter_name == 'PostgreSQL'
execute("ALTER TABLE abstract_courses ALTER sis_batch_id TYPE bigint USING CAST(sis_batch_id AS bigint)")
execute("ALTER TABLE accounts ALTER sis_batch_id TYPE bigint USING CAST(sis_batch_id AS bigint)")
execute("ALTER TABLE accounts ALTER current_sis_batch_id TYPE bigint USING CAST(sis_batch_id AS bigint)")
execute("ALTER TABLE accounts ALTER last_successful_sis_batch_id TYPE bigint USING CAST(sis_batch_id AS bigint)")
execute("ALTER TABLE course_sections ALTER sis_batch_id TYPE bigint USING CAST(sis_batch_id AS bigint)")
execute("ALTER TABLE courses ALTER sis_batch_id TYPE bigint USING CAST(sis_batch_id AS bigint)")
execute("ALTER TABLE enrollment_terms ALTER sis_batch_id TYPE bigint USING CAST(sis_batch_id AS bigint)")
execute("ALTER TABLE enrollments ALTER sis_batch_id TYPE bigint USING CAST(sis_batch_id AS bigint)")
execute("ALTER TABLE group_memberships ALTER sis_batch_id TYPE bigint USING CAST(sis_batch_id AS bigint)")
execute("ALTER TABLE groups ALTER sis_batch_id TYPE bigint USING CAST(sis_batch_id AS bigint)")
execute("ALTER TABLE pseudonyms ALTER sis_batch_id TYPE bigint USING CAST(sis_batch_id AS bigint)")
else
change_column :abstract_courses, :sis_batch_id, :integer, :limit => 8
change_column :accounts, :sis_batch_id, :integer, :limit => 8
change_column :accounts, :current_sis_batch_id, :integer, :limit => 8
change_column :accounts, :last_successful_sis_batch_id, :integer, :limit => 8
change_column :course_sections, :sis_batch_id, :integer, :limit => 8
change_column :courses, :sis_batch_id, :integer, :limit => 8
change_column :enrollment_terms, :sis_batch_id, :integer, :limit => 8
change_column :enrollments, :sis_batch_id, :integer, :limit => 8
change_column :group_memberships, :sis_batch_id, :integer, :limit => 8
change_column :groups, :sis_batch_id, :integer, :limit => 8
change_column :pseudonyms, :sis_batch_id, :integer, :limit => 8
end
end
def self.down
change_column :pseudonyms, :sis_batch_id, :string
change_column :groups, :sis_batch_id, :string
change_column :group_memberships, :sis_batch_id, :string
change_column :enrollments, :sis_batch_id, :string
change_column :enrollment_terms, :sis_batch_id, :string
change_column :courses, :sis_batch_id, :string
change_column :course_sections, :sis_batch_id, :string
change_column :accounts, :last_successful_sis_batch_id, :string
change_column :accounts, :current_sis_batch_id, :string
change_column :accounts, :sis_batch_id, :string
change_column :abstract_courses, :sis_batch_id, :string
end
end

View File

@ -45,7 +45,7 @@ def course_valid_attributes
:is_public => true,
:publish_grades_immediately => true,
:allow_student_wiki_edits => true,
:sis_batch_id => "factory",
:sis_batch_id => 1,
:allow_student_assignment_edits => true
}
end

View File

@ -64,12 +64,14 @@ describe SisBatch do
@term1 = @account.enrollment_terms.first
@term1.update_attribute(:sis_source_id, 'term1')
@term2 = @account.enrollment_terms.create!(:name => 'term2')
@previous_batch = SisBatch.create!
@old_batch = SisBatch.create!
@c1 = factory_with_protected_attributes(@subacct.courses, :name => "delete me", :enrollment_term => @term1, :sis_batch_id => "previous")
@c1 = factory_with_protected_attributes(@subacct.courses, :name => "delete me", :enrollment_term => @term1, :sis_batch_id => @previous_batch.id)
@c1.offer!
@c2 = factory_with_protected_attributes(@account.courses, :name => "don't delete me", :enrollment_term => @term1, :sis_source_id => 'my_course', :root_account => @account)
@c2.offer!
@c3 = factory_with_protected_attributes(@account.courses, :name => "delete me if terms", :enrollment_term => @term2, :sis_batch_id => "previous")
@c3 = factory_with_protected_attributes(@account.courses, :name => "delete me if terms", :enrollment_term => @term2, :sis_batch_id => @previous_batch.id)
@c3.offer!
# initial import of one course, to test courses that haven't changed at all between imports
@ -80,17 +82,17 @@ another_course,not-delete,not deleted not changed,,term1,active}
@c4 = @account.courses.find_by_course_code('not-delete')
# sections are keyed off what term their course is in
@s1 = factory_with_protected_attributes(@c1.course_sections, :name => "delete me", :sis_batch_id => 'old')
@s1 = factory_with_protected_attributes(@c1.course_sections, :name => "delete me", :sis_batch_id => @old_batch.id)
@s2 = factory_with_protected_attributes(@c2.course_sections, :name => "don't delete me", :sis_source_id => 'my_section')
@s3 = factory_with_protected_attributes(@c3.course_sections, :name => "delete me if terms", :sis_batch_id => 'old')
@s4 = factory_with_protected_attributes(@c2.course_sections, :name => "delete me", :sis_batch_id => 'old') # c2 won't be deleted, but this section should still be
@s3 = factory_with_protected_attributes(@c3.course_sections, :name => "delete me if terms", :sis_batch_id => @old_batch.id)
@s4 = factory_with_protected_attributes(@c2.course_sections, :name => "delete me", :sis_batch_id => @old_batch.id) # c2 won't be deleted, but this section should still be
# enrollments are keyed off what term their course is in
@e1 = factory_with_protected_attributes(@c1.enrollments, :workflow_state => 'active', :user => user, :sis_batch_id => 'old')
@e1 = factory_with_protected_attributes(@c1.enrollments, :workflow_state => 'active', :user => user, :sis_batch_id => @old_batch.id)
@e2 = factory_with_protected_attributes(@c2.enrollments, :workflow_state => 'active', :user => user)
@e3 = factory_with_protected_attributes(@c3.enrollments, :workflow_state => 'active', :user => user, :sis_batch_id => 'old')
@e4 = factory_with_protected_attributes(@c2.enrollments, :workflow_state => 'active', :user => user, :sis_batch_id => 'old') # c2 won't be deleted, but this enrollment should still be
@e5 = factory_with_protected_attributes(@c2.enrollments, :workflow_state => 'active', :user => user_with_pseudonym, :sis_batch_id => 'old', :course_section => @s2) # c2 won't be deleted, and this enrollment sticks around because it's specified in the new csv
@e3 = factory_with_protected_attributes(@c3.enrollments, :workflow_state => 'active', :user => user, :sis_batch_id => @old_batch.id)
@e4 = factory_with_protected_attributes(@c2.enrollments, :workflow_state => 'active', :user => user, :sis_batch_id => @old_batch.id) # c2 won't be deleted, but this enrollment should still be
@e5 = factory_with_protected_attributes(@c2.enrollments, :workflow_state => 'active', :user => user_with_pseudonym, :sis_batch_id => @old_batch.id, :course_section => @s2) # c2 won't be deleted, and this enrollment sticks around because it's specified in the new csv
@e5.user.pseudonym.update_attribute(:sis_user_id, 'my_user')
@e5.user.pseudonym.update_attribute(:account_id, @account.id)
@ -114,7 +116,7 @@ s2,test_1,section2,active},
@c4.reload.should be_claimed
@cnew = @account.reload.courses.find_by_course_code('TC 101')
@cnew.should_not be_nil
@cnew.sis_batch_id.should == @batch.id.to_s
@cnew.sis_batch_id.should == @batch.id
@cnew.should be_claimed
@s1.reload.should be_active
@ -136,12 +138,14 @@ s2,test_1,section2,active},
@term1 = @account.enrollment_terms.first
@term1.update_attribute(:sis_source_id, 'term1')
@term2 = @account.enrollment_terms.create!(:name => 'term2')
@previous_batch = SisBatch.create!
@old_batch = SisBatch.create!
@c1 = factory_with_protected_attributes(@subacct.courses, :name => "delete me", :enrollment_term => @term1, :sis_batch_id => "previous")
@c1 = factory_with_protected_attributes(@subacct.courses, :name => "delete me", :enrollment_term => @term1, :sis_batch_id => @previous_batch.id)
@c1.offer!
@c2 = factory_with_protected_attributes(@account.courses, :name => "don't delete me", :enrollment_term => @term1, :sis_source_id => 'my_course', :root_account => @account)
@c2.offer!
@c3 = factory_with_protected_attributes(@account.courses, :name => "delete me if terms", :enrollment_term => @term2, :sis_batch_id => "previous")
@c3 = factory_with_protected_attributes(@account.courses, :name => "delete me if terms", :enrollment_term => @term2, :sis_batch_id => @previous_batch.id)
@c3.offer!
# initial import of one course, to test courses that haven't changed at all between imports
@ -152,17 +156,17 @@ another_course,not-delete,not deleted not changed,,term1,active}
@c4 = @account.courses.find_by_course_code('not-delete')
# sections are keyed off what term their course is in
@s1 = factory_with_protected_attributes(@c1.course_sections, :name => "delete me", :sis_batch_id => 'old')
@s1 = factory_with_protected_attributes(@c1.course_sections, :name => "delete me", :sis_batch_id => @old_batch.id)
@s2 = factory_with_protected_attributes(@c2.course_sections, :name => "don't delete me", :sis_source_id => 'my_section')
@s3 = factory_with_protected_attributes(@c3.course_sections, :name => "delete me if terms", :sis_batch_id => 'old')
@s4 = factory_with_protected_attributes(@c2.course_sections, :name => "delete me", :sis_batch_id => 'old') # c2 won't be deleted, but this section should still be
@s3 = factory_with_protected_attributes(@c3.course_sections, :name => "delete me if terms", :sis_batch_id => @old_batch.id)
@s4 = factory_with_protected_attributes(@c2.course_sections, :name => "delete me", :sis_batch_id => @old_batch.id) # c2 won't be deleted, but this section should still be
# enrollments are keyed off what term their course is in
@e1 = factory_with_protected_attributes(@c1.enrollments, :workflow_state => 'active', :user => user, :sis_batch_id => 'old')
@e1 = factory_with_protected_attributes(@c1.enrollments, :workflow_state => 'active', :user => user, :sis_batch_id => @old_batch.id)
@e2 = factory_with_protected_attributes(@c2.enrollments, :workflow_state => 'active', :user => user)
@e3 = factory_with_protected_attributes(@c3.enrollments, :workflow_state => 'active', :user => user, :sis_batch_id => 'old')
@e4 = factory_with_protected_attributes(@c2.enrollments, :workflow_state => 'active', :user => user, :sis_batch_id => 'old') # c2 won't be deleted, but this enrollment should still be
@e5 = factory_with_protected_attributes(@c2.enrollments, :workflow_state => 'active', :user => user_with_pseudonym, :sis_batch_id => 'old', :course_section => @s2) # c2 won't be deleted, and this enrollment sticks around because it's specified in the new csv
@e3 = factory_with_protected_attributes(@c3.enrollments, :workflow_state => 'active', :user => user, :sis_batch_id => @old_batch.id)
@e4 = factory_with_protected_attributes(@c2.enrollments, :workflow_state => 'active', :user => user, :sis_batch_id => @old_batch.id) # c2 won't be deleted, but this enrollment should still be
@e5 = factory_with_protected_attributes(@c2.enrollments, :workflow_state => 'active', :user => user_with_pseudonym, :sis_batch_id => @old_batch.id, :course_section => @s2) # c2 won't be deleted, and this enrollment sticks around because it's specified in the new csv
@e5.user.pseudonym.update_attribute(:sis_user_id, 'my_user')
@e5.user.pseudonym.update_attribute(:account_id, @account.id)
@ -187,7 +191,7 @@ s2,test_1,section2,active},
@c4.reload.should be_claimed
@cnew = @account.reload.courses.find_by_course_code('TC 101')
@cnew.should_not be_nil
@cnew.sis_batch_id.should == @batch.id.to_s
@cnew.sis_batch_id.should == @batch.id
@cnew.should be_claimed
@s1.reload.should be_deleted
@ -207,8 +211,9 @@ s2,test_1,section2,active},
it "shouldn't do batch mode removals if not in batch mode" do
@term1 = @account.enrollment_terms.first
@term2 = @account.enrollment_terms.create!(:name => 'term2')
@previous_batch = SisBatch.create!
@c1 = factory_with_protected_attributes(@account.courses, :name => "delete me", :enrollment_term => @term1, :sis_batch_id => "previous")
@c1 = factory_with_protected_attributes(@account.courses, :name => "delete me", :enrollment_term => @term1, :sis_batch_id => @previous_batch.id)
@c1.offer!
@batch = process_csv_data(