Module: Rodauth::Tools::ConsoleHelpers

Defined in:
lib/rodauth/tools/console_helpers.rb

Overview

Console helper methods for inspecting Rodauth table configuration

Usage in console:

require 'rodauth/tools/console_helpers'
include Rodauth::Tools::ConsoleHelpers
rodauth.table_configuration

Or use the convenience loader:

Rodauth::Tools::ConsoleHelpers.load!(rodauth_instance)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Module

Class method to extend a context with helpers and set up rodauth method

Parameters:

  • rodauth_instance (Rodauth::Auth)

    Rodauth instance

Returns:

  • (Module)

    Extended module



152
153
154
155
# File 'lib/rodauth/tools/console_helpers.rb', line 152

def self.extended(base)
  puts "\nāœ“ Rodauth console helpers loaded!"
  base.help if base.respond_to?(:help)
end

Instance Method Details

#configObject

Get discovered table configuration



25
26
27
# File 'lib/rodauth/tools/console_helpers.rb', line 25

def config
  rodauth.table_configuration
end

#create_tables!Object

Create missing tables immediately



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rodauth/tools/console_helpers.rb', line 86

def create_tables!
  puts "\n=== Creating Missing Tables ==="
  missing_list = missing
  if missing_list.empty?
    puts 'āœ“ All tables already exist!'
    return
  end

  generator = Rodauth::SequelGenerator.new(missing_list, rodauth)
  generator.execute_creates(db)
  puts "āœ“ Created #{missing_list.size} table(s)"
  show_status
  nil
end

#dbObject

Access database connection



45
46
47
# File 'lib/rodauth/tools/console_helpers.rb', line 45

def db
  rodauth.db if rodauth.respond_to?(:db)
end

#helpObject

Show help message



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rodauth/tools/console_helpers.rb', line 116

def help
  puts <<~HELP

    šŸ” Rodauth Console Helpers
    ============================

    rodauth          # Get Rodauth instance
    config           # Get discovered table configuration
    missing          # Get missing tables
    tables           # List all required table names
    status           # Get detailed status for each table
    db               # Access Sequel database connection

    show_config      # Pretty-print table configuration
    show_missing     # Pretty-print missing tables
    show_status      # Pretty-print table status
    create_tables!   # Create all missing tables
    show_migration   # Display generated migration code

    help             # Show this help

    Examples:
    ---------
    config.keys                         # See all table methods
    missing.size                        # How many tables are missing?
    rodauth.table_exists?(:accounts)    # Check specific table
    db.tables                           # See what's in the database

  HELP
  nil
end

#missingObject

Get missing tables



30
31
32
# File 'lib/rodauth/tools/console_helpers.rb', line 30

def missing
  rodauth.missing_tables
end

#rodauthObject

Get or create a Rodauth instance

Override this method in your console script to provide the actual instance



20
21
22
# File 'lib/rodauth/tools/console_helpers.rb', line 20

def rodauth
  @rodauth ||= raise NotImplementedError, 'You must define a `rodauth` method that returns a Rodauth instance'
end

#show_configObject

Pretty-print table configuration



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rodauth/tools/console_helpers.rb', line 50

def show_config
  puts "\n=== Table Configuration ==="
  config.each do |method, info|
    puts "\n#{method}:"
    puts "  Table: #{info[:name]}"
    puts "  Feature: #{info[:feature]}"
    puts "  Type: #{info[:structure][:type]}"
    puts "  Exists: #{rodauth.table_exists?(info[:name])}"
  end
  nil
end

#show_migrationObject

Display generated migration code



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rodauth/tools/console_helpers.rb', line 102

def show_migration
  puts "\n=== Generated Migration ==="
  missing_list = missing
  if missing_list.empty?
    puts 'āœ“ All tables exist - no migration needed'
    return
  end

  generator = Rodauth::SequelGenerator.new(missing_list, rodauth)
  puts generator.generate_migration
  nil
end

#show_missingObject

Pretty-print missing tables



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rodauth/tools/console_helpers.rb', line 63

def show_missing
  puts "\n=== Missing Tables ==="
  if missing.empty?
    puts 'āœ“ All tables exist!'
  else
    missing.each do |info|
      puts "āœ— #{info[:table]} (feature: #{info[:feature]})"
    end
  end
  nil
end

#show_statusObject

Pretty-print table status



76
77
78
79
80
81
82
83
# File 'lib/rodauth/tools/console_helpers.rb', line 76

def show_status
  puts "\n=== Table Status ==="
  status.each do |info|
    marker = info[:exists] ? 'āœ“' : 'āœ—'
    puts "#{marker} #{info[:table].to_s.ljust(30)} (#{info[:feature]})"
  end
  nil
end

#statusObject

Get detailed status for each table



40
41
42
# File 'lib/rodauth/tools/console_helpers.rb', line 40

def status
  rodauth.table_status
end

#tablesObject

List all required table names



35
36
37
# File 'lib/rodauth/tools/console_helpers.rb', line 35

def tables
  rodauth.list_all_required_tables
end