Module: BetterAuth::Schema::SQL

Defined in:
lib/better_auth/schema/sql.rb

Class Method Summary collapse

Class Method Details

.add_column_statement(table_name, logical_field, attributes, dialect) ⇒ Object



106
107
108
109
# File 'lib/better_auth/schema/sql.rb', line 106

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



111
112
113
114
115
116
117
118
119
120
# File 'lib/better_auth/schema/sql.rb', line 111

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



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/better_auth/schema/sql.rb', line 62

def column_definition(table_name, logical_field, attributes, dialect)
  column = quote(attributes[:field_name] || physical_name(logical_field), dialect)
  parts = [column, sql_type(logical_field, attributes, dialect)]
  parts << "PRIMARY KEY" if logical_field == "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
# File 'lib/better_auth/schema/sql.rb', line 8

def create_statements(options, dialect:)
  dialect = dialect.to_sym
  tables = Schema.auth_tables(options)
  statements = tables.map { |logical_name, table| create_table_statement(logical_name, table, dialect, tables) }
  statements.concat(tables.flat_map { |_logical_name, table| index_statements(table, dialect) })
end

.create_table_statement(logical_name, table, dialect, tables = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/better_auth/schema/sql.rb', line 40

def create_table_statement(logical_name, table, dialect, tables = 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)
  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
    %(#{mssql_required_set_options}\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



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/better_auth/schema/sql.rb', line 201

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) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/better_auth/schema/sql.rb', line 76

def field_constraints(table_name, logical_field, attributes, dialect, tables = nil)
  constraints = []
  column = attributes[:field_name] || physical_name(logical_field)

  if attributes[:unique] && logical_field != "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)
  end

  constraints
end

.filtered_unique_index?(attributes, dialect) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/better_auth/schema/sql.rb', line 135

def filtered_unique_index?(attributes, dialect)
  dialect == :mssql && attributes[:unique] && !attributes[:required]
end

.foreign_key_constraint(table_name, column, reference, dialect, tables = nil) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/better_auth/schema/sql.rb', line 232

def foreign_key_constraint(table_name, column, reference, dialect, tables = 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)
  on_delete = reference[:on_delete] ? " ON DELETE #{reference[:on_delete].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



255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/better_auth/schema/sql.rb', line 255

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



248
249
250
251
252
253
# File 'lib/better_auth/schema/sql.rb', line 248

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



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/better_auth/schema/sql.rb', line 122

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" : ""
    %(#{mssql_required_set_options}\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



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/better_auth/schema/sql.rb', line 92

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_field != "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

.mssql_required_set_optionsObject



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/better_auth/schema/sql.rb', line 139

def mssql_required_set_options
  <<~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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/better_auth/schema/sql.rb', line 15

def pending_statements(plan)
  statements = plan.to_create.map do |change|
    create_table_statement(change.logical_name, change.table, plan.dialect, plan.tables)
  end
  statements.concat(plan.to_add.flat_map do |change|
    change.fields.map do |logical_field, attributes|
      if logical_field.to_s == "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



283
284
285
286
287
288
# File 'lib/better_auth/schema/sql.rb', line 283

def physical_name(value)
  value.to_s
    .gsub(/([a-z\d])([A-Z])/, "\\1_\\2")
    .tr("-", "_")
    .downcase
end

.quote(identifier, dialect) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/better_auth/schema/sql.rb', line 270

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



151
152
153
154
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
199
# File 'lib/better_auth/schema/sql.rb', line 151

def sql_type(logical_field, attributes, dialect)
  case attributes[: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
  else
    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
end

.unique_constraint(table_name, column, dialect) ⇒ Object



221
222
223
224
225
226
227
228
229
230
# File 'lib/better_auth/schema/sql.rb', line 221

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