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,9 +1,9 @@
1
- module MigrationComments::ActiveRecord::ConnectionAdapters
2
- module Mysql2Adapter
3
- def self.included(base)
4
- base.class_eval do
5
- include MigrationComments::ActiveRecord::ConnectionAdapters::MysqlAdapter
6
- end
7
- end
8
- end
1
+ module MigrationComments::ActiveRecord::ConnectionAdapters
2
+ module Mysql2Adapter
3
+ def self.included(base)
4
+ base.class_eval do
5
+ include MigrationComments::ActiveRecord::ConnectionAdapters::MysqlAdapter
6
+ end
7
+ end
8
+ end
9
9
  end
@@ -1,98 +1,104 @@
1
- module MigrationComments::ActiveRecord::ConnectionAdapters
2
- module MysqlAdapter
3
- def self.included(base)
4
- base.class_eval do
5
- attr_accessor :database_name
6
- alias_method_chain :create_table, :migration_comments
7
- alias_method_chain :change_column, :migration_comments
8
- end
9
- end
10
-
11
- def set_table_comment(table_name, comment_text)
12
- execute "ALTER TABLE #{table_name} COMMENT #{escaped_comment(comment_text)}"
13
- end
14
-
15
- def set_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(table_comment_sql(table_name))
22
- result[0].nil? || result[0][0].blank? ? nil : result[0][0]
23
- end
24
-
25
- def retrieve_column_comments(table_name, *column_names)
26
- result = select_rows(column_comment_sql(table_name, *column_names))
27
- return {} if result.nil?
28
- found = result.inject({}){|m, row| m[row[0].to_sym] = (row[1].blank? ? nil : row[1]); m}
29
-
30
- end
31
-
32
- def create_table_with_migration_comments(table_name, options={}, &block)
33
- local_table_definition = nil
34
- create_table_without_migration_comments(table_name, options) do |td|
35
- local_table_definition = td
36
- local_table_definition.comment options[:comment] if options.has_key?(:comment)
37
- block.call(td)
38
- end
39
- comments = local_table_definition.collect_comments(table_name)
40
- comments.each do |comment_definition|
41
- execute_comment comment_definition
42
- end
43
- end
44
-
45
- def change_column_with_migration_comments(table_name, column_name, type, options={})
46
- unless options.keys.include?(:comment)
47
- options.merge!(:comment => retrieve_column_comment(table_name, column_name))
48
- end
49
- change_column_without_migration_comments(table_name, column_name, type, options)
50
- end
51
-
52
- def add_column_options!(sql, options)
53
- super(sql, options)
54
- if options.keys.include?(:comment)
55
- sql << " COMMENT #{escaped_comment(options[:comment])}"
56
- end
57
- end
58
-
59
- def execute_comment(comment_definition)
60
- if comment_definition.table_comment?
61
- set_table_comment comment_definition.table_name, comment_definition.comment_text
62
- else
63
- set_column_comment comment_definition.table_name, comment_definition.column_name, comment_definition.comment_text
64
- end
65
- end
66
-
67
- private
68
- def escaped_comment(comment)
69
- comment.nil? ? "''" : "'#{comment.gsub("'", "''").gsub("\\", "\\\\\\\\")}'"
70
- end
71
-
72
- def table_comment_sql(table_name)
73
- ensure_database_name
74
- <<SQL
75
- SELECT table_comment FROM INFORMATION_SCHEMA.TABLES
76
- WHERE table_schema = '#{database_name}'
77
- AND table_name = '#{table_name}'
78
- SQL
79
- end
80
-
81
- def column_comment_sql(table_name, *column_names)
82
- ensure_database_name
83
- col_matcher_sql = column_names.empty? ? "" : " AND column_name IN (#{column_names.map{|c_name| "'#{c_name}'"}.join(',')})"
84
- <<SQL
85
- SELECT column_name, column_comment FROM INFORMATION_SCHEMA.COLUMNS
86
- WHERE table_schema = '#{database_name}'
87
- AND table_name = '#{table_name}' #{col_matcher_sql}
88
- SQL
89
- end
90
-
91
- def ensure_database_name
92
- return if database_name
93
- info = YAML::load(IO.read('config/database.yml'))
94
- @database_name = info[ENV['DB'] || Rails.env.to_s]["database"]
95
- end
96
-
97
- end
1
+ module MigrationComments::ActiveRecord::ConnectionAdapters
2
+ module MysqlAdapter
3
+ def self.included(base)
4
+ base.class_eval do
5
+ attr_accessor :database_name
6
+ alias_method_chain :create_table, :migration_comments
7
+ alias_method_chain :change_column, :migration_comments
8
+ end
9
+ end
10
+
11
+ def set_table_comment(table_name, comment_text)
12
+ execute "ALTER TABLE #{quote_table_name table_name} COMMENT #{escaped_comment(comment_text)}"
13
+ end
14
+
15
+ def set_column_comment(table_name, column_name, comment_text)
16
+ pk_info = pk_and_sequence_for(table_name)
17
+ if pk_info && pk_info.first == column_name.to_s # change_column broken for :primary_key
18
+ primary_col_def = native_database_types[:primary_key].sub(/ PRIMARY KEY/i, '')
19
+ execute "ALTER TABLE #{quote_table_name table_name} CHANGE #{quote_column_name column_name} #{quote_column_name column_name} #{primary_col_def} COMMENT #{escaped_comment(comment_text)};"
20
+ else
21
+ column = column_for(table_name, column_name)
22
+ change_column table_name, column_name, column.sql_type, :comment => comment_text
23
+ end
24
+ end
25
+
26
+ def retrieve_table_comment(table_name)
27
+ result = select_rows(table_comment_sql(table_name))
28
+ result[0].nil? || result[0][0].blank? ? nil : result[0][0]
29
+ end
30
+
31
+ def retrieve_column_comments(table_name, *column_names)
32
+ result = select_rows(column_comment_sql(table_name, *column_names))
33
+ return {} if result.nil?
34
+ found = result.inject({}){|m, row| m[row[0].to_sym] = (row[1].blank? ? nil : row[1]); m}
35
+
36
+ end
37
+
38
+ def create_table_with_migration_comments(table_name, options={}, &block)
39
+ local_table_definition = nil
40
+ create_table_without_migration_comments(table_name, options) do |td|
41
+ local_table_definition = td
42
+ local_table_definition.comment options[:comment] if options.has_key?(:comment)
43
+ block.call(td)
44
+ end
45
+ comments = local_table_definition.collect_comments(table_name)
46
+ comments.each do |comment_definition|
47
+ execute_comment comment_definition
48
+ end
49
+ end
50
+
51
+ def change_column_with_migration_comments(table_name, column_name, type, options={})
52
+ unless options.keys.include?(:comment)
53
+ options.merge!(:comment => retrieve_column_comment(table_name, column_name))
54
+ end
55
+ change_column_without_migration_comments(table_name, column_name, type, options)
56
+ end
57
+
58
+ def add_column_options!(sql, options)
59
+ super(sql, options)
60
+ if options.keys.include?(:comment)
61
+ sql << " COMMENT #{escaped_comment(options[:comment])}"
62
+ end
63
+ end
64
+
65
+ def execute_comment(comment_definition)
66
+ if comment_definition.table_comment?
67
+ set_table_comment comment_definition.table_name, comment_definition.comment_text
68
+ else
69
+ set_column_comment comment_definition.table_name, comment_definition.column_name, comment_definition.comment_text
70
+ end
71
+ end
72
+
73
+ private
74
+ def escaped_comment(comment)
75
+ comment.nil? ? "''" : "'#{comment.gsub("'", "''").gsub("\\", "\\\\\\\\")}'"
76
+ end
77
+
78
+ def table_comment_sql(table_name)
79
+ ensure_database_name
80
+ <<SQL
81
+ SELECT table_comment FROM INFORMATION_SCHEMA.TABLES
82
+ WHERE table_schema = '#{database_name}'
83
+ AND table_name = '#{table_name}'
84
+ SQL
85
+ end
86
+
87
+ def column_comment_sql(table_name, *column_names)
88
+ ensure_database_name
89
+ col_matcher_sql = column_names.empty? ? "" : " AND column_name IN (#{column_names.map{|c_name| "'#{c_name}'"}.join(',')})"
90
+ <<SQL
91
+ SELECT column_name, column_comment FROM INFORMATION_SCHEMA.COLUMNS
92
+ WHERE table_schema = '#{database_name}'
93
+ AND table_name = '#{table_name}' #{col_matcher_sql}
94
+ SQL
95
+ end
96
+
97
+ def ensure_database_name
98
+ return if database_name
99
+ info = YAML::load(IO.read('config/database.yml'))
100
+ @database_name = info[ENV['DB'] || Rails.env.to_s]["database"]
101
+ end
102
+
103
+ end
98
104
  end
@@ -1,111 +1,111 @@
1
- module MigrationComments::ActiveRecord::ConnectionAdapters
2
- module PostgreSQLAdapter
3
- def self.included(base)
4
- base.class_eval do
5
- alias_method_chain :create_table, :migration_comments
6
- alias_method_chain :add_column, :migration_comments
7
- alias_method_chain :change_column, :migration_comments
8
- end
9
- end
10
-
11
- def comments_supported?
12
- true
13
- end
14
-
15
- # Set a comment on a table
16
- def set_table_comment(table_name, comment_text)
17
- execute CommentDefinition.new(self, table_name, nil, comment_text).to_sql
18
- end
19
-
20
- # Set a comment on a column
21
- def set_column_comment(table_name, column_name, comment_text)
22
- execute CommentDefinition.new(self, table_name, column_name, comment_text).to_sql
23
- end
24
-
25
- def retrieve_table_comment(table_name)
26
- result = select_rows(table_comment_sql(table_name))
27
- result[0].nil? ? nil : result[0][0]
28
- end
29
-
30
- def retrieve_column_comments(table_name, *column_names)
31
- result = select_rows(column_comment_sql(table_name, *column_names))
32
- return {} if result.nil?
33
- return result.inject({}){|m, row| m[row[0].to_sym] = row[1]; m}
34
- end
35
-
36
- def create_table_with_migration_comments(table_name, options = {}, &block)
37
- local_table_definition = nil
38
- create_table_without_migration_comments(table_name, options) do |td|
39
- local_table_definition = td
40
- local_table_definition.comment options[:comment] if options.has_key?(:comment)
41
- block.call(td)
42
- end
43
- comments = local_table_definition.collect_comments(table_name)
44
- comments.each do |comment_definition|
45
- execute comment_definition.to_sql
46
- end
47
- end
48
-
49
- def add_column_with_migration_comments(table_name, column_name, type, options = {})
50
- add_column_without_migration_comments(table_name, column_name, type, options)
51
- if options[:comment]
52
- set_column_comment(table_name, column_name, options[:comment])
53
- end
54
- end
55
-
56
- def change_column_with_migration_comments(table_name, column_name, type, options = {})
57
- change_column_without_migration_comments(table_name, column_name, type, options)
58
- if options.keys.include?(:comment)
59
- set_column_comment(table_name, column_name, options[:comment])
60
- end
61
- end
62
-
63
- def comment_sql(comment_definition)
64
- "COMMENT ON #{comment_target(comment_definition)} IS #{escaped_comment(comment_definition.comment_text)}"
65
- end
66
-
67
- private
68
-
69
- private
70
- def comment_target(comment_definition)
71
- comment_definition.table_comment? ?
72
- "TABLE #{quote_table_name(comment_definition.table_name)}" :
73
- "COLUMN #{quote_table_name(comment_definition.table_name)}.#{quote_column_name(comment_definition.column_name)}"
74
- end
75
-
76
- def escaped_comment(comment)
77
- comment.nil? ? 'NULL' : "'#{comment.gsub("'", "''")}'"
78
- end
79
-
80
- def table_comment_sql(table_name)
81
- <<SQL
82
- SELECT d.description FROM (
83
- #{table_oids(table_name)}) tt
84
- JOIN pg_catalog.pg_description d
85
- ON tt.oid = d.objoid AND tt.tableoid = d.classoid AND d.objsubid = 0;
86
- SQL
87
- end
88
-
89
- def column_comment_sql(table_name, *column_names)
90
- col_matcher_sql = column_names.empty? ? "" : " a.attname IN (#{column_names.map{|c_name| "'#{c_name}'"}.join(',')}) AND "
91
- <<SQL
92
- SELECT a.attname, pg_catalog.col_description(a.attrelid, a.attnum)
93
- FROM pg_catalog.pg_attribute a
94
- JOIN (
95
- #{table_oids(table_name)}) tt
96
- ON tt.oid = a.attrelid
97
- WHERE #{col_matcher_sql} a.attnum > 0 AND NOT a.attisdropped;
98
- SQL
99
- end
100
-
101
- def table_oids(table_name)
102
- <<SQL
103
- SELECT c.oid, c.tableoid
104
- FROM pg_catalog.pg_class c
105
- WHERE c.relname = '#{table_name}'
106
- AND c.relkind = 'r'
107
- AND pg_catalog.pg_table_is_visible(c.oid)
108
- SQL
109
- end
110
- end
111
- end
1
+ module MigrationComments::ActiveRecord::ConnectionAdapters
2
+ module PostgreSQLAdapter
3
+ def self.included(base)
4
+ base.class_eval do
5
+ alias_method_chain :create_table, :migration_comments
6
+ alias_method_chain :add_column, :migration_comments
7
+ alias_method_chain :change_column, :migration_comments
8
+ end
9
+ end
10
+
11
+ def comments_supported?
12
+ true
13
+ end
14
+
15
+ # Set a comment on a table
16
+ def set_table_comment(table_name, comment_text)
17
+ execute CommentDefinition.new(self, table_name, nil, comment_text).to_sql
18
+ end
19
+
20
+ # Set a comment on a column
21
+ def set_column_comment(table_name, column_name, comment_text)
22
+ execute CommentDefinition.new(self, table_name, column_name, comment_text).to_sql
23
+ end
24
+
25
+ def retrieve_table_comment(table_name)
26
+ result = select_rows(table_comment_sql(table_name))
27
+ result[0].nil? ? nil : result[0][0]
28
+ end
29
+
30
+ def retrieve_column_comments(table_name, *column_names)
31
+ result = select_rows(column_comment_sql(table_name, *column_names))
32
+ return {} if result.nil?
33
+ return result.inject({}){|m, row| m[row[0].to_sym] = row[1]; m}
34
+ end
35
+
36
+ def create_table_with_migration_comments(table_name, options = {}, &block)
37
+ local_table_definition = nil
38
+ create_table_without_migration_comments(table_name, options) do |td|
39
+ local_table_definition = td
40
+ local_table_definition.comment options[:comment] if options.has_key?(:comment)
41
+ block.call(td)
42
+ end
43
+ comments = local_table_definition.collect_comments(table_name)
44
+ comments.each do |comment_definition|
45
+ execute comment_definition.to_sql
46
+ end
47
+ end
48
+
49
+ def add_column_with_migration_comments(table_name, column_name, type, options = {})
50
+ add_column_without_migration_comments(table_name, column_name, type, options)
51
+ if options[:comment]
52
+ set_column_comment(table_name, column_name, options[:comment])
53
+ end
54
+ end
55
+
56
+ def change_column_with_migration_comments(table_name, column_name, type, options = {})
57
+ change_column_without_migration_comments(table_name, column_name, type, options)
58
+ if options.keys.include?(:comment)
59
+ set_column_comment(table_name, column_name, options[:comment])
60
+ end
61
+ end
62
+
63
+ def comment_sql(comment_definition)
64
+ "COMMENT ON #{comment_target(comment_definition)} IS #{escaped_comment(comment_definition.comment_text)}"
65
+ end
66
+
67
+ private
68
+
69
+ private
70
+ def comment_target(comment_definition)
71
+ comment_definition.table_comment? ?
72
+ "TABLE #{quote_table_name(comment_definition.table_name)}" :
73
+ "COLUMN #{quote_table_name(comment_definition.table_name)}.#{quote_column_name(comment_definition.column_name)}"
74
+ end
75
+
76
+ def escaped_comment(comment)
77
+ comment.nil? ? 'NULL' : "'#{comment.gsub("'", "''")}'"
78
+ end
79
+
80
+ def table_comment_sql(table_name)
81
+ <<SQL
82
+ SELECT d.description FROM (
83
+ #{table_oids(table_name)}) tt
84
+ JOIN pg_catalog.pg_description d
85
+ ON tt.oid = d.objoid AND tt.tableoid = d.classoid AND d.objsubid = 0;
86
+ SQL
87
+ end
88
+
89
+ def column_comment_sql(table_name, *column_names)
90
+ col_matcher_sql = column_names.empty? ? "" : " a.attname IN (#{column_names.map{|c_name| "'#{c_name}'"}.join(',')}) AND "
91
+ <<SQL
92
+ SELECT a.attname, pg_catalog.col_description(a.attrelid, a.attnum)
93
+ FROM pg_catalog.pg_attribute a
94
+ JOIN (
95
+ #{table_oids(table_name)}) tt
96
+ ON tt.oid = a.attrelid
97
+ WHERE #{col_matcher_sql} a.attnum > 0 AND NOT a.attisdropped;
98
+ SQL
99
+ end
100
+
101
+ def table_oids(table_name)
102
+ <<SQL
103
+ SELECT c.oid, c.tableoid
104
+ FROM pg_catalog.pg_class c
105
+ WHERE c.relname = '#{table_name}'
106
+ AND c.relkind = 'r'
107
+ AND pg_catalog.pg_table_is_visible(c.oid)
108
+ SQL
109
+ end
110
+ end
111
+ end
@@ -1,140 +1,142 @@
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 set_table_comment(table_name, comment_text)
12
- alter_table(table_name, :comment => comment_text)
13
- end
14
-
15
- def set_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
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 set_table_comment(table_name, comment_text)
12
+ alter_table(table_name, :comment => comment_text)
13
+ end
14
+
15
+ def set_column_comment(table_name, column_name, comment_text)
16
+ sql_type = primary_key(table_name) == column_name.to_s ?
17
+ :primary_key :
18
+ column_for(table_name, column_name).sql_type
19
+ change_column table_name, column_name, sql_type, :comment => comment_text
20
+ end
21
+
22
+ def retrieve_table_comment(table_name)
23
+ result = select_rows(lookup_comment_sql(table_name))
24
+ if result[0][0] =~ /CREATE (?:TEMPORARY )?TABLE #{quote_table_name table_name} [^\(]*\/\*(.*)\*\/ \(/
25
+ $1
26
+ end
27
+ end
28
+
29
+ def retrieve_column_comments(table_name, *column_names)
30
+ if column_names.empty?
31
+ return columns(table_name).inject({}) { |m, v| m[v.name.to_sym] = v.comment if v.comment.present?; m }
32
+ end
33
+ result = select_rows(lookup_comment_sql(table_name))
34
+ result[0][0] =~ /^CREATE (?:TEMPORARY )?TABLE "\w*" [^\(]*(?:\/\*.*\*\/ )?\((.*)\)[^\)]*$/
35
+ col_defs = $1
36
+ comment_matches = col_defs.scan(/"([^",]+)"[^,]*\/\*(.+?)\*\//)
37
+ comment_matches.inject({}){|m, row| m[row.first.to_sym] = row.last; m}
38
+ end
39
+
40
+ def create_table(table_name, options = {})
41
+ td = table_definition
42
+ td.primary_key(options[:primary_key] || ActiveRecord::Base.get_primary_key(table_name.to_s.singularize)) unless options[:id] == false
43
+ td.comment options[:comment] if options.has_key?(:comment)
44
+
45
+ yield td if block_given?
46
+
47
+ if options[:force] && table_exists?(table_name)
48
+ drop_table(table_name)
49
+ end
50
+
51
+ create_sql = "CREATE#{' TEMPORARY' if options[:temporary]} TABLE "
52
+ create_sql << "#{quote_table_name(table_name)}#{td.table_comment} ("
53
+ create_sql << td.columns.map do |column|
54
+ column.to_sql + column.comment.to_s
55
+ end * ", "
56
+ create_sql << ") #{options[:options]}"
57
+ execute create_sql
58
+ end
59
+
60
+ def change_column_with_migration_comments(table_name, column_name, type, options = {}) #:nodoc:
61
+ adapter = self
62
+ alter_table(table_name) do |definition|
63
+ include_default = options_include_default?(options)
64
+ definition[column_name].instance_eval do
65
+ self.type = type
66
+ self.limit = options[:limit] if options.include?(:limit)
67
+ self.default = options[:default] if include_default
68
+ self.null = options[:null] if options.include?(:null)
69
+ self.precision = options[:precision] if options.include?(:precision)
70
+ self.scale = options[:scale] if options.include?(:scale)
71
+ self.comment = CommentDefinition.new(adapter, table_name, column_name, options[:comment]) if options.include?(:comment)
72
+ end
73
+ end
74
+ end
75
+
76
+ def columns_with_migration_comments(table_name, name = nil)
77
+ cols = columns_without_migration_comments(table_name, name)
78
+ comments = retrieve_column_comments(table_name, *(cols.map(&:name)))
79
+ cols.each do |col|
80
+ col.comment = comments[col.name.to_sym] if comments.has_key?(col.name.to_sym)
81
+ end
82
+ cols
83
+ end
84
+
85
+ def column_for(table_name, column_name)
86
+ columns(table_name).detect{|col| col.name == column_name.to_s}
87
+ end
88
+
89
+ def comment_sql(comment_definition)
90
+ if comment_definition.nil? || comment_definition.comment_text.blank?
91
+ ""
92
+ else
93
+ " /*#{escaped_comment(comment_definition.comment_text)}*/"
94
+ end
95
+
96
+ end
97
+
98
+ def copy_table_with_migration_comments(from, to, options = {}) #:nodoc:
99
+ options = options.merge(:id => (!columns(from).detect{|c| c.name == 'id'}.nil? && 'id' == primary_key(from).to_s))
100
+ unless options.has_key?(:comment)
101
+ table_comment = retrieve_table_comment(from)
102
+ options = options.merge(:comment => table_comment) if table_comment
103
+ end
104
+ create_table(to, options) do |definition|
105
+ @definition = definition
106
+ columns(from).each do |column|
107
+ column_name = options[:rename] ?
108
+ (options[:rename][column.name] ||
109
+ options[:rename][column.name.to_sym] ||
110
+ column.name) : column.name
111
+ @definition.column(column_name, column.type,
112
+ :limit => column.limit, :default => column.default,
113
+ :precision => column.precision, :scale => column.scale,
114
+ :null => column.null, :comment => column.comment)
115
+ end
116
+ @definition.primary_key(primary_key(from)) if primary_key(from)
117
+ yield @definition if block_given?
118
+ end
119
+
120
+ copy_table_indexes(from, to, options[:rename] || {})
121
+ copy_table_contents(from, to,
122
+ @definition.columns.map {|column| column.name},
123
+ options[:rename] || {})
124
+ end
125
+
126
+ def add_column_options!(sql, options)
127
+ super(sql, options)
128
+ if options.keys.include?(:comment)
129
+ sql << CommentDefinition.new(self, nil, nil, options[:comment]).to_sql
130
+ end
131
+ end
132
+
133
+ private
134
+ def escaped_comment(comment)
135
+ comment.gsub(/\*\//, "*-/")
136
+ end
137
+
138
+ def lookup_comment_sql(table_name)
139
+ "select sql from (select * from sqlite_master where type='table' union select * from sqlite_temp_master where type='table') where tbl_name = '#{table_name}'"
140
+ end
141
+ end
140
142
  end