Skip to content
GitLab
    • Explore Projects Groups Snippets
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • T torque-postgresql
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 6
    • Issues 6
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 1
    • Merge requests 1
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Carlos
  • torque-postgresql
  • Merge requests
  • !30
An error occurred while fetching the assigned milestone of the selected merge_request.

General fixes

  • Review changes

  • Download
  • Email patches
  • Plain diff
Merged Carlos requested to merge general-fixes into master 6 years ago
  • Overview 1
  • Commits 4
  • Pipelines 0
  • Changes 6
  1. Fix issue with Enum and Inheritance
  2. Fix error with Rails 2.2
  3. Fix schema dumper trying to export types defined by extensions
Compare
  • master (base)

and
  • latest version
    146a5ed7
    4 commits, 2 years ago

6 files
+ 61
- 31

    Preferences

    File browser
    Compare changes
lib/torque‎/postgresql‎
ada‎pter‎
database_st‎atements.rb‎ +25 -19
attribute‎s/builder‎
enu‎m.rb‎ +13 -6
inherit‎ance.rb‎ +2 -0
schema_d‎umper.rb‎ +2 -5
versi‎on.rb‎ +1 -1
spec/‎tests‎
enum_s‎pec.rb‎ +18 -0
lib/torque/postgresql/adapter/database_statements.rb
+ 25
- 19
  • View file @ 146a5ed7

  • Edit in single-file editor

  • Open in Web IDE


@@ -90,26 +90,32 @@ module Torque
# Gets a list of user defined types.
# You can even choose the +category+ filter
def user_defined_types(category = nil)
category_condition = "AND typtype = '#{category}'" unless category.nil?
def user_defined_types(*categories)
category_condition = categories.present? \
? "AND t.typtype IN ('#{categories.join("', '")}')" \
: "AND t.typtype NOT IN ('b', 'd')"
select_all(<<-SQL, 'SCHEMA').rows.to_h
SELECT t.typname AS name,
CASE t.typtype
WHEN 'e' THEN 'enum'
END AS type
FROM pg_type t
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
#{category_condition}
AND NOT EXISTS(
SELECT 1 FROM pg_catalog.pg_type el
WHERE el.oid = t.typelem AND el.typarray = t.oid
)
AND (t.typrelid = 0 OR (
SELECT c.relkind = 'c' FROM pg_catalog.pg_class c
WHERE c.oid = t.typrelid
))
ORDER BY t.typtype DESC
SELECT t.typname AS name,
CASE t.typtype
WHEN 'e' THEN 'enum'
END AS type
FROM pg_type t
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
#{category_condition}
AND NOT EXISTS(
SELECT 1
FROM pg_catalog.pg_type el
WHERE el.oid = t.typelem
AND el.typarray = t.oid
)
AND (t.typrelid = 0 OR (
SELECT c.relkind = 'c'
FROM pg_catalog.pg_class c
WHERE c.oid = t.typrelid
))
ORDER BY t.typtype DESC
SQL
end
lib/torque/postgresql/adapter/database_statements.rb
+ 25
- 19
  • View file @ 146a5ed7

  • Edit in single-file editor

  • Open in Web IDE


@@ -90,26 +90,32 @@ module Torque
# Gets a list of user defined types.
# You can even choose the +category+ filter
def user_defined_types(category = nil)
category_condition = "AND typtype = '#{category}'" unless category.nil?
def user_defined_types(*categories)
category_condition = categories.present? \
? "AND t.typtype IN ('#{categories.join("', '")}')" \
: "AND t.typtype NOT IN ('b', 'd')"
select_all(<<-SQL, 'SCHEMA').rows.to_h
SELECT t.typname AS name,
CASE t.typtype
WHEN 'e' THEN 'enum'
END AS type
FROM pg_type t
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
#{category_condition}
AND NOT EXISTS(
SELECT 1 FROM pg_catalog.pg_type el
WHERE el.oid = t.typelem AND el.typarray = t.oid
)
AND (t.typrelid = 0 OR (
SELECT c.relkind = 'c' FROM pg_catalog.pg_class c
WHERE c.oid = t.typrelid
))
ORDER BY t.typtype DESC
SELECT t.typname AS name,
CASE t.typtype
WHEN 'e' THEN 'enum'
END AS type
FROM pg_type t
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
#{category_condition}
AND NOT EXISTS(
SELECT 1
FROM pg_catalog.pg_type el
WHERE el.oid = t.typelem
AND el.typarray = t.oid
)
AND (t.typrelid = 0 OR (
SELECT c.relkind = 'c'
FROM pg_catalog.pg_class c
WHERE c.oid = t.typrelid
))
ORDER BY t.typtype DESC
SQL
end
lib/torque/postgresql/attributes/builder/enum.rb
+ 13
- 6
  • View file @ 146a5ed7

  • Edit in single-file editor

  • Open in Web IDE


@@ -4,7 +4,7 @@ module Torque
module Builder
class Enum
attr_accessor :klass, :attribute, :subtype, :options, :values
attr_accessor :klass, :attribute, :subtype, :options, :values, :enum_module
# Start a new builder of methods for composite values on
# ActiveRecord::Base
@@ -74,9 +74,14 @@ module Torque
# Create all methods needed
def build
@enum_module = Module.new
plural
text
all_values
klass.include enum_module
klass.extend enum_module::ClassMethods
end
private
@@ -98,7 +103,8 @@ module Torque
def plural
attr = attribute
enum_klass = subtype.klass
klass.singleton_class.module_eval do
enum_module.const_set('ClassMethods', Module.new)
enum_module::ClassMethods.module_eval do
# def self.statuses() statuses end
define_method(attr.pluralize) do
enum_klass.values
@@ -113,7 +119,7 @@ module Torque
# def self.statuses_options() statuses_texts.zip(statuses) end
define_method(attr.pluralize + '_options') do
enum_klass.values
public_send(attr.pluralize + '_texts').zip(enum_klass.values)
end
end
end
@@ -122,7 +128,7 @@ module Torque
# the model scope
def text
attr = attribute
klass.module_eval do
enum_module.module_eval do
# def status_text() status.text('status', self) end
define_method("#{attr}_text") { send(attr).text(attr, self) }
end
@@ -133,10 +139,11 @@ module Torque
def all_values
attr = attribute
vals = values_methods
klass.module_eval do
model_klass = klass
enum_module.module_eval do
vals.each do |val, list|
# scope :disabled, -> { where(status: 'disabled') }
scope list[0], -> { where(attr => val) }
model_klass.scope list[0], -> { where(attr => val) }
# def disabled? status.disabled? end
define_method(list[1]) { send(attr).public_send("#{val}?") }
lib/torque/postgresql/inheritance.rb
+ 2
- 0
  • View file @ 146a5ed7

  • Edit in single-file editor

  • Open in Web IDE


@@ -39,6 +39,8 @@ module Torque
# Check if the model's table depends on any inheritance
def physically_inherited?
return false unless connected?
@physically_inherited ||= connection.schema_cache.dependencies(
defined?(@table_name) ? @table_name : decorated_table_name,
).present?
lib/torque/postgresql/schema_dumper.rb
+ 2
- 5
  • View file @ 146a5ed7

  • Edit in single-file editor

  • Open in Web IDE


@@ -59,14 +59,11 @@ module Torque
# Dump user defined types like enum
def user_defined_types(stream)
types = @connection.user_defined_types
types = @connection.user_defined_types('e')
return unless types.any?
stream.puts " # These are user-defined types used on this database"
types.each do |name, type|
raise StandardError, "User-defined type '#{name}' cannot be dumped!" if type.blank?
send(type.to_sym, name, stream)
end
types.each { |name, type| send(type.to_sym, name, stream) }
stream.puts
rescue => e
stream.puts "# Could not dump user-defined types because of following #{e.class}"
lib/torque/postgresql/version.rb
+ 1
- 1
  • View file @ 146a5ed7

  • Edit in single-file editor

  • Open in Web IDE

module Torque
module PostgreSQL
VERSION = '0.2.14'
VERSION = '0.2.15'
end
end
spec/tests/enum_spec.rb
+ 18
- 0
  • View file @ 146a5ed7

  • Edit in single-file editor

  • Open in Web IDE


@@ -513,6 +513,24 @@ RSpec.describe 'Enum' do
expect { type_map.decorate!(AText, :conflict) }.to raise_error(ArgumentError, /already exists in/)
end
context 'on inherited classes' do
it 'has all enum methods' do
klass = ActivityBook
instance = klass.new
expect(klass).to respond_to(:kinds)
expect(klass).to respond_to(:kinds_texts)
expect(klass).to respond_to(:kinds_options)
expect(instance).to respond_to(:kind_text)
klass.kinds.each do |value|
expect(klass).to respond_to(value)
expect(instance).to respond_to(value + '?')
expect(instance).to respond_to(value + '!')
end
end
end
context 'without autoload' do
subject { Author }
let(:instance) { FactoryGirl.build(:author) }
0 Assignees
None
Assign to
0 Reviewers
None
Request review from
Labels
0
None
0
None
    Assign labels
  • Manage project labels

Milestone
No milestone
None
None
Time tracking
No estimate or time spent
Lock merge request
Unlocked
1
1 participant
Administrator
Reference:
Source branch: general-fixes

Menu

Explore Projects Groups Snippets