Skip to content

Commit

Permalink
Disabled serialization for JSON type columns.
Browse files Browse the repository at this point in the history
  • Loading branch information
colavitam committed Jul 15, 2015
1 parent 54c7890 commit 9ea86bb
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ script:
before_script:
- mysql -e 'create database devise_token_auth_test'
- psql -c 'create database devise_token_auth_test' -U postgres

addons:
postgresql: "9.3"
13 changes: 12 additions & 1 deletion app/models/devise_token_auth/concerns/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def self.tokens_match?(token_hash, token)
self.devise_modules.delete(:omniauthable)
end

serialize :tokens, JSON
unless tokens_has_json_column_type?
serialize :tokens, JSON
end

validates :email, presence: true, email: true, if: Proc.new { |u| u.provider == 'email' }
validates_presence_of :uid, if: Proc.new { |u| u.provider != 'email' }
Expand Down Expand Up @@ -82,6 +84,15 @@ def send_reset_password_instructions(opts=nil)
end
end

module ClassMethods
protected


def tokens_has_json_column_type?
table_exists? && self.columns_hash['tokens'] && self.columns_hash['tokens'].type.in?([:json, :jsonb])
end
end


def valid_token?(token, client_id='default')
client_id ||= 'default'
Expand Down
1 change: 1 addition & 0 deletions test/dummy/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ class Application < Rails::Application
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
config.autoload_paths << Rails.root.join('lib')
end
end
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include MigrationDatabaseHelper

class DeviseTokenAuthCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
Expand Down Expand Up @@ -42,7 +44,11 @@ def change
t.string :uid, :null => false, :default => ""

## Tokens
t.text :tokens
if json_supported_database?
t.json :tokens
else
t.text :tokens
end

t.timestamps
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include MigrationDatabaseHelper

class DeviseTokenAuthCreateMangs < ActiveRecord::Migration
def change
create_table(:mangs) do |t|
Expand Down Expand Up @@ -42,7 +44,11 @@ def change
t.string :uid, :null => false, :default => ""

## Tokens
t.text :tokens
if json_supported_database?
t.json :tokens
else
t.text :tokens
end

t.timestamps
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include MigrationDatabaseHelper

class DeviseTokenAuthCreateEvilUsers < ActiveRecord::Migration
def change
create_table(:evil_users) do |t|
Expand Down Expand Up @@ -40,7 +42,11 @@ def change
t.string :uid, :null => false, :default => ""

## Tokens
t.text :tokens
if json_supported_database?
t.json :tokens
else
t.text :tokens
end

## etc.
t.string :favorite_color
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include MigrationDatabaseHelper

class DeviseTokenAuthCreateOnlyEmailUsers < ActiveRecord::Migration
def change
create_table(:only_email_users) do |t|
Expand Down Expand Up @@ -40,7 +42,11 @@ def change
t.string :email

## Tokens
t.text :tokens
if json_supported_database?
t.json :tokens
else
t.text :tokens
end

t.timestamps
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include MigrationDatabaseHelper

class DeviseTokenAuthCreateUnregisterableUsers < ActiveRecord::Migration
def change
create_table(:unregisterable_users) do |t|
Expand Down Expand Up @@ -40,7 +42,11 @@ def change
t.string :email

## Tokens
t.text :tokens
if json_supported_database?
t.json :tokens
else
t.text :tokens
end

t.timestamps
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include MigrationDatabaseHelper

class DeviseTokenAuthCreateNiceUsers < ActiveRecord::Migration
def change
create_table(:nice_users) do |t|
Expand Down Expand Up @@ -40,7 +42,11 @@ def change
t.string :email

## Tokens
t.text :tokens
if json_supported_database?
t.json :tokens
else
t.text :tokens
end

t.timestamps
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include MigrationDatabaseHelper

class DeviseTokenAuthCreateUnconfirmableUsers < ActiveRecord::Migration
def change
create_table(:unconfirmable_users) do |t|
Expand Down Expand Up @@ -40,7 +42,11 @@ def change
t.string :email

## Tokens
t.text :tokens
if json_supported_database?
t.json :tokens
else
t.text :tokens
end

t.timestamps
end
Expand Down
29 changes: 29 additions & 0 deletions test/dummy/lib/migration_database_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module MigrationDatabaseHelper
def json_supported_database?
(postgres? && postgres_correct_version?) || (mysql? && mysql_correct_version?)
end

def postgres?
database_name == 'ActiveRecord::ConnectionAdapters::PostgreSQLAdapter'
end

def postgres_correct_version?
database_version > '9.3'
end

def mysql?
database_name == 'ActiveRecord::ConnectionAdapters::MysqlAdapter'
end

def mysql_correct_version?
database_version > '5.7.7'
end

def database_name
ActiveRecord::Base.connection.class.name
end

def database_version
ActiveRecord::Base.connection.select_value('SELECT VERSION()')
end
end

0 comments on commit 9ea86bb

Please sign in to comment.