Sha256: 3993843e790d6cff36e411d9a1de755e3e1b0060fb9fbc6c97d5bbd72a04b184

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

require_relative 'active_record_table_info/version'

module ActiveRecordTableInfo # rubocop:disable Style/Documentation
  def show_create_table
    puts connection.select_one("show create table #{table_name}")['Create Table'] # rubocop:disable Rails/Output
  end

  def table_info
    puts (['-- COLUMNS --'] + table_columns + ['-- INDEXES --'] + table_indexes).map { |line| "#{line}\n" }.join # rubocop:disable Rails/Output
  end

  def table_columns # rubocop:disable Metrics/AbcSize
    connection.columns(table_name).map do |column|
      [
        "`#{column.name}`",
        column.sql_type,
        column.null ? 'NULL' : 'NOT NULL',
        column.try(:collation) ? "COLLATE #{column.collation}" : nil,
        column.try(:extra) == 'auto_increment' ? 'AUTO_INCREMENT' : nil,
        column.null || !column.default.nil? ? "DEFAULT #{column.default.inspect}" : nil
      ].compact.join(' ')
    end
  end

  def table_indexes
    [table_primary_index] + table_non_primary_indexes
  end

  def table_primary_index
    "PRIMARY KEY (#{primary_key})"
  end

  def table_non_primary_indexes
    connection.indexes(table_name).map do |index|
      "#{index.unique ? 'UNIQUE KEY' : 'KEY'} `#{index.name}` (#{index.columns.join(', ')})"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_record_table_info-1.0.0 lib/active_record_table_info.rb