From 9309456c414e6ac8ead13d774e5b6b60308e60f6 Mon Sep 17 00:00:00 2001 From: Raphael Sofaer Date: Wed, 15 Jun 2011 15:59:08 -0700 Subject: [PATCH] Don't run non pg compatible migrations on empty tables --- ...328202414_post_visibilities_on_contacts.rb | 6 +++++- db/migrate/20110330230206_pm_foreign_keys.rb | 5 ++++- ...emove_pending_add_sharing_and_receiving.rb | 20 +++++++++++-------- .../20110406202932_drop_requests_table.rb | 4 +++- .../20110406203720_tag_name_uniqueness.rb | 8 ++++++-- .../20110421120744_downcase_usernames.rb | 4 +++- ...0513175000_eliminate_stray_user_records.rb | 3 +++ ...48_delete_all_new_request_notifications.rb | 3 ++- ...rd_reset_for_accounts_without_usernames.rb | 3 ++- .../20110524184202_add_object_id_to_post.rb | 5 +++-- db/schema.rb | 6 +----- 11 files changed, 44 insertions(+), 23 deletions(-) diff --git a/db/migrate/20110328202414_post_visibilities_on_contacts.rb b/db/migrate/20110328202414_post_visibilities_on_contacts.rb index c173ea2c1..26a3c0b26 100644 --- a/db/migrate/20110328202414_post_visibilities_on_contacts.rb +++ b/db/migrate/20110328202414_post_visibilities_on_contacts.rb @@ -57,7 +57,11 @@ SQL end def self.pv_count - @pv_count ||= execute('SELECT count(*) FROM post_visibilities').to_a.first.first + @pv_count ||= lambda { + count = execute('SELECT count(*) FROM post_visibilities').to_a.first.first + count = count.last.to_i if self.connection.class == ActiveRecord::ConnectionAdapters::PostgreSQLAdapter + count + }.call end def self.up diff --git a/db/migrate/20110330230206_pm_foreign_keys.rb b/db/migrate/20110330230206_pm_foreign_keys.rb index d70e4ab8a..a9218eb83 100644 --- a/db/migrate/20110330230206_pm_foreign_keys.rb +++ b/db/migrate/20110330230206_pm_foreign_keys.rb @@ -1,4 +1,7 @@ class PmForeignKeys < ActiveRecord::Migration + class Message < ActiveRecord::Base + end + def self.delete_disconnected_cvs execute < 0 + if Message.count > 0 delete_disconnected_conversations delete_disconnected_messages delete_disconnected_cvs diff --git a/db/migrate/20110405171412_contact_remove_pending_add_sharing_and_receiving.rb b/db/migrate/20110405171412_contact_remove_pending_add_sharing_and_receiving.rb index a3cc57b59..4a18d85ef 100644 --- a/db/migrate/20110405171412_contact_remove_pending_add_sharing_and_receiving.rb +++ b/db/migrate/20110405171412_contact_remove_pending_add_sharing_and_receiving.rb @@ -1,21 +1,25 @@ class ContactRemovePendingAddSharingAndReceiving < ActiveRecord::Migration + class Contact < ActiveRecord::Base; end + def self.up add_column :contacts, :sharing, :boolean, :default => false, :null => false add_column :contacts, :receiving, :boolean, :default => false, :null => false - execute( < 0 + execute( < :recipient_id remove_foreign_key :requests, :column => :sender_id @@ -14,7 +16,7 @@ class DropRequestsTable < ActiveRecord::Migration DELETE contacts.* FROM contacts WHERE contacts.sharing = false SQL -) + ) if Contact.count > 0 end def self.down diff --git a/db/migrate/20110406203720_tag_name_uniqueness.rb b/db/migrate/20110406203720_tag_name_uniqueness.rb index 4e07f2060..7b3ed72d5 100644 --- a/db/migrate/20110406203720_tag_name_uniqueness.rb +++ b/db/migrate/20110406203720_tag_name_uniqueness.rb @@ -1,4 +1,6 @@ class TagNameUniqueness < ActiveRecord::Migration + class Tag < ActiveRecord::Base; end + def self.downcase_tags execute < 0 + downcase_tags + consolidate_duplicate_tags + end add_index :tags, :name, :unique => true end diff --git a/db/migrate/20110421120744_downcase_usernames.rb b/db/migrate/20110421120744_downcase_usernames.rb index a4d24d1ab..34582eb63 100644 --- a/db/migrate/20110421120744_downcase_usernames.rb +++ b/db/migrate/20110421120744_downcase_usernames.rb @@ -1,6 +1,8 @@ class DowncaseUsernames < ActiveRecord::Migration + class User < ActiveRecord::Base; end + def self.up - execute < 0 UPDATE users SET users.username = LOWER(users.username) WHERE users.username != LOWER(users.username) diff --git a/db/migrate/20110513175000_eliminate_stray_user_records.rb b/db/migrate/20110513175000_eliminate_stray_user_records.rb index 7bd1ee00a..c06d325cb 100644 --- a/db/migrate/20110513175000_eliminate_stray_user_records.rb +++ b/db/migrate/20110513175000_eliminate_stray_user_records.rb @@ -1,5 +1,8 @@ class EliminateStrayUserRecords < ActiveRecord::Migration + class User < ActiveRecord::Base; end + def self.up + return unless User.count > 0 duplicated_emails = execute("SELECT LOWER(email) from users WHERE users.email != '' GROUP BY LOWER(email) HAVING COUNT(*) > 1").to_a duplicated_emails.each do |email| records = execute("SELECT users.id, users.username, users.created_at from users WHERE LOWER(users.email) = '#{email}'").to_a diff --git a/db/migrate/20110517180148_delete_all_new_request_notifications.rb b/db/migrate/20110517180148_delete_all_new_request_notifications.rb index 971e992a9..857f23070 100644 --- a/db/migrate/20110517180148_delete_all_new_request_notifications.rb +++ b/db/migrate/20110517180148_delete_all_new_request_notifications.rb @@ -1,6 +1,7 @@ class DeleteAllNewRequestNotifications < ActiveRecord::Migration + class Notification < ActiveRecord::Base; end def self.up - execute < 0 DELETE notifications.* FROM notifications WHERE notifications.type = 'Notifications::NewRequest' OR notifications.type = 'Notifications::RequestAccepted' diff --git a/db/migrate/20110518010050_disable_password_reset_for_accounts_without_usernames.rb b/db/migrate/20110518010050_disable_password_reset_for_accounts_without_usernames.rb index fca3c53d0..d8dce2ba3 100644 --- a/db/migrate/20110518010050_disable_password_reset_for_accounts_without_usernames.rb +++ b/db/migrate/20110518010050_disable_password_reset_for_accounts_without_usernames.rb @@ -1,6 +1,7 @@ class DisablePasswordResetForAccountsWithoutUsernames < ActiveRecord::Migration + class User < ActiveRecord::Base; end def self.up - execute < 0 UPDATE users SET email = "" WHERE username IS NULL diff --git a/db/migrate/20110524184202_add_object_id_to_post.rb b/db/migrate/20110524184202_add_object_id_to_post.rb index 77a707c79..5245032da 100644 --- a/db/migrate/20110524184202_add_object_id_to_post.rb +++ b/db/migrate/20110524184202_add_object_id_to_post.rb @@ -1,10 +1,11 @@ class AddObjectIdToPost < ActiveRecord::Migration + class Post < ActiveRecord::Base; end def self.up add_column(:posts, :objectId, :integer) - execute("UPDATE posts SET objectId = object_url") + execute("UPDATE posts SET objectId = object_url") if Post.count > 0 end def self.down - add_column(:posts, :objectId, :integer) + remove_column(:posts, :objectId, :integer) end end diff --git a/db/schema.rb b/db/schema.rb index 64324d449..d7e34da15 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -81,7 +81,7 @@ ActiveRecord::Schema.define(:version => 20110606192307) do t.datetime "updated_at" end - add_index "conversation_visibilities", ["conversation_id", "person_id"], :name => "index_conversation_visibilities_on_conversation_id_and_person_id", :unique => true + add_index "conversation_visibilities", ["conversation_id", "person_id"], :name => "index_conversation_visibilities_on_everything", :unique => true add_index "conversation_visibilities", ["conversation_id"], :name => "index_conversation_visibilities_on_conversation_id" add_index "conversation_visibilities", ["person_id"], :name => "index_conversation_visibilities_on_person_id" @@ -93,8 +93,6 @@ ActiveRecord::Schema.define(:version => 20110606192307) do t.datetime "updated_at" end - add_index "conversations", ["author_id"], :name => "conversations_author_id_fk" - create_table "invitations", :force => true do |t| t.text "message" t.integer "sender_id", :null => false @@ -119,7 +117,6 @@ ActiveRecord::Schema.define(:version => 20110606192307) do t.datetime "updated_at" end - add_index "likes", ["author_id"], :name => "likes_author_id_fk" add_index "likes", ["guid"], :name => "index_likes_on_guid", :unique => true add_index "likes", ["post_id"], :name => "index_likes_on_post_id" @@ -144,7 +141,6 @@ ActiveRecord::Schema.define(:version => 20110606192307) do end add_index "messages", ["author_id"], :name => "index_messages_on_author_id" - add_index "messages", ["conversation_id"], :name => "messages_conversation_id_fk" create_table "notification_actors", :force => true do |t| t.integer "notification_id"