Module: SchemaComments::ConnectionAdapters::ConcreteAdapter
- Defined in:
- lib/schema_comments/connection_adapters.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add_column_with_schema_comments(table_name, column_name, type, options = {}) ⇒ Object
- #change_column_with_schema_comments(table_name, column_name, type, options = {}) ⇒ Object
-
#columns_with_schema_comments(table_name, name = nil, &block) ⇒ Object
TODO: columnsメソッドに第二引数移行がないので本来は消すべき?.
- #create_table_with_schema_comments(table_name, options = {}, &block) ⇒ Object
- #drop_table_with_schema_comments(table_name, *args, &block) ⇒ Object
- #remove_column_with_schema_comments(table_name, *column_names) ⇒ Object
- #rename_column_with_schema_comments(table_name, column_name, new_column_name) ⇒ Object
- #rename_table_with_schema_comments(table_name, new_name) ⇒ Object
Class Method Details
.included(mod) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/schema_comments/connection_adapters.rb', line 84 def self.included(mod) mod.module_eval do alias_method_chain :columns , :schema_comments alias_method_chain :create_table , :schema_comments alias_method_chain :drop_table , :schema_comments alias_method_chain :rename_table , :schema_comments alias_method_chain :remove_column, :schema_comments alias_method_chain :add_column , :schema_comments alias_method_chain :change_column, :schema_comments alias_method_chain :rename_column, :schema_comments end end |
Instance Method Details
#add_column_with_schema_comments(table_name, column_name, type, options = {}) ⇒ Object
151 152 153 154 155 156 |
# File 'lib/schema_comments/connection_adapters.rb', line 151 def add_column_with_schema_comments(table_name, column_name, type, = {}) comment = .delete(:comment) result = add_column_without_schema_comments(table_name, column_name, type, ) column_comment(table_name, column_name, comment) if comment result end |
#change_column_with_schema_comments(table_name, column_name, type, options = {}) ⇒ Object
158 159 160 161 162 163 164 165 166 |
# File 'lib/schema_comments/connection_adapters.rb', line 158 def change_column_with_schema_comments(table_name, column_name, type, = {}) comment = .delete(:comment) @ignore_drop_table = true result = change_column_without_schema_comments(table_name, column_name, type, ) column_comment(table_name, column_name, comment) if comment result ensure @ignore_drop_table = false end |
#columns_with_schema_comments(table_name, name = nil, &block) ⇒ Object
TODO: columnsメソッドに第二引数移行がないので本来は消すべき?
98 99 100 101 102 103 104 105 |
# File 'lib/schema_comments/connection_adapters.rb', line 98 def columns_with_schema_comments(table_name, name = nil, &block) result = columns_without_schema_comments(table_name) column_comment_hash = column_comments(table_name) result.each do |column| column.comment = column_comment_hash[column.name] end result end |
#create_table_with_schema_comments(table_name, options = {}, &block) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/schema_comments/connection_adapters.rb', line 107 def create_table_with_schema_comments(table_name, = {}, &block) table_def = nil result = create_table_without_schema_comments(table_name, ) do |t| table_def = t yield(t) end table_comment(table_name, [:comment]) unless [:comment].blank? table_def.columns.each do |col| column_comment(table_name, col.name, col.comment) unless col.comment.blank? end result end |
#drop_table_with_schema_comments(table_name, *args, &block) ⇒ Object
120 121 122 123 124 |
# File 'lib/schema_comments/connection_adapters.rb', line 120 def drop_table_with_schema_comments(table_name, *args, &block) result = drop_table_without_schema_comments(table_name, *args) delete_schema_comments(table_name) unless @ignore_drop_table result end |
#remove_column_with_schema_comments(table_name, *column_names) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/schema_comments/connection_adapters.rb', line 132 def remove_column_with_schema_comments(table_name, *column_names) # sqlite3ではremove_columnがないので、以下のフローでスキーマ更新します。 # 1. CREATE TEMPORARY TABLE "altered_xxxxxx" (・・・) # 2. PRAGMA index_list("xxxxxx") # 3. DROP TABLE "xxxxxx" # 4. CREATE TABLE "xxxxxx" # 5. PRAGMA index_list("altered_xxxxxx") # 6. DROP TABLE "altered_xxxxxx" # # このdrop tableの際に、schema_commentsを変更しないようにフラグを立てています。 @ignore_drop_table = true remove_column_without_schema_comments(table_name, *column_names) column_names.each do |column_name| delete_schema_comments(table_name, column_name) end ensure @ignore_drop_table = false end |
#rename_column_with_schema_comments(table_name, column_name, new_column_name) ⇒ Object
168 169 170 171 172 |
# File 'lib/schema_comments/connection_adapters.rb', line 168 def rename_column_with_schema_comments(table_name, column_name, new_column_name) result = rename_column_without_schema_comments(table_name, column_name, new_column_name) comment = update_schema_comments_column_name(table_name, column_name, new_column_name) result end |
#rename_table_with_schema_comments(table_name, new_name) ⇒ Object
126 127 128 129 130 |
# File 'lib/schema_comments/connection_adapters.rb', line 126 def rename_table_with_schema_comments(table_name, new_name) result = rename_table_without_schema_comments(table_name, new_name) update_schema_comments_table_name(table_name, new_name) result end |