dumbo 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/Gemfile +1 -1
  4. data/bin/dumbo +65 -24
  5. data/config/boot.rb +2 -3
  6. data/dumbo.gemspec +1 -2
  7. data/lib/dumbo.rb +17 -16
  8. data/lib/dumbo/aggregate.rb +3 -5
  9. data/lib/dumbo/base_type.rb +17 -17
  10. data/lib/dumbo/binding_loader.rb +50 -0
  11. data/lib/dumbo/cast.rb +5 -5
  12. data/lib/dumbo/composite_type.rb +4 -4
  13. data/lib/dumbo/db_task.rb +6 -5
  14. data/lib/dumbo/dependency_resolver.rb +17 -18
  15. data/lib/dumbo/enum_type.rb +3 -4
  16. data/lib/dumbo/extension.rb +64 -20
  17. data/lib/dumbo/extension_migrator.rb +11 -11
  18. data/lib/dumbo/extension_version.rb +29 -11
  19. data/lib/dumbo/function.rb +21 -21
  20. data/lib/dumbo/operator.rb +4 -6
  21. data/lib/dumbo/pg_object.rb +17 -21
  22. data/lib/dumbo/rake_task.rb +63 -53
  23. data/lib/dumbo/range_type.rb +6 -9
  24. data/lib/dumbo/test.rb +8 -0
  25. data/lib/dumbo/test/fixture.rb +51 -0
  26. data/lib/dumbo/test/helper.rb +64 -0
  27. data/lib/dumbo/test/matchers.rb +76 -0
  28. data/lib/dumbo/test/regression_helper.rb +20 -0
  29. data/lib/dumbo/test/silence_unknown_oid.rb +12 -0
  30. data/lib/dumbo/type.rb +3 -4
  31. data/lib/dumbo/version.rb +1 -1
  32. data/spec/aggregate_spec.rb +9 -10
  33. data/spec/cast_spec.rb +5 -5
  34. data/spec/{Makefile → dumbo_sample/Makefile} +4 -0
  35. data/spec/{dumbo_sample--0.0.1.sql → dumbo_sample/dumbo_sample--0.0.1.sql} +0 -0
  36. data/spec/{dumbo_sample--0.0.2.sql → dumbo_sample/dumbo_sample--0.0.2.sql} +0 -0
  37. data/spec/dumbo_sample/dumbo_sample--0.0.3.sql +7 -0
  38. data/spec/dumbo_sample/dumbo_sample--0.0.4.sql +13 -0
  39. data/spec/{dumbo_sample.control → dumbo_sample/dumbo_sample.control} +0 -0
  40. data/spec/dumbo_sample/src/dumbo_sample.c +13 -0
  41. data/spec/dumbo_sample/src/dumbo_sample.h +17 -0
  42. data/spec/extension_migrator_spec.rb +12 -11
  43. data/spec/extension_spec.rb +41 -12
  44. data/spec/extension_version_spec.rb +27 -0
  45. data/spec/operator_spec.rb +6 -7
  46. data/spec/spec_helper.rb +15 -9
  47. data/spec/support/extension_helper.rb +31 -0
  48. data/spec/support/silence_unknown_oid.rb +12 -0
  49. data/spec/type_spec.rb +59 -55
  50. data/template/Rakefile +6 -6
  51. data/template/spec/sample_spec.rb.erb +1 -1
  52. metadata +33 -31
  53. data/config/database.yml +0 -31
  54. data/lib/tasks/db.rake +0 -52
  55. data/lib/tasks/dumbo.rake +0 -23
  56. data/spec/support/sql_helper.rb +0 -23
@@ -2,7 +2,6 @@ module Dumbo
2
2
  class EnumType < Type
3
3
  attr_accessor :labels
4
4
 
5
-
6
5
  def load_attributes
7
6
  super
8
7
 
@@ -12,11 +11,11 @@ module Dumbo
12
11
  WHERE enumtypid = #{oid}
13
12
  ORDER by enumsortorder
14
13
  SQL
15
- @labels = res.to_a.map{|r| r['enumlabel']}
14
+ @labels = res.to_a.map { |r| r['enumlabel'] }
16
15
  end
17
16
 
18
17
  def to_sql
19
- lbl_str = labels.map{|l| "'"+l+"'"}.join(",\n ")
18
+ lbl_str = labels.map { |l| "'" + l + "'" }.join(",\n ")
20
19
 
21
20
  <<-SQL.gsub(/^ {6}/, '')
22
21
  CREATE TYPE #{name} AS ENUM (
@@ -25,4 +24,4 @@ module Dumbo
25
24
  SQL
26
25
  end
27
26
  end
28
- end
27
+ end
@@ -1,35 +1,80 @@
1
1
  module Dumbo
2
- class Extension
3
- attr_reader :name, :version
4
- def initialize(name = nil, version = nil)
5
- @name = name || File.read('Makefile')[/EXTENSION *= *([^\s]*)/,1]
6
- @version = version || File.read("#{self.name}.control")[/default_version *= *'([^']*)'/,1]
2
+ class Extension < Struct.new(:name, :version)
3
+ class << self
4
+ def name
5
+ @_name ||= File.read(makefile)[/EXTENSION\s*=\s*([^\s]*)/, 1]
6
+ end
7
+
8
+ def version
9
+ @_version ||= File.read(control_file)[/default_version\s*=\s*'([^']*)'/, 1]
10
+ end
11
+
12
+ def versions
13
+ new.versions
14
+ end
15
+
16
+ def version!(string)
17
+ content = File.read(control_file)
18
+ new_content = content.gsub(version, new_version)
19
+ File.open(control_file, 'w') { |file| file.puts new_content }
20
+ end
21
+
22
+ def file_name
23
+ "#{name}--#{version}.sql"
24
+ end
25
+
26
+ private
27
+
28
+ def makefile
29
+ 'Makefile'
30
+ end
31
+
32
+ def control_file
33
+ "#{name}.control"
34
+ end
35
+ end
36
+
37
+ def name
38
+ self[:name] ||= Extension.name
39
+ end
40
+
41
+ def version
42
+ self[:version] ||= Extension.version
7
43
  end
8
44
 
9
45
  # main releases without migrations
10
46
  def releases
11
- Dir.glob("#{name}--*.sql").reject{|f| f=~/\d--\d/}
47
+ Dir.glob("#{name}--*.sql").reject { |f| f =~ /\d--\d/ }
12
48
  end
13
49
 
14
- def available_versions
15
- versions = releases.map{|f| ExtensionVersion.new f[/#{name}--([\d\.]*?)\.sql/,1] }
16
- versions.sort
50
+ def versions
51
+ releases.map do |file_name|
52
+ if version_string = file_name[/([\d+\.]+)\.sql$/, 1]
53
+ ExtensionVersion.new_from_string(version_string)
54
+ else
55
+ nil
56
+ end
57
+ end.compact.sort
17
58
  end
18
59
 
19
- def install
60
+ def create
20
61
  execute "DROP EXTENSION IF EXISTS #{name}"
21
- execute "CREATE EXTENSION #{name} VERSION '#{version}'"
62
+
63
+ create_sql = "CREATE EXTENSION #{name}"
64
+ create_sql = "#{create_sql} VERSION '#{version}'" unless version.nil?
65
+
66
+ execute create_sql
22
67
  self
23
68
  end
24
69
 
25
70
  def obj_id
26
71
  @obj_id ||= begin
27
- result = execute <<-sql
72
+ result = execute <<-SQL
28
73
  SELECT e.extname, e.oid
29
74
  FROM pg_catalog.pg_extension e
30
75
  WHERE e.extname ~ '^(#{name})$'
31
76
  ORDER BY 1;
32
- sql
77
+ SQL
33
78
  result.first['oid']
34
79
  end
35
80
  end
@@ -43,24 +88,24 @@ module Dumbo
43
88
  ORDER BY 1;
44
89
  SQL
45
90
 
46
- result.map{|r| PgObject.new(r['objid']).get(r['classid'])}
91
+ result.map { |r| PgObject.new(r['objid']).get(r['classid']) }
47
92
  end
48
93
  end
49
94
 
50
95
  def types
51
- objects.select{|o| o.kind_of?(Type)}
96
+ objects.select { |o| o.kind_of?(Type) }
52
97
  end
53
98
 
54
99
  def functions
55
- objects.select{|o| o.kind_of?(Function)}
100
+ objects.select { |o| o.kind_of?(Function) }
56
101
  end
57
102
 
58
103
  def casts
59
- objects.select{|o| o.kind_of?(Cast)}
104
+ objects.select { |o| o.kind_of?(Cast) }
60
105
  end
61
106
 
62
107
  def operators
63
- objects.select{|o| o.kind_of?(Operator)}
108
+ objects.select { |o| o.kind_of?(Operator) }
64
109
  end
65
110
 
66
111
  private
@@ -68,6 +113,5 @@ module Dumbo
68
113
  def execute(sql)
69
114
  ActiveRecord::Base.connection.execute sql
70
115
  end
71
-
72
116
  end
73
- end
117
+ end
@@ -5,31 +5,31 @@ module Dumbo
5
5
 
6
6
  def initialize(name, old_version, new_version)
7
7
  @name = name
8
- @old_version = Extension.new(name, old_version).install
8
+ @old_version = Extension.new(name, old_version).create
9
9
  @old_version.objects
10
- @new_version = Extension.new(name, new_version).install
10
+ @new_version = Extension.new(name, new_version).create
11
11
  @new_version.objects
12
12
  end
13
13
 
14
14
  def create
15
- File.open("#{name}--#{old_version.version}--#{new_version.version}.sql",'w') do |f|
15
+ File.open("#{name}--#{old_version.version}--#{new_version.version}.sql", 'w') do |f|
16
16
  f.puts upgrade
17
17
  end
18
- File.open("#{name}--#{new_version.version}--#{old_version.version}.sql",'w') do |f|
18
+ File.open("#{name}--#{new_version.version}--#{old_version.version}.sql", 'w') do |f|
19
19
  f.puts downgrade
20
20
  end
21
21
  end
22
22
 
23
23
  def upgrade
24
24
  TYPES.map do |type|
25
- diff = object_diff(type,:upgrade)
25
+ diff = object_diff(type, :upgrade)
26
26
  "----#{type}----\n" + diff if diff.present?
27
27
  end.compact.join("\n")
28
28
  end
29
29
 
30
30
  def downgrade
31
31
  TYPES.map do |type|
32
- diff = object_diff(type,:downgrade)
32
+ diff = object_diff(type, :downgrade)
33
33
  "----#{type}----\n" + diff if diff.present?
34
34
  end.compact.join("\n")
35
35
  end
@@ -46,12 +46,12 @@ module Dumbo
46
46
  ids = @old_version.public_send(type).map(&:identify) | @new_version.public_send(type).map(&:identify)
47
47
 
48
48
  sqls = ids.map do |id|
49
- n = @new_version.public_send(type).find{|n| n.identify == id }
50
- o = @old_version.public_send(type).find{|n| n.identify == id }
49
+ n = @new_version.public_send(type).find { |n| n.identify == id }
50
+ o = @old_version.public_send(type).find { |n| n.identify == id }
51
51
  if n
52
- n.public_send(dir,o)
52
+ n.public_send(dir, o)
53
53
  elsif o
54
- o.public_send(dir,o)
54
+ o.public_send(dir, o)
55
55
  end
56
56
  end
57
57
 
@@ -63,4 +63,4 @@ module Dumbo
63
63
  ActiveRecord::Base.connection.execute sql
64
64
  end
65
65
  end
66
- end
66
+ end
@@ -1,25 +1,43 @@
1
1
  module Dumbo
2
- class ExtensionVersion
2
+ class ExtensionVersion < Struct.new(:major, :minor, :patch)
3
3
  include Comparable
4
4
 
5
- attr_reader :major, :minor, :patch
5
+ class << self
6
+ def new_from_string(version)
7
+ ExtensionVersion.new(*version.split('.').map(&:to_i))
8
+ end
6
9
 
7
- def initialize(version="")
8
- @major, @minor, @patch = version.split(".").map(&:to_i)
10
+ def sort
11
+ sort! { |a, b| a <=> b }
12
+ end
9
13
  end
10
14
 
11
15
  def <=>(other)
12
- return major <=> other.major if ((major <=> other.major) != 0)
13
- return minor <=> other.minor if ((minor <=> other.minor) != 0)
14
- return patch <=> other.patch if ((patch <=> other.patch) != 0)
16
+ return major <=> other.major if (major <=> other.major) != 0
17
+ return minor <=> other.minor if (minor <=> other.minor) != 0
18
+ return patch <=> other.patch if (patch <=> other.patch) != 0
15
19
  end
16
20
 
17
- def self.sort
18
- self.sort!{|a,b| a <=> b}
21
+ def bump(level)
22
+ send("bump_#{level}")
19
23
  end
20
24
 
21
25
  def to_s
22
- [major,minor,patch].join('.')
26
+ to_a.compact.join('.')
27
+ end
28
+
29
+ private
30
+
31
+ def bump_major
32
+ ExtensionVersion.new(major + 1, 0, 0)
33
+ end
34
+
35
+ def bump_minor
36
+ ExtensionVersion.new(major, minor.to_i + 1, 0)
37
+ end
38
+
39
+ def bump_patch
40
+ ExtensionVersion.new(major, minor, patch.to_i + 1)
23
41
  end
24
42
  end
25
- end
43
+ end
@@ -17,40 +17,40 @@ module Dumbo
17
17
  end
18
18
 
19
19
  def drop
20
- "DROP FUNCTION #{name} #{arg_types}"
20
+ "DROP FUNCTION #{name}(#{arg_types});"
21
21
  end
22
22
 
23
23
  def upgrade(other)
24
- return self.to_sql if other.nil?
24
+ return to_sql if other.nil?
25
25
 
26
- if other.identify != self.identify
27
- raise "Not the Same Objects!"
26
+ if other.identify != identify
27
+ fail 'Not the Same Objects!'
28
28
  end
29
29
 
30
- return nil if other.to_sql == self.to_sql
30
+ return nil if other.to_sql == to_sql
31
31
 
32
- if other.result_type != self.result_type
33
- <<-SQL.gsub(/^ {8}/, '')
34
- #{self.drop}
35
- #{self.to_sql}
32
+ if other.result_type != result_type
33
+ <<-SQL.gsub(/^ {8}/, '')
34
+ #{drop}
35
+ #{to_sql}
36
36
  SQL
37
37
  else
38
- self.to_sql
38
+ to_sql
39
39
  end
40
40
  end
41
41
 
42
42
  def downgrade(other)
43
- return self.to_sql if other.nil?
43
+ return to_sql if other.nil?
44
44
 
45
- if other.identify != self.identify
46
- raise "Not the Same Objects!"
45
+ if other.identify != identify
46
+ fail 'Not the Same Objects!'
47
47
  end
48
48
 
49
- return nil if other.to_sql == self.to_sql
49
+ return nil if other.to_sql == to_sql
50
50
 
51
- if other.result_type != self.result_type
52
- <<-SQL.gsub(/^ {8}/, '')
53
- #{self.drop}
51
+ if other.result_type != result_type
52
+ <<-SQL.gsub(/^ {8}/, '')
53
+ #{drop}
54
54
  #{other.to_sql}
55
55
  SQL
56
56
  else
@@ -87,15 +87,15 @@ module Dumbo
87
87
  AND p.oid = #{oid};
88
88
  SQL
89
89
 
90
- result.first.each do |k,v|
91
- send("#{k}=",v) rescue nil
90
+ result.first.each do |k, v|
91
+ send("#{k}=", v) rescue nil
92
92
  end
93
93
 
94
94
  result.first
95
95
  end
96
96
 
97
97
  def to_sql
98
- definition.gsub("public.#{name}",name)
98
+ definition.gsub("public.#{name}", name)
99
99
  end
100
100
  end
101
- end
101
+ end
@@ -16,7 +16,6 @@ module Dumbo
16
16
  identfied_by :name, :leftarg, :rightarg
17
17
 
18
18
  def load_attributes
19
-
20
19
  result = execute <<-SQL
21
20
  SELECT
22
21
  op.oprname AS name,
@@ -42,16 +41,15 @@ module Dumbo
42
41
  WHERE op.oid = #{oid}
43
42
  SQL
44
43
 
45
- result.first.each do |k,v|
46
- send("#{k}=",v) rescue nil
44
+ result.first.each do |k, v|
45
+ send("#{k}=", v) rescue nil
47
46
  end
48
47
 
49
48
  result.first
50
-
51
49
  end
52
50
 
53
51
  def to_sql
54
- attrs = [:leftarg, :rightarg, :commutator, :negator, :restrict, :join].inject([]) do |mem, attr|
52
+ attrs = [:leftarg, :rightarg, :commutator, :negator, :restrict, :join].reduce([]) do |mem, attr|
55
53
  mem << "#{attr.to_s.upcase} = #{public_send(attr)}" if public_send(attr)
56
54
  mem
57
55
  end
@@ -71,4 +69,4 @@ module Dumbo
71
69
  "DROP OPERATOR #{name};"
72
70
  end
73
71
  end
74
- end
72
+ end
@@ -17,10 +17,10 @@ module Dumbo
17
17
  end
18
18
 
19
19
  def identify
20
- identifier.map{|a| public_send a}
20
+ identifier.map { |a| public_send a }
21
21
  end
22
22
 
23
- def get(type=nil)
23
+ def get(type = nil)
24
24
  case type
25
25
  when 'function', 'pg_proc'
26
26
  Function.new(oid).get
@@ -31,42 +31,39 @@ module Dumbo
31
31
  when 'type', 'pg_type'
32
32
  Type.new(oid).get
33
33
  else
34
- self.load_attributes
34
+ load_attributes
35
35
  self
36
36
  end
37
37
  end
38
38
 
39
39
  def load_attributes
40
-
41
40
  end
42
41
 
43
42
  def upgrade(other)
44
- return self.to_sql if other.nil?
43
+ return to_sql if other.nil?
45
44
 
46
- if other.identify != self.identify
47
- raise "Not the Same Objects!"
45
+ if other.identify != identify
46
+ fail 'Not the Same Objects!'
48
47
  end
49
48
 
50
- if other.to_sql != self.to_sql
51
- <<-SQL.gsub(/^ {8}/, '')
52
- #{self.drop}
53
- #{self.to_sql}
49
+ if other.to_sql != to_sql
50
+ <<-SQL.gsub(/^ {8}/, '')
51
+ #{drop}
52
+ #{to_sql}
54
53
  SQL
55
54
  end
56
-
57
-
58
55
  end
59
56
 
60
57
  def downgrade(other)
61
- return self.drop if other.nil?
58
+ return drop if other.nil?
62
59
 
63
- if other.identify != self.identify
64
- raise "Not the Same Objects!"
60
+ if other.identify != identify
61
+ fail 'Not the Same Objects!'
65
62
  end
66
63
 
67
- if other.to_sql != self.to_sql
68
- <<-SQL.gsub(/^ {8}/, '')
69
- #{self.drop}
64
+ if other.to_sql != to_sql
65
+ <<-SQL.gsub(/^ {8}/, '')
66
+ #{drop}
70
67
  #{other.to_sql}
71
68
  SQL
72
69
  end
@@ -75,6 +72,5 @@ module Dumbo
75
72
  def execute(sql)
76
73
  ActiveRecord::Base.connection.execute(sql)
77
74
  end
78
-
79
75
  end
80
- end
76
+ end