diff --git a/db/migrate/20100204214746_create_posts.rb b/db/migrate/20100204214746_create_posts.rb new file mode 100644 index 000000000..9a732cd1c --- /dev/null +++ b/db/migrate/20100204214746_create_posts.rb @@ -0,0 +1,96 @@ +class CreatePosts < ActiveRecord::Migration + def self.up + create_table :posts do |t| + t.timestamps + + t.column :score, :integer, :null => false, :default => 0 + t.column :source, :string + t.column :md5, :string, :null => false + t.column :rating, :character, :null => false, :default => 'q' + t.column :is_note_locked, :boolean, :null => false, :default => false + t.column :is_rating_locked, :boolean, :null => false, :default => false + t.column :is_pending, :boolean, :null => false, :default => false + t.column :is_flagged, :boolean, :null => false, :default => false + t.column :approver_id, :integer + t.column :change_seq, :integer, :default => "nextval('post_change_seq'::regclass)" + + # Uploader + t.column :uploader_id, :integer, :null => false + t.column :uploader_ip_addr, "inet", :null => false + + # Cached + t.column :last_noted_at, :datetime + t.column :last_commented_at, :datetime + + # Tags + t.column :tag_string, :text, :null => false + t.column :tag_index, "tsvector" + t.column :tag_count, :integer, :null => false, :default => 0 + t.column :tag_count_general, :integer, :null => false, :default => 0 + t.column :tag_count_artist, :integer, :null => false, :default => 0 + t.column :tag_count_character, :integer, :null => false, :default => 0 + t.column :tag_count_copyright, :integer, :null => false, :default => 0 + + # File + t.column :file_ext, :string, :null => false + t.column :image_width, :integer, :null => false + t.column :image_height, :integer, :null => false + t.column :file_size, :integer, :null => false + end + + add_index :posts, :md5, :unique => true + add_index :posts, :created_at + add_index :posts, :last_commented_at + add_index :posts, :last_noted_at + add_index :posts, :uploader_id + add_index :posts, :approver_id + add_index :posts, :change_seq + add_index :posts, :file_size + add_index :posts, :image_width + add_index :posts, :image_height + add_index :posts, :source + + execute "CREATE INDEX index_posts_on_mpixels ON posts (((image_width * image_height)::numeric / 1000000.0))" + + execute "SET statement_timeout = 0" + execute "SET search_path = public" + + execute "CREATE OR REPLACE FUNCTION testprs_start(internal, int4) + RETURNS internal + AS '$libdir/test_parser' + LANGUAGE C STRICT" + + execute "CREATE OR REPLACE FUNCTION testprs_getlexeme(internal, internal, internal) + RETURNS internal + AS '$libdir/test_parser' + LANGUAGE C STRICT" + + execute "CREATE OR REPLACE FUNCTION testprs_end(internal) + RETURNS void + AS '$libdir/test_parser' + LANGUAGE C STRICT" + + execute "CREATE OR REPLACE FUNCTION testprs_lextype(internal) + RETURNS internal + AS '$libdir/test_parser' + LANGUAGE C STRICT" + + execute "CREATE TEXT SEARCH PARSER testparser ( + START = testprs_start, + GETTOKEN = testprs_getlexeme, + END = testprs_end, + HEADLINE = pg_catalog.prsd_headline, + LEXTYPES = testprs_lextype + )" + + execute "CREATE INDEX index_posts_on_tags_index ON posts USING gin (tag_index)" + execute "CREATE TEXT SEARCH CONFIGURATION public.danbooru (PARSER = public.testparser)" + execute "ALTER TEXT SEARCH CONFIGURATION public.danbooru ADD MAPPING FOR WORD WITH SIMPLE" + execute "SET default_text_search_config = 'public.danbooru'" + execute "CREATE TRIGGER trigger_posts_on_tag_index_update BEFORE INSERT OR UPDATE ON posts FOR EACH ROW EXECUTE PROCEDURE tsvector_update_trigger('tag_index', 'public.danbooru', 'tag_string')" + end + + def self.down + drop_table :posts + end +end