schema_comments 0.4.3 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +0 -5
- data/.rspec +1 -0
- data/.travis.yml +0 -1
- data/Gemfile +12 -1
- data/MIT-LICENSE +20 -0
- data/README.md +11 -84
- data/Rakefile +6 -0
- data/gemfiles/Gemfile.rails-4.0 +2 -0
- data/gemfiles/Gemfile.rails-4.1 +2 -0
- data/gemfiles/Gemfile.rails-4.2 +2 -0
- data/lib/schema_comments.rb +25 -4
- data/lib/schema_comments/connection_adapters.rb +0 -14
- data/lib/schema_comments/dummy_migration.rb +7 -0
- data/lib/schema_comments/railtie.rb +1 -0
- data/lib/schema_comments/schema_comment.rb +42 -10
- data/lib/schema_comments/schema_dumper.rb +8 -3
- data/lib/schema_comments/schema_dumper/mysql.rb +7 -2
- data/lib/schema_comments/tasks/schema_comments.rake +23 -155
- data/lib/schema_comments/version.rb +1 -1
- data/lib/tasks/schema_comments_tasks.rake +4 -0
- data/log/.keep +0 -0
- data/schema_comments.gemspec +21 -22
- metadata +15 -22
- data/LICENSE.txt +0 -60
- data/VERSION +0 -1
- data/bin/console +0 -14
- data/bin/setup +0 -7
- data/init.rb +0 -4
- data/lib/annotate_models.rb +0 -234
- data/lib/schema_comments/base.rb +0 -69
- data/lib/schema_comments/migration.rb +0 -16
- data/lib/schema_comments/migrator.rb +0 -16
- data/lib/schema_comments/tasks/alter_comments.rake +0 -53
- data/lib/schema_comments/tasks/annotate_models_tasks.rake +0 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3e86f31a10d539ff6c38570c26e42ae901c381d0
|
4
|
+
data.tar.gz: 2cbd891ce4625a00b234429c4eb33f5a5841cdf4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bb6b51e194b02b595810294dfaf6390404118809082e68694644f5c97e10286dcf61ad47b873220df946b12f7504aeef321f0111d53cbdca223b1cd6b45bbab1
|
7
|
+
data.tar.gz: 4c6c5a2cc31b4b7e3541739623054d5df3a6f7badcb8359038b383d534a30876435821974f469778e7771430afccb584b71b0e1c61f82fa46999a4d79ba19965
|
data/.gitignore
CHANGED
data/.rspec
CHANGED
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
@@ -1,7 +1,18 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
|
3
|
+
# Declare your gem's dependencies in schema_comments.gemspec.
|
4
|
+
# Bundler will treat runtime dependencies like base dependencies, and
|
5
|
+
# development dependencies will be added by default to the :development group.
|
3
6
|
gemspec
|
4
7
|
|
8
|
+
# Declare any dependencies that are still in development here instead of in
|
9
|
+
# your gemspec. These might include edge Rails or gems from your path or
|
10
|
+
# Git. Remember to move these dependencies to your gemspec before releasing
|
11
|
+
# your gem to rubygems.org.
|
12
|
+
|
13
|
+
# To use a debugger
|
14
|
+
# gem 'byebug', group: [:development, :test]
|
15
|
+
|
5
16
|
group :development, :test do
|
6
17
|
gem "sqlite3"
|
7
18
|
gem "mysql2"
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2016 TODO: Write your name
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -43,10 +43,7 @@ class CreateProducts < ActiveRecord::Migration
|
|
43
43
|
end
|
44
44
|
```
|
45
45
|
|
46
|
-
|
47
|
-
|
48
|
-
でこのようなマイグレーションを実行すると、db/schema.rb には、
|
49
|
-
コメントが設定されているテーブル、カラムは以下のように出力されます。
|
46
|
+
このようなマイグレーションを実行すると、db/schema.rb のコメントが設定されているテーブル、カラムには以下のように出力されます。
|
50
47
|
|
51
48
|
```
|
52
49
|
ActiveRecord::Schema.define(:version => 0) do
|
@@ -60,15 +57,16 @@ ActiveRecord::Schema.define(:version => 0) do
|
|
60
57
|
end
|
61
58
|
```
|
62
59
|
|
63
|
-
|
60
|
+
コメントは以下のメソッドで使用することが可能です。
|
64
61
|
|
65
62
|
```
|
66
63
|
columns, create_table, drop_table, rename_table
|
67
|
-
remove_column, add_column, change_column
|
64
|
+
remove_column, add_column, change_column, rename_column
|
68
65
|
```
|
69
66
|
|
70
67
|
|
71
68
|
## コメントはどこに保存されるのか
|
69
|
+
|
72
70
|
db/schema_comments.yml にYAML形式で保存されます。
|
73
71
|
あまり推奨しませんが、もしマイグレーションにコメントを記述するのを忘れてしまった場合、db/schema_comments.yml
|
74
72
|
を直接編集した後、rake db:schema:dumpやマイグレーションを実行すると、db/schema.rbのコメントに反映されます。
|
@@ -76,69 +74,28 @@ db/schema_comments.yml にYAML形式で保存されます。
|
|
76
74
|
|
77
75
|
## I18nへの対応
|
78
76
|
|
79
|
-
|
80
|
-
rake i18n:schema_comments:update_config_locale
|
81
|
-
```
|
82
|
-
|
83
|
-
このタスクを実行すると、i18n用のYAMLを更新できます。
|
77
|
+
`schema_comments:i18n:update`タスクを実行すると、i18n用のYAMLを更新できます。
|
84
78
|
|
85
79
|
```
|
86
|
-
rake i18n:
|
80
|
+
rake schema_comments:i18n:update
|
87
81
|
```
|
88
82
|
|
89
|
-
|
83
|
+
環境変数`LOCALE`で対象のロケールを指定可能ですが、指定されていなければ`I18n.locale`から取得します。
|
90
84
|
|
91
|
-
|
85
|
+
これは `config/application.rb` で以下のように指定可能です。
|
92
86
|
|
93
87
|
```
|
94
|
-
|
88
|
+
config.i18n.default_locale = :ja
|
95
89
|
```
|
96
90
|
|
97
|
-
という記述を追加しておくと良いでしょう。
|
98
91
|
|
99
92
|
また出力先のYAMLのPATHを指定したい場合、YAML_PATHで指定が可能です。
|
100
93
|
|
101
94
|
```
|
102
|
-
rake i18n:
|
103
|
-
```
|
104
|
-
|
105
|
-
### コメント内コメント
|
106
|
-
コメント中の ((( から ))) は反映されませんので、モデル名/属性名に含めたくない箇所は ((( と ))) で括ってください。
|
107
|
-
((( ))) と同様に[[[ ]]]も使用できます。
|
108
|
-
例えば以下のようにdb/schema.rbに出力されている場合、
|
109
|
-
|
110
|
-
```
|
111
|
-
ActiveRecord::Schema.define(:version => 0) do
|
112
|
-
create_table "products", :force => true, :comment => '商品' do |t|
|
113
|
-
t.string "product_type_cd", :comment => '種別コード(((01:書籍, 02:靴, 03:パソコン)))'
|
114
|
-
t.integer "price", :comment => "価格"
|
115
|
-
t.string "name", :comment => "商品名"
|
116
|
-
t.datetime "created_at", :comment => "登録日時"
|
117
|
-
t.datetime "updated_at", :comment => "更新日時"
|
118
|
-
end
|
119
|
-
end
|
120
|
-
```
|
121
|
-
|
122
|
-
```
|
123
|
-
$ rake i18n:schema_comments:update_config_locale LOCALE=ja
|
124
|
-
```
|
125
|
-
|
126
|
-
とすると、以下のように出力されます。
|
127
|
-
|
128
|
-
```
|
129
|
-
ja:
|
130
|
-
activerecord:
|
131
|
-
attributes:
|
132
|
-
product:
|
133
|
-
product_type_cd: "種別コード"
|
134
|
-
price: "価格"
|
135
|
-
name: "商品名"
|
136
|
-
created_at: "登録日時"
|
137
|
-
updated_at: "更新日時"
|
95
|
+
rake schema_comments:i18n:update LOCALE=ja YAML_PATH=/path/to/yaml
|
138
96
|
```
|
139
97
|
|
140
98
|
|
141
|
-
|
142
99
|
## MySQLのビュー
|
143
100
|
MySQLのビューを使用した場合、元々MySQLではSHOW TABLES でビューも表示してしまうため、
|
144
101
|
ビューはテーブルとしてSchemaDumperに認識され、development環境ではMySQLのビューとして作成されているのに、
|
@@ -146,35 +103,5 @@ test環境ではテーブルとして作成されてしまい、テストが正
|
|
146
103
|
これを避けるため、schema_commentsでは、db/schema.rbを出力する際、テーブルに関する記述の後に、CREATE VIEWを行う記述を追加します。
|
147
104
|
|
148
105
|
|
149
|
-
## annotate_models
|
150
|
-
rake db:annotate で以下のようなコメントを、モデル、テスト、フィクスチャといったモデルに関係の強いファイルの
|
151
|
-
先頭に追加します。
|
152
|
-
|
153
|
-
```
|
154
|
-
# == Schema Info
|
155
|
-
#
|
156
|
-
# Schema version: 20090721185959
|
157
|
-
#
|
158
|
-
# Table name: books # 書籍
|
159
|
-
#
|
160
|
-
# id :integer not null, primary key
|
161
|
-
# title :string(100) not null # タイトル
|
162
|
-
# size :integer not null, default(1) # 判型
|
163
|
-
# price :decimal(17, 14) not null, default(0.0) # 価格
|
164
|
-
# created_at :datetime # 登録日時
|
165
|
-
# updated_at :datetime # 更新日時
|
166
|
-
#
|
167
|
-
# =================
|
168
|
-
#
|
169
|
-
```
|
170
|
-
|
171
|
-
また、rake db:updateで、rake db:migrateとrake db:annotateを実行します。
|
172
|
-
|
173
|
-
annotate_modelsは、達人プログラマーのDave Thomasさんが公開しているプラグインです。
|
174
|
-
http://repo.pragprog.com/svn/Public/plugins/annotate_models/
|
175
|
-
|
176
|
-
本プラグインでは、それを更に拡張したDave Boltonさんのプラグインannotate_models
|
177
|
-
( リポジトリは削除された様子・・・ )をベースに拡張を加えています。
|
178
|
-
|
179
106
|
## License
|
180
|
-
Copyright (c) 2008 Takeshi AKIMA, released under the Ruby License
|
107
|
+
Copyright (c) 2008 - 2016 Takeshi AKIMA, released under the Ruby License
|
data/Rakefile
CHANGED
data/gemfiles/Gemfile.rails-4.0
CHANGED
@@ -7,6 +7,8 @@ group :test do
|
|
7
7
|
gem "sqlite3", :platform => :ruby
|
8
8
|
gem "mysql2" , "~> 0.3.13", :platform => :ruby
|
9
9
|
gem "rails"
|
10
|
+
gem "fuubar"
|
11
|
+
gem "simplecov"
|
10
12
|
|
11
13
|
# gem "activerecord-jdbcsqlite3-adapter", :platform => :jruby
|
12
14
|
# gem "activerecord-jdbcmysql-adapter" , :platform => :jruby
|
data/gemfiles/Gemfile.rails-4.1
CHANGED
data/gemfiles/Gemfile.rails-4.2
CHANGED
data/lib/schema_comments.rb
CHANGED
@@ -7,8 +7,7 @@ module SchemaComments
|
|
7
7
|
|
8
8
|
autoload :Base , 'schema_comments/base'
|
9
9
|
autoload :ConnectionAdapters, 'schema_comments/connection_adapters'
|
10
|
-
autoload :
|
11
|
-
autoload :Migrator , 'schema_comments/migrator'
|
10
|
+
autoload :DummyMigration , 'schema_comments/dummy_migration'
|
12
11
|
autoload :Schema , 'schema_comments/schema'
|
13
12
|
autoload :SchemaComment , 'schema_comments/schema_comment'
|
14
13
|
autoload :SchemaDumper , 'schema_comments/schema_dumper'
|
@@ -21,8 +20,12 @@ module SchemaComments
|
|
21
20
|
|
22
21
|
class << self
|
23
22
|
def setup
|
24
|
-
|
25
|
-
|
23
|
+
defined?(Rails) && Rails.env.production? ? setup_on_production : setup_on_development
|
24
|
+
end
|
25
|
+
|
26
|
+
def setup_on_development
|
27
|
+
base_names = %w(Schema) +
|
28
|
+
%w(ColumnDefinition TableDefinition).map{|name| "ConnectionAdapters::#{name}"}
|
26
29
|
|
27
30
|
base_names.each do |base_name|
|
28
31
|
ar_class = "ActiveRecord::#{base_name}".constantize
|
@@ -51,6 +54,24 @@ module SchemaComments
|
|
51
54
|
end
|
52
55
|
end
|
53
56
|
|
57
|
+
def setup_on_production
|
58
|
+
ActiveRecord::Migration.__send__(:prepend, DummyMigration)
|
59
|
+
end
|
60
|
+
|
61
|
+
[
|
62
|
+
:table_comment,
|
63
|
+
:column_comment,
|
64
|
+
:column_comments,
|
65
|
+
:save_table_comment,
|
66
|
+
:save_column_comment,
|
67
|
+
:destroy_of,
|
68
|
+
:update_table_name,
|
69
|
+
:update_column_name,
|
70
|
+
:clear_cache,
|
71
|
+
].each do |m|
|
72
|
+
module_eval("def #{m}(*args); SchemaComment.#{m}(*args) end", __FILE__, __LINE__)
|
73
|
+
end
|
74
|
+
|
54
75
|
end
|
55
76
|
|
56
77
|
end
|
@@ -2,10 +2,6 @@
|
|
2
2
|
module SchemaComments
|
3
3
|
module ConnectionAdapters
|
4
4
|
|
5
|
-
module Column
|
6
|
-
attr_accessor :comment
|
7
|
-
end
|
8
|
-
|
9
5
|
module ColumnDefinition
|
10
6
|
attr_accessor :comment
|
11
7
|
end
|
@@ -76,16 +72,6 @@ module SchemaComments
|
|
76
72
|
end
|
77
73
|
|
78
74
|
module ConcreteAdapter
|
79
|
-
#TODO: columnsメソッドに第二引数移行がないので本来は消すべき?
|
80
|
-
def columns(table_name, name = nil, &block)
|
81
|
-
result = super(table_name)
|
82
|
-
column_comment_hash = column_comments(table_name)
|
83
|
-
result.each do |column|
|
84
|
-
column.comment = column_comment_hash[column.name]
|
85
|
-
end
|
86
|
-
result
|
87
|
-
end
|
88
|
-
|
89
75
|
def create_table(table_name, options = {}, &block)
|
90
76
|
table_def = nil
|
91
77
|
result = super(table_name, options) do |t|
|
@@ -11,19 +11,19 @@ module SchemaComments
|
|
11
11
|
|
12
12
|
class << self
|
13
13
|
def table_comment(table_name)
|
14
|
-
@table_names ||=
|
14
|
+
@table_names ||= yaml_read{|db| db[TABLE_KEY]}.dup
|
15
15
|
@table_names[table_name.to_s]
|
16
16
|
end
|
17
17
|
|
18
18
|
def column_comment(table_name, column_name)
|
19
|
-
@column_names ||=
|
19
|
+
@column_names ||= yaml_read{|db| db[COLUMN_KEY] }.dup
|
20
20
|
column_hash = @column_names[table_name.to_s] || {}
|
21
21
|
column_hash[column_name.to_s]
|
22
22
|
end
|
23
23
|
|
24
24
|
def column_comments(table_name)
|
25
25
|
result = nil
|
26
|
-
@column_names ||=
|
26
|
+
@column_names ||= yaml_read{|db| db[COLUMN_KEY] }.dup
|
27
27
|
result = @column_names[table_name.to_s]
|
28
28
|
result || {}
|
29
29
|
end
|
@@ -32,7 +32,7 @@ module SchemaComments
|
|
32
32
|
yaml_access do |db|
|
33
33
|
db[TABLE_KEY][table_name.to_s] = comment
|
34
34
|
end
|
35
|
-
|
35
|
+
clear_cache
|
36
36
|
end
|
37
37
|
|
38
38
|
def save_column_comment(table_name, column_name, comment)
|
@@ -40,7 +40,7 @@ module SchemaComments
|
|
40
40
|
db[COLUMN_KEY][table_name.to_s] ||= {}
|
41
41
|
db[COLUMN_KEY][table_name.to_s][column_name.to_s] = comment
|
42
42
|
end
|
43
|
-
|
43
|
+
clear_cache
|
44
44
|
end
|
45
45
|
|
46
46
|
def destroy_of(table_name, column_name)
|
@@ -48,7 +48,7 @@ module SchemaComments
|
|
48
48
|
column_hash = db[COLUMN_KEY][table_name.to_s]
|
49
49
|
column_hash.delete(column_name.to_s) if column_hash
|
50
50
|
end
|
51
|
-
|
51
|
+
clear_cache
|
52
52
|
end
|
53
53
|
|
54
54
|
def update_table_name(table_name, new_name)
|
@@ -56,8 +56,7 @@ module SchemaComments
|
|
56
56
|
db[TABLE_KEY][new_name.to_s] = db[TABLE_KEY].delete(table_name.to_s)
|
57
57
|
db[COLUMN_KEY][new_name.to_s] = db[COLUMN_KEY].delete(table_name.to_s)
|
58
58
|
end
|
59
|
-
|
60
|
-
@column_names = nil
|
59
|
+
clear_cache
|
61
60
|
end
|
62
61
|
|
63
62
|
def update_column_name(table_name, column_name, new_name)
|
@@ -67,12 +66,45 @@ module SchemaComments
|
|
67
66
|
table_cols[new_name.to_s] = table_cols.delete(column_name.to_s)
|
68
67
|
end
|
69
68
|
end
|
69
|
+
clear_cache
|
70
|
+
end
|
71
|
+
|
72
|
+
def model_comments
|
73
|
+
yaml_read{|db| db[TABLE_KEY] }.
|
74
|
+
each_with_object({}){|(k,v),d| d[k.singularize] = v }
|
75
|
+
end
|
76
|
+
|
77
|
+
def attribute_comments
|
78
|
+
yaml_read{|db| db[COLUMN_KEY] }.each_with_object({}) do |(k,v),d|
|
79
|
+
d[k.singularize] = v.each_with_object({}) do |(name, comment), dd|
|
80
|
+
dd[name.sub(/_id\z/, '')] = comment.sub(/id\z/i, '') if name =~ /_id\z/
|
81
|
+
dd[name] = comment
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def activerecord_comments
|
87
|
+
{
|
88
|
+
'activerecord' => {
|
89
|
+
'models' => model_comments,
|
90
|
+
'attributes' => attribute_comments,
|
91
|
+
}
|
92
|
+
}
|
93
|
+
end
|
94
|
+
|
95
|
+
def locale_yaml(locale)
|
96
|
+
YAML.dump({locale.to_s => activerecord_comments})
|
97
|
+
end
|
98
|
+
|
99
|
+
def clear_cache
|
70
100
|
@table_names = nil
|
71
101
|
@column_names = nil
|
102
|
+
self
|
72
103
|
end
|
73
104
|
|
74
|
-
def
|
75
|
-
|
105
|
+
def yaml_read(&block)
|
106
|
+
db = YAML.load_file(SchemaComments.yaml_path)
|
107
|
+
block_given? ? yield(db) : db
|
76
108
|
end
|
77
109
|
|
78
110
|
def yaml_access(&block)
|