Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Bug [#45093187] Juhan has too many columns

Cleans up the DB (removes duplicates and such) then adds an index on
(assignment_id, beginning_of_week) so that no duplicate can be inserted
in the future.
  • Loading branch information...
commit 368307c33d3452f99a937a241d5182f840c5b897 1 parent 44e9905
@Veejay Veejay authored
View
2  .ruby-version
@@ -1 +1 @@
-1.9.3-p374
+1.9.3-p385
View
55 db/migrate/20130227192027_add_index_to_work_weeks_and_remove_duplicates.rb
@@ -0,0 +1,55 @@
+class AddIndexToWorkWeeksAndRemoveDuplicates < ActiveRecord::Migration
+ def change
+
+ # First let us clean up the duplicates
+
+ @statement = <<-SQL
+ SELECT
+ w1.id
+ FROM
+ work_weeks w1,
+ work_weeks w2
+ WHERE
+ w1.id <> w2.id
+ AND
+ w1.assignment_id = w2.assignment_id
+ AND
+ w1.beginning_of_week = w2.beginning_of_week
+ SQL
+
+ @rows = ActiveRecord::Base.connection.execute @statement
+
+ @weeks = WorkWeek.where(:id => @rows.map { |elem| elem['id'] }).all
+
+ @grouped = @weeks.group_by do |elem|
+ "#{elem.assignment_id}-#{elem.beginning_of_week}"
+ end
+
+ @grouped.each do |key, weeks|
+ # First pass
+ # For all duplicates where the estimated_hours and actual_hours are nil, we can delete everything
+ if weeks.all? { |week| week.estimated_hours.nil? and week.actual_hours.nil? }
+ weeks.each do |w|
+ w.destroy
+ end
+ end
+
+ # Second pass
+ # For the rest of the weeks, we keep only keep one copy if the attributes are identical across all duplicates
+ first_week = weeks.first.attributes.slice("estimated_hours", "actual_hours", "cweek", "year")
+ if weeks.all? { |week| week.attributes.slice("estimated_hours", "actual_hours", "cweek", "year") == first_week }
+ weeks.slice(1..-1).each do |w|
+ w.destroy
+ end
+ end
+
+ # Finally, third pass
+ # We keep the last updated version of the duplicate, it's the one we're supposed to trust
+ weeks.sort { |a,b| a.updated_at <=> b.updated_at }.last.destroy
+
+ end
+ WorkWeek.reset_column_information
+
+ add_index :work_weeks, [:assignment_id, :beginning_of_week], :unique => true
+ end
+end
View
26 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20130204034907) do
+ActiveRecord::Schema.define(:version => 20130227192027) do
create_table "assignments", :force => true do |t|
t.integer "user_id"
@@ -28,8 +28,8 @@
t.string "name"
t.text "description"
t.boolean "active", :default => true
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.datetime "created_at"
+ t.datetime "updated_at"
t.integer "company_id"
end
@@ -59,8 +59,8 @@
t.integer "client_id"
t.string "name"
t.boolean "active", :default => true
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.datetime "created_at"
+ t.datetime "updated_at"
t.integer "company_id"
t.boolean "proposed", :default => false, :null => false
t.decimal "cost", :precision => 12, :scale => 2, :default => 0.0, :null => false
@@ -77,8 +77,8 @@
create_table "users", :force => true do |t|
t.string "email"
t.string "password_digest"
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.datetime "created_at"
+ t.datetime "updated_at"
t.integer "current_company_id"
t.string "registration_token"
t.string "first_name"
@@ -99,12 +99,14 @@
create_table "work_weeks", :force => true do |t|
t.integer "estimated_hours"
t.integer "actual_hours"
- t.integer "cweek", :limit => 2
- t.integer "year", :limit => 2
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.integer "cweek"
+ t.integer "year"
+ t.datetime "created_at"
+ t.datetime "updated_at"
t.integer "assignment_id"
- t.decimal "beginning_of_week", :precision => 15, :scale => 0
+ t.decimal "beginning_of_week", :precision => 15, :scale => 0
end
+ add_index "work_weeks", ["assignment_id", "beginning_of_week"], :name => "index_work_weeks_on_assignment_id_and_beginning_of_week", :unique => true
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.