Module: MigrationComments::ActiveRecord::ConnectionAdapters::SQLite3Adapter
- Defined in:
- lib/migration_comments/active_record/connection_adapters/sqlite3_adapter.rb
Instance Method Summary collapse
- #add_column_options!(sql, options) ⇒ Object
-
#change_column(table_name, column_name, type, options = {}) ⇒ Object
:nodoc:.
- #columns(table_name) ⇒ Object
- #comment_sql(comment_definition) ⇒ Object
- #comments_supported? ⇒ Boolean
-
#copy_table(from, to, options = {}) ⇒ Object
:nodoc:.
- #create_table(table_name, options = {}) ⇒ Object
- #inline_comments? ⇒ Boolean
- #retrieve_column_comments(table_name, *column_names) ⇒ Object
- #retrieve_table_comment(table_name) ⇒ Object
- #set_column_comment(table_name, column_name, comment_text) ⇒ Object
- #set_table_comment(table_name, comment_text) ⇒ Object
Instance Method Details
#add_column_options!(sql, options) ⇒ Object
86 87 88 89 90 91 |
# File 'lib/migration_comments/active_record/connection_adapters/sqlite3_adapter.rb', line 86 def (sql, ) super(sql, ) if .keys.include?(:comment) sql << comment_sql(CommentDefinition.new(nil, nil, [:comment])) end end |
#change_column(table_name, column_name, type, options = {}) ⇒ Object
:nodoc:
68 69 70 71 72 73 74 75 |
# File 'lib/migration_comments/active_record/connection_adapters/sqlite3_adapter.rb', line 68 def change_column(table_name, column_name, type, = {}) #:nodoc: super(table_name, column_name, type, ) if .has_key?(:comment) alter_table(table_name) do |definition| definition[column_name].comment = CommentDefinition.new(table_name, column_name, [:comment]) end end end |
#columns(table_name) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/migration_comments/active_record/connection_adapters/sqlite3_adapter.rb', line 46 def columns(table_name) cols = super(table_name) comments = retrieve_column_comments(table_name, *(cols.map(&:name))) cols.each do |col| col.comment = comments[col.name.to_sym] if comments.has_key?(col.name.to_sym) end cols end |
#comment_sql(comment_definition) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/migration_comments/active_record/connection_adapters/sqlite3_adapter.rb', line 77 def comment_sql(comment_definition) if comment_definition.nil? || comment_definition.comment_text.blank? "" else " /*#{escaped_comment(comment_definition.comment_text)}*/" end end |
#comments_supported? ⇒ Boolean
4 5 6 |
# File 'lib/migration_comments/active_record/connection_adapters/sqlite3_adapter.rb', line 4 def comments_supported? true end |
#copy_table(from, to, options = {}) ⇒ Object
:nodoc:
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/migration_comments/active_record/connection_adapters/sqlite3_adapter.rb', line 55 def copy_table(from, to, = {}) #:nodoc: unless .has_key?(:comment) table_comment = retrieve_table_comment(from) .merge!(comment: table_comment) if table_comment end super(from, to, ) do |definition| retrieve_column_comments(from).each do |col_name, comment| definition[col_name].comment = CommentDefinition.new(from, col_name, comment) end yield definition if block_given? end end |
#create_table(table_name, options = {}) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/migration_comments/active_record/connection_adapters/sqlite3_adapter.rb', line 39 def create_table(table_name, = {}) super(table_name, ) do |td| td.comment = [:comment] if .has_key?(:comment) yield td if block_given? end end |
#inline_comments? ⇒ Boolean
8 9 10 |
# File 'lib/migration_comments/active_record/connection_adapters/sqlite3_adapter.rb', line 8 def inline_comments? true end |
#retrieve_column_comments(table_name, *column_names) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/migration_comments/active_record/connection_adapters/sqlite3_adapter.rb', line 28 def retrieve_column_comments(table_name, *column_names) if column_names.empty? return columns(table_name).inject({}) { |m, v| m[v.name.to_sym] = v.comment if v.comment.present?; m } end result = select_value(lookup_comment_sql(table_name)) result =~ /^CREATE (?:TEMPORARY )?TABLE "\w*" [^\(]*(?:\/\*.*\*\/ )?\((.*)\)[^\)]*$/ col_defs = $1 comment_matches = col_defs.scan(/"([^",]+)"[^,]*\/\*(.+?)\*\//) Hash[comment_matches.map{|col_name, comment| [col_name.to_sym, comment.presence] }] end |
#retrieve_table_comment(table_name) ⇒ Object
23 24 25 26 |
# File 'lib/migration_comments/active_record/connection_adapters/sqlite3_adapter.rb', line 23 def retrieve_table_comment(table_name) result = select_value(lookup_comment_sql(table_name)) $1 if result =~ /CREATE (?:TEMPORARY )?TABLE #{quote_table_name table_name} [^\(]*\/\*(.*)\*\/ \(/ end |
#set_column_comment(table_name, column_name, comment_text) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/migration_comments/active_record/connection_adapters/sqlite3_adapter.rb', line 16 def set_column_comment(table_name, column_name, comment_text) sql_type = primary_key(table_name) == column_name.to_s ? :primary_key : column_for(table_name, column_name).sql_type change_column table_name, column_name, sql_type, :comment => comment_text end |
#set_table_comment(table_name, comment_text) ⇒ Object
12 13 14 |
# File 'lib/migration_comments/active_record/connection_adapters/sqlite3_adapter.rb', line 12 def set_table_comment(table_name, comment_text) alter_table(table_name, :comment => comment_text) end |