schema_comments 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6cf787ee52801a9d7e04e182ac66a11d5bd8c2f7
4
- data.tar.gz: 888fa0d6e255163ff7173103e6a2091b047baf2e
3
+ metadata.gz: 21a9d779680d54f251b51ab643ec747878b68924
4
+ data.tar.gz: 3ffa036c8dab55d4386d442e60bdf66aa6ff770e
5
5
  SHA512:
6
- metadata.gz: aeb01ef93ce21aa25ffa8e7050a54df0b24603dfdf3167c34e1eda9992031fda408f4f5f5ced2a2c248f37e1909791a4dfbb7784b9d07422f95d59f14c19b0dd
7
- data.tar.gz: b58f9028895a5bbf729fc7521b5884ba0be8fad8fe90954d040a354b7dc823fde426fc25082eb96637dfc7ae409bdc62a75761d73a423d33a5963dc5dd9958e5
6
+ metadata.gz: 6027c604d17578b22dfb68aa5e0b6a42c184629ae539c2cb79b9c1634ab76dbf4d55a82136d075d1a7be53756d68ee7e7ff2b2fbadc2e935d6108956806cb524
7
+ data.tar.gz: e576b7f6de6a7cf31ea2a73413bce1b3929347ac110a2f81c3a84bceab744854d50cf16f73bab85139a8b9f130a74463c17150aced1f21641be640473fa5346c
@@ -106,8 +106,13 @@ module SchemaComments
106
106
  io.rewind
107
107
  root = YAML.load(io)
108
108
  end
109
- table_comments = root['table_comments'] || {}
110
- column_comments = root['column_comments'] || {}
109
+ SortedStore.sort_yaml_content!(root)
110
+ root.to_yaml(@opt)
111
+ end
112
+
113
+ def self.sort_yaml_content!(root)
114
+ table_comments = (root['table_comments'] ||= {})
115
+ column_comments = (root['column_comments'] ||= {})
111
116
  # 大元は
112
117
  # table_comments:
113
118
  # ...
@@ -129,11 +134,13 @@ module SchemaComments
129
134
  column_comments.extend(HashKeyOrderable)
130
135
  column_comments.key_order = table_names
131
136
  # column_comments の各値はテーブルのカラム順
137
+ conn = ActiveRecord::Base.connection
138
+ columns_method = conn.respond_to?(:columns_without_schema_comments) ? :columns_without_schema_comments : :columns
132
139
  column_comments.each do |table_name, column_hash|
133
140
  column_hash ||= {}
134
141
  column_names = nil
135
142
  begin
136
- columns = ActiveRecord::Base.connection.columns_without_schema_comments(table_name)
143
+ columns = conn.send(columns_method, table_name)
137
144
  column_names = columns.map(&:name)
138
145
  rescue ActiveRecord::ActiveRecordError
139
146
  column_names = column_hash.keys.sort
@@ -143,7 +150,6 @@ module SchemaComments
143
150
  column_hash.extend(HashKeyOrderable)
144
151
  column_hash.key_order = column_names
145
152
  end
146
- root.to_yaml(@opt)
147
153
  end
148
154
  end
149
155
 
@@ -1 +1 @@
1
- Dir.glob(File.join(File.dirname(__FILE__), '../../tasks/*.rake')){|f| load(f)}
1
+ Dir.glob(File.expand_path('../tasks/*.rake', __FILE__)){|f| load(f)}
@@ -0,0 +1,53 @@
1
+ namespace :db do
2
+ namespace :schema do
3
+ namespace :comments do
4
+
5
+ desc "update #{SchemaComments.yaml_path} from mysql comments of tables and columns"
6
+ task :dump => :environment do
7
+ conn = ActiveRecord::Base.connection
8
+ db_table_comments = conn.select_all("SHOW TABLE STATUS").each_with_object({}){|row, d| d[row["Name"]] = row["Comment"]}
9
+ column_comments = {}
10
+ db_table_comments.keys.each do |t|
11
+ column_comments[t] = conn.select_all("SHOW FULL COLUMNS FROM #{t}").each_with_object({}){|row, d| d[row["Field"]] = row["Comment"]}
12
+ end
13
+ root = {"table_comments" => db_table_comments, "column_comments" => column_comments}
14
+ SchemaComments::SchemaComment::SortedStore.sort_yaml_content!(root)
15
+ open(SchemaComments.yaml_path, "w"){|f| f.puts root.to_yaml }
16
+ end
17
+
18
+ desc "load #{SchemaComments.yaml_path} and alter table for mysql comments"
19
+ task :load => :environment do
20
+ conn = ActiveRecord::Base.connection
21
+ creation = ActiveRecord::ConnectionAdapters::AbstractAdapter::SchemaCreation.new(conn)
22
+
23
+ db_tables = conn.tables
24
+ db_table_comments = conn.select_all("SHOW TABLE STATUS").each_with_object({}){|row, d| d[row["Name"]] = row["Comment"]}
25
+ root = YAML.load_file(SchemaComments.yaml_path)
26
+ column_comments_root = root["column_comments"]
27
+ root["table_comments"].each do |t, t_comment|
28
+ unless db_tables.include?(t)
29
+ $stderr.puts "\e[33mtable #{t.inspect} doesn't exist\e[0m"
30
+ next
31
+ end
32
+ unless t_comment == db_table_comments[t]
33
+ conn.execute("ALTER TABLE #{t} COMMENT '#{t_comment}'")
34
+ end
35
+
36
+ column_hash = conn.columns(t).each_with_object({}){|c, d| d[c.name] = c}
37
+ db_column_comments = conn.select_all("SHOW FULL COLUMNS FROM #{t}").each_with_object({}){|row, d| d[row["Field"]] = row["Comment"]}
38
+ column_comments = column_comments_root[t]
39
+ column_comments.each do |c, c_comment|
40
+ unless c_comment == db_column_comments[t]
41
+ if col = column_hash[c]
42
+ opts = col.sql_type.dup
43
+ creation.send(:add_column_options!, opts, column: col, null: col.null, default: col.default, auto_increment: (col.extra == "auto_increment"))
44
+ conn.execute("ALTER TABLE #{t} MODIFY #{c} #{opts} COMMENT '#{c_comment}'")
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ end
52
+ end
53
+ end
@@ -1,3 +1,3 @@
1
1
  module SchemaComments
2
- VERSION = "0.3.2"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schema_comments
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - akm
@@ -116,10 +116,11 @@ files:
116
116
  - lib/schema_comments/schema_dumper.rb
117
117
  - lib/schema_comments/schema_dumper/mysql.rb
118
118
  - lib/schema_comments/task.rb
119
+ - lib/schema_comments/tasks/alter_comments.rake
120
+ - lib/schema_comments/tasks/annotate_models_tasks.rake
121
+ - lib/schema_comments/tasks/schema_comments.rake
119
122
  - lib/schema_comments/version.rb
120
123
  - schema_comments.gemspec
121
- - tasks/annotate_models_tasks.rake
122
- - tasks/schema_comments.rake
123
124
  homepage: http://github.com/akm/schema_comments
124
125
  licenses:
125
126
  - MIT