Module: BetterAuth::SQLMigration
- Defined in:
- lib/better_auth/sql_migration.rb
Defined Under Namespace
Classes: UnsupportedAdapterError
Constant Summary collapse
- DEFAULT_MIGRATIONS_PATH =
"db/better_auth/migrate"- MISSING_MIGRATIONS_TABLE_MESSAGES =
[ /no such table/i, /relation .* does not exist/i, /table .* doesn't exist/i, /undefined table/i, /invalid object name/i ].freeze
Class Method Summary collapse
- .applied_migrations(connection, dialect) ⇒ Object
- .auth_for(value) ⇒ Object
- .camelize_lower(value) ⇒ Object
- .configuration_for(options) ⇒ Object
- .current_schema(connection, dialect) ⇒ Object
- .dollar_quote_tag_at(sql, index) ⇒ Object
- .empty_index_metadata ⇒ Object
- .ensure_schema_migrations!(connection, dialect) ⇒ Object
- .execute_sql(connection, sql) ⇒ Object
- .filtered_unique_index?(attributes, dialect) ⇒ Boolean
- .generate(options, dialect:, generator:, migrations_path: DEFAULT_MIGRATIONS_PATH, timestamp: Time.now.utc.strftime("%Y%m%d%H%M%S"), connection: nil) ⇒ Object
- .index_change(table_name, column, attributes, dialect:, unique:) ⇒ Object
- .index_name(table_name, column, attributes, dialect) ⇒ Object
- .index_present?(table, column, unique:) ⇒ Boolean
- .indexable_field?(attributes, dialect) ⇒ Boolean
- .information_schema(connection, dialect, columns_sql, indexes_sql) ⇒ Object
- .literal(value) ⇒ Object
- .matching_type?(actual_type, expected_type, dialect) ⇒ Boolean
- .migrate(auth_or_options, migrations_path: DEFAULT_MIGRATIONS_PATH) ⇒ Object
- .migrate_pending(auth_or_options) ⇒ Object
- .missing_schema_migrations_table?(error) ⇒ Boolean
- .mssql_columns_sql ⇒ Object
- .mssql_indexes_sql ⇒ Object
- .mysql_columns_sql ⇒ Object
- .mysql_indexes_sql ⇒ Object
- .normalize_dialect(value) ⇒ Object
- .physical_name(value) ⇒ Object
- .plan(options, connection:, dialect:) ⇒ Object
- .plan_from_existing(options, existing:, dialect:) ⇒ Object
- .postgres_columns_sql ⇒ Object
- .postgres_indexes_sql ⇒ Object
- .quote(identifier, dialect) ⇒ Object
- .record_migration(connection, dialect, version) ⇒ Object
- .render(options, dialect:, generator:) ⇒ Object
- .render_pending(options, connection:, dialect:, generator:) ⇒ Object
- .row_value(row, key) ⇒ Object
- .scan_block_comment(buffer, index, char, next_char, quote, line_comment, dollar_tag) ⇒ Object
- .scan_dollar_quote(sql, buffer, index, char, quote, line_comment, block_comment, dollar_tag) ⇒ Object
- .scan_line_comment(buffer, index, char) ⇒ Object
- .scan_quoted_string(sql, buffer, index, char, quote, line_comment, block_comment, dollar_tag) ⇒ Object
- .scan_sql_character(sql, buffer, index, state, output) ⇒ Object
- .split_sql_statements(sql) ⇒ Object
- .sqlite_schema(connection) ⇒ Object
- .statements(sql) ⇒ Object
- .truthy_database_value?(value) ⇒ Boolean
Class Method Details
.applied_migrations(connection, dialect) ⇒ Object
203 204 205 206 207 208 209 210 211 212 |
# File 'lib/better_auth/sql_migration.rb', line 203 def applied_migrations(connection, dialect) rows = execute_sql(connection, "SELECT #{quote("version", dialect)} FROM #{quote("better_auth_schema_migrations", dialect)};") Array(rows).map { |row| row["version"] || row[:version] } rescue UnsupportedAdapterError raise rescue => error raise error unless missing_schema_migrations_table?(error) [] end |
.auth_for(value) ⇒ Object
183 184 185 186 187 |
# File 'lib/better_auth/sql_migration.rb', line 183 def auth_for(value) return value if value.is_a?(BetterAuth::Auth) BetterAuth.auth(value) end |
.camelize_lower(value) ⇒ Object
446 447 448 449 |
# File 'lib/better_auth/sql_migration.rb', line 446 def camelize_lower(value) parts = value.to_s.split("_") ([parts.first] + parts.drop(1).map(&:capitalize)).join end |
.configuration_for(options) ⇒ Object
176 177 178 179 180 181 |
# File 'lib/better_auth/sql_migration.rb', line 176 def configuration_for() return . if .is_a?(BetterAuth::Auth) return if .is_a?(BetterAuth::Configuration) BetterAuth::Configuration.new() end |
.current_schema(connection, dialect) ⇒ Object
366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/better_auth/sql_migration.rb', line 366 def current_schema(connection, dialect) case dialect when :sqlite sqlite_schema(connection) when :postgres information_schema(connection, dialect, postgres_columns_sql, postgres_indexes_sql) when :mysql information_schema(connection, dialect, mysql_columns_sql, mysql_indexes_sql) when :mssql information_schema(connection, dialect, mssql_columns_sql, mssql_indexes_sql) else raise UnsupportedAdapterError, "Unsupported SQL dialect for Better Auth migrations: #{dialect}" end end |
.dollar_quote_tag_at(sql, index) ⇒ Object
334 335 336 337 |
# File 'lib/better_auth/sql_migration.rb', line 334 def dollar_quote_tag_at(sql, index) match = sql[index..]&.match(/\A\$[A-Za-z_][A-Za-z0-9_]*\$|\A\$\$/) match&.[](0) end |
.empty_index_metadata ⇒ Object
451 452 453 |
# File 'lib/better_auth/sql_migration.rb', line 451 def {names: Set.new, columns: Set.new, unique_columns: Set.new} end |
.ensure_schema_migrations!(connection, dialect) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/better_auth/sql_migration.rb', line 189 def ensure_schema_migrations!(connection, dialect) sql = case dialect when :postgres, :sqlite %(CREATE TABLE IF NOT EXISTS #{quote("better_auth_schema_migrations", dialect)} (#{quote("version", dialect)} text PRIMARY KEY);) when :mysql %(CREATE TABLE IF NOT EXISTS #{quote("better_auth_schema_migrations", dialect)} (#{quote("version", dialect)} varchar(191) PRIMARY KEY) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;) when :mssql %(IF OBJECT_ID(N'#{quote("better_auth_schema_migrations", dialect)}', N'U') IS NULL CREATE TABLE #{quote("better_auth_schema_migrations", dialect)} (#{quote("version", dialect)} varchar(255) PRIMARY KEY);) else raise UnsupportedAdapterError, "Unsupported SQL dialect for Better Auth migrations: #{dialect}" end execute_sql(connection, sql) end |
.execute_sql(connection, sql) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/better_auth/sql_migration.rb', line 219 def execute_sql(connection, sql) statements(sql).each_with_object([]) do |statement, results| result = if connection.respond_to?(:exec) connection.exec(statement) elsif connection.respond_to?(:execute) connection.execute(statement) elsif connection.respond_to?(:query) connection.query(statement) else raise UnsupportedAdapterError, "SQL connection does not support exec, execute, or query" end results.concat(result.to_a) if result.respond_to?(:to_a) end end |
.filtered_unique_index?(attributes, dialect) ⇒ Boolean
469 470 471 |
# File 'lib/better_auth/sql_migration.rb', line 469 def filtered_unique_index?(attributes, dialect) dialect == :mssql && attributes[:unique] && !attributes[:required] end |
.generate(options, dialect:, generator:, migrations_path: DEFAULT_MIGRATIONS_PATH, timestamp: Time.now.utc.strftime("%Y%m%d%H%M%S"), connection: nil) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/better_auth/sql_migration.rb', line 120 def generate(, dialect:, generator:, migrations_path: DEFAULT_MIGRATIONS_PATH, timestamp: Time.now.utc.strftime("%Y%m%d%H%M%S"), connection: nil) dialect = normalize_dialect(dialect) FileUtils.mkdir_p(migrations_path) path = File.join(migrations_path, "#{}_create_better_auth_tables.sql") return path if File.exist?(path) sql = if connection render_pending(, connection: connection, dialect: dialect, generator: generator) else render(, dialect: dialect, generator: generator) end return nil if sql.empty? File.write(path, sql) path end |
.index_change(table_name, column, attributes, dialect:, unique:) ⇒ Object
455 456 457 458 459 460 461 462 463 |
# File 'lib/better_auth/sql_migration.rb', line 455 def index_change(table_name, column, attributes, dialect:, unique:) BetterAuth::MigrationPlan::IndexChange.new( table_name: table_name, field_name: column, name: index_name(table_name, column, attributes, dialect), unique: unique, field: attributes ) end |
.index_name(table_name, column, attributes, dialect) ⇒ Object
473 474 475 |
# File 'lib/better_auth/sql_migration.rb', line 473 def index_name(table_name, column, attributes, dialect) filtered_unique_index?(attributes, dialect) ? "uniq_#{table_name}_#{column}" : "index_#{table_name}_on_#{column}" end |
.index_present?(table, column, unique:) ⇒ Boolean
477 478 479 480 481 482 483 484 |
# File 'lib/better_auth/sql_migration.rb', line 477 def index_present?(table, column, unique:) indexes = table.fetch(:indexes) if unique indexes[:unique_columns].include?(column) || indexes[:names].include?("index_#{table.fetch(:name, "")}_on_#{column}") else indexes[:columns].include?(column) end end |
.indexable_field?(attributes, dialect) ⇒ Boolean
465 466 467 |
# File 'lib/better_auth/sql_migration.rb', line 465 def indexable_field?(attributes, dialect) attributes[:index] || filtered_unique_index?(attributes, dialect) end |
.information_schema(connection, dialect, columns_sql, indexes_sql) ⇒ Object
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'lib/better_auth/sql_migration.rb', line 403 def information_schema(connection, dialect, columns_sql, indexes_sql) schema = {} execute_sql(connection, columns_sql).each do |row| table_name = row_value(row, "table_name") column_name = row_value(row, "column_name") data_type = row_value(row, "data_type") schema[table_name] ||= {name: table_name, columns: {}, indexes: } schema[table_name][:columns][column_name] = data_type end execute_sql(connection, indexes_sql).each do |row| table_name = row_value(row, "table_name") column_name = row_value(row, "column_name") index_name = row_value(row, "index_name") unique_value = row_value(row, "unique") || row_value(row, "is_unique") non_unique_value = row_value(row, "non_unique") unique = (!unique_value.nil?) ? truthy_database_value?(unique_value) : non_unique_value.to_i == 0 schema[table_name] ||= {name: table_name, columns: {}, indexes: } schema[table_name][:indexes][:names] << index_name if index_name schema[table_name][:indexes][:columns] << column_name if column_name schema[table_name][:indexes][:unique_columns] << column_name if column_name && !!unique end schema end |
.literal(value) ⇒ Object
348 349 350 |
# File 'lib/better_auth/sql_migration.rb', line 348 def literal(value) "'#{value.to_s.gsub("'", "''")}'" end |
.matching_type?(actual_type, expected_type, dialect) ⇒ Boolean
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 |
# File 'lib/better_auth/sql_migration.rb', line 486 def matching_type?(actual_type, expected_type, dialect) normalized = actual_type.to_s.downcase.split("(").first.strip type = expected_type.to_s expected = case type when "string" %w[text varchar character varying char uuid nvarchar uniqueidentifier] when "number" %w[integer int int4 bigint smallint numeric real decimal float double] when "boolean" %w[bool boolean integer tinyint bit smallint] when "date" %w[timestamptz timestamp date datetime datetime2] when "json", "string[]", "number[]" (dialect == :postgres) ? %w[json jsonb text] : %w[json text varchar nvarchar] else [normalized] end expected.include?(normalized) end |
.migrate(auth_or_options, migrations_path: DEFAULT_MIGRATIONS_PATH) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/better_auth/sql_migration.rb', line 137 def migrate(, migrations_path: DEFAULT_MIGRATIONS_PATH) auth = auth_for() adapter = auth.context.adapter unless adapter.respond_to?(:dialect) && adapter.respond_to?(:connection) raise UnsupportedAdapterError, "Better Auth SQL migrations require core SQL adapters with connection and dialect support" end connection = adapter.connection dialect = normalize_dialect(adapter.dialect) files = Dir[File.join(migrations_path, "*.sql")].sort ensure_schema_migrations!(connection, dialect) applied = applied_migrations(connection, dialect) files.reject { |file| applied.include?(File.basename(file)) }.each do |file| execute_sql(connection, File.read(file)) record_migration(connection, dialect, File.basename(file)) end end |
.migrate_pending(auth_or_options) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/better_auth/sql_migration.rb', line 156 def migrate_pending() auth = auth_for() adapter = auth.context.adapter unless adapter.respond_to?(:dialect) && adapter.respond_to?(:connection) raise UnsupportedAdapterError, "Better Auth SQL migrations require core SQL adapters with connection and dialect support" end connection = adapter.connection dialect = normalize_dialect(adapter.dialect) sql = render_pending(auth., connection: connection, dialect: dialect, generator: "better_auth") return false if sql.empty? if adapter.respond_to?(:transaction) adapter.transaction { execute_sql(connection, sql) } else execute_sql(connection, sql) end true end |
.missing_schema_migrations_table?(error) ⇒ Boolean
339 340 341 342 |
# File 'lib/better_auth/sql_migration.rb', line 339 def missing_schema_migrations_table?(error) = error..to_s MISSING_MIGRATIONS_TABLE_MESSAGES.any? { |pattern| .match?(pattern) } end |
.mssql_columns_sql ⇒ Object
542 543 544 545 546 547 548 549 |
# File 'lib/better_auth/sql_migration.rb', line 542 def mssql_columns_sql <<~SQL SELECT t.name AS table_name, c.name AS column_name, ty.name AS data_type FROM sys.tables t JOIN sys.columns c ON c.object_id = t.object_id JOIN sys.types ty ON ty.user_type_id = c.user_type_id; SQL end |
.mssql_indexes_sql ⇒ Object
551 552 553 554 555 556 557 558 559 560 |
# File 'lib/better_auth/sql_migration.rb', line 551 def mssql_indexes_sql <<~SQL SELECT t.name AS table_name, c.name AS column_name, i.name AS index_name, i.is_unique FROM sys.tables t JOIN sys.indexes i ON i.object_id = t.object_id JOIN sys.index_columns ic ON ic.object_id = t.object_id AND ic.index_id = i.index_id JOIN sys.columns c ON c.object_id = t.object_id AND c.column_id = ic.column_id WHERE i.name IS NOT NULL; SQL end |
.mysql_columns_sql ⇒ Object
526 527 528 529 530 531 532 |
# File 'lib/better_auth/sql_migration.rb', line 526 def mysql_columns_sql <<~SQL SELECT table_name, column_name, data_type FROM information_schema.columns WHERE table_schema = DATABASE(); SQL end |
.mysql_indexes_sql ⇒ Object
534 535 536 537 538 539 540 |
# File 'lib/better_auth/sql_migration.rb', line 534 def mysql_indexes_sql <<~SQL SELECT table_name, column_name, index_name, CASE WHEN non_unique = 0 THEN 1 ELSE 0 END AS is_unique FROM information_schema.statistics WHERE table_schema = DATABASE(); SQL end |
.normalize_dialect(value) ⇒ Object
352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/better_auth/sql_migration.rb', line 352 def normalize_dialect(value) dialect = case value.to_s.downcase when "postgresql" :postgres when "sqlite3" :sqlite else value.to_sym end return dialect if [:postgres, :sqlite, :mysql, :mssql].include?(dialect) raise UnsupportedAdapterError, "Unsupported SQL dialect for Better Auth migrations: #{dialect}" end |
.physical_name(value) ⇒ Object
562 563 564 |
# File 'lib/better_auth/sql_migration.rb', line 562 def physical_name(value) BetterAuth::Schema.send(:physical_name, value) end |
.plan(options, connection:, dialect:) ⇒ Object
34 35 36 37 |
# File 'lib/better_auth/sql_migration.rb', line 34 def plan(, connection:, dialect:) dialect = normalize_dialect(dialect) plan_from_existing(, existing: current_schema(connection, dialect), dialect: dialect) end |
.plan_from_existing(options, existing:, dialect:) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/better_auth/sql_migration.rb', line 39 def plan_from_existing(, existing:, dialect:) dialect = normalize_dialect(dialect) config = configuration_for() desired = BetterAuth::Schema.auth_tables(config) to_create = [] to_add = [] to_index = [] warnings = [] desired.each do |logical_name, table| table_name = table.fetch(:model_name) existing_table = existing[table_name] unless existing_table to_create << BetterAuth::MigrationPlan::TableChange.new( logical_name: logical_name, table_name: table_name, table: table, order: table[:order] || Float::INFINITY ) table.fetch(:fields).each do |field, attributes| next unless indexable_field?(attributes, dialect) column = attributes[:field_name] || physical_name(field) to_index << index_change(table_name, column, attributes, dialect: dialect, unique: !!attributes[:unique]) end next end missing_fields = {} table.fetch(:fields).each do |field, attributes| column = attributes[:field_name] || physical_name(field) existing_type = existing_table.fetch(:columns)[column] if existing_type.nil? missing_fields[field] = attributes elsif !matching_type?(existing_type, attributes[:type], dialect) warnings << "Type mismatch for #{table_name}.#{column}: expected #{attributes[:type]} but found #{existing_type.to_s.downcase}" end next unless attributes[:index] || attributes[:unique] next if index_present?(existing_table, column, unique: !!attributes[:unique]) to_index << index_change(table_name, column, attributes, dialect: dialect, unique: !!attributes[:unique]) end next if missing_fields.empty? to_add << BetterAuth::MigrationPlan::FieldChange.new( logical_name: logical_name, table_name: table_name, fields: missing_fields, table: table, order: table[:order] || Float::INFINITY ) end BetterAuth::MigrationPlan::Plan.new( to_create: to_create.sort_by(&:order), to_add: to_add.sort_by(&:order), to_index: to_index, warnings: warnings, dialect: dialect, tables: desired ) end |
.postgres_columns_sql ⇒ Object
506 507 508 509 510 511 512 |
# File 'lib/better_auth/sql_migration.rb', line 506 def postgres_columns_sql <<~SQL SELECT table_name, column_name, data_type FROM information_schema.columns WHERE table_schema = current_schema(); SQL end |
.postgres_indexes_sql ⇒ Object
514 515 516 517 518 519 520 521 522 523 524 |
# File 'lib/better_auth/sql_migration.rb', line 514 def postgres_indexes_sql <<~SQL SELECT t.relname AS table_name, a.attname AS column_name, i.relname AS index_name, ix.indisunique AS unique FROM pg_class t JOIN pg_index ix ON t.oid = ix.indrelid JOIN pg_class i ON i.oid = ix.indexrelid JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = ANY(ix.indkey) JOIN pg_namespace n ON n.oid = t.relnamespace WHERE t.relkind = 'r' AND n.nspname = current_schema(); SQL end |
.quote(identifier, dialect) ⇒ Object
344 345 346 |
# File 'lib/better_auth/sql_migration.rb', line 344 def quote(identifier, dialect) BetterAuth::Schema::SQL.quote(identifier, dialect) end |
.record_migration(connection, dialect, version) ⇒ Object
214 215 216 217 |
# File 'lib/better_auth/sql_migration.rb', line 214 def record_migration(connection, dialect, version) sql = "INSERT INTO #{quote("better_auth_schema_migrations", dialect)} (#{quote("version", dialect)}) VALUES (#{literal(version)});" execute_sql(connection, sql) end |
.render(options, dialect:, generator:) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/better_auth/sql_migration.rb', line 21 def render(, dialect:, generator:) dialect = normalize_dialect(dialect) config = configuration_for() statements = BetterAuth::Schema::SQL.create_statements(config, dialect: dialect) [ "-- Generated by #{generator}", "-- Dialect: #{dialect}", "", statements.join("\n\n"), "" ].join("\n") end |
.render_pending(options, connection:, dialect:, generator:) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/better_auth/sql_migration.rb', line 105 def render_pending(, connection:, dialect:, generator:) dialect = normalize_dialect(dialect) migration_plan = plan(, connection: connection, dialect: dialect) statements = BetterAuth::Schema::SQL.pending_statements(migration_plan) return "" if statements.empty? [ "-- Generated by #{generator}", "-- Dialect: #{dialect}", "", statements.join("\n\n"), "" ].join("\n") end |
.row_value(row, key) ⇒ Object
427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/better_auth/sql_migration.rb', line 427 def row_value(row, key) candidates = [ key, key.to_sym, key.upcase, key.upcase.to_sym, camelize_lower(key), camelize_lower(key).to_sym ] candidates.each { |candidate| return row[candidate] if row.key?(candidate) } nil end |
.scan_block_comment(buffer, index, char, next_char, quote, line_comment, dollar_tag) ⇒ Object
302 303 304 305 306 307 308 309 310 |
# File 'lib/better_auth/sql_migration.rb', line 302 def scan_block_comment(buffer, index, char, next_char, quote, line_comment, dollar_tag) buffer << char if char == "*" && next_char == "/" buffer << next_char [index + 2, quote, line_comment, false, dollar_tag] else [index + 1, quote, line_comment, true, dollar_tag] end end |
.scan_dollar_quote(sql, buffer, index, char, quote, line_comment, block_comment, dollar_tag) ⇒ Object
312 313 314 315 316 317 318 319 320 |
# File 'lib/better_auth/sql_migration.rb', line 312 def scan_dollar_quote(sql, buffer, index, char, quote, line_comment, block_comment, dollar_tag) if sql[index, dollar_tag.length] == dollar_tag buffer << dollar_tag [index + dollar_tag.length, quote, line_comment, block_comment, nil] else buffer << char [index + 1, quote, line_comment, block_comment, dollar_tag] end end |
.scan_line_comment(buffer, index, char) ⇒ Object
297 298 299 300 |
# File 'lib/better_auth/sql_migration.rb', line 297 def scan_line_comment(buffer, index, char) buffer << char [index + 1] end |
.scan_quoted_string(sql, buffer, index, char, quote, line_comment, block_comment, dollar_tag) ⇒ Object
322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/better_auth/sql_migration.rb', line 322 def scan_quoted_string(sql, buffer, index, char, quote, line_comment, block_comment, dollar_tag) buffer << char if char == quote && sql[index + 1] == quote buffer << sql[index + 1] [index + 2, quote, line_comment, block_comment, dollar_tag] elsif char == quote [index + 1, nil, line_comment, block_comment, dollar_tag] else [index + 1, quote, line_comment, block_comment, dollar_tag] end end |
.scan_sql_character(sql, buffer, index, state, output) ⇒ Object
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/better_auth/sql_migration.rb', line 266 def scan_sql_character(sql, buffer, index, state, output) char = sql[index] next_char = sql[index + 1] quote = state[:quote] line_comment = state[:line_comment] block_comment = state[:block_comment] dollar_tag = state[:dollar_tag] return scan_line_comment(buffer, index, char) + [quote, false, block_comment, dollar_tag] if line_comment && char == "\n" return scan_line_comment(buffer, index, char) + [quote, true, block_comment, dollar_tag] if line_comment return scan_block_comment(buffer, index, char, next_char, quote, line_comment, dollar_tag) if block_comment return scan_dollar_quote(sql, buffer, index, char, quote, line_comment, block_comment, dollar_tag) if dollar_tag return scan_quoted_string(sql, buffer, index, char, quote, line_comment, block_comment, dollar_tag) if quote return [index + 2, quote, true, block_comment, dollar_tag].tap { buffer << char << next_char } if char == "-" && next_char == "-" return [index + 2, quote, line_comment, true, dollar_tag].tap { buffer << char << next_char } if char == "/" && next_char == "*" tag = dollar_quote_tag_at(sql, index) return [index + tag.length, quote, line_comment, block_comment, tag].tap { buffer << tag } if tag return [index + 1, char, line_comment, block_comment, dollar_tag].tap { buffer << char } if char == "'" || char == "\"" if char == ";" statement = buffer.strip output << statement unless statement.empty? buffer.clear return [index + 1, quote, line_comment, block_comment, dollar_tag] end buffer << char [index + 1, quote, line_comment, block_comment, dollar_tag] end |
.split_sql_statements(sql) ⇒ Object
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/better_auth/sql_migration.rb', line 242 def split_sql_statements(sql) output = [] buffer = +"" index = 0 quote = nil line_comment = false block_comment = false dollar_tag = nil while index < sql.length state = { quote: quote, line_comment: line_comment, block_comment: block_comment, dollar_tag: dollar_tag } index, quote, line_comment, block_comment, dollar_tag = scan_sql_character(sql, buffer, index, state, output) end tail = buffer.strip output << tail unless tail.empty? output end |
.sqlite_schema(connection) ⇒ Object
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/better_auth/sql_migration.rb', line 381 def sqlite_schema(connection) table_rows = execute_sql(connection, "SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%';") table_rows.each_with_object({}) do |row, schema| table_name = row["name"] || row[:name] columns = execute_sql(connection, "PRAGMA table_info(#{quote(table_name, :sqlite)});").each_with_object({}) do |column, result| result[column["name"] || column[:name]] = column["type"] || column[:type] end indexes = execute_sql(connection, "PRAGMA index_list(#{quote(table_name, :sqlite)});").each do |index| index_name = index["name"] || index[:name] unique = (index["unique"] || index[:unique]).to_i == 1 indexes[:names] << index_name execute_sql(connection, "PRAGMA index_info(#{quote(index_name, :sqlite)});").each do |field| column = field["name"] || field[:name] indexes[:columns] << column indexes[:unique_columns] << column if unique end end schema[table_name] = {name: table_name, columns: columns, indexes: indexes} end end |
.statements(sql) ⇒ Object
235 236 237 238 239 240 |
# File 'lib/better_auth/sql_migration.rb', line 235 def statements(sql) normalized = sql.to_s.gsub("\r\n", "\n").strip return [] if normalized.empty? split_sql_statements(normalized) end |
.truthy_database_value?(value) ⇒ Boolean
440 441 442 443 444 |
# File 'lib/better_auth/sql_migration.rb', line 440 def truthy_database_value?(value) return value if value == true || value == false value.to_i != 0 end |