Module: Exwiw::DbIntrospector

Defined in:
lib/exwiw/db_introspector.rb,
lib/exwiw/db_introspector/mysql_introspector.rb,
lib/exwiw/db_introspector/postgresql_introspector.rb

Overview

Reads a database's structure — tables, primary keys, columns, unique indexes, foreign keys — straight from its catalog, with no application code in the process.

This is what lets a non-Ruby application keep an exwiw schema config: the ActiveRecord generator needs the app's models loaded in memory, which only its own runtime can do, while the database schema is a description every application shares regardless of the language it is written in. What is lost by reading the database instead of the models is relations that exist only in application code (no foreign-key constraint backs them) — which is why DbSchemaGenerator only ever adds belongs_tos and never rewrites the ones already in the config.

Everything is scoped to the connection's current database/schema (MySQL: DATABASE(), PostgreSQL: current_schema()), i.e. exactly the objects the extraction itself would see through the same connection. There is no multi-database grouping equivalent to the ActiveRecord generator's: a connection points at one database, so one run generates one config directory, and a second database is a second run.

Defined Under Namespace

Classes: Base, Column, MysqlIntrospector, PostgresqlIntrospector

Constant Summary collapse

SUPPORTED_ADAPTERS =

The adapters that can be introspected. sqlite is excluded because its catalog (PRAGMA-based) is a different shape entirely, and mongodb has no fixed schema to read — MongoidSchemaGenerator covers that case from the application side.

%w[mysql postgresql].freeze

Class Method Summary collapse

Class Method Details

.build(connection_config) ⇒ Object

Build the introspector for a connection. Takes the same ConnectionConfig the adapters do, so the CLI can hand over exactly what it already parsed.



47
48
49
50
51
52
53
54
55
56
# File 'lib/exwiw/db_introspector.rb', line 47

def self.build(connection_config)
  case Adapter.normalize_name(connection_config.adapter)
  when "mysql" then MysqlIntrospector.new(connection_config)
  when "postgresql" then PostgresqlIntrospector.new(connection_config)
  else
    raise ArgumentError,
          "Schema generation from a database connection supports " \
          "#{SUPPORTED_ADAPTERS.join(' / ')} only, got #{connection_config.adapter.inspect}."
  end
end