Module: BetterAuth::Schema::SQL
- Defined in:
- lib/better_auth/schema/sql.rb
Class Method Summary collapse
- .add_column_statement(table_name, logical_field, attributes, dialect) ⇒ Object
- .add_postgres_id_column_statements(table_name) ⇒ Object
- .column_definition(table_name, logical_field, attributes, dialect) ⇒ Object
- .create_statements(options, dialect:) ⇒ Object
- .create_table_statement(logical_name, table, dialect, tables = nil, delete_actions: nil) ⇒ Object
- .default_sql(attributes, dialect) ⇒ Object
- .field_constraints(table_name, logical_field, attributes, dialect, tables = nil, delete_actions: nil) ⇒ Object
- .filtered_unique_index?(attributes, dialect) ⇒ Boolean
- .foreign_key_constraint(table_name, column, reference, dialect, tables = nil, delete_actions: nil) ⇒ Object
- .foreign_key_target_field(reference, target_table) ⇒ Object
- .foreign_key_target_table(reference, tables) ⇒ Object
- .index_statement(table_name, column, name, dialect, unique: false, where_not_null: false) ⇒ Object
- .index_statements(table, dialect) ⇒ Object
- .indexed_string_sql_type(logical_field, attributes, dialect) ⇒ Object
- .logical_name(logical_field, attributes) ⇒ Object
- .mssql_cascade_conflict?(graph, parent, child) ⇒ Boolean
- .mssql_cascading_action?(action) ⇒ Boolean
- .mssql_delete_actions(tables, dialect) ⇒ Object
- .mssql_reachable?(graph, source, target, visited = {}) ⇒ Boolean
- .mssql_required_set_options ⇒ Object
- .pending_statements(plan) ⇒ Object
- .physical_name(value) ⇒ Object
- .quote(identifier, dialect) ⇒ Object
- .sql_type(logical_field, attributes, dialect) ⇒ Object
- .unique_constraint(table_name, column, dialect) ⇒ Object
Class Method Details
.add_column_statement(table_name, logical_field, attributes, dialect) ⇒ Object
110 111 112 113 |
# File 'lib/better_auth/schema/sql.rb', line 110 def add_column_statement(table_name, logical_field, attributes, dialect) keyword = (dialect == :mssql) ? "ADD" : "ADD COLUMN" %(ALTER TABLE #{quote(table_name, dialect)} #{keyword} #{column_definition(table_name, logical_field, attributes, dialect)};) end |
.add_postgres_id_column_statements(table_name) ⇒ Object
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/better_auth/schema/sql.rb', line 115 def add_postgres_id_column_statements(table_name) quoted_table = quote(table_name, :postgres) quoted_id = quote("id", :postgres) [ %(ALTER TABLE #{quoted_table} ADD COLUMN #{quoted_id} text;), %(UPDATE #{quoted_table} SET #{quoted_id} = md5(random()::text || clock_timestamp()::text || ctid::text) WHERE #{quoted_id} IS NULL;), %(ALTER TABLE #{quoted_table} ALTER COLUMN #{quoted_id} SET NOT NULL;), %(ALTER TABLE #{quoted_table} ADD PRIMARY KEY (#{quoted_id});) ] end |
.column_definition(table_name, logical_field, attributes, dialect) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/better_auth/schema/sql.rb', line 66 def column_definition(table_name, logical_field, attributes, dialect) column = quote(attributes[:field_name] || physical_name(logical_field), dialect) parts = [column, sql_type(logical_name(logical_field, attributes), attributes, dialect)] parts << "PRIMARY KEY" if logical_name(logical_field, attributes) == "id" if attributes[:required] parts << "NOT NULL" elsif dialect == :mssql parts << "NULL" end default = default_sql(attributes, dialect) parts << "DEFAULT #{default}" if default parts.join(" ") end |
.create_statements(options, dialect:) ⇒ Object
8 9 10 11 12 13 14 15 16 |
# File 'lib/better_auth/schema/sql.rb', line 8 def create_statements(, dialect:) dialect = dialect.to_sym tables = Schema.migration_tables() delete_actions = mssql_delete_actions(tables, dialect) statements = tables.map do |logical_name, table| create_table_statement(logical_name, table, dialect, tables, delete_actions: delete_actions) end statements.concat(tables.flat_map { |_logical_name, table| index_statements(table, dialect) }) end |
.create_table_statement(logical_name, table, dialect, tables = nil, delete_actions: nil) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/better_auth/schema/sql.rb', line 44 def create_table_statement(logical_name, table, dialect, tables = nil, delete_actions: nil) table_name = table.fetch(:model_name) columns = table.fetch(:fields).map do |logical_field, attributes| column_definition(table_name, logical_field, attributes, dialect) end constraints = table.fetch(:fields).flat_map do |logical_field, attributes| field_constraints(table_name, logical_field, attributes, dialect, tables, delete_actions: delete_actions) end body = (columns + constraints).join(",\n ") case dialect when :postgres, :sqlite %(CREATE TABLE IF NOT EXISTS #{quote(table_name, dialect)} (\n #{body}\n);) when :mysql %(CREATE TABLE IF NOT EXISTS #{quote(table_name, dialect)} (\n #{body}\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;) when :mssql %(#{}\nIF OBJECT_ID(N'#{quote(table_name, dialect)}', N'U') IS NULL\nCREATE TABLE #{quote(table_name, dialect)} (\n #{body}\n);) else raise ArgumentError, "Unsupported SQL dialect: #{dialect}" end end |
.default_sql(attributes, dialect) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/better_auth/schema/sql.rb', line 212 def default_sql(attributes, dialect) default = attributes[:default_value] return unless default == false || default == true || default.is_a?(Numeric) || default.is_a?(String) || default.respond_to?(:call) if attributes[:type] == "date" && default.respond_to?(:call) return (dialect == :mysql) ? "CURRENT_TIMESTAMP(6)" : "CURRENT_TIMESTAMP" end case default when true (dialect == :mysql || dialect == :sqlite || dialect == :mssql) ? "1" : "true" when false (dialect == :mysql || dialect == :sqlite || dialect == :mssql) ? "0" : "false" when Numeric default.to_s when String "'#{default.gsub("'", "''")}'" end end |
.field_constraints(table_name, logical_field, attributes, dialect, tables = nil, delete_actions: nil) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/better_auth/schema/sql.rb', line 80 def field_constraints(table_name, logical_field, attributes, dialect, tables = nil, delete_actions: nil) constraints = [] column = attributes[:field_name] || physical_name(logical_field) if attributes[:unique] && logical_name(logical_field, attributes) != "id" && !(dialect == :mssql && !attributes[:required]) constraints << unique_constraint(table_name, column, dialect) end reference = attributes[:references] if reference constraints << foreign_key_constraint(table_name, column, reference, dialect, tables, delete_actions: delete_actions) end constraints end |
.filtered_unique_index?(attributes, dialect) ⇒ Boolean
139 140 141 |
# File 'lib/better_auth/schema/sql.rb', line 139 def filtered_unique_index?(attributes, dialect) dialect == :mssql && attributes[:unique] && !attributes[:required] end |
.foreign_key_constraint(table_name, column, reference, dialect, tables = nil, delete_actions: nil) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/better_auth/schema/sql.rb', line 243 def foreign_key_constraint(table_name, column, reference, dialect, tables = nil, delete_actions: nil) target_table = foreign_key_target_table(reference, tables) target_model = target_table&.fetch(:model_name) || reference.fetch(:model) target_field = foreign_key_target_field(reference, target_table) delete_action = delete_actions&.fetch([table_name.to_s, column.to_s], reference[:on_delete]) || reference[:on_delete] on_delete = delete_action ? " ON DELETE #{delete_action.to_s.upcase}" : "" case dialect when :postgres, :sqlite %(FOREIGN KEY (#{quote(column, dialect)}) REFERENCES #{quote(target_model, dialect)} (#{quote(target_field, dialect)})#{on_delete}) when :mysql %(CONSTRAINT #{quote("fk_#{table_name}_#{column}", dialect)} FOREIGN KEY (#{quote(column, dialect)}) REFERENCES #{quote(target_model, dialect)} (#{quote(target_field, dialect)})#{on_delete}) when :mssql %(CONSTRAINT #{quote("fk_#{table_name}_#{column}", dialect)} FOREIGN KEY (#{quote(column, dialect)}) REFERENCES #{quote(target_model, dialect)} (#{quote(target_field, dialect)})#{on_delete}) end end |
.foreign_key_target_field(reference, target_table) ⇒ Object
317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/better_auth/schema/sql.rb', line 317 def foreign_key_target_field(reference, target_table) field = reference.fetch(:field).to_s return field unless target_table fields = target_table.fetch(:fields) attributes = fields.fetch(field, nil) return attributes[:field_name] || physical_name(field) if attributes if fields.each_value.any? { |data| data[:field_name].to_s == field } field else physical_name(field) end end |
.foreign_key_target_table(reference, tables) ⇒ Object
310 311 312 313 314 315 |
# File 'lib/better_auth/schema/sql.rb', line 310 def foreign_key_target_table(reference, tables) return unless tables model = reference.fetch(:model).to_s tables.fetch(model, nil) || tables.each_value.find { |table| table.fetch(:model_name).to_s == model } end |
.index_statement(table_name, column, name, dialect, unique: false, where_not_null: false) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/better_auth/schema/sql.rb', line 126 def index_statement(table_name, column, name, dialect, unique: false, where_not_null: false) unique_prefix = unique ? "UNIQUE " : "" case dialect when :postgres, :sqlite %(CREATE #{unique_prefix}INDEX IF NOT EXISTS #{quote(name, dialect)} ON #{quote(table_name, dialect)} (#{quote(column, dialect)});) when :mysql %(CREATE #{unique_prefix}INDEX #{quote(name, dialect)} ON #{quote(table_name, dialect)} (#{quote(column, dialect)});) when :mssql filter = where_not_null ? " WHERE #{quote(column, dialect)} IS NOT NULL" : "" %(#{}\nIF NOT EXISTS (SELECT name FROM sys.indexes WHERE name = '#{name.gsub("'", "''")}' AND object_id = OBJECT_ID(N'#{quote(table_name, dialect)}')) CREATE #{unique_prefix}INDEX #{quote(name, dialect)} ON #{quote(table_name, dialect)} (#{quote(column, dialect)})#{filter};) end end |
.index_statements(table, dialect) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/better_auth/schema/sql.rb', line 96 def index_statements(table, dialect) table_name = table.fetch(:model_name) table.fetch(:fields).filter_map do |logical_field, attributes| nullable_unique_mssql = dialect == :mssql && attributes[:unique] && logical_name(logical_field, attributes) != "id" && !attributes[:required] next if attributes[:unique] && !nullable_unique_mssql next unless attributes[:index] || nullable_unique_mssql column = attributes[:field_name] || Schema.physical_name(logical_field) unique = attributes[:unique] && dialect == :mssql name = unique ? "uniq_#{table_name}_#{column}" : "index_#{table_name}_on_#{column}" index_statement(table_name, column, name, dialect, unique: unique, where_not_null: filtered_unique_index?(attributes, dialect)) end end |
.indexed_string_sql_type(logical_field, attributes, dialect) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/better_auth/schema/sql.rb', line 200 def indexed_string_sql_type(logical_field, attributes, dialect) if dialect == :mysql indexed = logical_field == "id" || attributes[:unique] || attributes[:index] || attributes[:references] || attributes[:sortable] || attributes.key?(:default_value) indexed ? "varchar(191)" : "text" elsif dialect == :mssql indexed = logical_field == "id" || attributes[:unique] || attributes[:index] || attributes[:references] || attributes[:sortable] indexed ? "varchar(255)" : "varchar(8000)" else "text" end end |
.logical_name(logical_field, attributes) ⇒ Object
352 353 354 |
# File 'lib/better_auth/schema/sql.rb', line 352 def logical_name(logical_field, attributes) (attributes[:logical_name] || logical_field).to_s end |
.mssql_cascade_conflict?(graph, parent, child) ⇒ Boolean
295 296 297 298 299 300 |
# File 'lib/better_auth/schema/sql.rb', line 295 def mssql_cascade_conflict?(graph, parent, child) nodes = (graph.keys + graph.values.flatten + [parent, child]).uniq nodes.any? do |source| mssql_reachable?(graph, source, parent) && mssql_reachable?(graph, source, child) end end |
.mssql_cascading_action?(action) ⇒ Boolean
291 292 293 |
# File 'lib/better_auth/schema/sql.rb', line 291 def mssql_cascading_action?(action) %w[cascade set\ null set\ default].include?(action.to_s.downcase.tr("_", " ")) end |
.mssql_delete_actions(tables, dialect) ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/better_auth/schema/sql.rb', line 260 def mssql_delete_actions(tables, dialect) return unless dialect.to_sym == :mssql graph = {} tables.each_with_object({}) do |(_logical_name, table), actions| child = table.fetch(:model_name).to_s table.fetch(:fields).each do |logical_field, attributes| reference = attributes[:references] next unless reference column = (attributes[:field_name] || physical_name(logical_field)).to_s action = reference[:on_delete]&.to_s key = [child, column] unless mssql_cascading_action?(action) actions[key] = action next end target_table = foreign_key_target_table(reference, tables) parent = (target_table&.fetch(:model_name) || reference.fetch(:model)).to_s if mssql_cascade_conflict?(graph, parent, child) actions[key] = "no action" else graph[parent] ||= [] graph[parent] << child unless graph[parent].include?(child) actions[key] = action end end end end |
.mssql_reachable?(graph, source, target, visited = {}) ⇒ Boolean
302 303 304 305 306 307 308 |
# File 'lib/better_auth/schema/sql.rb', line 302 def mssql_reachable?(graph, source, target, visited = {}) return true if source == target return false if visited[source] visited[source] = true Array(graph[source]).any? { |child| mssql_reachable?(graph, child, target, visited) } end |
.mssql_required_set_options ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/better_auth/schema/sql.rb', line 143 def <<~SQL.strip SET ANSI_NULLS ON; SET QUOTED_IDENTIFIER ON; SET ANSI_WARNINGS ON; SET ANSI_PADDING ON; SET CONCAT_NULL_YIELDS_NULL ON; SET ARITHABORT ON; SET NUMERIC_ROUNDABORT OFF; SQL end |
.pending_statements(plan) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/better_auth/schema/sql.rb', line 18 def pending_statements(plan) delete_actions = mssql_delete_actions(plan.tables, plan.dialect) statements = plan.to_create.map do |change| create_table_statement(change.logical_name, change.table, plan.dialect, plan.tables, delete_actions: delete_actions) end statements.concat(plan.to_add.flat_map do |change| change.fields.map do |logical_field, attributes| if logical_name(logical_field, attributes) == "id" && plan.dialect == :postgres add_postgres_id_column_statements(change.table_name) else add_column_statement(change.table_name, logical_field, attributes, plan.dialect) end end end.flatten) statements.concat(plan.to_index.map do |change| index_statement( change.table_name, change.field_name, change.name, plan.dialect, unique: change.unique, where_not_null: filtered_unique_index?(change.field, plan.dialect) ) end) end |
.physical_name(value) ⇒ Object
345 346 347 348 349 350 |
# File 'lib/better_auth/schema/sql.rb', line 345 def physical_name(value) value.to_s .gsub(/([a-z\d])([A-Z])/, "\\1_\\2") .tr("-", "_") .downcase end |
.quote(identifier, dialect) ⇒ Object
332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/better_auth/schema/sql.rb', line 332 def quote(identifier, dialect) case dialect when :postgres, :sqlite %("#{identifier.to_s.gsub("\"", "\"\"")}") when :mysql "`#{identifier.to_s.gsub("`", "``")}`" when :mssql "[#{identifier.to_s.gsub("]", "]]")}]" else raise ArgumentError, "Unsupported SQL dialect: #{dialect}" end end |
.sql_type(logical_field, attributes, dialect) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/better_auth/schema/sql.rb', line 155 def sql_type(logical_field, attributes, dialect) type = attributes[:type] || "string" case type when "boolean" case dialect when :mysql "tinyint(1)" when :sqlite "integer" when :mssql "smallint" else "boolean" end when "date" case dialect when :mysql "datetime(6)" when :sqlite "date" when :mssql "datetime2(3)" else "timestamptz" end when "number" attributes[:bigint] ? "bigint" : "integer" when "json", "string[]", "number[]" case dialect when :postgres "jsonb" when :mysql "json" when :mssql "varchar(8000)" else "text" end when "string" indexed_string_sql_type(logical_field, attributes, dialect) else raise BetterAuth::Error, "Unsupported field type: #{type}" end end |
.unique_constraint(table_name, column, dialect) ⇒ Object
232 233 234 235 236 237 238 239 240 241 |
# File 'lib/better_auth/schema/sql.rb', line 232 def unique_constraint(table_name, column, dialect) case dialect when :postgres, :sqlite %(UNIQUE (#{quote(column, dialect)})) when :mysql %(UNIQUE KEY #{quote("uniq_#{table_name}_#{column}", dialect)} (#{quote(column, dialect)})) when :mssql %(CONSTRAINT #{quote("uniq_#{table_name}_#{column}", dialect)} UNIQUE (#{quote(column, dialect)})) end end |