migration_comments 0.3.2 → 0.4.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.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/.travis.yml +27 -0
- data/README.rdoc +12 -3
- data/Rakefile +24 -0
- data/docker-compose.yml +12 -0
- data/gemfiles/rails4.gemfile +4 -0
- data/gemfiles/rails5.gemfile +4 -0
- data/lib/migration_comments.rb +12 -40
- data/lib/migration_comments/active_record/connection_adapters/abstract_adapter.rb +1 -10
- data/lib/migration_comments/active_record/connection_adapters/abstract_adapter/schema_creation.rb +22 -25
- data/lib/migration_comments/active_record/connection_adapters/alter_table.rb +5 -10
- data/lib/migration_comments/active_record/connection_adapters/column.rb +1 -5
- data/lib/migration_comments/active_record/connection_adapters/comment_definition.rb +1 -9
- data/lib/migration_comments/active_record/connection_adapters/mysql2_adapter.rb +97 -3
- data/lib/migration_comments/active_record/connection_adapters/postgresql_adapter.rb +19 -22
- data/lib/migration_comments/active_record/connection_adapters/sqlite3_adapter.rb +73 -46
- data/lib/migration_comments/active_record/connection_adapters/table.rb +3 -3
- data/lib/migration_comments/active_record/connection_adapters/table_definition.rb +9 -18
- data/lib/migration_comments/active_record/schema_dumper.rb +6 -10
- data/lib/migration_comments/annotate_models.rb +6 -8
- data/lib/migration_comments/schema_formatter.rb +8 -1
- data/lib/migration_comments/version.rb +1 -1
- data/migration_comments.gemspec +3 -10
- data/test/add_comments_test.rb +43 -2
- data/test/annotate_models_test.rb +6 -4
- data/test/auto_increment_test.rb +39 -9
- data/test/config/database.yml +2 -2
- data/test/primary_uuid_test.rb +27 -0
- data/test/schema_dumper_test.rb +35 -11
- data/test/test_helper.rb +8 -1
- metadata +34 -11
- data/lib/migration_comments/active_record/connection_adapters/abstract_sqlite_adapter.rb +0 -85
- data/lib/migration_comments/active_record/connection_adapters/mysql_adapter.rb +0 -107
- data/lib/migration_comments/active_record/connection_adapters/sqlite_adapter.rb +0 -71
@@ -1,107 +0,0 @@
|
|
1
|
-
module MigrationComments::ActiveRecord::ConnectionAdapters
|
2
|
-
module MysqlAdapter
|
3
|
-
def self.included(base)
|
4
|
-
base.class_eval do
|
5
|
-
alias_method_chain :create_table, :migration_comments
|
6
|
-
alias_method_chain :change_column, :migration_comments
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
def comments_supported?
|
11
|
-
true
|
12
|
-
end
|
13
|
-
|
14
|
-
def set_table_comment(table_name, comment_text)
|
15
|
-
execute "ALTER TABLE #{quote_table_name table_name} COMMENT #{escaped_comment(comment_text)}"
|
16
|
-
end
|
17
|
-
|
18
|
-
def set_column_comment(table_name, column_name, comment_text)
|
19
|
-
pk_info = pk_and_sequence_for(table_name)
|
20
|
-
if pk_info && pk_info.first == column_name.to_s # change_column broken for :primary_key
|
21
|
-
primary_col_def = native_database_types[:primary_key].sub(/ PRIMARY KEY/i, '')
|
22
|
-
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)};"
|
23
|
-
else
|
24
|
-
column = column_for(table_name, column_name)
|
25
|
-
change_column table_name, column_name, column.sql_type, :comment => comment_text
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def retrieve_table_comment(table_name)
|
30
|
-
result = select_rows(table_comment_sql(table_name))
|
31
|
-
result[0].nil? || result[0][0].blank? ? nil : result[0][0]
|
32
|
-
end
|
33
|
-
|
34
|
-
def retrieve_column_comments(table_name, *column_names)
|
35
|
-
result = select_rows(column_comment_sql(table_name, *column_names))
|
36
|
-
return {} if result.nil?
|
37
|
-
result.inject({}){|m, row| m[row[0].to_sym] = (row[1].blank? ? nil : row[1]); m}
|
38
|
-
end
|
39
|
-
|
40
|
-
def create_table_with_migration_comments(table_name, options={})
|
41
|
-
local_table_definition = nil
|
42
|
-
create_table_without_migration_comments(table_name, options) do |td|
|
43
|
-
local_table_definition = td
|
44
|
-
local_table_definition.base = self
|
45
|
-
local_table_definition.comment options[:comment] if options.has_key?(:comment)
|
46
|
-
yield td if block_given?
|
47
|
-
end
|
48
|
-
comments = local_table_definition.collect_comments(table_name)
|
49
|
-
comments.each do |comment_definition|
|
50
|
-
execute_comment comment_definition
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def change_column_with_migration_comments(table_name, column_name, type, options={})
|
55
|
-
unless options.keys.include?(:comment)
|
56
|
-
options.merge!(:comment => retrieve_column_comment(table_name, column_name))
|
57
|
-
end
|
58
|
-
change_column_without_migration_comments(table_name, column_name, type, options)
|
59
|
-
end
|
60
|
-
|
61
|
-
def add_column_options!(sql, options)
|
62
|
-
super(sql, options)
|
63
|
-
if options.keys.include?(:comment)
|
64
|
-
sql << CommentDefinition.new(self, nil, nil, options[:comment]).to_sql
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
def comment_sql(comment_definition)
|
69
|
-
" COMMENT #{escaped_comment(comment_definition.comment_text)}"
|
70
|
-
end
|
71
|
-
|
72
|
-
def execute_comment(comment_definition)
|
73
|
-
if comment_definition.table_comment?
|
74
|
-
set_table_comment comment_definition.table_name, comment_definition.comment_text
|
75
|
-
else
|
76
|
-
set_column_comment comment_definition.table_name, comment_definition.column_name, comment_definition.comment_text
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
private
|
81
|
-
def escaped_comment(comment)
|
82
|
-
comment.nil? ? "''" : "'#{comment.gsub("'", "''").gsub("\\", "\\\\\\\\")}'"
|
83
|
-
end
|
84
|
-
|
85
|
-
def table_comment_sql(table_name)
|
86
|
-
<<SQL
|
87
|
-
SELECT table_comment FROM INFORMATION_SCHEMA.TABLES
|
88
|
-
WHERE table_schema = '#{database_name}'
|
89
|
-
AND table_name = '#{table_name}'
|
90
|
-
SQL
|
91
|
-
end
|
92
|
-
|
93
|
-
def column_comment_sql(table_name, *column_names)
|
94
|
-
col_matcher_sql = column_names.empty? ? "" : " AND column_name IN (#{column_names.map{|c_name| "'#{c_name}'"}.join(',')})"
|
95
|
-
<<SQL
|
96
|
-
SELECT column_name, column_comment FROM INFORMATION_SCHEMA.COLUMNS
|
97
|
-
WHERE table_schema = '#{database_name}'
|
98
|
-
AND table_name = '#{table_name}' #{col_matcher_sql}
|
99
|
-
SQL
|
100
|
-
end
|
101
|
-
|
102
|
-
def database_name
|
103
|
-
@database_name ||= select_rows("SELECT DATABASE()")[0][0]
|
104
|
-
end
|
105
|
-
|
106
|
-
end
|
107
|
-
end
|
@@ -1,71 +0,0 @@
|
|
1
|
-
module MigrationComments::ActiveRecord::ConnectionAdapters
|
2
|
-
module SQLiteAdapter
|
3
|
-
include AbstractSQLiteAdapter
|
4
|
-
|
5
|
-
def self.included(base)
|
6
|
-
base.module_eval do
|
7
|
-
alias_method_chain :columns, :migration_comments
|
8
|
-
alias_method_chain :copy_table, :migration_comments
|
9
|
-
alias_method_chain :change_column, :migration_comments
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
def create_table(table_name, options = {})
|
14
|
-
td = ActiveRecord::ConnectionAdapters::TableDefinition.new(self)
|
15
|
-
td.primary_key(options[:primary_key] || ActiveRecord::Base.get_primary_key(table_name.to_s.singularize)) unless options[:id] == false
|
16
|
-
td.comment options[:comment] if options.has_key?(:comment)
|
17
|
-
td.base = self
|
18
|
-
|
19
|
-
yield td if block_given?
|
20
|
-
|
21
|
-
if options[:force] && table_exists?(table_name)
|
22
|
-
drop_table(table_name)
|
23
|
-
end
|
24
|
-
|
25
|
-
create_sql = "CREATE#{' TEMPORARY' if options[:temporary]} TABLE "
|
26
|
-
create_sql << "#{quote_table_name(table_name)}#{td.table_comment} ("
|
27
|
-
create_sql << td.columns.map do |column|
|
28
|
-
column.to_sql + column.comment.to_s
|
29
|
-
end * ", "
|
30
|
-
create_sql << ") #{options[:options]}"
|
31
|
-
execute create_sql
|
32
|
-
end
|
33
|
-
|
34
|
-
def columns_with_migration_comments(table_name, name = nil)
|
35
|
-
cols = columns_without_migration_comments(table_name, name)
|
36
|
-
comments = retrieve_column_comments(table_name, *(cols.map(&:name)))
|
37
|
-
cols.each do |col|
|
38
|
-
col.comment = comments[col.name.to_sym] if comments.has_key?(col.name.to_sym)
|
39
|
-
end
|
40
|
-
cols
|
41
|
-
end
|
42
|
-
|
43
|
-
def copy_table_with_migration_comments(from, to, options = {}) #:nodoc:
|
44
|
-
options = options.merge(:id => (!columns(from).detect{|c| c.name == 'id'}.nil? && 'id' == primary_key(from).to_s))
|
45
|
-
unless options.has_key?(:comment)
|
46
|
-
table_comment = retrieve_table_comment(from)
|
47
|
-
options = options.merge(:comment => table_comment) if table_comment
|
48
|
-
end
|
49
|
-
create_table(to, options) do |definition|
|
50
|
-
@definition = definition
|
51
|
-
columns(from).each do |column|
|
52
|
-
column_name = options[:rename] ?
|
53
|
-
(options[:rename][column.name] ||
|
54
|
-
options[:rename][column.name.to_sym] ||
|
55
|
-
column.name) : column.name
|
56
|
-
@definition.column(column_name, column.type,
|
57
|
-
:limit => column.limit, :default => column.default,
|
58
|
-
:precision => column.precision, :scale => column.scale,
|
59
|
-
:null => column.null, :comment => column.comment)
|
60
|
-
end
|
61
|
-
@definition.primary_key(primary_key(from)) if primary_key(from)
|
62
|
-
yield @definition if block_given?
|
63
|
-
end
|
64
|
-
|
65
|
-
copy_table_indexes(from, to, options[:rename] || {})
|
66
|
-
copy_table_contents(from, to,
|
67
|
-
@definition.columns.map {|column| column.name},
|
68
|
-
options[:rename] || {})
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|