[Git][noosfero/noosfero][master] 3 commits: Move from hstore to jsonb (no extension needed!)

Leandro Nunes gitlab at mg.gitlab.com
Fri May 5 15:50:48 BRT 2017


Leandro Nunes pushed to branch master at Noosfero / noosfero


Commits:
6c5b8ee3 by Braulio Bhavamitra at 2017-05-05T14:25:24-03:00
Move from hstore to jsonb (no extension needed!)

- - - - -
fca61b2e by Braulio Bhavamitra at 2017-05-05T14:41:18-03:00
Don't try to add hstore fields

- - - - -
b2b5b4bd by Leandro Nunes at 2017-05-05T18:50:39+00:00
Merge branch 'hstore-to-jsonb' into 'master'

Move from hstore to jsonb (no extension needed!)

See merge request !1200
- - - - -


5 changed files:

- app/models/concerns/metadata_scopes.rb
- − db/migrate/20170417135607_add_hstore_fields.rb
- + db/migrate/20170417135607_add_jsonb_fields.rb
- + db/migrate/20170505164823_move_from_hstore_to_jsonb.rb
- db/schema.rb


Changes:

=====================================
app/models/concerns/metadata_scopes.rb
=====================================
--- a/app/models/concerns/metadata_scopes.rb
+++ b/app/models/concerns/metadata_scopes.rb
@@ -3,12 +3,11 @@ module MetadataScopes
 
   included do
     scope :with_metadata, -> metadata {
-      term = metadata.map { |key, value| "#{key}=>#{value}"}.join(',')
-      where("metadata @> '#{term}'")
+      where metadata.map{ |k, v| "metadata->>'#{k}' = '#{v}'"}.join(' AND ')
     }
 
     scope :has_metadata, -> key {
-      where("metadata ? '#{key}'")
+      where "metadata ? '#{key}'"
     }
   end
 end


=====================================
db/migrate/20170417135607_add_hstore_fields.rb deleted
=====================================
--- a/db/migrate/20170417135607_add_hstore_fields.rb
+++ /dev/null
@@ -1,17 +0,0 @@
-class AddHstoreFields < ActiveRecord::Migration
-  def change
-    enable_extension :hstore
-
-    add_column :profiles, :metadata, :hstore, :default => {}
-    add_column :articles, :metadata, :hstore, :default => {}
-    add_column :tasks, :metadata, :hstore, :default => {}
-    add_column :blocks, :metadata, :hstore, :default => {}
-    add_column :users, :metadata, :hstore, :default => {}
-
-    add_index :profiles, :metadata, using: :gist
-    add_index :articles, :metadata, using: :gist
-    add_index :tasks, :metadata, using: :gist
-    add_index :blocks, :metadata, using: :gist
-    add_index :users, :metadata, using: :gist
-  end
-end


=====================================
db/migrate/20170417135607_add_jsonb_fields.rb
=====================================
--- /dev/null
+++ b/db/migrate/20170417135607_add_jsonb_fields.rb
@@ -0,0 +1,8 @@
+class AddJsonbFields < ActiveRecord::Migration
+  def change
+    %w[profiles articles tasks blocks users].each do |table|
+      add_column table, :metadata, :jsonb, :default => {}
+      add_index  table, :metadata, using: :gin
+    end
+  end
+end


=====================================
db/migrate/20170505164823_move_from_hstore_to_jsonb.rb
=====================================
--- /dev/null
+++ b/db/migrate/20170505164823_move_from_hstore_to_jsonb.rb
@@ -0,0 +1,12 @@
+class MoveFromHstoreToJsonb < ActiveRecord::Migration
+  def up
+    %w[profiles articles tasks blocks users].each do |table|
+      connection.execute "ALTER TABLE #{table} ALTER COLUMN metadata SET DEFAULT null"
+      connection.execute "DROP INDEX index_#{table}_on_metadata"
+
+      connection.execute "ALTER TABLE #{table} ALTER COLUMN metadata TYPE JSONB USING CAST(metadata as JSONB)"
+      connection.execute "ALTER TABLE #{table} ALTER COLUMN metadata SET DEFAULT '{}'"
+      add_index table, :metadata, using: :gin
+    end
+  end
+end


=====================================
db/schema.rb
=====================================
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,11 +11,10 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 20170427104432) do
+ActiveRecord::Schema.define(version: 20170505164823) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "plpgsql"
-  enable_extension "hstore"
 
   create_table "abuse_reports", force: :cascade do |t|
     t.integer  "reporter_id"
@@ -170,13 +169,13 @@ ActiveRecord::Schema.define(version: 20170427104432) do
     t.integer  "followers_count",      default: 0
     t.boolean  "archived",             default: false
     t.string   "editor",               default: "tiny_mce", null: false
-    t.hstore   "metadata",             default: {}
+    t.jsonb    "metadata",             default: {}
   end
 
   add_index "articles", ["comments_count"], name: "index_articles_on_comments_count", using: :btree
   add_index "articles", ["created_at"], name: "index_articles_on_created_at", using: :btree
   add_index "articles", ["hits"], name: "index_articles_on_hits", using: :btree
-  add_index "articles", ["metadata"], name: "index_articles_on_metadata", using: :gist
+  add_index "articles", ["metadata"], name: "index_articles_on_metadata", using: :gin
   add_index "articles", ["name"], name: "index_articles_on_name", using: :btree
   add_index "articles", ["parent_id"], name: "index_articles_on_parent_id", using: :btree
   add_index "articles", ["path", "profile_id"], name: "index_articles_on_path_and_profile_id", using: :btree
@@ -212,13 +211,13 @@ ActiveRecord::Schema.define(version: 20170427104432) do
     t.integer  "mirror_block_id"
     t.integer  "observers_id"
     t.string   "subtitle",        default: ""
-    t.hstore   "metadata",        default: {}
+    t.jsonb    "metadata",        default: {}
   end
 
   add_index "blocks", ["box_id"], name: "index_blocks_on_box_id", using: :btree
   add_index "blocks", ["enabled"], name: "index_blocks_on_enabled", using: :btree
   add_index "blocks", ["fetched_at"], name: "index_blocks_on_fetched_at", using: :btree
-  add_index "blocks", ["metadata"], name: "index_blocks_on_metadata", using: :gist
+  add_index "blocks", ["metadata"], name: "index_blocks_on_metadata", using: :gin
   add_index "blocks", ["type"], name: "index_blocks_on_type", using: :btree
 
   create_table "boxes", force: :cascade do |t|
@@ -665,7 +664,7 @@ ActiveRecord::Schema.define(version: 20170427104432) do
     t.boolean  "secret",                             default: false
     t.string   "editor",                             default: "tiny_mce", null: false
     t.integer  "top_image_id"
-    t.hstore   "metadata",                           default: {}
+    t.jsonb    "metadata",                           default: {}
   end
 
   add_index "profiles", ["activities_count"], name: "index_profiles_on_activities_count", using: :btree
@@ -674,7 +673,7 @@ ActiveRecord::Schema.define(version: 20170427104432) do
   add_index "profiles", ["friends_count"], name: "index_profiles_on_friends_count", using: :btree
   add_index "profiles", ["identifier"], name: "index_profiles_on_identifier", using: :btree
   add_index "profiles", ["members_count"], name: "index_profiles_on_members_count", using: :btree
-  add_index "profiles", ["metadata"], name: "index_profiles_on_metadata", using: :gist
+  add_index "profiles", ["metadata"], name: "index_profiles_on_metadata", using: :gin
   add_index "profiles", ["region_id"], name: "index_profiles_on_region_id", using: :btree
   add_index "profiles", ["user_id", "type"], name: "index_profiles_on_user_id_and_type", using: :btree
   add_index "profiles", ["user_id"], name: "index_profiles_on_user_id", using: :btree
@@ -832,10 +831,10 @@ ActiveRecord::Schema.define(version: 20170427104432) do
     t.boolean  "spam",                      default: false
     t.integer  "responsible_id"
     t.integer  "closed_by_id"
-    t.hstore   "metadata",                  default: {}
+    t.jsonb    "metadata",                  default: {}
   end
 
-  add_index "tasks", ["metadata"], name: "index_tasks_on_metadata", using: :gist
+  add_index "tasks", ["metadata"], name: "index_tasks_on_metadata", using: :gin
   add_index "tasks", ["requestor_id"], name: "index_tasks_on_requestor_id", using: :btree
   add_index "tasks", ["spam"], name: "index_tasks_on_spam", using: :btree
   add_index "tasks", ["status"], name: "index_tasks_on_status", using: :btree
@@ -892,10 +891,10 @@ ActiveRecord::Schema.define(version: 20170427104432) do
     t.datetime "last_login_at"
     t.string   "private_token"
     t.datetime "private_token_generated_at"
-    t.hstore   "metadata",                              default: {}
+    t.jsonb    "metadata",                              default: {}
   end
 
-  add_index "users", ["metadata"], name: "index_users_on_metadata", using: :gist
+  add_index "users", ["metadata"], name: "index_users_on_metadata", using: :gin
 
   create_table "validation_infos", force: :cascade do |t|
     t.text    "validation_methodology"



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/0fbfcad262b8e10c02fc22753ea9a347dccea59c...b2b5b4bd6cd3427e5cf81bd05dddd6799a5ffcfd

---
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/0fbfcad262b8e10c02fc22753ea9a347dccea59c...b2b5b4bd6cd3427e5cf81bd05dddd6799a5ffcfd
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20170505/7742b266/attachment-0001.html>


More information about the Noosfero-dev mailing list