From 3cac0daa68bdc90b0c1f46165ebb8c8bf51bff64 Mon Sep 17 00:00:00 2001 From: Gibheer Date: Mon, 3 Feb 2014 23:43:46 +0100 Subject: [PATCH] add initial migration file and further adjustments The 01_setup.rb contains the old table structure to make it possible to migrate to the new system. 02_move_to_new on the other hand adjusts the old schema to the new format and adds the full text function. With that, the new schema is ready and the completion of the full text search can continue. --- migrations/01_setup.rb | 47 ++++++++++++++++++++++++++++++++ migrations/02_move_to_new.rb | 52 ++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 migrations/01_setup.rb create mode 100644 migrations/02_move_to_new.rb diff --git a/migrations/01_setup.rb b/migrations/01_setup.rb new file mode 100644 index 0000000..af2c882 --- /dev/null +++ b/migrations/01_setup.rb @@ -0,0 +1,47 @@ +Sequel.migration do + change do + create_table(:accounts) do + primary_key :id + String :username, :size=>50 + String :email, :size=>50 + String :crypted_password, :size=>70 + String :role, :size=>50 + end + + create_table(:tags) do + primary_key :id + String :name, :text=>true + end + + create_table(:posts, :ignore_index_errors=>true) do + primary_key :id + String :title, :text=>true, :null=>false + DateTime :written + TrueClass :released, :default=>false + String :markup, :default=>"markdown", :text=>true + String :content, :text=>true + foreign_key :account_id, :accounts, :null=>false, :key=>[:id] + + index [:account_id], :name=>:index_posts_account + index [:written, :id], :name=>:posts_written_id_idx + end + + create_table(:comments, :ignore_index_errors=>true) do + primary_key :id + String :author, :size=>50 + String :email, :size=>50 + TrueClass :acknowledged, :default=>false + String :body, :text=>true + foreign_key :post_id, :posts, :null=>false, :key=>[:id] + + index [:post_id], :name=>:index_comments_post + end + + create_table(:post_tags) do + foreign_key :post_id, :posts, :null=>false, :key=>[:id] + foreign_key :tag_id, :tags, :null=>false, :key=>[:id] + + primary_key [:post_id, :tag_id] + end + end +end diff --git a/migrations/02_move_to_new.rb b/migrations/02_move_to_new.rb new file mode 100644 index 0000000..b89b774 --- /dev/null +++ b/migrations/02_move_to_new.rb @@ -0,0 +1,52 @@ +# this is the schema from the old platform +Sequel.migration do + up do + drop_table :post_tags + drop_table :comments + drop_table :tags + + alter_table :posts do + add_column :language, String, :size => 10 + end + + from(:posts).update(:language => 'english') + + alter_table :posts do + set_column_not_null :language + end + + run 'create function search_field(posts) returns tsvector as $$' + + "select to_tsvector($1.language::regconfig, $1.title || ' ' || $1.content);" + + '$$ language sql immutable;' + end + + down do + alter_table :posts do + drop_column :language + end + run 'drop function search_field(posts);' + + create_table(:tags) do + primary_key :id + String :name, :text=>true + end + + create_table(:comments, :ignore_index_errors=>true) do + primary_key :id + String :author, :size=>50 + String :email, :size=>50 + TrueClass :acknowledged, :default=>false + String :body, :text=>true + foreign_key :post_id, :posts, :null=>false, :key=>[:id] + + index [:post_id], :name=>:index_comments_post + end + + create_table(:post_tags) do + foreign_key :post_id, :posts, :null=>false, :key=>[:id] + foreign_key :tag_id, :tags, :null=>false, :key=>[:id] + + primary_key [:post_id, :tag_id] + end + end +end