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.



8
9
10
11
# File 'lib/exwiw/adapter.rb', line 8

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.



6
7
8
# File 'lib/exwiw/adapter.rb', line 6

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.



16
17
18
# File 'lib/exwiw/adapter.rb', line 16

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)


24
25
26
# File 'lib/exwiw/adapter.rb', line 24

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).



109
110
111
# File 'lib/exwiw/adapter.rb', line 109

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



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

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)


57
58
59
# File 'lib/exwiw/adapter.rb', line 57

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)


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

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).



29
30
31
# File 'lib/exwiw/adapter.rb', line 29

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).



70
71
72
# File 'lib/exwiw/adapter.rb', line 70

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.



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

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).



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

def schema_output_extension
  'sql'
end

#sql_query_comment(query_ast) ⇒ Object

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



99
100
101
102
103
104
105
# File 'lib/exwiw/adapter.rb', line 99

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)


50
51
52
# File 'lib/exwiw/adapter.rb', line 50

def supports_bulk_delete?
  true
end

#to_copy_from_stdin(_results, _table) ⇒ Object

Raises:

  • (NotImplementedError)


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

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.



63
64
# File 'lib/exwiw/adapter.rb', line 63

def validate_as_dump_target!(_config)
end