mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-22 07:46:56 +08:00
Some checks failed
Tests / core system (push) Has been cancelled
Tests / plugins system (push) Has been cancelled
Licenses / run (push) Has been cancelled
Linting / run (push) Has been cancelled
Migration Tests / Tests (push) Has been cancelled
Publish Assets / publish-assets (push) Has been cancelled
Tests / core backend (push) Has been cancelled
Tests / plugins backend (push) Has been cancelled
Tests / core frontend (Chrome) (push) Has been cancelled
Tests / plugins frontend (push) Has been cancelled
Tests / themes frontend (push) Has been cancelled
Tests / themes system (push) Has been cancelled
Tests / core frontend (Firefox ESR) (push) Has been cancelled
Tests / core frontend (Firefox Evergreen) (push) Has been cancelled
Tests / chat system (push) Has been cancelled
Tests / merge (push) Has been cancelled
Allow table-level check constraints in the Intermediate DB In https://github.com/discourse/discourse/pull/34339, I’ve worked on distinguishing between `user_custom_fields` tied to `user_fields` and arbitrary `user_custom_fields` entries. To support this, I’ve made both `field_id` and `name` nullable, which requires a table-level constraint to ensure each entry has either a `field_id` (referencing `user_fields`) or an arbitrary `name` for the value. This first pass supports only named table-level `CHECK` constraints. ## Usage ```yaml user_custom_fields: columns: exclude: - "id" modify: - name: "name" nullable: true add: - name: "field_id" datatype: numeric - name: "is_multiselect_field" datatype: boolean indexes: # ... constraints: - name: "require_field_id_or_name" condition: "field_id IS NOT NULL OR name IS NOT NULL" - name: "disallow_both_field_id_and_name" type: check # default, only `check` supported for now condition: "NOT (field_id IS NOT NULL AND name IS NOT NULL)" ``` ```sql CREATE TABLE user_custom_fields ( created_at DATETIME, field_id NUMERIC, is_multiselect_field BOOLEAN, name TEXT, user_id NUMERIC NOT NULL, value TEXT, CONSTRAINT require_field_id_or_name CHECK (field_id IS NOT NULL OR name IS NOT NULL), CONSTRAINT disallow_both_field_id_and_name CHECK (NOT (field_id IS NOT NULL AND name IS NOT NULL)) ); ```
110 lines
3.1 KiB
Ruby
Vendored
110 lines
3.1 KiB
Ruby
Vendored
# frozen_string_literal: true
|
|
|
|
module Migrations::Database::Schema
|
|
class TableWriter
|
|
def initialize(output_stream)
|
|
@output = output_stream
|
|
end
|
|
|
|
def output_file_header(header)
|
|
@output.puts header.gsub(/^/, "-- ")
|
|
@output.puts
|
|
end
|
|
|
|
def output_table(table)
|
|
output_create_table_statement(table) { output_table_definitions(table) }
|
|
output_indexes(table)
|
|
@output.puts ""
|
|
end
|
|
|
|
private
|
|
|
|
def output_create_table_statement(table)
|
|
@output.puts "CREATE TABLE #{escape_identifier(table.name)}"
|
|
@output.puts "("
|
|
yield
|
|
@output.puts ");"
|
|
end
|
|
|
|
def output_table_definitions(table)
|
|
definitions = create_column_definitions(table)
|
|
|
|
if table.primary_key_column_names.size > 1
|
|
primary_key_column_names =
|
|
table.primary_key_column_names.map { |name| escape_identifier(name) }.join(", ")
|
|
definitions << " PRIMARY KEY (#{primary_key_column_names})"
|
|
end
|
|
|
|
if table.constraints&.any?
|
|
definitions.concat(
|
|
table.constraints.map do |constraint|
|
|
" CONSTRAINT #{escape_identifier(constraint.name)} CHECK (#{constraint.condition})"
|
|
end,
|
|
)
|
|
end
|
|
|
|
@output.puts definitions.join(",\n")
|
|
end
|
|
|
|
def create_column_definitions(table)
|
|
columns = table.sorted_columns
|
|
has_composite_primary_key = table.primary_key_column_names.size > 1
|
|
|
|
max_column_name_length = columns.map { |c| escape_identifier(c.name).length }.max
|
|
max_datatype_length = columns.map { |c| convert_datatype(c.datatype).length }.max
|
|
|
|
columns.map do |c|
|
|
definition = [
|
|
escape_identifier(c.name).ljust(max_column_name_length),
|
|
convert_datatype(c.datatype).ljust(max_datatype_length),
|
|
]
|
|
|
|
if c.is_primary_key && !has_composite_primary_key
|
|
definition << "NOT NULL" if c.datatype != :integer
|
|
definition << "PRIMARY KEY"
|
|
else
|
|
definition << "NOT NULL" unless c.nullable
|
|
end
|
|
|
|
definition = definition.join(" ")
|
|
definition.strip!
|
|
|
|
" #{definition}"
|
|
end
|
|
end
|
|
|
|
def convert_datatype(type)
|
|
case type
|
|
when :blob, :boolean, :date, :datetime, :float, :integer, :numeric, :text
|
|
type.to_s.upcase
|
|
when :inet
|
|
"INET_TEXT"
|
|
when :json
|
|
"JSON_TEXT"
|
|
else
|
|
raise "Unknown datatype: #{type}"
|
|
end
|
|
end
|
|
|
|
def escape_identifier(identifier)
|
|
::Migrations::Database::Schema.escape_identifier(identifier)
|
|
end
|
|
|
|
def output_indexes(table)
|
|
return unless table.indexes
|
|
|
|
@output.puts ""
|
|
table.indexes.each do |index|
|
|
index_name = escape_identifier(index.name)
|
|
table_name = escape_identifier(table.name)
|
|
column_names = index.column_names.map { |name| escape_identifier(name) }
|
|
|
|
@output.print "CREATE "
|
|
@output.print "UNIQUE " if index.unique
|
|
@output.print "INDEX #{index_name} ON #{table_name} (#{column_names.join(", ")})"
|
|
@output.print " #{index.condition}" if index.condition.present?
|
|
@output.puts ";"
|
|
end
|
|
end
|
|
end
|
|
end
|