migration_comments 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,12 +1,12 @@
1
- module MigrationComments::ActiveRecord::ConnectionAdapters
2
- module Table
3
- def change_comment(column_name, comment_text)
4
- @base.set_column_comment(@table_name, column_name, comment_text)
5
- end
6
-
7
- def change_table_comment(comment_text)
8
- @base.set_table_comment(@table_name, comment_text)
9
- end
10
- alias comment :change_table_comment
11
- end
1
+ module MigrationComments::ActiveRecord::ConnectionAdapters
2
+ module Table
3
+ def change_comment(column_name, comment_text)
4
+ @base.set_column_comment(@table_name, column_name, comment_text)
5
+ end
6
+
7
+ def change_table_comment(comment_text)
8
+ @base.set_table_comment(@table_name, comment_text)
9
+ end
10
+ alias comment :change_table_comment
11
+ end
12
12
  end
@@ -1,30 +1,32 @@
1
- module MigrationComments::ActiveRecord::ConnectionAdapters
2
- module TableDefinition
3
-
4
- attr_accessor :table_comment
5
- def self.included(base)
6
- base.class_eval do
7
- alias_method_chain :column, :migration_comments
8
- end
9
- end
10
-
11
- def comment(text)
12
- @table_comment = CommentDefinition.new(@base, nil, nil, text)
13
- self
14
- end
15
-
16
- def column_with_migration_comments(name, type, options = {})
17
- column_without_migration_comments(name, type, options)
18
- col = self[name]
19
- col.comment = CommentDefinition.new(@base, nil, name, options[:comment])
20
- self
21
- end
22
-
23
- def collect_comments(table_name)
24
- comments = []
25
- comments << @table_comment << @columns.map(&:comment)
26
- comments.flatten!.compact!
27
- comments.each{|comment| comment.table = table_name}
28
- end
29
- end
1
+ module MigrationComments::ActiveRecord::ConnectionAdapters
2
+ module TableDefinition
3
+
4
+ attr_accessor :table_comment
5
+ def self.included(base)
6
+ base.class_eval do
7
+ alias_method_chain :column, :migration_comments
8
+ end
9
+ end
10
+
11
+ def comment(text)
12
+ @table_comment = CommentDefinition.new(@base, nil, nil, text)
13
+ self
14
+ end
15
+
16
+ def column_with_migration_comments(name, type, options = {})
17
+ column_without_migration_comments(name, type, options)
18
+ if options.has_key?(:comment)
19
+ col = self[name]
20
+ col.comment = CommentDefinition.new(@base, nil, name, options[:comment])
21
+ end
22
+ self
23
+ end
24
+
25
+ def collect_comments(table_name)
26
+ comments = []
27
+ comments << @table_comment << @columns.map(&:comment)
28
+ comments.flatten!.compact!
29
+ comments.each{|comment| comment.table = table_name}
30
+ end
31
+ end
30
32
  end
@@ -1,54 +1,54 @@
1
- module MigrationComments::ActiveRecord
2
- module SchemaDumper
3
- def self.included(base)
4
- base.class_eval do
5
- alias_method_chain :table, :migration_comments
6
- end
7
- end
8
-
9
- def table_with_migration_comments(table, stream)
10
- tbl_stream = StringIO.new
11
- table_without_migration_comments(table, tbl_stream)
12
- tbl_stream.rewind
13
- commented_stream = append_comments(table, tbl_stream)
14
- tbl_stream.close
15
- stream.print commented_stream.read
16
- end
17
-
18
- def append_comments(table, stream)
19
- table_name = table.inspect.gsub('"', '')
20
- table_comment = @connection.retrieve_table_comment(table_name)
21
- column_comments = @connection.retrieve_column_comments(table_name)
22
- comment_stream = StringIO.new
23
- lines = []
24
- table_line = 0
25
- col_names = {}
26
- while (line = stream.gets)
27
- content = line.chomp
28
- if content =~ /create_table\s/
29
- table_line = lines.size
30
- elsif content =~ /t\.\w+\s+"(\w+)"/
31
- col_names[lines.size] = $1.to_sym
32
- end
33
- lines << content
34
- end
35
- len = col_names.keys.map{|index| lines[index]}.map(&:length).max + 2
36
- lines.each_with_index do |line, index|
37
- if table_line == index && table_comment.present?
38
- block_init = " do |t|"
39
- line.chomp!(block_init) << ", " << render_comment(table_comment) << block_init
40
- elsif col_names[index]
41
- comment = column_comments[col_names[index]]
42
- line << ',' << ' ' * (len - line.length) << render_comment(comment) unless comment.blank?
43
- end
44
- comment_stream.puts line
45
- end
46
- comment_stream.rewind
47
- comment_stream
48
- end
49
-
50
- def render_comment(comment)
51
- ":comment => \"#{comment}\""
52
- end
53
- end
1
+ module MigrationComments::ActiveRecord
2
+ module SchemaDumper
3
+ def self.included(base)
4
+ base.class_eval do
5
+ alias_method_chain :table, :migration_comments
6
+ end
7
+ end
8
+
9
+ def table_with_migration_comments(table, stream)
10
+ tbl_stream = StringIO.new
11
+ table_without_migration_comments(table, tbl_stream)
12
+ tbl_stream.rewind
13
+ commented_stream = append_comments(table, tbl_stream)
14
+ tbl_stream.close
15
+ stream.print commented_stream.read
16
+ end
17
+
18
+ def append_comments(table, stream)
19
+ table_name = table.inspect.gsub('"', '')
20
+ table_comment = @connection.retrieve_table_comment(table_name)
21
+ column_comments = @connection.retrieve_column_comments(table_name)
22
+ comment_stream = StringIO.new
23
+ lines = []
24
+ table_line = 0
25
+ col_names = {}
26
+ while (line = stream.gets)
27
+ content = line.chomp
28
+ if content =~ /create_table\s/
29
+ table_line = lines.size
30
+ elsif content =~ /t\.\w+\s+"(\w+)"/
31
+ col_names[lines.size] = $1.to_sym
32
+ end
33
+ lines << content
34
+ end
35
+ len = col_names.keys.map{|index| lines[index]}.map(&:length).max + 2
36
+ lines.each_with_index do |line, index|
37
+ if table_line == index && table_comment.present?
38
+ block_init = " do |t|"
39
+ line.chomp!(block_init) << ", " << render_comment(table_comment) << block_init
40
+ elsif col_names[index]
41
+ comment = column_comments[col_names[index]]
42
+ line << ',' << ' ' * (len - line.length) << render_comment(comment) unless comment.blank?
43
+ end
44
+ comment_stream.puts line
45
+ end
46
+ comment_stream.rewind
47
+ comment_stream
48
+ end
49
+
50
+ def render_comment(comment)
51
+ ":comment => \"#{comment}\""
52
+ end
53
+ end
54
54
  end
@@ -1,40 +1,40 @@
1
- module MigrationComments
2
- module AnnotateModels
3
- def self.included(base)
4
- base.class_eval do
5
- class << self
6
- include ClassMethods
7
- alias_method_chain :get_schema_info, :migration_comments
8
- end
9
- end
10
- end
11
-
12
- module ClassMethods
13
- def get_schema_info_with_migration_comments(*args)
14
- info = get_schema_info_without_migration_comments(*args)
15
- klass = args[0]
16
- commented_info(klass, info)
17
- end
18
-
19
- def commented_info(klass, info)
20
- table_name = klass.table_name
21
- adapter = klass.connection
22
- table_comment = adapter.retrieve_table_comment(table_name)
23
- column_comments = adapter.retrieve_column_comments(table_name)
24
- lines = []
25
- info.each_line{|l| lines << l.chomp}
26
- column_regex = /^#\s+(\w+)\s+:\w+/
27
- len = lines.select{|l| l =~ column_regex}.map{|l| l.length}.max
28
- lines.each do |line|
29
- if line =~ /# Table name: |# table \+\w+\+ /
30
- line << " # #{table_comment}" if table_comment
31
- elsif line =~ column_regex
32
- comment = column_comments[$1.to_sym]
33
- line << " " * (len - line.length) << " # #{comment}" if comment
34
- end
35
- end
36
- lines.join($/) + $/
37
- end
38
- end
39
- end
1
+ module MigrationComments
2
+ module AnnotateModels
3
+ def self.included(base)
4
+ base.class_eval do
5
+ class << self
6
+ include ClassMethods
7
+ alias_method_chain :get_schema_info, :migration_comments
8
+ end
9
+ end
10
+ end
11
+
12
+ module ClassMethods
13
+ def get_schema_info_with_migration_comments(*args)
14
+ info = get_schema_info_without_migration_comments(*args)
15
+ klass = args[0]
16
+ commented_info(klass, info)
17
+ end
18
+
19
+ def commented_info(klass, info)
20
+ table_name = klass.table_name
21
+ adapter = klass.connection
22
+ table_comment = adapter.retrieve_table_comment(table_name)
23
+ column_comments = adapter.retrieve_column_comments(table_name)
24
+ lines = []
25
+ info.each_line{|l| lines << l.chomp}
26
+ column_regex = /^#\s+(\w+)\s+:\w+/
27
+ len = lines.select{|l| l =~ column_regex}.map{|l| l.length}.max
28
+ lines.each do |line|
29
+ if line =~ /# Table name: |# table \+\w+\+ /
30
+ line << " # #{table_comment}" if table_comment
31
+ elsif line =~ column_regex
32
+ comment = column_comments[$1.to_sym]
33
+ line << " " * (len - line.length) << " # #{comment}" if comment
34
+ end
35
+ end
36
+ lines.join($/) + $/
37
+ end
38
+ end
39
+ end
40
40
  end
@@ -1,3 +1,3 @@
1
- module MigrationComments
2
- VERSION = "0.1.3"
3
- end
1
+ module MigrationComments
2
+ VERSION = "0.2.0"
3
+ end
@@ -1,34 +1,34 @@
1
- # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
3
- require "migration_comments/version"
4
-
5
- Gem::Specification.new do |s|
6
- s.name = "migration_comments"
7
- s.version = MigrationComments::VERSION
8
- s.authors = ["Pinny"]
9
- s.email = ["pinny@medwiztech.com"]
10
- s.homepage = "https://github.com/pinnymz/migration_comments"
11
- s.summary = %q{Comments for your migrations}
12
- s.description = %q{Add schema comments in your migrations, see them in model annotations and db/schema.rb dump}
13
-
14
- s.rubyforge_project = "migration_comments"
15
-
16
- s.files = `git ls-files`.split("\n")
17
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
- s.require_paths = ["lib"]
20
-
21
- s.add_runtime_dependency 'rails', '>= 2.3.2'
22
-
23
- # for development, we are testing against the 'annotate' gem
24
- # however, the comments should work with the original 'annotate_models' plugin as well at:
25
- # http://repo.pragprog.com/svn/Public/plugins/annotate_models
26
- # provided the environment is not loaded until _after_ the AnnotateModels module is declared
27
- s.add_development_dependency 'annotate'
28
-
29
- s.add_development_dependency 'pg' # replace with other adapter as needed
30
- # s.add_development_dependency 'postgres-pr'
31
- # s.add_development_dependency 'mysql'
32
- # s.add_development_dependency 'mysql2'
33
- # s.add_development_dependency 'sqlite3'
34
- end
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "migration_comments/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "migration_comments"
7
+ s.version = MigrationComments::VERSION
8
+ s.authors = ["Pinny"]
9
+ s.email = ["pinny@medwiztech.com"]
10
+ s.homepage = "https://github.com/pinnymz/migration_comments"
11
+ s.summary = %q{Comments for your migrations}
12
+ s.description = %q{Add schema comments in your migrations, see them in model annotations and db/schema.rb dump}
13
+
14
+ s.rubyforge_project = "migration_comments"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_runtime_dependency 'rails', '>= 2.3.2'
22
+
23
+ # for development, we are testing against the 'annotate' gem
24
+ # however, the comments should work with the original 'annotate_models' plugin as well at:
25
+ # http://repo.pragprog.com/svn/Public/plugins/annotate_models
26
+ # provided the environment is not loaded until _after_ the AnnotateModels module is declared
27
+ s.add_development_dependency 'annotate', '~> 2.5.0'
28
+
29
+ s.add_development_dependency 'pg' # replace with other adapter as needed
30
+ # s.add_development_dependency 'postgres-pr'
31
+ # s.add_development_dependency 'mysql'
32
+ # s.add_development_dependency 'mysql2'
33
+ # s.add_development_dependency 'sqlite3'
34
+ end
@@ -1,135 +1,135 @@
1
- require 'test_helper'
2
-
3
- class AddCommentsTest < Test::Unit::TestCase
4
- include TestHelper
5
-
6
- def test_adding_a_table_comment
7
- comment_text = "a comment on the sample table"
8
- result = nil
9
- ActiveRecord::Schema.define do
10
- set_table_comment :sample, comment_text
11
- result = retrieve_table_comment :sample
12
- end
13
- assert_equal comment_text, result
14
- end
15
-
16
- def test_adding_a_column_comment
17
- comment_text = "a comment on the sample table in the column field"
18
- result_field1 = nil
19
- result_field2 = nil
20
- ActiveRecord::Schema.define do
21
- set_column_comment :sample, :field1, comment_text
22
- result_field1 = retrieve_column_comment :sample, :field1
23
- result_field2 = retrieve_column_comment :sample, :field2
24
- end
25
- assert_equal comment_text, result_field1
26
- assert_nil result_field2
27
- end
28
-
29
- def test_creating_a_table_with_table_and_column_comments
30
- table_comment = "a table comment"
31
- column_comment = "a column comment"
32
- result_table_comment = nil
33
- result_column_comments = nil
34
- ActiveRecord::Schema.define do
35
- begin
36
- create_table :sample2, :comment => table_comment do |t|
37
- t.integer :field1, :comment => column_comment
38
- t.string :field2
39
- end
40
- result_table_comment = retrieve_table_comment :sample2
41
- result_column_comments = retrieve_column_comments :sample2
42
- ensure
43
- drop_table :sample2
44
- end
45
- end
46
- assert_equal table_comment, result_table_comment
47
- assert_equal column_comment, result_column_comments[:field1]
48
- assert_nil result_column_comments[:field2]
49
- end
50
-
51
- def test_changing_a_table_with_new_comments
52
- table_comment = "a table comment"
53
- column_comment1 = "a column comment"
54
- column_comment2 = "another column comment"
55
- result_table_comment = nil
56
- result_column_comments = nil
57
- ActiveRecord::Schema.define do
58
- change_table :sample do |t|
59
- t.comment table_comment
60
- t.change :field1, :string, :comment => column_comment1
61
- t.change :field2, :integer
62
- t.boolean :field3, :comment => column_comment2
63
- end
64
- result_table_comment = retrieve_table_comment :sample
65
- result_column_comments = retrieve_column_comments :sample
66
- end
67
- assert_equal table_comment, result_table_comment
68
- assert_equal column_comment1, result_column_comments[:field1]
69
- assert_equal column_comment2, result_column_comments[:field3]
70
- assert_nil result_column_comments[:field2]
71
- end
72
-
73
- def test_partially_modifying_comments_from_a_table
74
- table_comment = "a table comment"
75
- column_comment1 = "a column comment"
76
- column_comment2 = "another column comment"
77
- column_comment3 = "yet a third column comment"
78
- modified_comment = "modified comment"
79
- result_table_comment = nil
80
- result_column_comments = nil
81
- ActiveRecord::Schema.define do
82
- change_table :sample do |t|
83
- t.comment table_comment
84
- t.change :field1, :string, :comment => column_comment1
85
- t.change :field2, :integer, :comment => column_comment2
86
- t.boolean :field3, :comment => column_comment3
87
- end
88
- change_table :sample do |t|
89
- t.comment nil
90
- t.change :field1, :string
91
- t.change :field2, :integer, :comment => modified_comment
92
- t.change :field3, :boolean, :comment => nil
93
- end
94
- result_table_comment = retrieve_table_comment :sample
95
- result_column_comments = retrieve_column_comments :sample
96
- end
97
- assert_nil result_table_comment
98
- assert_equal column_comment1, result_column_comments[:field1]
99
- assert_equal modified_comment, result_column_comments[:field2]
100
- assert_nil result_column_comments[:field3]
101
- end
102
-
103
- def test_removing_comments_from_a_table
104
- comment_text = "a comment on the sample table"
105
- result = nil
106
- ActiveRecord::Schema.define do
107
- set_table_comment :sample, comment_text
108
- remove_table_comment :sample
109
- result = retrieve_table_comment :sample
110
- end
111
- assert_nil result
112
- end
113
-
114
- def test_removing_comments_from_a_column
115
- comment_text = "a comment on field1 of sample table"
116
- result = nil
117
- ActiveRecord::Schema.define do
118
- set_column_comment :sample, :field1, comment_text
119
- remove_column_comment :sample, :field1
120
- result = retrieve_column_comment :sample, :field1
121
- end
122
- assert_nil result
123
- end
124
-
125
- def test_comment_text_is_escaped_properly
126
- comment_text = "a \"comment\" \\ that ' needs; escaping''"
127
- result = nil
128
- ActiveRecord::Schema.define do
129
- set_table_comment :sample, comment_text
130
- result = retrieve_table_comment :sample
131
- end
132
- assert_equal comment_text, result
133
- end
134
-
135
- end
1
+ require './test_helper'
2
+
3
+ class AddCommentsTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ def test_adding_a_table_comment
7
+ comment_text = "a comment on the sample table"
8
+ result = nil
9
+ ActiveRecord::Schema.define do
10
+ set_table_comment :sample, comment_text
11
+ result = retrieve_table_comment :sample
12
+ end
13
+ assert_equal comment_text, result
14
+ end
15
+
16
+ def test_adding_a_column_comment
17
+ comment_text = "a comment on the sample table in the column field"
18
+ result_field1 = nil
19
+ result_field2 = nil
20
+ ActiveRecord::Schema.define do
21
+ set_column_comment :sample, :field1, comment_text
22
+ result_field1 = retrieve_column_comment :sample, :field1
23
+ result_field2 = retrieve_column_comment :sample, :field2
24
+ end
25
+ assert_equal comment_text, result_field1
26
+ assert_nil result_field2
27
+ end
28
+
29
+ def test_creating_a_table_with_table_and_column_comments
30
+ table_comment = "a table comment"
31
+ column_comment = "a column comment"
32
+ result_table_comment = nil
33
+ result_column_comments = nil
34
+ ActiveRecord::Schema.define do
35
+ begin
36
+ create_table :sample2, :comment => table_comment do |t|
37
+ t.integer :field1, :comment => column_comment
38
+ t.string :field2
39
+ end
40
+ result_table_comment = retrieve_table_comment :sample2
41
+ result_column_comments = retrieve_column_comments :sample2
42
+ ensure
43
+ drop_table :sample2
44
+ end
45
+ end
46
+ assert_equal table_comment, result_table_comment
47
+ assert_equal column_comment, result_column_comments[:field1]
48
+ assert_nil result_column_comments[:field2]
49
+ end
50
+
51
+ def test_changing_a_table_with_new_comments
52
+ table_comment = "a table comment"
53
+ column_comment1 = "a column comment"
54
+ column_comment2 = "another column comment"
55
+ result_table_comment = nil
56
+ result_column_comments = nil
57
+ ActiveRecord::Schema.define do
58
+ change_table :sample do |t|
59
+ t.comment table_comment
60
+ t.change :field1, :text, :comment => column_comment1
61
+ t.change :field2, :string
62
+ t.boolean :field3, :comment => column_comment2
63
+ end
64
+ result_table_comment = retrieve_table_comment :sample
65
+ result_column_comments = retrieve_column_comments :sample
66
+ end
67
+ assert_equal table_comment, result_table_comment
68
+ assert_equal column_comment1, result_column_comments[:field1]
69
+ assert_equal column_comment2, result_column_comments[:field3]
70
+ assert_nil result_column_comments[:field2]
71
+ end
72
+
73
+ def test_partially_modifying_comments_from_a_table
74
+ table_comment = "a table comment"
75
+ column_comment1 = "a column comment"
76
+ column_comment2 = "another column comment"
77
+ column_comment3 = "yet a third column comment"
78
+ modified_comment = "modified comment"
79
+ result_table_comment = nil
80
+ result_column_comments = nil
81
+ ActiveRecord::Schema.define do
82
+ change_table :sample do |t|
83
+ t.comment table_comment
84
+ t.change :field1, :string, :comment => column_comment1
85
+ t.change :field2, :integer, :comment => column_comment2
86
+ t.boolean :field3, :comment => column_comment3
87
+ end
88
+ change_table :sample do |t|
89
+ t.comment nil
90
+ t.change :field1, :text
91
+ t.change :field2, :string, :comment => modified_comment
92
+ t.change :field3, :boolean, :comment => nil
93
+ end
94
+ result_table_comment = retrieve_table_comment :sample
95
+ result_column_comments = retrieve_column_comments :sample
96
+ end
97
+ assert_nil result_table_comment
98
+ assert_equal column_comment1, result_column_comments[:field1]
99
+ assert_equal modified_comment, result_column_comments[:field2]
100
+ assert_nil result_column_comments[:field3]
101
+ end
102
+
103
+ def test_removing_comments_from_a_table
104
+ comment_text = "a comment on the sample table"
105
+ result = nil
106
+ ActiveRecord::Schema.define do
107
+ set_table_comment :sample, comment_text
108
+ remove_table_comment :sample
109
+ result = retrieve_table_comment :sample
110
+ end
111
+ assert_nil result
112
+ end
113
+
114
+ def test_removing_comments_from_a_column
115
+ comment_text = "a comment on field1 of sample table"
116
+ result = nil
117
+ ActiveRecord::Schema.define do
118
+ set_column_comment :sample, :field1, comment_text
119
+ remove_column_comment :sample, :field1
120
+ result = retrieve_column_comment :sample, :field1
121
+ end
122
+ assert_nil result
123
+ end
124
+
125
+ def test_comment_text_is_escaped_properly
126
+ comment_text = "a \"comment\" \\ that ' needs; escaping''"
127
+ result = nil
128
+ ActiveRecord::Schema.define do
129
+ set_table_comment :sample, comment_text
130
+ result = retrieve_table_comment :sample
131
+ end
132
+ assert_equal comment_text, result
133
+ end
134
+
135
+ end