migration_comments 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
- *.gem
2
- .bundle
3
- Gemfile.lock
4
- pkg/*
5
- .idea/*
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .idea/*
6
+ test/db/migration_comments_test
data/README.rdoc CHANGED
@@ -4,7 +4,7 @@ Comments for your migrations
4
4
 
5
5
  Tested on:
6
6
 
7
- Ruby 1.8.7 using Rails 3.x
7
+ Ruby 1.8.7 using Rails 3.x.
8
8
  Ruby 1.8.6 using Rails 2.3.x.
9
9
 
10
10
  == Why?
@@ -78,7 +78,7 @@ Or you can combine these commands while modifying a table...
78
78
 
79
79
  == Requirements
80
80
 
81
- You must be using a DBMS that supports COMMENTing (currently only PostgreSQL and MySQL).
81
+ You must be using a supported DBMS (currently PostgreSQL, MySQL, and SQLite).
82
82
 
83
83
  If this isn't an option for you, check out the 'schema_comments' gem:
84
84
  https://github.com/akm/schema_comments
@@ -0,0 +1,9 @@
1
+ module MigrationComments::ActiveRecord::ConnectionAdapters
2
+ module Column
3
+ def self.included(base)
4
+ base.class_eval do
5
+ attr_accessor :comment
6
+ end
7
+ end
8
+ end
9
+ end
@@ -8,13 +8,13 @@ module MigrationComments::ActiveRecord::ConnectionAdapters
8
8
  end
9
9
  end
10
10
 
11
- def add_table_comment(table_name, comment)
12
- execute "ALTER TABLE #{table_name} COMMENT #{escaped_comment(comment)}"
11
+ def add_table_comment(table_name, comment_text)
12
+ execute "ALTER TABLE #{table_name} COMMENT #{escaped_comment(comment_text)}"
13
13
  end
14
14
 
15
- def add_column_comment(table_name, column_name, comment)
15
+ def add_column_comment(table_name, column_name, comment_text)
16
16
  column = column_for(table_name, column_name)
17
- change_column table_name, column_name, column.sql_type, :comment => comment
17
+ change_column table_name, column_name, column.sql_type, :comment => comment_text
18
18
  end
19
19
 
20
20
  def retrieve_table_comment(table_name)
@@ -25,7 +25,8 @@ module MigrationComments::ActiveRecord::ConnectionAdapters
25
25
  def retrieve_column_comments(table_name, *column_names)
26
26
  result = select_rows(column_comment_sql(table_name, *column_names))
27
27
  return {} if result.nil?
28
- return result.inject({}){|m, row| m[row[0].to_sym] = (row[1].blank? ? nil : row[1]); m}
28
+ found = result.inject({}){|m, row| m[row[0].to_sym] = (row[1].blank? ? nil : row[1]); m}
29
+
29
30
  end
30
31
 
31
32
  def create_table_with_migration_comments(table_name, options={}, &block)
@@ -0,0 +1,140 @@
1
+ module MigrationComments::ActiveRecord::ConnectionAdapters
2
+ module SQLiteAdapter
3
+ def self.included(base)
4
+ base.class_eval do
5
+ alias_method_chain :columns, :migration_comments
6
+ alias_method_chain :copy_table, :migration_comments
7
+ alias_method_chain :change_column, :migration_comments
8
+ end
9
+ end
10
+
11
+ def add_table_comment(table_name, comment_text)
12
+ alter_table(table_name, :comment => comment_text)
13
+ end
14
+
15
+ def add_column_comment(table_name, column_name, comment_text)
16
+ column = column_for(table_name, column_name)
17
+ change_column table_name, column_name, column.sql_type, :comment => comment_text
18
+ end
19
+
20
+ def retrieve_table_comment(table_name)
21
+ result = select_rows(lookup_comment_sql(table_name))
22
+ if result[0][0] =~ /CREATE (?:TEMPORARY )?TABLE #{quote_table_name table_name} [^\(]*\/\*(.*)\*\/ \(/
23
+ $1
24
+ end
25
+ end
26
+
27
+ def retrieve_column_comments(table_name, *column_names)
28
+ if column_names.empty?
29
+ return columns(table_name).inject({}) { |m, v| m[v.name.to_sym] = v.comment if v.comment.present?; m }
30
+ end
31
+ result = select_rows(lookup_comment_sql(table_name))
32
+ result[0][0] =~ /^CREATE (?:TEMPORARY )?TABLE "\w*" [^\(]*(?:\/\*.*\*\/ )?\((.*)\)[^\)]*$/
33
+ col_defs = $1
34
+ comment_matches = col_defs.scan(/"([^",]+)"[^,]*\/\*(.+?)\*\//)
35
+ comment_matches.inject({}){|m, row| m[row.first.to_sym] = row.last; m}
36
+ end
37
+
38
+ def create_table(table_name, options = {})
39
+ td = table_definition
40
+ td.primary_key(options[:primary_key] || ActiveRecord::Base.get_primary_key(table_name.to_s.singularize)) unless options[:id] == false
41
+ td.comment options[:comment] if options.has_key?(:comment)
42
+
43
+ yield td if block_given?
44
+
45
+ if options[:force] && table_exists?(table_name)
46
+ drop_table(table_name)
47
+ end
48
+
49
+ create_sql = "CREATE#{' TEMPORARY' if options[:temporary]} TABLE "
50
+ create_sql << "#{quote_table_name(table_name)}#{td.table_comment} ("
51
+ create_sql << td.columns.map do |column|
52
+ column.to_sql + column.comment.to_sql
53
+ end * ", "
54
+ create_sql << ") #{options[:options]}"
55
+ execute create_sql
56
+ end
57
+
58
+ def change_column_with_migration_comments(table_name, column_name, type, options = {}) #:nodoc:
59
+ adapter = self
60
+ alter_table(table_name) do |definition|
61
+ include_default = options_include_default?(options)
62
+ definition[column_name].instance_eval do
63
+ self.type = type
64
+ self.limit = options[:limit] if options.include?(:limit)
65
+ self.default = options[:default] if include_default
66
+ self.null = options[:null] if options.include?(:null)
67
+ self.precision = options[:precision] if options.include?(:precision)
68
+ self.scale = options[:scale] if options.include?(:scale)
69
+ self.comment = CommentDefinition.new(adapter, table_name, column_name, options[:comment]) if options.include?(:comment)
70
+ end
71
+ end
72
+ end
73
+
74
+ def columns_with_migration_comments(table_name, name = nil)
75
+ cols = columns_without_migration_comments(table_name, name)
76
+ comments = retrieve_column_comments(table_name, *(cols.map(&:name)))
77
+ cols.each do |col|
78
+ col.comment = comments[col.name.to_sym] if comments.has_key?(col.name.to_sym)
79
+ end
80
+ cols
81
+ end
82
+
83
+ def column_for(table_name, column_name)
84
+ columns(table_name).detect{|col| col.name == column_name.to_s}
85
+ end
86
+
87
+ def comment_sql(comment_definition)
88
+ if comment_definition.nil? || comment_definition.comment_text.blank?
89
+ ""
90
+ else
91
+ " /*#{escaped_comment(comment_definition.comment_text)}*/"
92
+ end
93
+
94
+ end
95
+
96
+ def copy_table_with_migration_comments(from, to, options = {}) #:nodoc:
97
+ options = options.merge(:id => (!columns(from).detect{|c| c.name == 'id'}.nil? && 'id' == primary_key(from).to_s))
98
+ unless options.has_key?(:comment)
99
+ table_comment = retrieve_table_comment(from)
100
+ options = options.merge(:comment => table_comment) if table_comment
101
+ end
102
+ create_table(to, options) do |definition|
103
+ @definition = definition
104
+ columns(from).each do |column|
105
+ column_name = options[:rename] ?
106
+ (options[:rename][column.name] ||
107
+ options[:rename][column.name.to_sym] ||
108
+ column.name) : column.name
109
+ @definition.column(column_name, column.type,
110
+ :limit => column.limit, :default => column.default,
111
+ :precision => column.precision, :scale => column.scale,
112
+ :null => column.null, :comment => column.comment)
113
+ end
114
+ @definition.primary_key(primary_key(from)) if primary_key(from)
115
+ yield @definition if block_given?
116
+ end
117
+
118
+ copy_table_indexes(from, to, options[:rename] || {})
119
+ copy_table_contents(from, to,
120
+ @definition.columns.map {|column| column.name},
121
+ options[:rename] || {})
122
+ end
123
+
124
+ def add_column_options!(sql, options)
125
+ super(sql, options)
126
+ if options.keys.include?(:comment)
127
+ sql << CommentDefinition.new(self, nil, nil, options[:comment]).to_sql
128
+ end
129
+ end
130
+
131
+ private
132
+ def escaped_comment(comment)
133
+ comment.gsub(/\*\//, "*-/")
134
+ end
135
+
136
+ def lookup_comment_sql(table_name)
137
+ "select sql from (select * from sqlite_master where type='table' union select * from sqlite_temp_master where type='table') where tbl_name = '#{table_name}'"
138
+ end
139
+ end
140
+ end
@@ -1,3 +1,3 @@
1
1
  module MigrationComments
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -3,17 +3,19 @@ require "migration_comments/version"
3
3
  require 'migration_comments/active_record/schema_dumper'
4
4
  require 'migration_comments/active_record/connection_adapters/comment_definition'
5
5
  require 'migration_comments/active_record/connection_adapters/column_definition'
6
+ require 'migration_comments/active_record/connection_adapters/column'
6
7
  require 'migration_comments/active_record/connection_adapters/table'
7
8
  require 'migration_comments/active_record/connection_adapters/table_definition'
8
9
  require 'migration_comments/active_record/connection_adapters/abstract_adapter'
9
10
  require 'migration_comments/active_record/connection_adapters/mysql_adapter'
10
11
  require 'migration_comments/active_record/connection_adapters/mysql2_adapter'
11
12
  require 'migration_comments/active_record/connection_adapters/postgresql_adapter'
13
+ require 'migration_comments/active_record/connection_adapters/sqlite_adapter'
12
14
 
13
15
  module MigrationComments
14
16
  def self.setup
15
17
  base_names = %w(SchemaDumper) +
16
- %w(ColumnDefinition Table TableDefinition AbstractAdapter).map{|name| "ConnectionAdapters::#{name}"}
18
+ %w(ColumnDefinition Column Table TableDefinition AbstractAdapter).map{|name| "ConnectionAdapters::#{name}"}
17
19
 
18
20
  base_names.each do |base_name|
19
21
  ar_class = "ActiveRecord::#{base_name}".constantize
@@ -23,7 +25,7 @@ module MigrationComments
23
25
  end
24
26
  end
25
27
 
26
- %w(PostgreSQL Mysql Mysql2).each do |adapter|
28
+ %w(PostgreSQL Mysql Mysql2 SQLite).each do |adapter|
27
29
  begin
28
30
  require("active_record/connection_adapters/#{adapter.downcase}_adapter")
29
31
  adapter_class = ('ActiveRecord::ConnectionAdapters::' << "#{adapter}Adapter").constantize
@@ -30,4 +30,5 @@ Gem::Specification.new do |s|
30
30
  # s.add_development_dependency 'postgres-pr'
31
31
  # s.add_development_dependency 'mysql'
32
32
  # s.add_development_dependency 'mysql2'
33
+ # s.add_development_dependency 'sqlite3'
33
34
  end
@@ -14,15 +14,15 @@ class AnnotateModelsTest < Test::Unit::TestCase
14
14
  def test_annotate_includes_comments
15
15
  db_type = :default
16
16
  ActiveRecord::Schema.define do
17
- db_type = :postgres if connection.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) rescue false
17
+ db_type = :mysql if connection.is_a?(ActiveRecord::ConnectionAdapters::MysqlAdapter) rescue false
18
18
 
19
19
  add_table_comment :sample, "a table comment"
20
20
  add_column_comment :sample, :field1, "a \"comment\" \\ that ' needs; escaping''"
21
- add_column :sample, :field3, :string, :null => false, :comment => "third column comment"
21
+ add_column :sample, :field3, :string, :null => false, :default => '', :comment => "third column comment"
22
22
  end
23
23
 
24
24
  result = AnnotateModels.get_schema_info(Sample, TEST_PREFIX)
25
- postgres_expected = <<EOS
25
+ default_expected = <<EOS
26
26
  # #{TEST_PREFIX}
27
27
  #
28
28
  # Table name: sample # a table comment
@@ -30,11 +30,11 @@ class AnnotateModelsTest < Test::Unit::TestCase
30
30
  # id :integer not null, primary key
31
31
  # field1 :string(255) # a "comment" \\ that ' needs; escaping''
32
32
  # field2 :integer
33
- # field3 :string(255) not null # third column comment
33
+ # field3 :string(255) default(""), not null # third column comment
34
34
  #
35
35
 
36
36
  EOS
37
- default_expected = <<EOS
37
+ mysql_expected = <<EOS
38
38
  # #{TEST_PREFIX}
39
39
  #
40
40
  # Table name: sample # a table comment
@@ -42,7 +42,7 @@ EOS
42
42
  # id :integer(4) not null, primary key
43
43
  # field1 :string(255) # a "comment" \\ that ' needs; escaping''
44
44
  # field2 :integer(4)
45
- # field3 :string(255) not null # third column comment
45
+ # field3 :string(255) default(""), not null # third column comment
46
46
  #
47
47
 
48
48
  EOS
@@ -9,4 +9,10 @@ mysql:
9
9
  adapter: mysql # mysql2
10
10
  database: migration_comments_test
11
11
  user: root
12
- password: password
12
+ password: password
13
+
14
+ sqlite:
15
+ adapter: sqlite3
16
+ database: db/migration_comments_test
17
+ pool: 5
18
+ timeout: 5000
@@ -4,10 +4,13 @@ class SchemaDumperTest < Test::Unit::TestCase
4
4
  include TestHelper
5
5
 
6
6
  def test_dump
7
+ db_type = :default
7
8
  ActiveRecord::Schema.define do
9
+ db_type = :sqlite if connection.is_a?(ActiveRecord::ConnectionAdapters::SQLiteAdapter) rescue false
10
+
8
11
  add_table_comment :sample, "a table comment"
9
12
  add_column_comment :sample, :field1, "a \"comment\" \\ that ' needs; escaping''"
10
- add_column :sample, :field3, :string, :null => false, :comment => "third column comment"
13
+ add_column :sample, :field3, :string, :null => false, :default => "", :comment => "third column comment"
11
14
  end
12
15
  dest = StringIO.new
13
16
  ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, dest)
@@ -17,13 +20,14 @@ class SchemaDumperTest < Test::Unit::TestCase
17
20
  ActiveRecord::Schema.define(:version => 1) do
18
21
 
19
22
  create_table "sample", :force => true, :comment => "a table comment" do |t|
20
- t.string "field1", :comment => "a \"comment\" \\ that ' needs; escaping''"
23
+ t.string "field1", :comment => "a \"comment\" \\ that ' needs; escaping''"
21
24
  t.integer "field2"
22
- t.string "field3", :null => false, :comment => "third column comment"
25
+ t.string "field3", :default => "", :null => false, :comment => "third column comment"
23
26
  end
24
27
 
25
28
  end
26
29
  EOS
30
+
27
31
  assert_match /#{Regexp.escape expected}/, result
28
32
  end
29
33
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: migration_comments
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Pinny
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-02-13 00:00:00 -05:00
18
+ date: 2012-02-22 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -78,11 +78,13 @@ files:
78
78
  - Rakefile
79
79
  - lib/migration_comments.rb
80
80
  - lib/migration_comments/active_record/connection_adapters/abstract_adapter.rb
81
+ - lib/migration_comments/active_record/connection_adapters/column.rb
81
82
  - lib/migration_comments/active_record/connection_adapters/column_definition.rb
82
83
  - lib/migration_comments/active_record/connection_adapters/comment_definition.rb
83
84
  - lib/migration_comments/active_record/connection_adapters/mysql2_adapter.rb
84
85
  - lib/migration_comments/active_record/connection_adapters/mysql_adapter.rb
85
86
  - lib/migration_comments/active_record/connection_adapters/postgresql_adapter.rb
87
+ - lib/migration_comments/active_record/connection_adapters/sqlite_adapter.rb
86
88
  - lib/migration_comments/active_record/connection_adapters/table.rb
87
89
  - lib/migration_comments/active_record/connection_adapters/table_definition.rb
88
90
  - lib/migration_comments/active_record/schema_dumper.rb