Initial release of Discourse

This commit is contained in:
Robin Ward
2013-02-05 14:16:51 -05:00
commit 21b5628528
2932 changed files with 143949 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
class CreateForumThreads < ActiveRecord::Migration
def change
create_table :forum_threads do |t|
t.integer :forum_id, null: false
t.string :title, null: false
t.integer :last_post_id
t.datetime :last_posted_at
t.timestamps
end
end
end

View File

@@ -0,0 +1,14 @@
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.integer :user_id, null: false
t.integer :forum_thread_id, null: false
t.integer :post_number, null: false
t.text :content, null: false
t.text :formatted_content, null: false
t.timestamps
end
add_index :posts, [:forum_thread_id, :created_at]
end
end

View File

@@ -0,0 +1,9 @@
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :username, :limit => 20, null: false
t.string :avatar_url, null: false
t.timestamps
end
end
end

View File

@@ -0,0 +1,9 @@
class CreateForums < ActiveRecord::Migration
def change
create_table :forums do |t|
t.integer :site_id, null: false
t.string :title, limit: 100, null: false
t.timestamps
end
end
end

View File

@@ -0,0 +1,8 @@
class CreateSites < ActiveRecord::Migration
def change
create_table :sites do |t|
t.string :title, limit: 100, null: false
t.timestamps
end
end
end

View File

@@ -0,0 +1,6 @@
class AddReplyToToPosts < ActiveRecord::Migration
def change
add_column :posts, :reply_to_post_number, :integer, null: true
add_index :posts, :reply_to_post_number
end
end

View File

@@ -0,0 +1,5 @@
class AddViewsToForumThreads < ActiveRecord::Migration
def change
add_column :forum_threads, :views, :integer, default: 0, null: false
end
end

View File

@@ -0,0 +1,7 @@
class AddPostsCountToForumThreads < ActiveRecord::Migration
def change
add_column :forum_threads, :posts_count, :integer, default: 0, null: false
execute "UPDATE forum_threads SET posts_count = (SELECT count(*) FROM posts WHERE posts.forum_thread_id = forum_threads.id)"
end
end

View File

@@ -0,0 +1,11 @@
class FixPostIndices < ActiveRecord::Migration
def up
remove_index :posts, [:forum_thread_id, :created_at]
add_index :posts, [:forum_thread_id, :post_number]
end
def down
remove_index :posts, [:forum_thread_id, :post_number]
add_index :posts, [:forum_thread_id, :created_at]
end
end

View File

@@ -0,0 +1,9 @@
class RemoveLastPostId < ActiveRecord::Migration
def up
remove_column :forum_threads, :last_post_id
end
def down
add_column :forum_threads, :last_post_id, :integer, :default => 0
end
end

View File

@@ -0,0 +1,15 @@
class AddDisplayUsernameToUsers < ActiveRecord::Migration
def up
add_column :users, :display_username, :string
execute "UPDATE users SET display_username = username"
execute "UPDATE users SET username = REPLACE(username, ' ', '')"
add_index :users, :username, :unique
end
def down
remove_index :users, :username
execute "UPDATE users SET username = display_username"
remove_column :users, :display_username
end
end

View File

@@ -0,0 +1,5 @@
class AddUserIdIndexToPosts < ActiveRecord::Migration
def change
add_index :posts, :user_id
end
end

View File

@@ -0,0 +1,6 @@
class CookedMigration < ActiveRecord::Migration
def change
rename_column :posts, :content, :raw
rename_column :posts, :formatted_content, :cooked
end
end

View File

@@ -0,0 +1,28 @@
class CreateVestalVersions < ActiveRecord::Migration
def self.up
create_table :versions do |t|
t.belongs_to :versioned, :polymorphic => true
t.belongs_to :user, :polymorphic => true
t.string :user_name
t.text :modifications
t.integer :number
t.integer :reverted_from
t.string :tag
t.timestamps
end
change_table :versions do |t|
t.index [:versioned_id, :versioned_type]
t.index [:user_id, :user_type]
t.index :user_name
t.index :number
t.index :tag
t.index :created_at
end
end
def self.down
drop_table :versions
end
end

View File

@@ -0,0 +1,5 @@
class AddVersionToPosts < ActiveRecord::Migration
def change
add_column :posts, :cached_version, :integer, null: false, default: 1
end
end

View File

@@ -0,0 +1,15 @@
class AddCreatedByToForumThreads < ActiveRecord::Migration
def up
add_column :forum_threads, :user_id, :integer
execute "update forum_threads t
set user_id = (select user_id from posts where forum_thread_id = t.Id order by post_number asc limit 1)"
change_column :forum_threads, :user_id, :integer, null: false
end
def down
remove_column :forum_threads, :user_id
end
end

View File

@@ -0,0 +1,17 @@
class AddLastPostUserIdToForumThreads < ActiveRecord::Migration
def up
add_column :forum_threads, :last_post_user_id, :integer
execute "update forum_threads t
set last_post_user_id = (select user_id from posts where forum_thread_id = t.Id order by post_number desc limit 1)"
change_column :forum_threads, :last_post_user_id, :integer, null: false
end
def down
remove_column :forum_threads, :last_post_user_id
end
end

View File

@@ -0,0 +1,9 @@
class AddSiteIdToUsers < ActiveRecord::Migration
def change
add_column :users, :site_id, :integer
add_column :users, :bio, :text
add_index :users, :site_id
execute "UPDATE users SET site_id = 1"
end
end

View File

@@ -0,0 +1,13 @@
class CreateExpressions < ActiveRecord::Migration
def change
create_table :expressions, id: false, force: true do |t|
t.integer :parent_id, null: false
t.string :parent_type, null: false, limit: 50
t.integer :expression_type_id, null: false
t.integer :user_id, null: false
t.timestamps
end
add_index :expressions, [:parent_id, :parent_type, :expression_type_id, :user_id], unique: true, name: "expressions_pk"
end
end

View File

@@ -0,0 +1,12 @@
class CreateExpressionTypes < ActiveRecord::Migration
def change
create_table :expression_types do |t|
t.integer :site_id, null: false
t.string :name, null: false, limit: 50
t.string :long_form, null: false, limit: 100
t.timestamps
end
add_index :expression_types, [:site_id, :name], unique: true
end
end

View File

@@ -0,0 +1,13 @@
class AddReplyCountToPosts < ActiveRecord::Migration
def up
add_column :posts, :reply_count, :integer, null: false, default: 0
execute "UPDATE posts
SET reply_count = (SELECT count(*) FROM posts AS p2 WHERE p2.reply_to_post_number = posts.post_number)"
end
def down
remove_column :posts, :reply_count
end
end

View File

@@ -0,0 +1,5 @@
class AddFlagToExpressionTypes < ActiveRecord::Migration
def change
add_column :expression_types, :flag, :boolean, default: false
end
end

View File

@@ -0,0 +1,5 @@
class AddDescriptionToExpressionTypes < ActiveRecord::Migration
def change
add_column :expression_types, :description, :text, null: true
end
end

View File

@@ -0,0 +1,5 @@
class AddQuotelessToPost < ActiveRecord::Migration
def change
add_column :posts, :quoteless, :boolean, default: false
end
end

View File

@@ -0,0 +1,17 @@
class CreateReadPosts < ActiveRecord::Migration
def up
create_table :read_posts, id: false do |t|
t.integer :forum_thread_id, null: false
t.integer :user_id, null: false
t.column :page, :integer, null: false
t.column :seen, :integer, null: false
end
add_index :read_posts, [:forum_thread_id, :user_id, :page], :unique => true
end
def down
drop_table :read_posts
end
end

View File

@@ -0,0 +1,12 @@
class CreateLastReadPosts < ActiveRecord::Migration
def change
create_table :last_read_posts do |t|
t.integer :user_id, null: false
t.integer :forum_thread_id, null: false
t.integer :post_number, null: false
t.timestamps
end
add_index :last_read_posts, [:user_id, :forum_thread_id], unique: true
end
end

View File

@@ -0,0 +1,14 @@
class CreateViews < ActiveRecord::Migration
def change
create_table :views, id: false do |t|
t.integer :parent_id, null: false
t.string :parent_type, limit: 50, null: false
t.integer :ip, limit: 8, null: false
t.datetime :viewed_at, null: false
t.integer :user_id, null: true
end
add_index :views, [:parent_id, :parent_type]
add_index :views, [:parent_id, :parent_type, :ip, :viewed_at], unique: true, name: "unique_views"
end
end

View File

@@ -0,0 +1,7 @@
class AddRepliesToForumThreads < ActiveRecord::Migration
def change
add_column :forum_threads, :reply_count, :integer, default: 0, null: false
execute "UPDATE forum_threads SET reply_count = (SELECT COUNT(*) FROM posts WHERE posts.reply_to_post_number IS NOT NULL AND posts.forum_thread_id = forum_threads.id)"
end
end

View File

@@ -0,0 +1,21 @@
class AddFeaturedToForumThreads < ActiveRecord::Migration
def up
add_column :forum_threads, :featured_user1_id, :integer, null: true
add_column :forum_threads, :featured_user2_id, :integer, null: true
add_column :forum_threads, :featured_user3_id, :integer, null: true
# Migrate old threads
ForumThread.all.each do |forum_thread|
posts_count = Post.where(forum_thread_id: forum_thread.id).group(:user_id).order('count_all desc').limit(3).count
posts_count.keys.each_with_index {|user_id, i| forum_thread.send("featured_user#{i+1}_id=", user_id) }
forum_thread.save
end
end
def down
remove_column :forum_threads, :featured_user1_id
remove_column :forum_threads, :featured_user2_id
remove_column :forum_threads, :featured_user3_id
end
end

View File

@@ -0,0 +1,12 @@
class AddAvgTimeToForumThreads < ActiveRecord::Migration
def up
add_column :forum_threads, :avg_time, :integer
execute "update forum_threads SET avg_time = abs(random() * 1200)"
end
def down
remove_column :forum_threads, :avg_time
end
end

View File

@@ -0,0 +1,18 @@
class CreateUploads < ActiveRecord::Migration
def change
create_table :uploads do |t|
t.integer :user_id, null: false
t.integer :forum_thread_id, null: false
t.string :original_filename, null: false
t.integer :filesize, null: false
t.integer :width, null: true
t.integer :height, null: true
t.string :url, null: false
t.timestamps
end
add_index :uploads, :forum_thread_id
add_index :uploads, :user_id
end
end

View File

@@ -0,0 +1,12 @@
class CreateStars < ActiveRecord::Migration
def change
create_table :stars, id: false do |t|
t.integer :parent_id, null: false
t.string :parent_type, limit: 50, null: false
t.integer :user_id, null: true
t.timestamps
end
add_index :stars, [:parent_id, :parent_type, :user_id]
end
end

View File

@@ -0,0 +1,41 @@
class CreateForumThreadUser < ActiveRecord::Migration
def up
create_table :forum_thread_users, id: false do |t|
t.integer :user_id, null: false
t.integer :forum_thread_id, null: false
t.boolean :starred, null: false, default: false
t.boolean :posted, null: false, default: false
t.integer :last_read_post_number, null: false, default: 1
t.timestamps
end
execute "DELETE FROM read_posts"
add_index :forum_thread_users, [:forum_thread_id, :user_id], unique: true
drop_table :stars
drop_table :last_read_posts
end
def down
drop_table :forum_thread_users
create_table :stars, id: false do |t|
t.integer :parent_id, null: false
t.string :parent_type, limit: 50, null: false
t.integer :user_id, null: true
t.timestamps
end
add_index :stars, [:parent_id, :parent_type, :user_id]
create_table :last_read_posts do |t|
t.integer :user_id, null: false
t.integer :forum_thread_id, null: false
t.integer :post_number, null: false
t.timestamps
end
add_index :last_read_posts, [:user_id, :forum_thread_id], unique: true
end
end

View File

@@ -0,0 +1,10 @@
class MigratePosted < ActiveRecord::Migration
def up
Post.all.each do |p|
ForumThreadUser.change(p.user, p.forum_thread_id, posted: true)
end
end
def down
end
end

View File

@@ -0,0 +1,5 @@
class AddIndexToForumThreads < ActiveRecord::Migration
def change
add_index :forum_threads, :last_posted_at
end
end

View File

@@ -0,0 +1,16 @@
class CreateForumThreadLinks < ActiveRecord::Migration
def change
create_table :forum_thread_links do |t|
t.integer :forum_thread_id, null: false
t.integer :post_id, null: false
t.integer :user_id, null: false
t.string :url, limit: 500, null: false
t.string :domain, limit: 100, null: false
t.boolean :internal, null: false, default: false
t.integer :link_forum_thread_id, null: true
t.timestamps
end
add_index :forum_thread_links, :forum_thread_id
end
end

View File

@@ -0,0 +1,5 @@
class AddTagsToForumThreads < ActiveRecord::Migration
def change
add_column :forum_threads, :tag, :string, null: true, limit: 25
end
end

View File

@@ -0,0 +1,12 @@
class AddQuoteCountToPosts < ActiveRecord::Migration
def up
add_column :posts, :quote_count, :integer, default: 0, null: false
execute "UPDATE posts SET quote_count = 1 WHERE quoteless = 'f'"
remove_column :posts, :quoteless
end
def down
remove_column :posts, :quote_count
add_column :posts, :quoteless, :boolean, default: false
end
end

View File

@@ -0,0 +1,11 @@
class CreateBookmarks < ActiveRecord::Migration
def change
create_table :bookmarks do |t|
t.integer :user_id
t.integer :post_id
t.timestamps
end
add_index :bookmarks, [:user_id, :post_id], unique: true
end
end

View File

@@ -0,0 +1,5 @@
class AddReplyBelowToPosts < ActiveRecord::Migration
def change
add_column :posts, :reply_below_post_number, :integer, null: true
end
end

View File

@@ -0,0 +1,13 @@
class CreatePostTimings < ActiveRecord::Migration
def change
create_table :post_timings do |t|
t.integer :thread_id, null: false
t.integer :post_number, null: false
t.integer :user_id, null: false
t.integer :msecs, null: false
end
add_index :post_timings, [:thread_id, :post_number]
add_index :post_timings, [:thread_id, :post_number, :user_id], unique: true
end
end

View File

@@ -0,0 +1,13 @@
class CreateMessageBus < ActiveRecord::Migration
def change
create_table :message_bus do |t|
t.string :name
t.string :context
t.text :data
t.datetime :created_at
end
add_index :message_bus, [:created_at]
end
end

View File

@@ -0,0 +1,14 @@
class FixPostTimings < ActiveRecord::Migration
def up
remove_index :post_timings, [:thread_id, :post_number]
remove_index :post_timings, [:thread_id, :post_number, :user_id]
rename_column :post_timings, :thread_id, :forum_thread_id
add_index :post_timings, [:forum_thread_id, :post_number], name: 'post_timings_summary'
add_index :post_timings, [:forum_thread_id, :post_number, :user_id], unique: true, name: 'post_timings_unique'
end
def down
rename_column :post_timings, :forum_thread_id, :thread_id
end
end

View File

@@ -0,0 +1,14 @@
class DropReadPosts < ActiveRecord::Migration
def up
drop_table :read_posts
end
def down
create_table :read_posts, id: false do |t|
t.integer :forum_thread_id, null: false
t.integer :user_id, null: false
t.column :page, :integer, null: false
t.column :seen, :integer, null: false
end
end
end

View File

@@ -0,0 +1,14 @@
class AddPostNumberToBookmarks < ActiveRecord::Migration
def change
drop_table :bookmarks
create_table :bookmarks do |t|
t.integer :user_id, null: false
t.integer :forum_thread_id, null: false
t.integer :post_number, null: false
t.timestamps
end
add_index :bookmarks, [:user_id, :forum_thread_id, :post_number], unique: true
end
end

View File

@@ -0,0 +1,8 @@
class AddSeenPostCountToForumThreadUsers < ActiveRecord::Migration
def change
remove_column :post_timings, :id
remove_column :forum_thread_users, :created_at
remove_column :forum_thread_users, :updated_at
add_column :forum_thread_users, :seen_post_count, :integer
end
end

View File

@@ -0,0 +1,5 @@
class AddDeletedAtToForumThreads < ActiveRecord::Migration
def change
add_column :forum_threads, :deleted_at, :datetime
end
end

View File

@@ -0,0 +1,13 @@
class CreateNotifications < ActiveRecord::Migration
def change
create_table :notifications do |t|
t.integer :notification_type, null: false
t.references :user, null: false
t.string :data, null: false
t.boolean :read, default: false, null: false
t.timestamps
end
add_index :notifications, [:user_id, :created_at]
end
end

View File

@@ -0,0 +1,10 @@
class AddSeenNotificationIdToUsers < ActiveRecord::Migration
def change
execute "TRUNCATE TABLE notifications"
add_column :users, :seen_notificaiton_id, :integer, default: 0, null: false
add_column :notifications, :forum_thread_id, :integer, null: true
add_column :notifications, :post_number, :integer, null: true
end
end

View File

@@ -0,0 +1,5 @@
class AddDeletedAtToPosts < ActiveRecord::Migration
def change
add_column :posts, :deleted_at, :datetime
end
end

View File

@@ -0,0 +1,7 @@
class AddHighestPostNumberToForumThreads < ActiveRecord::Migration
def change
add_column :forum_threads, :highest_post_number, :integer, default: 0, null: false
execute "UPDATE forum_threads SET highest_post_number = (SELECT MAX(post_number) FROM posts WHERE posts.forum_thread_id = forum_threads.id)"
end
end

View File

@@ -0,0 +1,5 @@
class AddImageUrlToForumThreads < ActiveRecord::Migration
def change
add_column :forum_threads, :image_url, :string
end
end

View File

@@ -0,0 +1,16 @@
class RenameExpressionTypeId < ActiveRecord::Migration
def up
add_column :expression_types, :expression_index, :integer
execute "UPDATE expression_types SET expression_index = id"
remove_column :expression_types, :id
add_index :expression_types, [:site_id, :expression_index], unique: true
end
def down
add_column :expression_types, :id, :integer
execute "UPDATE expression_types SET id = expression_index"
remove_column :expression_types, :expression_index
end
end

View File

@@ -0,0 +1,25 @@
class DenormalizeExpressions < ActiveRecord::Migration
def change
# Denormalizing this makes our queries so, so, so much nicer
add_column :posts, :expression1_count, :integer, null: false, default: 0
add_column :posts, :expression2_count, :integer, null: false, default: 0
add_column :posts, :expression3_count, :integer, null: false, default: 0
add_column :posts, :expression4_count, :integer, null: false, default: 0
add_column :posts, :expression5_count, :integer, null: false, default: 0
add_column :forum_threads, :expression1_count, :integer, null: false, default: 0
add_column :forum_threads, :expression2_count, :integer, null: false, default: 0
add_column :forum_threads, :expression3_count, :integer, null: false, default: 0
add_column :forum_threads, :expression4_count, :integer, null: false, default: 0
add_column :forum_threads, :expression5_count, :integer, null: false, default: 0
(1..5).each do |i|
execute "update posts set expression#{i}_count = (select count(*) from expressions where parent_id = posts.id and expression_type_id = #{i})"
execute "update forum_threads set expression#{i}_count = (select sum(expression#{i}_count) from posts where forum_thread_id = forum_threads.id)"
end
end
end

View File

@@ -0,0 +1,16 @@
class MakeExpressionsLessGeneric < ActiveRecord::Migration
def up
rename_column :expressions, :parent_id, :post_id
rename_column :expressions, :expression_type_id, :expression_index
remove_column :expressions, :parent_type
add_index :expressions, [:post_id, :expression_index, :user_id], unique: true, name: 'unique_by_user'
end
def down
rename_column :expressions, :post_id, :parent_id
rename_column :expressions, :expression_index, :expression_type_id
add_column :expressions, :parent_type, :string, null: true
end
end

View File

@@ -0,0 +1,15 @@
class CreateIncomingLinks < ActiveRecord::Migration
def change
create_table :incoming_links do |t|
t.integer :site_id, null: false
t.string :url, limit: 1000, null: false
t.string :referer, limit: 1000, null: false
t.string :domain, limit: 100, null: false
t.integer :forum_thread_id, null: true
t.integer :post_number, null: true
t.timestamps
end
add_index :incoming_links, [:site_id, :forum_thread_id, :post_number], name: 'incoming_index'
end
end

View File

@@ -0,0 +1,17 @@
class CreateReplies < ActiveRecord::Migration
def change
create_table :post_replies, id: false do |t|
t.references :post
t.integer :reply_id
t.timestamps
end
add_index :post_replies, [:post_id, :reply_id], unique: true
execute "INSERT INTO post_replies (post_id, reply_id, created_at, updated_at)
SELECT p2.id, p.id, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
FROM posts AS p
INNER JOIN posts AS p2 on p2.post_number = p.reply_to_post_number AND p2.forum_thread_id = P.forum_thread_id
WHERE p.forum_thread_id IS NOT NULL"
end
end

View File

@@ -0,0 +1,6 @@
class AddReflectionToForumThreadLinks < ActiveRecord::Migration
def change
add_column :forum_thread_links, :reflection, :boolean, default: false
change_column :forum_thread_links, :post_id, :integer, null: true
end
end

View File

@@ -0,0 +1,5 @@
class AddIncomingLinkCountToPosts < ActiveRecord::Migration
def change
add_column :posts, :incoming_link_count, :integer, default: 0, null: false
end
end

View File

@@ -0,0 +1,5 @@
class AddIncomingLinkCountToForumThreads < ActiveRecord::Migration
def change
add_column :forum_threads, :incoming_link_count, :integer, default: 0, null: false
end
end

View File

@@ -0,0 +1,19 @@
class AddBookmarkCountToPosts < ActiveRecord::Migration
def change
add_column :posts, :bookmark_count, :integer, default: 0, null: false
add_column :forum_threads, :bookmark_count, :integer, default: 0, null: false
add_column :forum_threads, :star_count, :integer, default: 0, null: false
execute "UPDATE posts SET bookmark_count = (SELECT COUNT(*)
FROM bookmarks
WHERE post_number = posts.post_number AND forum_thread_id = posts.forum_thread_id)"
execute "UPDATE forum_threads SET bookmark_count = (SELECT COUNT(*)
FROM bookmarks
WHERE forum_thread_id = forum_threads.id)"
execute "UPDATE forum_threads SET star_count = (SELECT COUNT(*)
FROM forum_thread_users
WHERE forum_thread_id = forum_threads.id AND starred = true)"
end
end

View File

@@ -0,0 +1,6 @@
class AddAvgTimeToPosts < ActiveRecord::Migration
def change
add_column :posts, :avg_time, :integer, null: true
add_column :posts, :score, :float, null: true
end
end

View File

@@ -0,0 +1,8 @@
class AddViewCountToPosts < ActiveRecord::Migration
def change
add_column :posts, :views, :integer, default: 0, null: false
execute "UPDATE posts SET views =
(SELECT COUNT(*) FROM post_timings WHERE forum_thread_id = posts.forum_thread_id AND post_number = posts.post_number)"
end
end

View File

@@ -0,0 +1,7 @@
class AddUserToVersions < ActiveRecord::Migration
def change
execute "UPDATE versions SET user_type = 'User', user_id = posts.user_id
FROM posts
WHERE posts.id = versions.versioned_id"
end
end

View File

@@ -0,0 +1,11 @@
class AddLastPostedAtToUsers < ActiveRecord::Migration
def change
add_column :users, :last_posted_at, :datetime, null: true
add_index :users, :last_posted_at
execute "UPDATE users
SET last_posted_at = (SELECT MAX(posts.created_at)
FROM posts
WHERE posts.user_id = users.id)"
end
end

View File

@@ -0,0 +1,28 @@
class CreateCategories < ActiveRecord::Migration
def up
create_table :categories do |t|
t.string :name, limit: 50, null: false
t.string :color, limit: 6, null: false, default: 'AB9364'
t.integer :forum_thread_id, null: true
t.integer :top1_forum_thread_id, null: true
t.integer :top2_forum_thread_id, null: true
t.integer :top1_user_id, null: true
t.integer :top2_user_id, null: true
t.integer :forum_thread_count, null: false, default: 0
t.timestamps
end
add_index :categories, :name, unique: true
add_index :categories, :forum_thread_count
execute "INSERT INTO categories (name, forum_thread_count, created_at, updated_At)
SELECT tag, count(*), CURRENT_TIMESTAMP, CURRENT_TIMESTAMP from forum_threads
WHERE tag IS NOT NULL AND tag <> 'null'
GROUP BY tag"
end
def down
drop_table :categories
end
end

View File

@@ -0,0 +1,18 @@
class AddCategoryIdToForumThreads < ActiveRecord::Migration
def up
add_column :forum_threads, :category_id, :integer
execute "UPDATE forum_threads SET category_id =
(SELECT id
FROM categories
WHERE name = forum_threads.tag)"
remove_column :forum_threads, :tag
end
def down
remove_column :forum_threads, :category_id
add_column :forum_threads, :tag, :string, limit: 20
end
end

View File

@@ -0,0 +1,11 @@
class CreateCategoryFeaturedThreads < ActiveRecord::Migration
def change
create_table :category_featured_threads, id: false do |t|
t.references :category, null: false
t.references :forum_thread, null: false
t.timestamps
end
add_index :category_featured_threads, [:category_id, :forum_thread_id], unique: true, name: 'cat_featured_threads'
end
end

View File

@@ -0,0 +1,12 @@
class CreateSiteSettings < ActiveRecord::Migration
def change
create_table :site_settings do |t|
t.string :name, :null => false
t.text :description, :null => false
t.integer :data_type, :null => false
t.text :value
t.timestamps
end
end
end

View File

@@ -0,0 +1,7 @@
class AddStatsToCategories < ActiveRecord::Migration
def change
add_column :categories, :posts_year, :integer
add_column :categories, :posts_month, :integer
add_column :categories, :posts_week, :integer
end
end

View File

@@ -0,0 +1,13 @@
class CreateUserOpenIds < ActiveRecord::Migration
def change
create_table :user_open_ids do |t|
t.integer :user_id
t.string :email
t.string :url
t.timestamps
end
add_index :user_open_ids, [:url]
end
end

View File

@@ -0,0 +1,30 @@
class AddEmailHashedPasswordNameSaltToUsers < ActiveRecord::Migration
def up
add_column :users, :email, :string, limit: 256
execute "update users set email= md5(random()::text) || 'domain.com'"
change_column :users, :email, :string, limit:256, null: false
add_index :users, [:email], unique: true
rename_column :users, :display_username, :name
add_column :users, :password_hash, :string, limit: 64
add_column :users, :salt, :string, limit: 32
add_column :users, :active, :boolean
add_column :users, :activation_key,:string, limit: 32
add_column :user_open_ids, :active, :boolean, null: false
end
def down
remove_column :users, :email
remove_column :users, :password_hash
remove_column :users, :salt
rename_column :users, :name, :display_username
remove_column :users, :active
remove_column :users, :activation_key
remove_column :user_open_ids, :active
end
end

View File

@@ -0,0 +1,11 @@
class AddUsernameLowerToUsers < ActiveRecord::Migration
def up
add_column :users, :username_lower, :string, limit: 20
execute "update users set username_lower = lower(username)"
add_index :users, [:username_lower], :unique => true
change_column :users, :username_lower, :string, limit: 20, null:false
end
def down
remove_column :users, :username_lower
end
end

View File

@@ -0,0 +1,6 @@
class AddAuthTokenToUsers < ActiveRecord::Migration
def change
add_column :users, :auth_token, :string, limit: 32
add_index :users, [:auth_token]
end
end

View File

@@ -0,0 +1,12 @@
class AddForumIdToCategories < ActiveRecord::Migration
def up
add_column :categories, :forum_id, :integer
execute "UPDATE categories SET forum_id = (SELECT MIN(id) FROM forums)"
change_column :categories, :forum_id, :integer, null: false
end
def down
remove_column :categories, :forum_id
end
end

View File

@@ -0,0 +1,7 @@
class AddNotNullsToUserOpenIds < ActiveRecord::Migration
def change
change_column :user_open_ids, :user_id, :integer, null: false
change_column :user_open_ids, :email, :string, null: false
change_column :user_open_ids, :url, :string, null: false
end
end

View File

@@ -0,0 +1,5 @@
class AddLastSeenAtToUsers < ActiveRecord::Migration
def change
add_column :users, :last_seen_at, :datetime
end
end

View File

@@ -0,0 +1,5 @@
class AddWebsiteToUsers < ActiveRecord::Migration
def change
add_column :users, :website, :string
end
end

View File

@@ -0,0 +1,5 @@
class AddExcerptToCategories < ActiveRecord::Migration
def change
add_column :categories, :excerpt, :string, limit: 250
end
end

View File

@@ -0,0 +1,12 @@
class AddInvisibleToForumThread < ActiveRecord::Migration
def up
add_column :forum_threads, :invisible, :boolean, default: false, null: false
change_column :categories, :excerpt, :text, null: true
end
def down
remove_column :forum_threads, :invisible
change_column :categories, :excerpt, :string, limit: 250, null: true
end
end

View File

@@ -0,0 +1,7 @@
class AddUserIdToCategories < ActiveRecord::Migration
def change
add_column :categories, :user_id, :integer
execute "UPDATE categories SET user_id = 1186"
change_column :categories, :user_id, :integer, null: false
end
end

View File

@@ -0,0 +1,9 @@
class RemoveExcerptFromCategories < ActiveRecord::Migration
def up
remove_column :categories, :excerpt
end
def down
add_column :categories, :excerpt, :string, limit: 250
end
end

View File

@@ -0,0 +1,9 @@
class RenameInvisible < ActiveRecord::Migration
def change
add_column :forum_threads, :visible, :boolean, default: true, null: false
execute "UPDATE forum_threads SET visible = CASE WHEN invisible THEN false ELSE true END"
remove_column :forum_threads, :invisible
end
end

View File

@@ -0,0 +1,11 @@
class AddThreadCountsToCategories < ActiveRecord::Migration
def change
add_column :categories, :threads_year, :integer
add_column :categories, :threads_month, :integer
add_column :categories, :threads_week, :integer
remove_column :categories, :posts_year
remove_column :categories, :posts_month
remove_column :categories, :posts_week
end
end

View File

@@ -0,0 +1,7 @@
class AddIconToExpressionTypes < ActiveRecord::Migration
def change
add_column :expression_types, :icon, :string, limit: 20
execute "UPDATE expression_types SET icon = 'heart' WHERE expression_index = 1"
end
end

View File

@@ -0,0 +1,9 @@
class AddAdminFlagToUsers < ActiveRecord::Migration
def change
add_column :users, :admin, :boolean, default: false, null: false
add_column :users, :moderator, :boolean, default: false, null: false
# Make all of us admins
execute "UPDATE users SET admin = TRUE where lower(username) in ('eviltrout', 'codinghorror', 'sam', 'hanzo')"
end
end

View File

@@ -0,0 +1,9 @@
class AddNewPasswordNewSaltEmailTokenToUsers < ActiveRecord::Migration
def change
add_column :users, :new_salt, :string, :limit => 32
add_column :users, :new_password_hash, :string, :limit => 64
# email token is more flexible, can be used for both intial activation AND password change confirmation
add_column :users, :email_token, :string, :limit => 32
remove_column :users, :activation_key
end
end

View File

@@ -0,0 +1,6 @@
class RemoveNewPasswordStuffFromUser < ActiveRecord::Migration
def change
remove_column :users, :new_password_hash
remove_column :users, :new_salt
end
end

View File

@@ -0,0 +1,25 @@
class CreateActions < ActiveRecord::Migration
def change
create_table :actions do |t|
# I elected for multiple ids as opposed to using :as cause it makes the table
# thinner, and the joining semantics much simpler (a simple multiple left join will do)
#
# There is a notificiation table as well that covers much of this,
# but this table is wider and is intended for non-notifying actions as well
t.integer :action_type, :null => false
t.integer :user_id, :null => false
t.integer :target_forum_thread_id
t.integer :target_post_id
t.integer :target_user_id
t.integer :acting_user_id
t.timestamps
end
add_index :actions, [:user_id, :action_type]
add_index :actions, [:acting_user_id]
end
end

View File

@@ -0,0 +1,22 @@
class RemoveSiteId < ActiveRecord::Migration
def up
drop_table 'sites'
remove_index 'incoming_links', :name => "incoming_index"
add_index "incoming_links", ["forum_thread_id", "post_number"], :name => "incoming_index"
remove_column 'incoming_links', 'site_id'
remove_index 'users', :name => 'index_users_on_site_id'
remove_column 'users', 'site_id'
remove_index 'expression_types', :name => 'index_expression_types_on_site_id_and_expression_index'
remove_index 'expression_types', :name => 'index_expression_types_on_site_id_and_name'
remove_column 'expression_types','site_id'
add_index "expression_types", ["expression_index"], :unique => true
add_index "expression_types", ["name"], :unique => true
drop_table 'forums'
end
def down
raise 'not reversable'
end
end

View File

@@ -0,0 +1,10 @@
class RemoveForumId < ActiveRecord::Migration
def up
remove_column 'forum_threads', 'forum_id'
remove_column 'categories', 'forum_id'
end
def down
raise 'not reversible'
end
end

View File

@@ -0,0 +1,23 @@
class CorrectIndexingOnPosts < ActiveRecord::Migration
def up
execute "update posts pp
set post_number = c.real_number
from
(
select p1.id, count(*) real_number from posts p1
join posts p2 on p1.forum_thread_id = p2.forum_thread_id
where p2.id <= p1.id and p1.forum_thread_id = p2.forum_thread_id
group by p1.id
) as c
where pp.id = c.id and pp.post_number <> c.real_number"
remove_index "posts", ["forum_thread_id","post_number"]
# this needs to be unique if it is not we can not use post_number to identify a post
add_index "posts", ["forum_thread_id","post_number"], :unique => true
end
def down
end
end

View File

@@ -0,0 +1,11 @@
class RemoveIndexForNow < ActiveRecord::Migration
def up
remove_index "posts", ["forum_thread_id","post_number"]
add_index "posts", ["forum_thread_id","post_number"], unique: false
end
def down
remove_index "posts", ["forum_thread_id","post_number"]
add_index "posts", ["forum_thread_id","post_number"], :unique => true
end
end

View File

@@ -0,0 +1,21 @@
class CreatePostActions < ActiveRecord::Migration
def up
create_table :post_actions do |t|
t.integer :post_id, null: false
t.integer :user_id, null: false
t.integer :post_action_type_id, null:false
t.datetime :deleted_at
t.timestamps
end
add_index :post_actions, ["post_id"]
# no support for this till rails 4
execute 'create unique index idx_unique_actions on
post_actions(user_id, post_action_type_id, post_id) where deleted_at is null'
end
def down
drop_table :post_actions
end
end

View File

@@ -0,0 +1,14 @@
class CreatePostActionTypes < ActiveRecord::Migration
def change
create_table(:post_action_types, id: false) do |t|
t.integer :id, options: "PRIMARY KEY", null: false
t.string :name, null: false, limit: 50
t.string :long_form, null: false, limit: 100
t.boolean :is_flag, null: false, default: false
t.text :description
t.string :icon, limit: 20
t.timestamps
end
end
end

View File

@@ -0,0 +1,14 @@
class MigrateBookmarksToPostActions < ActiveRecord::Migration
def up
execute "insert into post_actions(user_id, post_action_type_id, post_id, created_at, updated_at)
select distinct b.user_id, #{PostActionType.bookmark.id} , p.id, b.created_at, b.updated_at
from bookmarks b
join posts p on p.forum_thread_id = b.forum_thread_id and p.post_number = b.post_number"
drop_table "bookmarks"
end
def down
# I can reverse this, but not really worth the work
raise ActiveRecord::IrriversableMigration
end
end

View File

@@ -0,0 +1,5 @@
class RenameActionsToUserActions < ActiveRecord::Migration
def change
rename_table 'actions', 'user_actions'
end
end

View File

@@ -0,0 +1,20 @@
class RetireExpressions < ActiveRecord::Migration
def up
execute 'insert into post_actions (post_action_type_id, user_id, post_id, created_at, updated_at)
select
case
when expression_index=1 then 3
when expression_index=2 then 4
when expression_index=3 then 2
end
, user_id, post_id, created_at, updated_at from expressions'
drop_table 'expressions'
drop_table 'expression_types'
end
def down
raise ActiveRecord::IrriversableMigration
end
end

View File

@@ -0,0 +1,10 @@
class RenameExpressionColumnsInForumThread < ActiveRecord::Migration
def change
rename_column 'forum_threads', 'expression1_count', 'off_topic_count'
rename_column 'forum_threads', 'expression2_count', 'offensive_count'
rename_column 'forum_threads', 'expression3_count', 'like_count'
remove_column 'forum_threads', 'expression4_count'
remove_column 'forum_threads', 'expression5_count'
end
end

View File

@@ -0,0 +1,9 @@
class RenameExpressionColumnsInPosts < ActiveRecord::Migration
def change
rename_column 'posts', 'expression1_count', 'off_topic_count'
rename_column 'posts', 'expression2_count', 'offensive_count'
rename_column 'posts', 'expression3_count', 'like_count'
remove_column 'posts', 'expression4_count'
remove_column 'posts', 'expression5_count'
end
end

Some files were not shown because too many files have changed in this diff Show More