Merge pull request #1025 from ZogStriP/reverse-index-take-2

Reverse index take 2
This commit is contained in:
Sam 2013-06-16 16:49:57 -07:00
commit b545a49d32
9 changed files with 53 additions and 6 deletions

View File

@ -27,7 +27,8 @@ class Post < ActiveRecord::Base
has_many :post_actions has_many :post_actions
has_many :topic_links has_many :topic_links
has_and_belongs_to_many :upload has_many :post_uploads
has_many :uploads, through: :post_uploads
has_one :post_search_data has_one :post_search_data

View File

@ -0,0 +1,4 @@
class PostUpload < ActiveRecord::Base
belongs_to :post
belongs_to :upload
end

View File

@ -7,7 +7,8 @@ class Upload < ActiveRecord::Base
belongs_to :user belongs_to :user
belongs_to :topic belongs_to :topic
has_and_belongs_to_many :post has_many :post_uploads
has_many :posts, through: :post_uploads
validates_presence_of :filesize validates_presence_of :filesize
validates_presence_of :original_filename validates_presence_of :original_filename

View File

@ -0,0 +1,16 @@
class DropPostsUploads < ActiveRecord::Migration
def up
drop_table :posts_uploads
end
def down
create_table :posts_uploads, id: false do |t|
t.integer :post_id
t.integer :upload_id
end
add_index :posts_uploads, :post_id
add_index :posts_uploads, :upload_id
add_index :posts_uploads, [:post_id, :upload_id], unique: true
end
end

View File

@ -0,0 +1,15 @@
class CreatePostUploads < ActiveRecord::Migration
def up
create_table :post_uploads do |t|
t.integer :post_id, null: false
t.integer :upload_id, null: false
end
# no support for this till rails 4
execute 'create unique index idx_unique_post_uploads on post_uploads(post_id, upload_id)'
end
def down
drop_table :post_uploads
end
end

View File

@ -20,7 +20,7 @@ task "images:reindex" => :environment do
src = img['src'] src = img['src']
if src.present? && has_been_uploaded?(src) && m = uploaded_regex.match(src) if src.present? && has_been_uploaded?(src) && m = uploaded_regex.match(src)
begin begin
Post.exec_sql("INSERT INTO posts_uploads (post_id, upload_id) VALUES (?, ?)", p.id, m[:upload_id]) PostUpload.create({ post_id: p.id, upload_id: m[:upload_id] })
rescue ActiveRecord::RecordNotUnique rescue ActiveRecord::RecordNotUnique
end end
end end

View File

@ -26,7 +26,8 @@ describe Post do
it { should have_many :post_replies } it { should have_many :post_replies }
it { should have_many :replies } it { should have_many :replies }
it { should have_and_belong_to_many :upload } it { should have_many :post_uploads }
it { should have_many :uploads }
it { should rate_limit } it { should rate_limit }

View File

@ -0,0 +1,8 @@
require 'spec_helper'
describe PostUpload do
it { should belong_to :post }
it { should belong_to :upload }
end

View File

@ -5,7 +5,8 @@ describe Upload do
it { should belong_to :user } it { should belong_to :user }
it { should belong_to :topic } it { should belong_to :topic }
it { should have_and_belong_to_many :post } it { should have_many :post_uploads }
it { should have_many :posts }
it { should validate_presence_of :original_filename } it { should validate_presence_of :original_filename }
it { should validate_presence_of :filesize } it { should validate_presence_of :filesize }