sequel-pg-comment 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,36 @@
1
+ require 'git-version-bump'
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "sequel-pg-comment"
5
+
6
+ s.version = GVB.version
7
+ s.date = GVB.date
8
+
9
+ s.platform = Gem::Platform::RUBY
10
+
11
+ s.homepage = "http://theshed.hezmatt.org/sequel-pg-comment"
12
+ s.summary = "Set comments on PgSQL objects from seuqel migrations"
13
+ s.authors = ["Matt Palmer"]
14
+
15
+ s.extra_rdoc_files = ["README.md"]
16
+ s.files = `git ls-files -z`.split("\0")
17
+
18
+ s.add_runtime_dependency "git-version-bump", "~> 0.10"
19
+ s.add_runtime_dependency "sequel"
20
+
21
+ s.add_development_dependency 'bundler'
22
+ s.add_development_dependency 'github-release'
23
+ s.add_development_dependency 'guard-spork'
24
+ s.add_development_dependency 'guard-rspec'
25
+ # Needed for guard
26
+ s.add_development_dependency 'rb-inotify', '~> 0.9'
27
+ if RUBY_VERSION =~ /^1\./
28
+ s.add_development_dependency 'pry-debugger'
29
+ else
30
+ s.add_development_dependency 'pry-byebug'
31
+ end
32
+ s.add_development_dependency 'rake', '~> 10.4', '>= 10.4.2'
33
+ s.add_development_dependency 'redcarpet'
34
+ s.add_development_dependency 'rspec', '~> 3.0'
35
+ s.add_development_dependency 'yard'
36
+ end
@@ -0,0 +1,124 @@
1
+ require_relative 'spec_helper'
2
+ require 'sequel'
3
+
4
+ describe "schema modification" do
5
+ let(:db) do
6
+ Sequel.connect("mock://postgres").extension(:pg_comment)
7
+ end
8
+
9
+ it "sets a column comment on add_column" do
10
+ db.alter_table :foo do
11
+ add_column(:data, String, :comment => "Owhatanight")
12
+ end
13
+ expect(db.sqls.last).
14
+ to eq("COMMENT ON COLUMN \"foo\".\"data\" IS 'Owhatanight'")
15
+ end
16
+
17
+ it "sets a column comment on add_primary_key" do
18
+ db.alter_table :foo do
19
+ add_primary_key :id, :comment => "Identify!"
20
+ end
21
+ expect(db.sqls.last).
22
+ to eq("COMMENT ON COLUMN \"foo\".\"id\" IS 'Identify!'")
23
+ end
24
+
25
+ it "sets an index comment on composite add_primary_key" do
26
+ db.alter_table :foo do
27
+ add_primary_key [:name, :dob], :comment => "Uniquify!"
28
+ end
29
+ expect(db.sqls.last).
30
+ to eq("COMMENT ON INDEX \"foo_pkey\" IS 'Uniquify!'")
31
+ end
32
+
33
+ it "sets a column comment on add_foreign_key" do
34
+ db.alter_table :foo do
35
+ add_foreign_key :bar_id, :bar, :comment => "Over there!"
36
+ end
37
+ expect(db.sqls.last).
38
+ to eq("COMMENT ON COLUMN \"foo\".\"bar_id\" IS 'Over there!'")
39
+ end
40
+
41
+ it "sets a column comment on add_foreign_key with custom constraint name" do
42
+ db.alter_table :foo do
43
+ add_foreign_key :bar_id, :bar, :comment => "Over there!", :name => :fkr
44
+ end
45
+ expect(db.sqls.last).
46
+ to eq("COMMENT ON COLUMN \"foo\".\"bar_id\" IS 'Over there!'")
47
+ end
48
+
49
+ it "sets a constraint comment on composite add_foreign_key" do
50
+ db.alter_table :foo do
51
+ add_foreign_key [:name, :dob], :bar, :comment => "Over there!"
52
+ end
53
+ expect(db.sqls.last).
54
+ to eq("COMMENT ON CONSTRAINT \"foo_name_fkey\" ON \"foo\" IS 'Over there!'")
55
+ end
56
+
57
+ it "sets a constraint comment on composite add_foreign_key with custom constraint name" do
58
+ db.alter_table :foo do
59
+ add_foreign_key [:name, :dob], :bar, :comment => "Over there!", :name => :fkr
60
+ end
61
+ expect(db.sqls.last).
62
+ to eq("COMMENT ON CONSTRAINT \"fkr\" ON \"foo\" IS 'Over there!'")
63
+ end
64
+
65
+ it "sets an index comment" do
66
+ db.alter_table :foo do
67
+ add_index :name, :comment => "Speedy!"
68
+ end
69
+ expect(db.sqls.last).
70
+ to eq("COMMENT ON INDEX \"foo_name_index\" IS 'Speedy!'")
71
+ end
72
+
73
+ it "sets an index comment with custom name" do
74
+ db.alter_table :foo do
75
+ add_index :name, :name => :some_idx, :comment => "Speedify!"
76
+ end
77
+ expect(db.sqls.last).
78
+ to eq("COMMENT ON INDEX \"some_idx\" IS 'Speedify!'")
79
+ end
80
+
81
+ it "sets an index comment on multi-column index" do
82
+ db.alter_table :foo do
83
+ add_index [:name, :dob], :comment => "Speedizer!"
84
+ end
85
+ expect(db.sqls.last).
86
+ to eq("COMMENT ON INDEX \"foo_name_dob_index\" IS 'Speedizer!'")
87
+ end
88
+
89
+ it "sets an index comment on multi-column index with custom name" do
90
+ db.alter_table :foo do
91
+ add_index [:name, :dob], :name => :my_idx, :comment => "Digispeed!"
92
+ end
93
+ expect(db.sqls.last).
94
+ to eq("COMMENT ON INDEX \"my_idx\" IS 'Digispeed!'")
95
+ end
96
+
97
+ it "sets a constraint comment" do
98
+ db.alter_table :foo do
99
+ add_constraint(:min_length, :comment => "Bigger is better!") do
100
+ char_length(name) > 2
101
+ end
102
+ end
103
+ expect(db.sqls.last).
104
+ to eq("COMMENT ON CONSTRAINT \"min_length\" ON \"foo\" IS 'Bigger is better!'")
105
+ end
106
+
107
+ it "sets a unique constraint comment" do
108
+ db.alter_table :foo do
109
+ add_unique_constraint [:name, :dob], :comment => "Only one"
110
+ end
111
+ expect(db.sqls.last).
112
+ to eq("COMMENT ON INDEX \"foo_name_dob_key\" IS 'Only one'")
113
+ end
114
+
115
+ it "sets a unique constraint comment with custom name" do
116
+ db.alter_table :foo do
117
+ add_unique_constraint [:name, :dob],
118
+ :comment => "Only one",
119
+ :name => :uniquify
120
+ end
121
+ expect(db.sqls.last).
122
+ to eq("COMMENT ON INDEX \"uniquify\" IS 'Only one'")
123
+ end
124
+ end
@@ -0,0 +1,34 @@
1
+ require_relative 'spec_helper'
2
+ require 'sequel'
3
+
4
+ describe "#comment_for" do
5
+ let(:db) do
6
+ Sequel.connect("mock://postgres").extension(:pg_comment)
7
+ end
8
+
9
+ it "gets a table comment" do
10
+ db.comment_for(:foo)
11
+ expect(db.sqls).
12
+ to eq(["SELECT obj_description('foo'::regclass, 'pg_class')"])
13
+ end
14
+
15
+ it "gets a column comment" do
16
+ db.comment_for(:foo__column)
17
+ expect(db.sqls).
18
+ to eq(["SELECT col_description(c.oid, a.attnum) " +
19
+ "FROM pg_class c " +
20
+ "JOIN pg_attribute a ON (c.oid=a.attrelid) " +
21
+ "WHERE c.relname='foo' AND a.attname='column'"
22
+ ])
23
+ end
24
+
25
+ it "gets a column comment via the dataset" do
26
+ db[:foo].comment_for(:column)
27
+ expect(db.sqls).
28
+ to eq(["SELECT col_description(c.oid, a.attnum) " +
29
+ "FROM pg_class c " +
30
+ "JOIN pg_attribute a ON (c.oid=a.attrelid) " +
31
+ "WHERE c.relname='foo' AND a.attname='column'"
32
+ ])
33
+ end
34
+ end
@@ -0,0 +1,56 @@
1
+ require_relative 'spec_helper'
2
+ require 'sequel'
3
+
4
+ describe "#comment_on" do
5
+ let(:db) do
6
+ Sequel.connect("mock://postgres").extension(:pg_comment)
7
+ end
8
+
9
+ it "sets a table comment" do
10
+ db.comment_on(:table, :foo, "Ohai!")
11
+ expect(db.sqls).
12
+ to eq(["COMMENT ON TABLE \"foo\" IS 'Ohai!'"])
13
+ end
14
+
15
+ it "accepts a string as object type" do
16
+ db.comment_on("table", :foo, "Ohai!")
17
+ expect(db.sqls).
18
+ to eq(["COMMENT ON TABLE \"foo\" IS 'Ohai!'"])
19
+ end
20
+
21
+ it "accepts a string as object name" do
22
+ db.comment_on(:table, "foo", "Ohai!")
23
+ expect(db.sqls).
24
+ to eq(["COMMENT ON TABLE foo IS 'Ohai!'"])
25
+ end
26
+
27
+ it "sets an aggregate comment" do
28
+ db.comment_on(:aggregate, :foo, "Ohai!")
29
+ expect(db.sqls).
30
+ to eq(["COMMENT ON AGGREGATE \"foo\" IS 'Ohai!'"])
31
+ end
32
+
33
+ it "escapes the comment" do
34
+ db.comment_on(:table, :foo, "O'hai!")
35
+ expect(db.sqls).
36
+ to eq(["COMMENT ON TABLE \"foo\" IS 'O''hai!'"])
37
+ end
38
+
39
+ it "explodes if an invalid object type is given" do
40
+ expect do
41
+ db.comment_on(:foobooblee, :foo, "O'hai!")
42
+ end.to raise_error(ArgumentError, /invalid object type/i)
43
+ end
44
+
45
+ it "quotes the object name" do
46
+ db.comment_on(:table, :"foo bar", "Ohai!")
47
+ expect(db.sqls).
48
+ to eq(["COMMENT ON TABLE \"foo bar\" IS 'Ohai!'"])
49
+ end
50
+
51
+ it "sets a column comment correctly" do
52
+ db.comment_on(:column, :foo__bar_id, "Ohai, column!")
53
+ expect(db.sqls).
54
+ to eq(["COMMENT ON COLUMN \"foo\".\"bar_id\" IS 'Ohai, column!'"])
55
+ end
56
+ end
@@ -0,0 +1,20 @@
1
+ require_relative 'spec_helper'
2
+ require 'sequel'
3
+
4
+ describe "#create_join_table" do
5
+ let(:db) do
6
+ Sequel.connect("mock://postgres").extension(:pg_comment)
7
+ end
8
+
9
+ it "sets a table comment" do
10
+ db.create_join_table(
11
+ {
12
+ :category_id => :categories,
13
+ :term_id => :terms
14
+ },
15
+ :comment => "HABTM FTW!"
16
+ )
17
+ expect(db.sqls.last).
18
+ to eq("COMMENT ON TABLE categories_terms IS 'HABTM FTW!'")
19
+ end
20
+ end
@@ -0,0 +1,193 @@
1
+ require_relative 'spec_helper'
2
+ require 'sequel'
3
+
4
+ describe "schema creation" do
5
+ let(:db) do
6
+ Sequel.connect("mock://postgres").extension(:pg_comment)
7
+ end
8
+
9
+ it "sets a table comment" do
10
+ db.create_table(:foo, :comment => "Ohai!") do
11
+ String :data
12
+ end
13
+
14
+ expect(db.sqls.last).
15
+ to eq("COMMENT ON TABLE \"foo\" IS 'Ohai!'")
16
+ end
17
+
18
+ it "sets a column comment" do
19
+ db.create_table :foo do
20
+ String :data, :comment => "Owhatanight"
21
+ end
22
+
23
+ expect(db.sqls.last).
24
+ to eq("COMMENT ON COLUMN \"foo\".\"data\" IS 'Owhatanight'")
25
+ end
26
+
27
+ it "sets a table comment on :as query" do
28
+ db.create_table(
29
+ :older_items,
30
+ :as => db[:items].where { updated_at < Date.today << 6 },
31
+ :comment => "WTF?"
32
+ )
33
+ expect(db.sqls.last).
34
+ to eq("COMMENT ON TABLE \"older_items\" IS 'WTF?'")
35
+ end
36
+
37
+ it "sets a primary key comment" do
38
+ db.create_table :foo do
39
+ primary_key :id, :comment => "I am unique"
40
+ end
41
+
42
+ expect(db.sqls.last).
43
+ to eq("COMMENT ON COLUMN \"foo\".\"id\" IS 'I am unique'")
44
+ end
45
+
46
+ it "sets a primary key comment on a custom constraint name" do
47
+ db.create_table :foo do
48
+ primary_key :id,
49
+ :comment => "I am unique",
50
+ :name => :custom_pk
51
+ end
52
+
53
+ expect(db.sqls.last).
54
+ to eq("COMMENT ON COLUMN \"foo\".\"id\" IS 'I am unique'")
55
+ end
56
+
57
+ it "sets a composite primary key comment" do
58
+ db.create_table :foo do
59
+ primary_key [:bar, :baz],
60
+ :comment => "So many things"
61
+ end
62
+
63
+ expect(db.sqls.last).
64
+ to eq("COMMENT ON INDEX \"foo_pkey\" IS 'So many things'")
65
+ end
66
+
67
+ it "sets a composite primary key comment with custom constraint name" do
68
+ db.create_table :foo do
69
+ primary_key [:bar, :baz],
70
+ :comment => "So many things",
71
+ :name => :custom_pk
72
+ end
73
+
74
+ expect(db.sqls.last).
75
+ to eq("COMMENT ON INDEX \"custom_pk\" IS 'So many things'")
76
+ end
77
+
78
+ it "sets a foreign_key comment" do
79
+ db.create_table :foo do
80
+ foreign_key :bar_id, :bar, :comment => "Over there!"
81
+ end
82
+
83
+ expect(db.sqls.last).
84
+ to eq("COMMENT ON COLUMN \"foo\".\"bar_id\" IS 'Over there!'")
85
+ end
86
+
87
+ it "sets a composite foreign_key comment" do
88
+ db.create_table :foo do
89
+ foreign_key [:bar_name, :bar_dob], :bar, :comment => "Over there!"
90
+ end
91
+
92
+ expect(db.sqls.last).
93
+ to eq("COMMENT ON CONSTRAINT \"bar_bar_name_fkey\" ON \"foo\" IS 'Over there!'")
94
+ end
95
+
96
+ it "sets a composite foreign_key comment with custom name" do
97
+ db.create_table :foo do
98
+ foreign_key [:bar_name, :bar_dob],
99
+ :bar,
100
+ :comment => "Over there!",
101
+ :name => :fkr
102
+ end
103
+
104
+ expect(db.sqls.last).
105
+ to eq("COMMENT ON CONSTRAINT \"fkr\" ON \"foo\" IS 'Over there!'")
106
+ end
107
+
108
+ it "sets an index comment" do
109
+ db.create_table :foo do
110
+ Integer :id
111
+ index :id, :comment => "Makes it fast"
112
+ end
113
+
114
+ expect(db.sqls.last).
115
+ to eq("COMMENT ON INDEX \"foo_id_index\" IS 'Makes it fast'")
116
+ end
117
+
118
+ it "sets an index comment on multiple columns" do
119
+ db.create_table :foo do
120
+ Integer :id
121
+ index [:name, :dob], :comment => "Makes it fast"
122
+ end
123
+
124
+ expect(db.sqls.last).
125
+ to eq("COMMENT ON INDEX \"foo_name_dob_index\" IS 'Makes it fast'")
126
+ end
127
+
128
+ it "sets an index comment with custom index name" do
129
+ db.create_table :foo do
130
+ Integer :id
131
+ index :id, :comment => "Makes it fast", :name => :zoom
132
+ end
133
+
134
+ expect(db.sqls.last).
135
+ to eq("COMMENT ON INDEX \"zoom\" IS 'Makes it fast'")
136
+ end
137
+
138
+ it "sets a unique index comment" do
139
+ db.create_table :foo do
140
+ Integer :id
141
+ unique :id, :comment => "There can be only one"
142
+ end
143
+
144
+ expect(db.sqls.last).
145
+ to eq("COMMENT ON INDEX \"foo_id_key\" IS 'There can be only one'")
146
+ end
147
+
148
+ it "sets a unique index comment on multiple columns" do
149
+ db.create_table :foo do
150
+ String :name
151
+ Date :dob
152
+ unique [:name, :dob], :comment => "Going solo"
153
+ end
154
+
155
+ expect(db.sqls.last).
156
+ to eq("COMMENT ON INDEX \"foo_name_dob_key\" IS 'Going solo'")
157
+ end
158
+
159
+ it "sets a unique index comment with custom name" do
160
+ db.create_table :foo do
161
+ Integer :id
162
+ unique :id, :comment => "Going solo", :name => :zoom
163
+ end
164
+
165
+ expect(db.sqls.last).
166
+ to eq("COMMENT ON INDEX \"zoom\" IS 'Going solo'")
167
+ end
168
+
169
+ it "sets a constraint comment" do
170
+ db.create_table :foo do
171
+ constraint :clamp, :num => 1..5, :comment => "Toight"
172
+ end
173
+
174
+ expect(db.sqls.last).
175
+ to eq("COMMENT ON CONSTRAINT \"clamp\" ON \"foo\" IS 'Toight'")
176
+ end
177
+
178
+ it "blows up trying to an unnamed constraint comment" do
179
+ expect do
180
+ db.create_table :foo do
181
+ constraint nil, :num => 1..5, :comment => "Kaboom"
182
+ end
183
+ end.to raise_error(/not supported/i)
184
+ end
185
+
186
+ it "blows up trying to comment on a check" do
187
+ expect do
188
+ db.create_table :foo do
189
+ check(:comment => "Kaboom") { char_length(name) > 2 }
190
+ end
191
+ end.to raise_error(/not supported/i)
192
+ end
193
+ end