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.
- data/.gitignore +5 -5
- data/MIT-LICENSE +7 -0
- data/README.rdoc +84 -81
- data/lib/migration_comments.rb +59 -59
- data/lib/migration_comments/active_record/connection_adapters/abstract_adapter.rb +44 -44
- data/lib/migration_comments/active_record/connection_adapters/column.rb +8 -8
- data/lib/migration_comments/active_record/connection_adapters/column_definition.rb +4 -4
- data/lib/migration_comments/active_record/connection_adapters/comment_definition.rb +21 -21
- data/lib/migration_comments/active_record/connection_adapters/mysql2_adapter.rb +8 -8
- data/lib/migration_comments/active_record/connection_adapters/mysql_adapter.rb +103 -97
- data/lib/migration_comments/active_record/connection_adapters/postgresql_adapter.rb +111 -111
- data/lib/migration_comments/active_record/connection_adapters/sqlite_adapter.rb +141 -139
- data/lib/migration_comments/active_record/connection_adapters/table.rb +11 -11
- data/lib/migration_comments/active_record/connection_adapters/table_definition.rb +31 -29
- data/lib/migration_comments/active_record/schema_dumper.rb +53 -53
- data/lib/migration_comments/annotate_models.rb +39 -39
- data/lib/migration_comments/version.rb +3 -3
- data/migration_comments.gemspec +34 -34
- data/test/add_comments_test.rb +135 -135
- data/test/annotate_models_test.rb +37 -53
- data/test/auto_increment_test.rb +50 -0
- data/test/config/database.yml +17 -17
- data/test/schema_dumper_test.rb +33 -33
- data/test/test_helper.rb +32 -32
- metadata +62 -76
@@ -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
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
if
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
end
|
96
|
-
|
97
|
-
|
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
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
td
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
create_sql
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
self.
|
66
|
-
self.
|
67
|
-
self.
|
68
|
-
self.
|
69
|
-
self.
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
cols
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
"
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
:
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
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
|