better_structure_sql 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +69 -0
  3. data/README.md +218 -686
  4. data/app/CLAUDE.md +12 -0
  5. data/app/controllers/better_structure_sql/application_controller.rb +71 -31
  6. data/app/controllers/better_structure_sql/schema_versions_controller.rb +94 -153
  7. data/app/helpers/better_structure_sql/schema_versions_helper.rb +48 -24
  8. data/app/views/better_structure_sql/schema_versions/index.html.erb +46 -49
  9. data/app/views/better_structure_sql/schema_versions/show.html.erb +73 -120
  10. data/app/views/layouts/better_structure_sql/application.html.erb +27 -18
  11. data/config/routes.rb +1 -1
  12. data/lib/CLAUDE.md +73 -0
  13. data/lib/better_structure_sql/adapters/base_adapter.rb +16 -1
  14. data/lib/better_structure_sql/adapters/mysql_adapter.rb +396 -364
  15. data/lib/better_structure_sql/adapters/postgresql_adapter.rb +574 -478
  16. data/lib/better_structure_sql/adapters/registry.rb +18 -15
  17. data/lib/better_structure_sql/adapters/sqlite_adapter.rb +221 -474
  18. data/lib/better_structure_sql/configuration.rb +9 -2
  19. data/lib/better_structure_sql/creation_order.rb +157 -0
  20. data/lib/better_structure_sql/dependency_resolver.rb +4 -2
  21. data/lib/better_structure_sql/dumper.rb +173 -315
  22. data/lib/better_structure_sql/engine.rb +50 -4
  23. data/lib/better_structure_sql/file_writer.rb +108 -97
  24. data/lib/better_structure_sql/generators/base.rb +31 -4
  25. data/lib/better_structure_sql/generators/comment_generator.rb +16 -12
  26. data/lib/better_structure_sql/generators/domain_generator.rb +8 -6
  27. data/lib/better_structure_sql/generators/extension_generator.rb +7 -2
  28. data/lib/better_structure_sql/generators/foreign_key_generator.rb +14 -4
  29. data/lib/better_structure_sql/generators/index_generator.rb +0 -20
  30. data/lib/better_structure_sql/generators/materialized_view_generator.rb +17 -2
  31. data/lib/better_structure_sql/generators/sequence_generator.rb +13 -5
  32. data/lib/better_structure_sql/generators/table_generator.rb +164 -47
  33. data/lib/better_structure_sql/generators/type_generator.rb +25 -14
  34. data/lib/better_structure_sql/generators/view_generator.rb +12 -8
  35. data/lib/better_structure_sql/introspection/extensions.rb +1 -14
  36. data/lib/better_structure_sql/introspection/foreign_keys.rb +1 -14
  37. data/lib/better_structure_sql/introspection/functions.rb +1 -14
  38. data/lib/better_structure_sql/introspection/indexes.rb +1 -14
  39. data/lib/better_structure_sql/introspection/sequences.rb +1 -14
  40. data/lib/better_structure_sql/introspection/tables.rb +1 -14
  41. data/lib/better_structure_sql/introspection/triggers.rb +1 -14
  42. data/lib/better_structure_sql/introspection/types.rb +1 -14
  43. data/lib/better_structure_sql/introspection/views.rb +2 -19
  44. data/lib/better_structure_sql/introspection.rb +12 -1
  45. data/lib/better_structure_sql/migration_patch.rb +39 -139
  46. data/lib/better_structure_sql/railtie.rb +53 -56
  47. data/lib/better_structure_sql/schema_loader.rb +107 -86
  48. data/lib/better_structure_sql/schema_version.rb +3 -2
  49. data/lib/better_structure_sql/schema_versions.rb +70 -71
  50. data/lib/better_structure_sql/version.rb +1 -1
  51. data/lib/better_structure_sql/zip_generator.rb +43 -6
  52. data/lib/better_structure_sql.rb +2 -1
  53. data/lib/generators/better_structure_sql/migration_generator.rb +1 -1
  54. data/lib/generators/better_structure_sql/templates/README +17 -34
  55. data/lib/tasks/better_structure_sql.rake +19 -40
  56. metadata +9 -147
  57. data/config/database.yml +0 -3
@@ -4,8 +4,10 @@ module BetterStructureSql
4
4
  module Generators
5
5
  # Generates CREATE TABLE statements with columns and constraints
6
6
  #
7
- # Handles column definitions, primary keys, constraints, and
8
- # inline foreign keys for SQLite.
7
+ # Handles column definitions (identity, generated, collation), primary keys,
8
+ # constraints and PostgreSQL partitioning; SQLite tables are replayed verbatim.
9
+ # Statements that need the table to exist (NOT VALID constraints, sequence
10
+ # ownership, partitions) follow the CREATE TABLE statement.
9
11
  class TableGenerator < Base
10
12
  attr_reader :adapter
11
13
 
@@ -17,17 +19,96 @@ module BetterStructureSql
17
19
  # Generates CREATE TABLE statement
18
20
  #
19
21
  # @param table [Hash] Table metadata with columns and constraints
20
- # @return [String] SQL statement
22
+ # @return [String] SQL statement(s)
21
23
  def generate(table)
22
- lines = ["CREATE TABLE IF NOT EXISTS #{table[:name]} ("]
24
+ statements = [create_table_statement(table)]
25
+ statements.concat(not_valid_constraint_statements(table))
26
+ statements.concat(owned_sequence_statements(table))
27
+ statements.concat(Array(table[:partitions]).map { |partition| generate(partition) })
28
+ statements.join("\n\n")
29
+ end
30
+
31
+ private
32
+
33
+ # Build the CREATE TABLE statement itself
34
+ #
35
+ # SQLite tables are emitted as stored in sqlite_master (:sql): only that
36
+ # statement carries AUTOINCREMENT, WITHOUT ROWID, STRICT, collations,
37
+ # unnamed CHECKs and inline foreign keys, and replaying it keeps
38
+ # sqlite_master identical after a load.
39
+ #
40
+ # @param table [Hash] Table metadata
41
+ # @return [String] CREATE TABLE statement
42
+ def create_table_statement(table)
43
+ return create_partition_statement(table) if table[:partition_of]
44
+ return "#{table.fetch(:sql)};" if sqlite_adapter?
45
+
46
+ lines = ["#{create_table_prefix(table)} ("]
23
47
  lines << build_table_contents(table)
24
- lines << ');'
48
+ lines << ")#{inherits_clause(table)}#{partition_by_clause(table)}#{storage_parameters_clause(table)}#{table_options(table)};"
25
49
  lines.join("\n")
26
50
  end
27
51
 
28
- private
52
+ # @param table [Hash] Table metadata with optional :unlogged
53
+ # @return [String] CREATE [UNLOGGED] TABLE IF NOT EXISTS <name>
54
+ def create_table_prefix(table)
55
+ "CREATE #{'UNLOGGED ' if table[:unlogged]}TABLE IF NOT EXISTS #{quote_identifier(table[:name])}"
56
+ end
29
57
 
30
- # Build table contents (columns, primary key, constraints, foreign keys)
58
+ # Build INHERITS clause for a PostgreSQL inheritance child
59
+ #
60
+ # @param table [Hash] Table metadata with optional :inherits (parent names)
61
+ # @return [String] INHERITS clause or empty string
62
+ def inherits_clause(table)
63
+ parents = Array(table[:inherits])
64
+ parents.any? ? " INHERITS (#{parents.map { |parent| quote_identifier(parent) }.join(', ')})" : ''
65
+ end
66
+
67
+ # Build WITH (...) clause for PostgreSQL storage parameters
68
+ #
69
+ # @param table [Hash] Table metadata with optional :storage_parameters (e.g. "fillfactor=70")
70
+ # @return [String] WITH clause or empty string
71
+ def storage_parameters_clause(table)
72
+ table[:storage_parameters] ? " WITH (#{table[:storage_parameters]})" : ''
73
+ end
74
+
75
+ # Build MySQL table options (storage engine and default collation)
76
+ #
77
+ # Without them the loaded table takes the server/database defaults, which
78
+ # changes every column whose collation was the table default.
79
+ #
80
+ # @param table [Hash] Table metadata with optional :engine and :collation
81
+ # @return [String] Table options or empty string
82
+ def table_options(table)
83
+ return '' unless table[:engine]
84
+
85
+ " ENGINE=#{table[:engine]} DEFAULT COLLATE=#{table[:collation]}"
86
+ end
87
+
88
+ # Build CREATE TABLE ... PARTITION OF for a partition
89
+ #
90
+ # Columns come from the parent; only the partition's own constraints are listed.
91
+ #
92
+ # @param table [Hash] Partition metadata
93
+ # @return [String] CREATE TABLE statement
94
+ def create_partition_statement(table)
95
+ constraints = build_constraints_clauses(table)
96
+ constraints_list = constraints.any? ? " (\n#{constraints.map { |line| indent(line) }.join(",\n")}\n)" : ''
97
+
98
+ "#{create_table_prefix(table)} " \
99
+ "PARTITION OF #{quote_identifier(table[:partition_of])}#{constraints_list} " \
100
+ "#{table[:partition_bound]}#{partition_by_clause(table)}#{storage_parameters_clause(table)};"
101
+ end
102
+
103
+ # Build PARTITION BY clause for partitioned tables
104
+ #
105
+ # @param table [Hash] Table metadata
106
+ # @return [String] PARTITION BY clause or empty string
107
+ def partition_by_clause(table)
108
+ table[:partition_key] ? " PARTITION BY #{table[:partition_key]}" : ''
109
+ end
110
+
111
+ # Build table contents (columns, primary key, constraints)
31
112
  #
32
113
  # @param table [Hash] Table metadata
33
114
  # @return [String] Formatted table contents
@@ -35,7 +116,6 @@ module BetterStructureSql
35
116
  column_defs = build_column_definitions(table)
36
117
  column_defs << build_primary_key_clause(table) if table[:primary_key]&.any?
37
118
  column_defs.concat(build_constraints_clauses(table))
38
- column_defs.concat(build_foreign_keys_clauses(table)) if sqlite_adapter? && table[:foreign_keys]&.any?
39
119
 
40
120
  column_defs.map { |def_line| indent(def_line) }.join(",\n")
41
121
  end
@@ -45,39 +125,73 @@ module BetterStructureSql
45
125
  # @param table [Hash] Table metadata
46
126
  # @return [Array<String>] Array of column definition strings
47
127
  def build_column_definitions(table)
48
- table[:columns].map { |col| column_definition(col) }
128
+ table[:columns].map { |col| column_definition(col, table) }
49
129
  end
50
130
 
51
131
  # Build primary key clause
52
132
  #
133
+ # The constraint name is only spelled out when it differs from PostgreSQL's
134
+ # default "<table>_pkey".
135
+ #
53
136
  # @param table [Hash] Table metadata
54
137
  # @return [String] PRIMARY KEY clause
55
138
  def build_primary_key_clause(table)
56
- pk_cols = table[:primary_key].map { |col| quote_column_name(col) }.join(', ')
57
- "PRIMARY KEY (#{pk_cols})"
139
+ pk_cols = table[:primary_key].map { |col| quote_identifier(col) }.join(', ')
140
+ clause = "PRIMARY KEY (#{pk_cols})"
141
+ name = table[:primary_key_name]
142
+ return clause if name.nil? || name == "#{table[:name]}_pkey"
143
+
144
+ "CONSTRAINT #{quote_identifier(name)} #{clause}"
58
145
  end
59
146
 
60
- # Build constraints clauses
147
+ # Build constraints clauses (NOT VALID constraints are added after the table)
61
148
  #
62
149
  # @param table [Hash] Table metadata
63
150
  # @return [Array<String>] Array of constraint definition strings
64
151
  def build_constraints_clauses(table)
65
- table[:constraints]&.map { |constraint| constraint_definition(constraint) } || []
152
+ Array(table[:constraints]).reject { |constraint| not_valid?(constraint) }
153
+ .map { |constraint| constraint_definition(constraint) }
154
+ end
155
+
156
+ # Build ALTER TABLE statements for NOT VALID constraints
157
+ #
158
+ # PostgreSQL ignores NOT VALID inside CREATE TABLE, so these must be added afterwards.
159
+ #
160
+ # @param table [Hash] Table metadata
161
+ # @return [Array<String>] ALTER TABLE statements
162
+ def not_valid_constraint_statements(table)
163
+ Array(table[:constraints]).select { |constraint| not_valid?(constraint) }.map do |constraint|
164
+ "ALTER TABLE #{quote_identifier(table[:name])} ADD #{constraint_definition(constraint)};"
165
+ end
66
166
  end
67
167
 
68
- # Build foreign keys clauses for SQLite
168
+ # Build ALTER SEQUENCE ... OWNED BY statements for serial columns
69
169
  #
70
170
  # @param table [Hash] Table metadata
71
- # @return [Array<String>] Array of foreign key definition strings
72
- def build_foreign_keys_clauses(table)
73
- table[:foreign_keys].map { |fk| foreign_key_definition(fk) }
171
+ # @return [Array<String>] ALTER SEQUENCE statements
172
+ def owned_sequence_statements(table)
173
+ Array(table[:owned_sequences]).map do |owned|
174
+ "ALTER SEQUENCE #{quote_identifier(owned[:sequence])} " \
175
+ "OWNED BY #{quote_identifier(table[:name])}.#{quote_identifier(owned[:column])};"
176
+ end
74
177
  end
75
178
 
76
- def column_definition(column)
77
- # Quote column name for MySQL compatibility (reserved words like 'key')
78
- column_name = quote_column_name(column[:name])
79
- parts = [column_name, column[:type]]
179
+ def not_valid?(constraint)
180
+ constraint[:validated] == false
181
+ end
182
+
183
+ # Build one column definition
184
+ #
185
+ # The collation is omitted when it is the table's default collation.
186
+ #
187
+ # @param column [Hash] Column metadata
188
+ # @param table [Hash] Table metadata (for its default :collation)
189
+ # @return [String] Column definition
190
+ def column_definition(column, table)
191
+ parts = [quote_identifier(column[:name]), column[:type]]
80
192
 
193
+ parts << "COLLATE #{column[:collation]}" if column[:collation] && column[:collation] != table[:collation]
194
+ parts << generated_clause(column[:generated]) if column[:generated]
81
195
  parts << 'NOT NULL' unless column[:nullable]
82
196
 
83
197
  if column[:default]
@@ -85,20 +199,40 @@ module BetterStructureSql
85
199
  parts << "DEFAULT #{default_value}"
86
200
  end
87
201
 
202
+ parts << 'AUTO_INCREMENT' if column[:auto_increment]
203
+ parts << "ON UPDATE #{column[:on_update]}" if column[:on_update]
204
+ parts << identity_clause(column[:identity]) if column[:identity]
205
+
88
206
  parts.join(' ')
89
207
  end
90
208
 
209
+ # Build GENERATED ALWAYS AS (...) clause for generated columns
210
+ #
211
+ # @param generated [Hash] Generated column metadata with :expression and :kind
212
+ # @return [String] Generated column clause
213
+ def generated_clause(generated)
214
+ "GENERATED ALWAYS AS (#{generated[:expression]}) #{generated[:kind]}"
215
+ end
216
+
217
+ # Build GENERATED ... AS IDENTITY clause with sequence options
218
+ #
219
+ # @param identity [Hash] Identity metadata with :generation and sequence options
220
+ # @return [String] Identity clause
221
+ def identity_clause(identity)
222
+ options = SequenceGenerator.new(config).options(identity)
223
+ "GENERATED #{identity[:generation]} AS IDENTITY (#{options.join(' ')})"
224
+ end
225
+
91
226
  def constraint_definition(constraint)
92
- case constraint[:type]
93
- when :check
94
- when :unique
95
- end
96
- "CONSTRAINT #{constraint[:name]} #{constraint[:definition]}"
227
+ "CONSTRAINT #{quote_identifier(constraint[:name])} #{constraint[:definition]}"
97
228
  end
98
229
 
99
230
  def format_default(default_value)
100
231
  return default_value if default_value.nil?
101
232
 
233
+ # PostgreSQL (pg_get_expr) and MySQL adapters report defaults as SQL, never as raw values
234
+ return default_value if postgresql_adapter? || mysql_adapter_instance?
235
+
102
236
  # Handle nextval for sequences
103
237
  return default_value if default_value.start_with?('nextval(')
104
238
 
@@ -122,29 +256,12 @@ module BetterStructureSql
122
256
  adapter&.class&.name == 'BetterStructureSql::Adapters::SqliteAdapter'
123
257
  end
124
258
 
125
- def foreign_key_definition(foreign_key)
126
- parts = ["FOREIGN KEY (#{foreign_key[:column]})"]
127
- parts << "REFERENCES #{foreign_key[:foreign_table]} (#{foreign_key[:foreign_column]})"
128
- parts << "ON DELETE #{foreign_key[:on_delete]}" if foreign_key[:on_delete] && foreign_key[:on_delete] != 'NO ACTION'
129
- parts << "ON UPDATE #{foreign_key[:on_update]}" if foreign_key[:on_update] && foreign_key[:on_update] != 'NO ACTION'
130
-
131
- parts.join(' ')
259
+ def mysql_adapter_instance?
260
+ adapter&.class&.name == 'BetterStructureSql::Adapters::MysqlAdapter'
132
261
  end
133
262
 
134
- def quote_column_name(column_name)
135
- # Detect adapter from ActiveRecord connection
136
- adapter_name = begin
137
- ActiveRecord::Base.connection.adapter_name.downcase
138
- rescue StandardError
139
- nil
140
- end
141
-
142
- # MySQL/MariaDB use backticks, PostgreSQL/SQLite use double quotes
143
- if %w[mysql mysql2 trilogy].include?(adapter_name)
144
- "`#{column_name}`"
145
- else
146
- "\"#{column_name}\""
147
- end
263
+ def postgresql_adapter?
264
+ adapter&.class&.name == 'BetterStructureSql::Adapters::PostgresqlAdapter'
148
265
  end
149
266
  end
150
267
  end
@@ -2,11 +2,11 @@
2
2
 
3
3
  module BetterStructureSql
4
4
  module Generators
5
- # Generates CREATE TYPE statements for enums and composite types
5
+ # Generates CREATE TYPE statements for enums, composite and range types
6
6
  class TypeGenerator < Base
7
7
  # Generates CREATE TYPE statement
8
8
  #
9
- # @param type [Hash] Type metadata (enum or composite)
9
+ # @param type [Hash] Type metadata (enum, composite, range or domain)
10
10
  # @return [String, nil] SQL statement or nil if unsupported
11
11
  def generate(type)
12
12
  case type[:type]
@@ -14,8 +14,10 @@ module BetterStructureSql
14
14
  generate_enum(type)
15
15
  when 'composite'
16
16
  generate_composite(type)
17
+ when 'range'
18
+ generate_range(type)
17
19
  when 'domain'
18
- generate_domain(type)
20
+ DomainGenerator.new(config).generate(type)
19
21
  else
20
22
  # Unknown type, skip
21
23
  nil
@@ -25,24 +27,33 @@ module BetterStructureSql
25
27
  private
26
28
 
27
29
  def generate_enum(type)
28
- values = type[:values].map { |v| "'#{v}'" }.join(', ')
29
- "CREATE TYPE #{type[:name]} AS ENUM (#{values});"
30
+ values = type[:values].map { |value| quote_literal(value) }.join(', ')
31
+ "CREATE TYPE #{type_name(type)} AS ENUM (#{values});"
30
32
  end
31
33
 
32
34
  def generate_composite(type)
33
- # Composite types have attributes
34
- # Note: PostgreSQL does not support IF NOT EXISTS for composite types
35
+ # NOTE: PostgreSQL does not support IF NOT EXISTS for composite types
35
36
  attrs = type[:attributes].map do |attr|
36
- "#{attr[:name]} #{attr[:type]}"
37
+ "#{quote_identifier(attr[:name])} #{attr[:type]}"
37
38
  end.join(', ')
38
- "CREATE TYPE #{type[:name]} AS (#{attrs});"
39
+ "CREATE TYPE #{type_name(type)} AS (#{attrs});"
39
40
  end
40
41
 
41
- def generate_domain(type)
42
- # NOTE: PostgreSQL does not support IF NOT EXISTS for domains
43
- parts = ["CREATE DOMAIN #{type[:name]} AS #{type[:base_type]}"]
44
- parts << type[:constraint] if type[:constraint]
45
- "#{parts.join(' ')};"
42
+ def generate_range(type)
43
+ options = ["SUBTYPE = #{type[:subtype]}"]
44
+ options << "SUBTYPE_OPCLASS = #{type[:subtype_opclass]}" if type[:subtype_opclass]
45
+ options << "COLLATION = #{type[:collation]}" if type[:collation]
46
+ options << "CANONICAL = #{type[:canonical]}" if type[:canonical]
47
+ options << "SUBTYPE_DIFF = #{type[:subtype_diff]}" if type[:subtype_diff]
48
+ "CREATE TYPE #{type_name(type)} AS RANGE (#{options.join(', ')});"
49
+ end
50
+
51
+ def type_name(type)
52
+ qualified_identifier(type[:schema], type[:name])
53
+ end
54
+
55
+ def quote_literal(value)
56
+ "'#{value.gsub("'", "''")}'"
46
57
  end
47
58
  end
48
59
  end
@@ -6,21 +6,25 @@ module BetterStructureSql
6
6
  class ViewGenerator < Base
7
7
  # Generates CREATE VIEW statement
8
8
  #
9
- # @param view [Hash] View metadata with definition
9
+ # Views carrying their original CREATE VIEW statement (:sql, supplied by
10
+ # the SQLite adapter) are emitted verbatim: SQLite has no CREATE OR
11
+ # REPLACE VIEW, and replaying the stored statement keeps sqlite_master
12
+ # identical after a load.
13
+ #
14
+ # @param view [Hash] View metadata with definition and optional :options
15
+ # (PostgreSQL reloptions such as "check_option=local, security_barrier=true")
16
+ # and :sql (original CREATE VIEW statement)
10
17
  # @return [String] SQL statement
11
18
  def generate(view)
12
- # Only add schema prefix for non-default schemas
13
- # PostgreSQL default: 'public'
14
- # SQLite default: 'main'
15
- # MySQL default: current database
16
- default_schemas = %w[public main]
17
- schema_prefix = default_schemas.include?(view[:schema]) ? '' : "#{view[:schema]}."
19
+ return "#{view[:sql]};" if view[:sql]
20
+
18
21
  definition = view[:definition].strip
19
22
 
20
23
  # Ensure definition ends with semicolon
21
24
  definition += ';' unless definition.end_with?(';')
22
25
 
23
- "CREATE OR REPLACE VIEW #{schema_prefix}#{view[:name]} AS\n#{definition}"
26
+ options = view[:options].present? ? " WITH (#{view[:options]})" : ''
27
+ "CREATE OR REPLACE VIEW #{qualified_identifier(view[:schema], view[:name])}#{options} AS\n#{definition}"
24
28
  end
25
29
  end
26
30
  end
@@ -9,20 +9,7 @@ module BetterStructureSql
9
9
  # @param connection [ActiveRecord::ConnectionAdapters::AbstractAdapter] Database connection
10
10
  # @return [Array<Hash>] Array of extension metadata hashes
11
11
  def fetch_extensions(connection)
12
- adapter = get_adapter(connection)
13
- adapter.fetch_extensions(connection)
14
- rescue StandardError => e
15
- warn "Warning: Failed to fetch extensions: #{e.message}"
16
- []
17
- end
18
-
19
- private
20
-
21
- def get_adapter(connection)
22
- @get_adapter ||= Adapters::Registry.adapter_for(
23
- connection,
24
- adapter_override: BetterStructureSql.configuration.adapter
25
- )
12
+ adapter_for(connection).fetch_extensions(connection)
26
13
  end
27
14
  end
28
15
  end
@@ -9,20 +9,7 @@ module BetterStructureSql
9
9
  # @param connection [ActiveRecord::ConnectionAdapters::AbstractAdapter] Database connection
10
10
  # @return [Array<Hash>] Array of foreign key metadata hashes
11
11
  def fetch_foreign_keys(connection)
12
- adapter = get_adapter(connection)
13
- adapter.fetch_foreign_keys(connection)
14
- rescue StandardError => e
15
- warn "Warning: Failed to fetch foreign keys: #{e.message}"
16
- []
17
- end
18
-
19
- private
20
-
21
- def get_adapter(connection)
22
- @get_adapter ||= Adapters::Registry.adapter_for(
23
- connection,
24
- adapter_override: BetterStructureSql.configuration.adapter
25
- )
12
+ adapter_for(connection).fetch_foreign_keys(connection)
26
13
  end
27
14
  end
28
15
  end
@@ -9,20 +9,7 @@ module BetterStructureSql
9
9
  # @param connection [ActiveRecord::ConnectionAdapters::AbstractAdapter] Database connection
10
10
  # @return [Array<Hash>] Array of function metadata hashes
11
11
  def fetch_functions(connection)
12
- adapter = get_adapter(connection)
13
- adapter.fetch_functions(connection)
14
- rescue StandardError => e
15
- warn "Warning: Failed to fetch functions: #{e.message}"
16
- []
17
- end
18
-
19
- private
20
-
21
- def get_adapter(connection)
22
- @get_adapter ||= Adapters::Registry.adapter_for(
23
- connection,
24
- adapter_override: BetterStructureSql.configuration.adapter
25
- )
12
+ adapter_for(connection).fetch_functions(connection)
26
13
  end
27
14
  end
28
15
  end
@@ -9,20 +9,7 @@ module BetterStructureSql
9
9
  # @param connection [ActiveRecord::ConnectionAdapters::AbstractAdapter] Database connection
10
10
  # @return [Array<Hash>] Array of index metadata hashes
11
11
  def fetch_indexes(connection)
12
- adapter = get_adapter(connection)
13
- adapter.fetch_indexes(connection)
14
- rescue StandardError => e
15
- warn "Warning: Failed to fetch indexes: #{e.message}"
16
- []
17
- end
18
-
19
- private
20
-
21
- def get_adapter(connection)
22
- @get_adapter ||= Adapters::Registry.adapter_for(
23
- connection,
24
- adapter_override: BetterStructureSql.configuration.adapter
25
- )
12
+ adapter_for(connection).fetch_indexes(connection)
26
13
  end
27
14
  end
28
15
  end
@@ -9,20 +9,7 @@ module BetterStructureSql
9
9
  # @param connection [ActiveRecord::ConnectionAdapters::AbstractAdapter] Database connection
10
10
  # @return [Array<Hash>] Array of sequence metadata hashes
11
11
  def fetch_sequences(connection)
12
- adapter = get_adapter(connection)
13
- adapter.fetch_sequences(connection)
14
- rescue StandardError => e
15
- warn "Warning: Failed to fetch sequences: #{e.message}"
16
- []
17
- end
18
-
19
- private
20
-
21
- def get_adapter(connection)
22
- @get_adapter ||= Adapters::Registry.adapter_for(
23
- connection,
24
- adapter_override: BetterStructureSql.configuration.adapter
25
- )
12
+ adapter_for(connection).fetch_sequences(connection)
26
13
  end
27
14
  end
28
15
  end
@@ -9,20 +9,7 @@ module BetterStructureSql
9
9
  # @param connection [ActiveRecord::ConnectionAdapters::AbstractAdapter] Database connection
10
10
  # @return [Array<Hash>] Array of table metadata hashes
11
11
  def fetch_tables(connection)
12
- adapter = get_adapter(connection)
13
- adapter.fetch_tables(connection)
14
- rescue StandardError => e
15
- warn "Warning: Failed to fetch tables: #{e.message}"
16
- []
17
- end
18
-
19
- private
20
-
21
- def get_adapter(connection)
22
- @get_adapter ||= Adapters::Registry.adapter_for(
23
- connection,
24
- adapter_override: BetterStructureSql.configuration.adapter
25
- )
12
+ adapter_for(connection).fetch_tables(connection)
26
13
  end
27
14
  end
28
15
  end
@@ -9,20 +9,7 @@ module BetterStructureSql
9
9
  # @param connection [ActiveRecord::ConnectionAdapters::AbstractAdapter] Database connection
10
10
  # @return [Array<Hash>] Array of trigger metadata hashes
11
11
  def fetch_triggers(connection)
12
- adapter = get_adapter(connection)
13
- adapter.fetch_triggers(connection)
14
- rescue StandardError => e
15
- warn "Warning: Failed to fetch triggers: #{e.message}"
16
- []
17
- end
18
-
19
- private
20
-
21
- def get_adapter(connection)
22
- @get_adapter ||= Adapters::Registry.adapter_for(
23
- connection,
24
- adapter_override: BetterStructureSql.configuration.adapter
25
- )
12
+ adapter_for(connection).fetch_triggers(connection)
26
13
  end
27
14
  end
28
15
  end
@@ -9,11 +9,7 @@ module BetterStructureSql
9
9
  # @param connection [ActiveRecord::ConnectionAdapters::AbstractAdapter] Database connection
10
10
  # @return [Array<Hash>] Array of type metadata hashes
11
11
  def fetch_custom_types(connection)
12
- adapter = get_adapter(connection)
13
- adapter.fetch_custom_types(connection)
14
- rescue StandardError => e
15
- warn "Warning: Failed to fetch custom types: #{e.message}"
16
- []
12
+ adapter_for(connection).fetch_custom_types(connection)
17
13
  end
18
14
 
19
15
  # Fetches enum types only
@@ -23,15 +19,6 @@ module BetterStructureSql
23
19
  def fetch_enums(connection)
24
20
  fetch_custom_types(connection).select { |t| t[:type] == 'enum' }
25
21
  end
26
-
27
- private
28
-
29
- def get_adapter(connection)
30
- @get_adapter ||= Adapters::Registry.adapter_for(
31
- connection,
32
- adapter_override: BetterStructureSql.configuration.adapter
33
- )
34
- end
35
22
  end
36
23
  end
37
24
  end
@@ -9,11 +9,7 @@ module BetterStructureSql
9
9
  # @param connection [ActiveRecord::ConnectionAdapters::AbstractAdapter] Database connection
10
10
  # @return [Array<Hash>] Array of view metadata hashes
11
11
  def fetch_views(connection)
12
- adapter = get_adapter(connection)
13
- adapter.fetch_views(connection)
14
- rescue StandardError => e
15
- warn "Warning: Failed to fetch views: #{e.message}"
16
- []
12
+ adapter_for(connection).fetch_views(connection)
17
13
  end
18
14
 
19
15
  # Fetches materialized views
@@ -21,20 +17,7 @@ module BetterStructureSql
21
17
  # @param connection [ActiveRecord::ConnectionAdapters::AbstractAdapter] Database connection
22
18
  # @return [Array<Hash>] Array of materialized view metadata hashes
23
19
  def fetch_materialized_views(connection)
24
- adapter = get_adapter(connection)
25
- adapter.fetch_materialized_views(connection)
26
- rescue StandardError => e
27
- warn "Warning: Failed to fetch materialized views: #{e.message}"
28
- []
29
- end
30
-
31
- private
32
-
33
- def get_adapter(connection)
34
- @get_adapter ||= Adapters::Registry.adapter_for(
35
- connection,
36
- adapter_override: BetterStructureSql.configuration.adapter
37
- )
20
+ adapter_for(connection).fetch_materialized_views(connection)
38
21
  end
39
22
  end
40
23
  end
@@ -14,7 +14,8 @@ module BetterStructureSql
14
14
  # Introspection facade for database metadata extraction
15
15
  #
16
16
  # Provides a unified interface for querying database objects across
17
- # all supported adapters (PostgreSQL, MySQL, SQLite).
17
+ # all supported adapters (PostgreSQL, MySQL, SQLite). Introspection errors
18
+ # propagate: a dump silently missing objects is worse than a failed dump.
18
19
  module Introspection
19
20
  class << self
20
21
  include Extensions
@@ -26,6 +27,16 @@ module BetterStructureSql
26
27
  include Views
27
28
  include Functions
28
29
  include Triggers
30
+
31
+ private
32
+
33
+ # Adapter for the given connection (cached per connection by the Registry)
34
+ #
35
+ # @param connection [ActiveRecord::ConnectionAdapters::AbstractAdapter] Database connection
36
+ # @return [Adapters::BaseAdapter] Adapter instance
37
+ def adapter_for(connection)
38
+ Adapters::Registry.adapter_for(connection, adapter_override: BetterStructureSql.configuration.adapter)
39
+ end
29
40
  end
30
41
  end
31
42
  end