Class: Exwiw::Adapter::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/exwiw/adapter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection_config, logger) ⇒ Base

Returns a new instance of Base.



38
39
40
41
# File 'lib/exwiw/adapter.rb', line 38

def initialize(connection_config, logger)
  @connection_config = connection_config
  @logger = logger
end

Instance Attribute Details

#connection_configObject (readonly)

Returns the value of attribute connection_config.



36
37
38
# File 'lib/exwiw/adapter.rb', line 36

def connection_config
  @connection_config
end

Class Method Details

.table_config_classObject

The config class that this adapter consumes. Runner uses this to decide which Serdes type to load scenario JSON into. SQL adapters share the SQL-shaped TableConfig; non-SQL adapters override.



46
47
48
# File 'lib/exwiw/adapter.rb', line 46

def self.table_config_class
  TableConfig
end

Instance Method Details

#build_query(table, dump_target, table_by_name) ⇒ Object

Returns adapter-specific query object (e.g. Exwiw::QueryAst::Select for SQL).

Returns:

  • (Object)

    adapter-specific query object (e.g. Exwiw::QueryAst::Select for SQL)

Raises:

  • (NotImplementedError)


54
55
56
# File 'lib/exwiw/adapter.rb', line 54

def build_query(table, dump_target, table_by_name)
  raise NotImplementedError
end

#commented_sql(query_ast) ⇒ Object

Comment-prefixed SELECT. Relies on the SQL adapter’s #compile_ast (dispatched to the subclass at runtime).



139
140
141
# File 'lib/exwiw/adapter.rb', line 139

def commented_sql(query_ast)
  "#{sql_query_comment(query_ast)} #{compile_ast(query_ast)}"
end

#dump_schema(ordered_tables, output_path) ⇒ Object

Write the leading schema-creation file for this adapter to ‘output_path`. Default is a no-op; subclasses override to emit idempotent DDL so the generated dump can be applied to an empty database.

Parameters:

  • ordered_tables (Array)

    table configs in dependency order

  • output_path (String)

    absolute path to write to



76
77
# File 'lib/exwiw/adapter.rb', line 76

def dump_schema(ordered_tables, output_path)
end

#dumpable?(_config) ⇒ Boolean

Whether the given config produces its own dump output and needs an independent processing pass. SQL adapters always do; non-SQL adapters may exclude e.g. embedded subdocument configs.

Returns:

  • (Boolean)


87
88
89
# File 'lib/exwiw/adapter.rb', line 87

def dumpable?(_config)
  true
end

#explain(_query_ast) ⇒ Object

Run the database-specific EXPLAIN for the given query and return the output as a single string for ‘explain` subcommand to print. SQL adapters override; MongodbAdapter currently raises.

Raises:

  • (NotImplementedError)


111
112
113
# File 'lib/exwiw/adapter.rb', line 111

def explain(_query_ast)
  raise NotImplementedError, "#{self.class.name} does not implement #explain"
end

#output_extensionObject

File extension used for dump output (e.g. ‘sql’ for SQL, ‘jsonl’ for MongoDB).



59
60
61
# File 'lib/exwiw/adapter.rb', line 59

def output_extension
  'sql'
end

#post_insert_sql(_table) ⇒ Object

Optional SQL appended to the per-table insert-NNN-<table>.* file after the bulk INSERT statements. Use to bring side-state in sync with the explicit IDs that were just inserted (e.g. PostgreSQL sequences). Default: nil (nothing appended).



100
101
102
# File 'lib/exwiw/adapter.rb', line 100

def post_insert_sql(_table)
  nil
end

#query_comment_text(label = nil) ⇒ Object

Identifier text prepended to every query exwiw sends to the (often production) source DB, so the statement is recognizable in the processlist / slow-query log / db.currentOp() and can be killed if it runs long. e.g. “exwiw table=shops”. ‘label` is “table=…” / “collection=…”. The version is intentionally omitted to keep the comment stable across releases (snapshots / diffs). Strips `*/` to avoid breaking out of the comment.



122
123
124
125
126
# File 'lib/exwiw/adapter.rb', line 122

def query_comment_text(label = nil)
  parts = ["exwiw"]
  parts << label if label
  parts.join(' ').gsub('*/', '')
end

#schema_output_extensionObject

File extension used for the leading ‘insert-000-schema.*` file. SQL adapters emit `.sql` (CREATE TABLE IF NOT EXISTS …); MongodbAdapter overrides to `.js` (mongosh-runnable createCollection / createIndex).



66
67
68
# File 'lib/exwiw/adapter.rb', line 66

def schema_output_extension
  'sql'
end

#sql_query_comment(query_ast) ⇒ Object

SQL block-comment form, prefixed to SELECT / EXPLAIN.



129
130
131
132
133
134
135
# File 'lib/exwiw/adapter.rb', line 129

def sql_query_comment(query_ast)
  label =
    if query_ast.respond_to?(:from_table_name) && query_ast.from_table_name
      "table=#{query_ast.from_table_name}"
    end
  "/* #{query_comment_text(label)} */"
end

#supports_bulk_delete?Boolean

Whether this adapter emits delete-NNN-*.sql files.

Returns:

  • (Boolean)


80
81
82
# File 'lib/exwiw/adapter.rb', line 80

def supports_bulk_delete?
  true
end

#to_copy_from_stdin(_results, _table) ⇒ Object

Raises:

  • (NotImplementedError)


104
105
106
# File 'lib/exwiw/adapter.rb', line 104

def to_copy_from_stdin(_results, _table)
  raise NotImplementedError, "COPY format is not supported by #{self.class.name}"
end

#validate_as_dump_target!(_config) ⇒ Object

Hook for adapter-specific validation when this config is used as the dump_target. Default: nothing to validate.



93
94
# File 'lib/exwiw/adapter.rb', line 93

def validate_as_dump_target!(_config)
end