A platform for community discussion. Free, open, simple.
MT: Refactor schema configuration from YAML to Ruby DSL
Gerhard Schlager554977c7e6f5
diff --git a/.github/workflows/migration-tests.yml b/.github/workflows/migration-tests.ymlindex 996c1091f98..37b1cf7fd3f 100644--- a/.github/workflows/migration-tests.yml+++ b/.github/workflows/migration-tests.yml@@ -112,12 +112,35 @@ jobs:- name: Validate IntermediateDB schemarun: |+ # Show structured differences between DSL config and database+ migrations/bin/cli schema diff --db=intermediate_db || true++ # Regenerate schema from DSL configmigrations/bin/cli schema generate --db=intermediate_db- if [ ! -z "$(git status --porcelain migrations/db/intermediate_db_schema/)" ]; then+ # Check if generated output matches what's committed+ if [ ! -z "$(git status --porcelain migrations/db/intermediate_db_schema/ migrations/lib/database/intermediate_db/ migrations/lib/database/intermediate_db/enums/)" ]; then+ echo ""+ echo "=========================================="echo "IntermediateDB schema is not up to date."- echo "---------------------------------------------"- git -c color.ui=always diff migrations/db/intermediate_db_schema/+ echo "=========================================="+ echo ""+ echo "The generated schema files differ from what is committed."+ echo "This usually means the schema DSL config or the database"+ echo "structure changed without regenerating."+ echo ""+ echo "To fix:"+ echo " 1. Run: migrations/bin/cli schema diff"+ echo " to see what changed between config and database"+ echo " 2. Update config files in migrations/config/schema/intermediate_db/"+ echo " - New tables: migrations/bin/cli schema add <table>"+ echo " - Ignore tables: migrations/bin/cli schema ignore <table> --reason '...'"+ echo " - New columns: add to the table's include list or ignore with reason"+ echo " 3. Run: migrations/bin/cli schema generate"+ echo " 4. Commit the updated config and generated files"+ echo ""+ echo "Generated file diff:"+ git -c color.ui=always diff -- migrations/db/intermediate_db_schema/ migrations/lib/database/intermediate_db/ migrations/lib/database/intermediate_db/enums/exit 1fidiff --git a/lib/temporary_db.rb b/lib/temporary_db.rbindex 98f08e44bef..00a746f2f5c 100644--- a/lib/temporary_db.rb+++ b/lib/temporary_db.rb@@ -87,6 +87,10 @@ class TemporaryDbrestore_discourse_pg_portend+ def connection_hash+ { adapter: "postgresql", database: "discourse", port: pg_port, host: "localhost" }+ end+def with_env(&block)old_host = ENV["PGHOST"]old_user = ENV["PGUSER"]@@ -116,12 +120,7 @@ class TemporaryDbdef migrateraise "Error: the database must be started before it can be migrated." if !@started- ActiveRecord::Base.establish_connection(- adapter: "postgresql",- database: "discourse",- port: pg_port,- host: "localhost",- )+ ActiveRecord::Base.establish_connection(connection_hash)puts "Running migrations on blank database!"diff --git a/migrations/.gitignore b/migrations/.gitignoreindex 7b9145e0f7b..c0d7eb64e9d 100644--- a/migrations/.gitignore+++ b/migrations/.gitignore@@ -6,3 +6,5 @@# converters within this directory should be ignored/private/++/tmp/diff --git a/migrations/.reek.yml b/migrations/.reek.ymlnew file mode 100644index 00000000000..1cbfab7a815--- /dev/null+++ b/migrations/.reek.yml@@ -0,0 +1,32 @@+---+detectors:+ IrresponsibleModule:+ enabled: false+ FeatureEnvy:+ enabled: false+ UtilityFunction:+ enabled: false+ BooleanParameter:+ enabled: false+ UncommunicativeVariableName:+ enabled: false+ MissingSafeMethod:+ enabled: false+ InstanceVariableAssumption:+ enabled: false+ NilCheck:+ enabled: false++ TooManyStatements:+ max_statements: 20+ TooManyInstanceVariables:+ max_instance_variables: 10+ TooManyMethods:+ max_methods: 30+ LongParameterList:+ max_params: 4+ NestedIterators:+ max_allowed_nesting: 2++ DuplicateMethodCall:+ max_calls: 3diff --git a/migrations/AGENTS.md b/migrations/AGENTS.mdnew file mode 100644index 00000000000..601b4e5908a--- /dev/null+++ b/migrations/AGENTS.md@@ -0,0 +1,50 @@+# Migrations Tooling - AI Agent Guide++## Running Tests++Tests must be run from the project root with `--default-path migrations/spec`:++```bash+bin/rspec --default-path migrations/spec+bin/rspec --default-path migrations/spec migrations/spec/lib/database/schema/dsl/+bin/rspec --default-path migrations/spec migrations/spec/path/to/file_spec.rb+```++## CLI++The CLI binary is at `migrations/bin/cli`:++```bash+migrations/bin/cli help+migrations/bin/cli schema generate+migrations/bin/cli schema validate+migrations/bin/cli schema diff+```++## Schema DSL++The schema DSL lives in `migrations/lib/database/schema/dsl/`. Config files are in `migrations/config/schema/`.++Key files:+- `table_builder.rb` - DSL for defining table configs+- `schema_resolver.rb` - Resolves DSL config + DB introspection into final schema+- `conventions_builder.rb` - Global column conventions (renames, type overrides)+- `generator.rb` - Generates SQL, models, and enums from resolved schema+- `validator.rb` - Validates DSL config+- `resolved_schema_validator.rb` - Validates resolved schema before generation++## Linting++```bash+bin/lint path/to/file+bin/lint --fix path/to/file+```++Uses both rubocop and syntax_tree. Always lint changed files.++## Gems++```bash+bundle config set --local with migrations+bundle install+```diff --git a/migrations/CLAUDE.md b/migrations/CLAUDE.mdnew file mode 120000index 00000000000..47dc3e3d863--- /dev/null+++ b/migrations/CLAUDE.md@@ -0,0 +1 @@+AGENTS.md\ No newline at end of filediff --git a/migrations/bin/cli b/migrations/bin/cliindex 342c8f7c90e..4502b1e59cd 100755--- a/migrations/bin/cli+++ b/migrations/bin/cli@@ -13,6 +13,8 @@ module Migrationsmodule CLIclass Application < Thor+ remove_command :tree+desc "convert [FROM]", "Convert a file"option :settings, type: :string, desc: "Path of settings file", banner: "path"option :reset, type: :boolean, desc: "Reset database before converting data"diff --git a/migrations/config/json_schemas/db_schema.json b/migrations/config/json_schemas/db_schema.jsondeleted file mode 100644index 432ec69c50c..00000000000--- a/migrations/config/json_schemas/db_schema.json+++ /dev/null@@ -1,373 +0,0 @@-{- "$schema": "https://json-schema.org/draft/2020-12/schema",- "$defs": {- "datatypes": {- "type": "string",- "enum": [- "blob",- "boolean",- "date",- "datetime",- "float",- "inet",- "integer",- "json",- "numeric",- "text"- ]- }- },- "type": "object",- "required": [- "output",- "schema",- "plugins"- ],- "properties": {- "output": {- "type": "object",- "additionalProperties": false,- "properties": {- "schema_file": {- "type": "string"- },- "models_directory": {- "type": "string"- },- "models_namespace": {- "type": "string"- },- "enums_directory": {- "type": "string"- },- "enums_namespace": {- "type": "string"- }- },- "required": [- "schema_file",- "models_directory",- "models_namespace",- "enums_directory",- "enums_namespace"- ]- },- "schema": {- "type": "object",- "required": [- "tables",- "global"- ],- "properties": {- "tables": {- "type": "object",- "additionalProperties": {- "type": "object",- "properties": {- "columns": {- "type": "object",- "properties": {- "exclude": {- "type": "array",- "uniqueItems": true,- "items": {- "type": "string"- }- },- "include": {- "type": "array",- "uniqueItems": true,- "items": {- "type": "string"- }- },- "modify": {- "type": "array",- "items": {- "type": "object",- "properties": {- "name": {- "type": "string"- },- "datatype": {- "$ref": "#/$defs/datatypes"- },- "enum": {- "type": "string"- },- "nullable": {- "type": "boolean"- }- },- "additionalProperties": false,- "required": [- "name"- ],- "anyOf": [- {- "required": [- "datatype"- ]- },- {- "required": [- "enum"- ]- },- {- "required": [- "nullable"- ]- }- ],- "not": {- "required": [- "datatype",- "enum"- ]- }- }- },- "add": {- "type": "array",- "items": {- "type": "object",- "properties": {- "name": {- "type": "string"- },- "datatype": {- "$ref": "#/$defs/datatypes"- },- "enum": {- "type": "string"- },- "nullable": {- "type": "boolean"- },- "max_length": {- "type": "integer"- }- },- "additionalProperties": false,- "required": [- "name"- ],- "oneOf": [- {- "required": [- "datatype"- ]- },- {- "required": [- "enum"- ]- }- ]- }- }- },- "additionalProperties": false,- "not": {- "required": [- "exclude",- "include"- ]- }- },- "indexes": {- "type": "array",- "items": {- "type": "object",- "properties": {- "name": {- "type": "string"- },- "columns": {- "type": "array",- "items": {- "type": "string"- },- "uniqueItems": true- },- "unique": {- "type": "boolean"- },- "condition": {- "type": "string"- }- },- "additionalProperties": false,- "required": [- "name",- "columns"- ]- }- },- "primary_key_column_names": {- "type": "array",- "items": {- "type": "string"- },- "uniqueItems": true- },- "copy_of": {- "type": "string"- },- "constraints": {- "type": "array",- "items": {- "type": "object",- "properties": {- "name": {- "type": "string"- },- "type": {- "type": "string",- "enum": [ "check" ]- },- "condition": {- "type": "string"- }- },- "additionalProperties": false,- "required": [- "name",- "condition"- ]- }- }- },- "additionalProperties": false- }- },- "global": {- "type": "object",- "properties": {- "columns": {- "properties": {- "modify": {- "type": "array",- "items": {- "type": "object",- "properties": {- "name": {- "type": "string"- },- "name_regex": {- "type": "string",- "format": "regex"- },- "datatype": {- "$ref": "#/$defs/datatypes"- },- "rename_to": {- "type": "string"- },- "nullable": {- "type": "boolean"- }- },- "additionalProperties": false,- "anyOf": [- {- "required": [- "datatype"- ]- },- {- "required": [- "rename_to"- ]- },- {- "required": [- "nullable"- ]- }- ],- "oneOf": [- {- "required": [- "name"- ]- },- {- "required": [- "name_regex"- ]- }- ]- }- },- "exclude": {- "type": "array",- "uniqueItems": true,- "items": {- "type": "string"- }- }- }- },- "tables": {- "properties": {- "exclude": {- "type": "array",- "uniqueItems": true,- "items": {- "type": "string"- }- }- }- }- },- "additionalProperties": false- },- "enums": {- "type": "object",- "additionalProperties": {- "type": "object",- "properties": {- "values": {- "oneOf": [- { "type": "array", "items": { "type": "string" } },- { "type": "object", "additionalProperties": { "type": "integer" } }- ]- },- "strings": {- "oneOf": [- { "type": "array", "items": { "type": "string" } },- { "type": "object", "additionalProperties": { "type": "string" } }- ]- },- "source": { "type": "string" }- },- "not": {- "anyOf": [- { "required": ["values", "strings"] },- { "required": ["values", "source"] },- { "required": ["strings", "source"] }- ]- },- "additionalProperties": false,- "oneOf": [- { "required": ["values"] },- { "required": ["strings"] },- { "required": ["source"] }- ]- }- },- "additionalProperties": false- }- },- "plugins": {- "type": "array",- "uniqueItems": true,- "items": {- "type": "string"- }- }- },- "additionalProperties": false-}diff --git a/migrations/config/locales/migrations.en.yml b/migrations/config/locales/migrations.en.ymlindex 1844ad7ebc5..c2a8d8961d0 100644--- a/migrations/config/locales/migrations.en.yml+++ b/migrations/config/locales/migrations.en.yml@@ -29,36 +29,3 @@ en:group: "group"category: "Category"badge: "Badge"-- schema:- validator:- include_exclude_not_allowed: "Cannot use `include` and `exclude` together at %{path}"- invalid_name_regex: "Invalid `name_regex`: %{message}"-- output:- schema_file_directory_not_found: "Schema file directory not found"- models_directory_not_found: "Models directory not found"- models_namespace_undefined: "Models namespace not defined"-- global:- excluded_tables_missing: "Missing globally excluded tables: %{table_names}"- excluded_columns_missing: "Missing globally excluded columns: %{column_names}"- modified_columns_missing: "Missing globally modified columns: %{column_names}"-- tables:- excluded_tables_configured: "Excluded tables configured: %{table_names}"- not_configured: "Tables not configured or excluded: %{table_names}"- copy_table_not_found: "Can't copy table '%{table_name}' because it doesn't exist"-- added_columns_exist: "Added columns already exist in '%{table_name}': %{column_names}"- included_columns_missing: "Included columns do not exist in '%{table_name}': %{column_names}"- excluded_columns_missing: "Excluded columns do not exist in '%{table_name}': %{column_names}"- modified_columns_missing: "Missing modified columns in '%{table_name}': %{column_names}"- modified_columns_included: "Modified columns included in '%{table_name}': %{column_names}"- modified_columns_excluded: "Modified columns excluded in '%{table_name}': %{column_names}"- modified_columns_globally_excluded: "Modified columns globally excluded in '%{table_name}': %{column_names}"- no_columns_configured: "No columns configured in '%{table_name}'"-- plugins:- not_installed: "Plugins not installed: %{plugin_names}"- additional_installed: "Unconfigured plugins installed: %{plugin_names}"diff --git a/migrations/config/schema/intermediate_db/config.rb b/migrations/config/schema/intermediate_db/config.rbnew file mode 100644index 00000000000..7658aefc223--- /dev/null+++ b/migrations/config/schema/intermediate_db/config.rb@@ -0,0 +1,13 @@+# frozen_string_literal: true++Migrations::Database::Schema.configure do+ output do+ schema_file "db/intermediate_db_schema/100-base-schema.sql"++ models_directory "lib/database/intermediate_db"+ models_namespace "Migrations::Database::IntermediateDB"++ enums_directory "lib/database/intermediate_db/enums"+ enums_namespace "Migrations::Database::IntermediateDB::Enums"+ end+enddiff --git a/migrations/config/schema/intermediate_db/conventions.rb b/migrations/config/schema/intermediate_db/conventions.rbnew file mode 100644index 00000000000..057efd8a9d9--- /dev/null+++ b/migrations/config/schema/intermediate_db/conventions.rb@@ -0,0 +1,18 @@+# frozen_string_literal: true++Migrations::Database::Schema.conventions do+ column :id do+ rename_to :original_id+ type :numeric+ end++ column :created_at do+ required false+ end++ columns_matching(/.*upload.*_id$/) { type :text }+ columns_matching(/.*_id$/) { type :numeric }++ # Globally ignored columns+ ignore_columns :updated_at+enddiff --git a/migrations/config/schema/intermediate_db/enums/log_entry_type.rb b/migrations/config/schema/intermediate_db/enums/log_entry_type.rbnew file mode 100644index 00000000000..74f964c1d99--- /dev/null+++ b/migrations/config/schema/intermediate_db/enums/log_entry_type.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++Migrations::Database::Schema.enum :log_entry_type do+ value :info, "info"+ value :warning, "warning"+ value :error, "error"+enddiff --git a/migrations/config/schema/intermediate_db/enums/site_setting_datatype.rb b/migrations/config/schema/intermediate_db/enums/site_setting_datatype.rbnew file mode 100644index 00000000000..970d6e8447d--- /dev/null+++ b/migrations/config/schema/intermediate_db/enums/site_setting_datatype.rb@@ -0,0 +1,5 @@+# frozen_string_literal: true++Migrations::Database::Schema.enum :site_setting_datatype do+ source { ::SiteSettings::TypeSupervisor.types }+enddiff --git a/migrations/config/schema/intermediate_db/enums/site_setting_import_mode.rb b/migrations/config/schema/intermediate_db/enums/site_setting_import_mode.rbnew file mode 100644index 00000000000..cf88d1bacc0--- /dev/null+++ b/migrations/config/schema/intermediate_db/enums/site_setting_import_mode.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++Migrations::Database::Schema.enum :site_setting_import_mode do+ value :auto, 0+ value :override, 1+ value :append, 2+enddiff --git a/migrations/config/schema/intermediate_db/enums/upload_type.rb b/migrations/config/schema/intermediate_db/enums/upload_type.rbnew file mode 100644index 00000000000..7882b60e19e--- /dev/null+++ b/migrations/config/schema/intermediate_db/enums/upload_type.rb@@ -0,0 +1,5 @@+# frozen_string_literal: true++Migrations::Database::Schema.enum :upload_type do+ source { ::UploadCreator::TYPES_TO_CROP }+enddiff --git a/migrations/config/schema/intermediate_db/ignored.rb b/migrations/config/schema/intermediate_db/ignored.rbnew file mode 100644index 00000000000..0dd60c79da6--- /dev/null+++ b/migrations/config/schema/intermediate_db/ignored.rb@@ -0,0 +1,212 @@+# frozen_string_literal: true++Migrations::Database::Schema.ignored do+ # Plugins — all their tables and columns on core tables are auto-ignored via manifest+ plugin :automation, "Not migrated yet"+ plugin :chat, "Not migrated yet"+ plugin :discourse_adplugin, "Not migrated yet"+ plugin :discourse_ai, "Not migrated yet"+ plugin :discourse_assign, "Not migrated yet"+ plugin :discourse_calendar, "Not migrated yet"+ plugin :discourse_data_explorer, "Not migrated yet"+ plugin :discourse_gamification, "Not migrated yet"+ plugin :discourse_github, "Not migrated yet"+ plugin :discourse_oauth2_basic, "Not migrated yet"+ plugin :discourse_policy, "Not migrated yet"+ plugin :discourse_post_voting, "Not migrated yet"+ plugin :discourse_reactions, "Not migrated yet"+ plugin :discourse_rewind, "Not migrated yet"+ plugin :discourse_rss_polling, "Not migrated yet"+ plugin :discourse_solved, "Not migrated yet"+ plugin :discourse_subscriptions, "Not migrated yet"+ plugin :discourse_templates, "Not migrated yet"+ plugin :discourse_topic_voting, "Not migrated yet"+ plugin :poll, "Not migrated yet"++ # Core tables excluded from the intermediate database++ tables :allowed_pm_users,+ :anonymous_users,+ :ignored_users,+ :single_sign_on_records,+ :user_actions,+ :user_api_key_client_scopes,+ :user_api_key_clients,+ :user_api_key_scopes,+ :user_api_keys,+ :user_archived_messages,+ :user_associated_groups,+ :user_auth_token_logs,+ :user_auth_tokens,+ :user_avatars,+ :user_badges,+ :user_exports,+ :user_histories,+ :user_ip_address_histories,+ :user_notification_schedules,+ :user_open_ids,+ :user_passwords,+ :user_profile_views,+ :user_profiles,+ :user_required_fields_versions,+ :user_search_data,+ :user_second_factors,+ :user_security_keys,+ :user_stats,+ :user_statuses,+ :user_uploads,+ :user_visits,+ :user_warnings++ tables :draft_sequences,+ :drafts,+ :moved_posts,+ :post_action_types,+ :post_actions,+ :post_custom_fields,+ :post_details,+ :post_hotlinked_media,+ :post_localizations,+ :post_replies,+ :post_reply_keys,+ :post_revisions,+ :post_search_data,+ :post_stats,+ :post_timings,+ :posts,+ :quoted_posts,+ :shared_drafts++ tables :dismissed_topic_users,+ :linked_topics,+ :top_topics,+ :topic_custom_fields,+ :topic_embeds,+ :topic_groups,+ :topic_hot_scores,+ :topic_invites,+ :topic_link_clicks,+ :topic_links,+ :topic_localizations,+ :topic_search_data,+ :topic_thumbnails,+ :topic_timers,+ :topic_view_stats,+ :topic_views++ tables :category_featured_topics,+ :category_form_templates,+ :category_groups,+ :category_localizations,+ :category_posting_review_groups,+ :category_required_tag_groups,+ :category_search_data,+ :category_settings,+ :category_tag_groups,+ :category_tag_stats,+ :category_tags++ tables :group_archived_messages,+ :group_associated_groups,+ :group_category_notification_defaults,+ :group_custom_fields,+ :group_histories,+ :group_mentions,+ :group_requests,+ :group_tag_notification_defaults++ tables :child_themes,+ :color_scheme_colors,+ :color_schemes,+ :remote_themes,+ :theme_fields,+ :theme_modifier_sets,+ :theme_settings,+ :theme_settings_migrations,+ :theme_site_settings,+ :theme_svg_sprites,+ :theme_translation_overrides,+ :themes++ tables :incoming_domains, :incoming_emails, :incoming_links, :incoming_referers++ tables :reviewable_claimed_topics,+ :reviewable_histories,+ :reviewable_notes,+ :reviewable_scores,+ :reviewables++ tables :categories_web_hooks,+ :groups_web_hooks,+ :redelivering_webhook_events,+ :tags_web_hooks,+ :web_hook_event_types,+ :web_hook_event_types_hooks,+ :web_hook_events,+ :web_hook_events_daily_aggregates,+ :web_hooks++ tables :external_upload_stubs, :optimized_images, :optimized_videos, :upload_references, :uploads++ tables :admin_notices,+ :api_key_scopes,+ :api_keys,+ :application_requests,+ :ar_internal_metadata,+ :associated_groups,+ :backup_draft_posts,+ :backup_draft_topics,+ :backup_metadata,+ :badge_types,+ :bookmarks,+ :custom_emojis,+ :developers,+ :directory_columns,+ :directory_items,+ :do_not_disturb_timings,+ :email_change_requests,+ :email_logs,+ :email_tokens,+ :embeddable_host_tags,+ :embeddable_hosts,+ :flags,+ :form_templates,+ :given_daily_likes,+ :invited_groups,+ :invited_users,+ :invites,+ :javascript_caches,+ :message_bus,+ :notifications,+ :oauth2_user_infos,+ :onceoff_logs,+ :permalinks,+ :plugin_store_rows,+ :problem_check_trackers,+ :published_pages,+ :push_subscriptions,+ :scheduler_stats,+ :schema_migration_details,+ :schema_migrations,+ :screened_emails,+ :screened_ip_addresses,+ :screened_urls,+ :search_logs,+ :shelved_notifications,+ :sidebar_section_links,+ :sidebar_sections,+ :sidebar_urls,+ :site_setting_groups,+ :sitemaps,+ :skipped_email_logs,+ :stylesheet_cache,+ :summary_sections,+ :tag_localizations,+ :tag_search_data,+ :translation_overrides,+ :unsubscribe_keys,+ :upcoming_change_events,+ :watched_word_groups,+ :watched_words,+ :web_crawler_requests+enddiff --git a/migrations/config/schema/intermediate_db/tables/badge_groupings.rb b/migrations/config/schema/intermediate_db/tables/badge_groupings.rbnew file mode 100644index 00000000000..aa259b4caba--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/badge_groupings.rb@@ -0,0 +1,5 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :badge_groupings do+ include_all+enddiff --git a/migrations/config/schema/intermediate_db/tables/badges.rb b/migrations/config/schema/intermediate_db/tables/badges.rbnew file mode 100644index 00000000000..4d5e0c3d39a--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/badges.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :badges do+ add_column :existing_id, :numeric++ ignore :grant_count, :system, reason: "Calculated columns"+enddiff --git a/migrations/config/schema/intermediate_db/tables/categories.rb b/migrations/config/schema/intermediate_db/tables/categories.rbnew file mode 100644index 00000000000..8dc15f1b788--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/categories.rb@@ -0,0 +1,25 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :categories do+ add_column :about_topic_title, :text+ add_column :existing_id, :numeric++ ignore :contains_messages,+ :reviewable_by_group_id,+ reason: "TODO: Figure out what these columns are for and if we need them"++ ignore :latest_post_id,+ :latest_topic_id,+ :name_lower,+ :post_count,+ :posts_day,+ :posts_month,+ :posts_week,+ :posts_year,+ :topic_count,+ :topics_day,+ :topics_month,+ :topics_week,+ :topics_year,+ reason: "Calculated columns"+enddiff --git a/migrations/config/schema/intermediate_db/tables/category_custom_fields.rb b/migrations/config/schema/intermediate_db/tables/category_custom_fields.rbnew file mode 100644index 00000000000..86af466683e--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/category_custom_fields.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :category_custom_fields do+ primary_key :category_id, :name++ ignore :created_at, :id+enddiff --git a/migrations/config/schema/intermediate_db/tables/category_moderation_groups.rb b/migrations/config/schema/intermediate_db/tables/category_moderation_groups.rbnew file mode 100644index 00000000000..a4c0e05b4ad--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/category_moderation_groups.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :category_moderation_groups do+ primary_key :category_id, :group_id++ ignore :created_at, :id+enddiff --git a/migrations/config/schema/intermediate_db/tables/category_users.rb b/migrations/config/schema/intermediate_db/tables/category_users.rbnew file mode 100644index 00000000000..220a325be5a--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/category_users.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :category_users do+ primary_key :category_id, :user_id++ ignore :id+enddiff --git a/migrations/config/schema/intermediate_db/tables/group_users.rb b/migrations/config/schema/intermediate_db/tables/group_users.rbnew file mode 100644index 00000000000..5574008278c--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/group_users.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :group_users do+ primary_key :group_id, :user_id++ ignore :first_unread_pm_at, :id+enddiff --git a/migrations/config/schema/intermediate_db/tables/groups.rb b/migrations/config/schema/intermediate_db/tables/groups.rbnew file mode 100644index 00000000000..7d600a7b72c--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/groups.rb@@ -0,0 +1,64 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :groups do+ include :allow_membership_requests,+ :allow_unknown_sender_topic_replies,+ :automatic_membership_email_domains,+ :bio_raw,+ :created_at,+ :default_notification_level,+ :flair_bg_color,+ :flair_color,+ :flair_icon,+ :flair_upload_id,+ :full_name,+ :grant_trust_level,+ :id,+ :members_visibility_level,+ :membership_request_template,+ :mentionable_level,+ :messageable_level,+ :name,+ :primary_group,+ :public_admission,+ :public_exit,+ :publish_read_state,+ :title,+ :visibility_level++ add_column :existing_id, :numeric++ ignore :automatic,+ :bio_cooked,+ :user_count,+ reason: "These fields are either calculated or not relevant for the import process"++ ignore :email_from_alias,+ :email_password,+ :email_username,+ :has_messages,+ :incoming_email,+ reason: "TODO: Figure out if we need to import these settings"++ ignore :smtp_enabled,+ :smtp_port,+ :smtp_server,+ :smtp_ssl_mode,+ :smtp_updated_at,+ :smtp_updated_by_id,+ reason: "TODO: Add support for importing SMTP settings for groups"++ ignore :imap_enabled,+ :imap_last_error,+ :imap_last_uid,+ :imap_mailbox_name,+ :imap_new_emails,+ :imap_old_emails,+ :imap_port,+ :imap_server,+ :imap_ssl,+ :imap_uid_validity,+ :imap_updated_at,+ :imap_updated_by_id,+ reason: "IMAP has been deprecated in Discourse"+enddiff --git a/migrations/config/schema/intermediate_db/tables/log_entries.rb b/migrations/config/schema/intermediate_db/tables/log_entries.rbnew file mode 100644index 00000000000..92e6e490ef6--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/log_entries.rb@@ -0,0 +1,13 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :log_entries do+ model :manual++ synthetic!++ add_column :created_at, :datetime, required: true+ add_column :type, :text, required: true, enum: :log_entry_type+ add_column :message, :text, required: true+ add_column :exception, :text+ add_column :details, :json+enddiff --git a/migrations/config/schema/intermediate_db/tables/muted_users.rb b/migrations/config/schema/intermediate_db/tables/muted_users.rbnew file mode 100644index 00000000000..4b4621f0ec7--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/muted_users.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :muted_users do+ primary_key :user_id, :muted_user_id++ ignore :id+enddiff --git a/migrations/config/schema/intermediate_db/tables/permalink_normalizations.rb b/migrations/config/schema/intermediate_db/tables/permalink_normalizations.rbnew file mode 100644index 00000000000..6872d2a43fa--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/permalink_normalizations.rb@@ -0,0 +1,9 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :permalink_normalizations do+ synthetic!++ primary_key :normalization++ add_column :normalization, :text+enddiff --git a/migrations/config/schema/intermediate_db/tables/site_settings.rb b/migrations/config/schema/intermediate_db/tables/site_settings.rbnew file mode 100644index 00000000000..8f6451ebb18--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/site_settings.rb@@ -0,0 +1,12 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :site_settings do+ primary_key :name++ include :name, :value++ add_column :import_mode, :integer, enum: :site_setting_import_mode, required: true+ add_column :last_changed_at, :datetime++ ignore :created_at, :id, :data_type+enddiff --git a/migrations/config/schema/intermediate_db/tables/tag_group_memberships.rb b/migrations/config/schema/intermediate_db/tables/tag_group_memberships.rbnew file mode 100644index 00000000000..5c183a21009--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/tag_group_memberships.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :tag_group_memberships do+ primary_key :tag_group_id, :tag_id++ ignore :id+enddiff --git a/migrations/config/schema/intermediate_db/tables/tag_group_permissions.rb b/migrations/config/schema/intermediate_db/tables/tag_group_permissions.rbnew file mode 100644index 00000000000..d803297c64a--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/tag_group_permissions.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :tag_group_permissions do+ primary_key :tag_group_id, :group_id, :permission_type++ ignore :id+enddiff --git a/migrations/config/schema/intermediate_db/tables/tag_groups.rb b/migrations/config/schema/intermediate_db/tables/tag_groups.rbnew file mode 100644index 00000000000..e91a75223e5--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/tag_groups.rb@@ -0,0 +1,5 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :tag_groups do+ include_all+enddiff --git a/migrations/config/schema/intermediate_db/tables/tag_synonyms.rb b/migrations/config/schema/intermediate_db/tables/tag_synonyms.rbnew file mode 100644index 00000000000..139c202a1d4--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/tag_synonyms.rb@@ -0,0 +1,10 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :tag_synonyms do+ synthetic!++ primary_key :synonym_tag_id++ add_column :synonym_tag_id, :numeric+ add_column :target_tag_id, :numeric, required: true+enddiff --git a/migrations/config/schema/intermediate_db/tables/tag_users.rb b/migrations/config/schema/intermediate_db/tables/tag_users.rbnew file mode 100644index 00000000000..5fc2978c181--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/tag_users.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :tag_users do+ primary_key :tag_id, :user_id++ ignore :id+enddiff --git a/migrations/config/schema/intermediate_db/tables/tags.rb b/migrations/config/schema/intermediate_db/tables/tags.rbnew file mode 100644index 00000000000..f33371a5533--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/tags.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :tags do+ ignore :pm_topic_count, :public_topic_count, :staff_topic_count, reason: "Calculated columns"++ ignore :target_tag_id, reason: "We have the `tag_synonyms` table for this"+enddiff --git a/migrations/config/schema/intermediate_db/tables/topic_allowed_groups.rb b/migrations/config/schema/intermediate_db/tables/topic_allowed_groups.rbnew file mode 100644index 00000000000..8f20d702890--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/topic_allowed_groups.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :topic_allowed_groups do+ primary_key :topic_id, :group_id++ ignore :id+enddiff --git a/migrations/config/schema/intermediate_db/tables/topic_allowed_users.rb b/migrations/config/schema/intermediate_db/tables/topic_allowed_users.rbnew file mode 100644index 00000000000..f92edb202ac--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/topic_allowed_users.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :topic_allowed_users do+ primary_key :topic_id, :user_id++ ignore :id+enddiff --git a/migrations/config/schema/intermediate_db/tables/topic_tags.rb b/migrations/config/schema/intermediate_db/tables/topic_tags.rbnew file mode 100644index 00000000000..9165f88896f--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/topic_tags.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :topic_tags do+ primary_key :topic_id, :tag_id++ ignore :id+enddiff --git a/migrations/config/schema/intermediate_db/tables/topic_users.rb b/migrations/config/schema/intermediate_db/tables/topic_users.rbnew file mode 100644index 00000000000..ba2d595990f--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/topic_users.rb@@ -0,0 +1,8 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :topic_users do+ primary_key :topic_id, :user_id++ ignore :id+ ignore :bookmarked, :liked, :posted, reason: "Calculated columns"+enddiff --git a/migrations/config/schema/intermediate_db/tables/topics.rb b/migrations/config/schema/intermediate_db/tables/topics.rbnew file mode 100644index 00000000000..375ea02f184--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/topics.rb@@ -0,0 +1,35 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :topics do+ index :archetype++ ignore :bumped_at,+ :excerpt,+ :fancy_title,+ :featured_user1_id,+ :featured_user2_id,+ :featured_user3_id,+ :featured_user4_id,+ :has_summary,+ :highest_post_number,+ :highest_staff_post_number,+ :image_upload_id,+ :incoming_link_count,+ :last_post_user_id,+ :last_posted_at,+ :like_count,+ :locale,+ :moderator_posts_count,+ :notify_moderators_count,+ :participant_count,+ :percent_rank,+ :posts_count,+ :reply_count,+ :reviewable_score,+ :score,+ :slug,+ :slow_mode_seconds,+ :spam_count,+ :word_count,+ reason: "Calculated columns"+enddiff --git a/migrations/config/schema/intermediate_db/tables/uploads.rb b/migrations/config/schema/intermediate_db/tables/uploads.rbnew file mode 100644index 00000000000..c558138de6c--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/uploads.rb@@ -0,0 +1,19 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :uploads do+ model :manual++ synthetic!++ primary_key :id++ add_column :id, :text+ add_column :filename, :text, required: true+ add_column :path, :text+ add_column :data, :blob+ add_column :url, :text+ add_column :type, :text, enum: :upload_type+ add_column :description, :text+ add_column :origin, :text+ add_column :user_id, :numeric+enddiff --git a/migrations/config/schema/intermediate_db/tables/user_associated_accounts.rb b/migrations/config/schema/intermediate_db/tables/user_associated_accounts.rbnew file mode 100644index 00000000000..0faedab8071--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/user_associated_accounts.rb@@ -0,0 +1,9 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :user_associated_accounts do+ primary_key :user_id, :provider_name++ column :last_used, required: false++ ignore :credentials, :extra, :id+enddiff --git a/migrations/config/schema/intermediate_db/tables/user_custom_fields.rb b/migrations/config/schema/intermediate_db/tables/user_custom_fields.rbnew file mode 100644index 00000000000..ae6c3945a2a--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/user_custom_fields.rb@@ -0,0 +1,9 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :user_custom_fields do+ primary_key :user_id, :name, :value++ column :value, required: true++ ignore :id+enddiff --git a/migrations/config/schema/intermediate_db/tables/user_emails.rb b/migrations/config/schema/intermediate_db/tables/user_emails.rbnew file mode 100644index 00000000000..e7d23822251--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/user_emails.rb@@ -0,0 +1,9 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :user_emails do+ primary_key :user_id, :email++ include :email, :primary, :user_id, :created_at++ ignore :id, :normalized_email+enddiff --git a/migrations/config/schema/intermediate_db/tables/user_field_options.rb b/migrations/config/schema/intermediate_db/tables/user_field_options.rbnew file mode 100644index 00000000000..27a1c5d5abf--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/user_field_options.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :user_field_options do+ primary_key :user_field_id, :value++ ignore :id+enddiff --git a/migrations/config/schema/intermediate_db/tables/user_field_values.rb b/migrations/config/schema/intermediate_db/tables/user_field_values.rbnew file mode 100644index 00000000000..b0fc6a34146--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/user_field_values.rb@@ -0,0 +1,22 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :user_field_values do+ copy_structure_from :user_custom_fields+ primary_key :user_id, :field_id, :value++ add_column :field_id, :numeric+ add_column :is_multiselect_field, :boolean++ unique_index :user_id,+ :field_id,+ :value,+ where: "is_multiselect_field = TRUE",+ name: "idx_unique_user_field_values_multiselect"++ unique_index :user_id,+ :field_id,+ where: "is_multiselect_field = FALSE",+ name: "idx_unique_user_field_values_not_multiselect"++ ignore :id, :name+enddiff --git a/migrations/config/schema/intermediate_db/tables/user_fields.rb b/migrations/config/schema/intermediate_db/tables/user_fields.rbnew file mode 100644index 00000000000..82fd98f9cf1--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/user_fields.rb@@ -0,0 +1,5 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :user_fields do+ ignore :field_type, :required+enddiff --git a/migrations/config/schema/intermediate_db/tables/user_options.rb b/migrations/config/schema/intermediate_db/tables/user_options.rbnew file mode 100644index 00000000000..5a82ce19b23--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/user_options.rb@@ -0,0 +1,6 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :user_options do+ include_all+ primary_key :user_id+enddiff --git a/migrations/config/schema/intermediate_db/tables/user_suspensions.rb b/migrations/config/schema/intermediate_db/tables/user_suspensions.rbnew file mode 100644index 00000000000..be091440b17--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/user_suspensions.rb@@ -0,0 +1,13 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :user_suspensions do+ synthetic!++ primary_key :user_id, :suspended_at++ add_column :user_id, :numeric+ add_column :suspended_at, :datetime+ add_column :suspended_till, :datetime+ add_column :suspended_by_id, :numeric+ add_column :reason, :text+enddiff --git a/migrations/config/schema/intermediate_db/tables/users.rb b/migrations/config/schema/intermediate_db/tables/users.rbnew file mode 100644index 00000000000..a9d97f7c6d6--- /dev/null+++ b/migrations/config/schema/intermediate_db/tables/users.rb@@ -0,0 +1,21 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :users do+ add_column :original_username, :text+ add_column :avatar_type, :integer++ column :created_at, required: true++ ignore :flag_level,+ :last_emailed_at,+ :last_posted_at,+ :last_seen_reviewable_id,+ :previous_visit_at,+ :required_fields_version,+ :secure_identifier,+ :seen_notification_id,+ :username_lower,+ reason: "Calculated columns"++ ignore :suspended_at, :suspended_till, reason: "We have the `user_suspensions` table for this"+enddiff --git a/migrations/config/schema/plugin_manifest.yml b/migrations/config/schema/plugin_manifest.ymlnew file mode 100644index 00000000000..c459f9e0cc4--- /dev/null+++ b/migrations/config/schema/plugin_manifest.yml@@ -0,0 +1,226 @@+plugins:+ automation:+ tables:+ - discourse_automation_automations+ - discourse_automation_fields+ - discourse_automation_pending_automations+ - discourse_automation_pending_pms+ - discourse_automation_stats+ - discourse_automation_user_global_notices+ columns: {}+ chat:+ tables:+ - chat_channel_archives+ - chat_channel_custom_fields+ - chat_channels+ - chat_drafts+ - chat_mention_notifications+ - chat_mentions+ - chat_message_custom_fields+ - chat_message_interactions+ - chat_message_links+ - chat_message_reactions+ - chat_message_revisions+ - chat_message_search_data+ - chat_messages+ - chat_pinned_messages+ - chat_thread_custom_fields+ - chat_threads+ - chat_webhook_events+ - direct_message_channels+ - direct_message_users+ - incoming_chat_webhooks+ - user_chat_channel_memberships+ - user_chat_thread_memberships+ columns:+ user_options:+ - chat_email_frequency+ - chat_enabled+ - chat_header_indicator_preference+ - chat_quick_reaction_type+ - chat_quick_reactions_custom+ - chat_send_shortcut+ - chat_separate_sidebar_mode+ - chat_sound+ - dismissed_channel_retention_reminder+ - dismissed_dm_retention_reminder+ - ignore_channel_wide_mention+ - only_chat_push_notifications+ - show_thread_title_prompts+ discourse-adplugin:+ tables:+ - ad_plugin_house_ads+ - ad_plugin_house_ads_categories+ - ad_plugin_house_ads_groups+ - ad_plugin_house_ads_routes+ - ad_plugin_impressions+ columns: {}+ discourse-ai:+ tables:+ - ai_agents+ - ai_api_audit_logs+ - ai_api_request_stats+ - ai_artifact_key_values+ - ai_artifact_versions+ - ai_artifacts+ - ai_document_fragments_embeddings+ - ai_moderation_settings+ - ai_posts_embeddings+ - ai_secrets+ - ai_spam_logs+ - ai_summaries+ - ai_tool_actions+ - ai_tool_secret_bindings+ - ai_tools+ - ai_topics_embeddings+ - chat_message_custom_prompts+ - classification_results+ - completion_prompts+ - embedding_definitions+ - inferred_concept_posts+ - inferred_concept_topics+ - inferred_concepts+ - llm_credit_allocations+ - llm_credit_daily_usages+ - llm_feature_credit_costs+ - llm_models+ - llm_quota_usages+ - llm_quotas+ - model_accuracies+ - post_custom_prompts+ - rag_document_fragments+ - shared_ai_conversations+ columns:+ user_options:+ - ai_search_discoveries+ - auto_image_caption+ discourse-assign:+ tables:+ - assignments+ - silenced_assignments+ columns:+ groups:+ - assignable_level+ user_options:+ - notification_level_when_assigned+ discourse-calendar:+ tables:+ - calendar_events+ - discourse_calendar_disabled_holidays+ - discourse_calendar_post_event_dates+ - discourse_post_event_events+ - discourse_post_event_invitees+ columns: {}+ discourse-data-explorer:+ tables:+ - data_explorer_queries+ - data_explorer_query_groups+ columns: {}+ discourse-gamification:+ tables:+ - gamification_leaderboards+ - gamification_score_events+ - gamification_scores+ columns:+ directory_items:+ - gamification_score+ discourse-github:+ tables:+ - github_commits+ - github_repos+ columns: {}+ discourse-policy:+ tables:+ - policy_users+ - post_policies+ - post_policy_groups+ columns:+ user_options:+ - policy_email_frequency+ discourse-post-voting:+ tables:+ - post_voting_comment_custom_fields+ - post_voting_comments+ - post_voting_votes+ columns:+ posts:+ - qa_vote_count+ discourse-reactions:+ tables:+ - discourse_reactions_reaction_users+ - discourse_reactions_reactions+ columns: {}+ discourse-rewind:+ tables: []+ columns:+ user_options:+ - discourse_rewind_dismissed_at+ - discourse_rewind_enabled+ - discourse_rewind_share_publicly+ discourse-rss-polling:+ tables:+ - discourse_rss_polling_rss_feeds+ columns: {}+ discourse-solved:+ tables:+ - discourse_solved_solved_topics+ columns:+ directory_items:+ - solutions+ discourse-subscriptions:+ tables:+ - discourse_subscriptions_customers+ - discourse_subscriptions_products+ - discourse_subscriptions_subscriptions+ columns: {}+ discourse-templates:+ tables:+ - discourse_templates_usage_count+ columns: {}+ discourse-topic-voting:+ tables:+ - topic_voting_category_settings+ - topic_voting_topic_vote_count+ - topic_voting_votes+ columns: {}+ poll:+ tables:+ - poll_options+ - poll_votes+ - polls+ columns: {}+plugin_checksums:+ automation: e69972690991bc04d92b2d161aac8921+ chat: a44b3089f09fe0cef90e270aa2b4bedb+ discourse-adplugin: 9e52419decf1099c5bc301a90e6cc91a+ discourse-affiliate: '08c6e1a851441a082e9be9ddddc63910'+ discourse-ai: c85508b1ece1b90b668d1a65a1d8af85+ discourse-assign: 65afd026c76ff19a8d4f0ed1d9c21aa1+ discourse-cakeday: a5bb91366c8dc8d267088eb0c2679187+ discourse-calendar: 8ec0c0ab443991a508c337ee137c4bba+ discourse-chat-integration: e39798f3f1a02b0c774f9d6c0595a2c7+ discourse-data-explorer: 3c8891eb000d2f91f1edf1a48c28a96e+ discourse-gamification: 864797c200fde3faf8508c500af6915d+ discourse-github: 3bd17b2c09828e232e7565edbf2c746f+ discourse-graphviz: 2182f4029ce73b9b77ea682a4558087c+ discourse-lazy-videos: a916a4f265d1cc6f99a3e53f5bcd1e9e+ discourse-local-dates: 18d3ec627bf8546fe13bae24c6a5bb6c+ discourse-microsoft-auth: 3a95a5b50edc156ad06f40aadda7d6bc+ discourse-narrative-bot: '086bde6a2d7bdddf802800977eb727ac'+ discourse-oauth2-basic: a9151339b643380530db0eb6548bac36+ discourse-patreon: f0113218c1d5b0d127a6021c5a49d2af+ discourse-policy: 41786c59418426f1933f8b95f6c00e18+ discourse-post-voting: ece13b87f2fbac3e64f8aa3abe07fe3d+ discourse-reactions: e9de82d2ebb48eb434be2b646c7fec26+ discourse-rewind: 1e0f2acf6e8409f2d2f011e14405fa06+ discourse-rss-polling: adb232423452557f71219f3b61ff0465+ discourse-solved: 88ac1f71ea5f4a5cc1c9dcd0ce457511+ discourse-subscriptions: 05eb12877a6a842a290a98fa0e70d462+ discourse-templates: a131c89a3e2b765f1eb6ec811c0fecea+ discourse-topic-voting: b3ab6519c9cbad4ccfbefd17ef2aa41b+ discourse-user-notes: 4c1e04fac5708b51c1b09bd8ed0d786c+ discourse-zendesk-plugin: b78b305590ffedda38455574a19fdd38+ poll: 4af8698c5fcc1dc9a786b31ec8234171+ styleguide: 0fd92e5ef0c99ee047f206961a587544+failed_plugins: []+incomplete: falsediff --git a/migrations/db/intermediate_db_schema/002-log_entries.sql b/migrations/db/intermediate_db_schema/002-log_entries.sqldeleted file mode 100644index a1e50dab1ea..00000000000--- a/migrations/db/intermediate_db_schema/002-log_entries.sql+++ /dev/null@@ -1,8 +0,0 @@-CREATE TABLE log_entries-(- created_at DATETIME NOT NULL,- type TEXT NOT NULL,- message TEXT NOT NULL,- exception TEXT,- details JSON_TEXT-);diff --git a/migrations/db/intermediate_db_schema/003-uploads.sql b/migrations/db/intermediate_db_schema/003-uploads.sqldeleted file mode 100644index deda4841749..00000000000--- a/migrations/db/intermediate_db_schema/003-uploads.sql+++ /dev/null@@ -1,12 +0,0 @@-CREATE TABLE uploads-(- id TEXT NOT NULL PRIMARY KEY,- filename TEXT NOT NULL,- path TEXT,- data BLOB,- url TEXT,- type TEXT,- description TEXT,- origin TEXT,- user_id NUMERIC-);diff --git a/migrations/db/intermediate_db_schema/004-user_suspensions.sql b/migrations/db/intermediate_db_schema/004-user_suspensions.sqldeleted file mode 100644index 70bf4beb8ff..00000000000--- a/migrations/db/intermediate_db_schema/004-user_suspensions.sql+++ /dev/null@@ -1,9 +0,0 @@-CREATE TABLE user_suspensions-(- user_id NUMERIC NOT NULL,- suspended_at DATETIME NOT NULL,- suspended_till DATETIME,- suspended_by_id NUMERIC,- reason TEXT,- PRIMARY KEY (user_id, suspended_at)-);diff --git a/migrations/db/intermediate_db_schema/005-permalink_normalizations.sql b/migrations/db/intermediate_db_schema/005-permalink_normalizations.sqldeleted file mode 100644index 2f262abb356..00000000000--- a/migrations/db/intermediate_db_schema/005-permalink_normalizations.sql+++ /dev/null@@ -1,4 +0,0 @@-CREATE TABLE permalink_normalizations-(- normalization TEXT NOT NULL PRIMARY KEY-);diff --git a/migrations/db/intermediate_db_schema/006-tag_synonyms.sql b/migrations/db/intermediate_db_schema/006-tag_synonyms.sqldeleted file mode 100644index f38980039ac..00000000000--- a/migrations/db/intermediate_db_schema/006-tag_synonyms.sql+++ /dev/null@@ -1,5 +0,0 @@-CREATE TABLE tag_synonyms-(- synonym_tag_id NUMERIC NOT NULL PRIMARY KEY,- target_tag_id NUMERIC NOT NULL-);diff --git a/migrations/db/intermediate_db_schema/100-base-schema.sql b/migrations/db/intermediate_db_schema/100-base-schema.sqlindex 66e8bece499..179d20ce691 100644--- a/migrations/db/intermediate_db_schema/100-base-schema.sql+++ b/migrations/db/intermediate_db_schema/100-base-schema.sql@@ -1,6 +1,6 @@-- This file is auto-generated from the IntermediateDB schema. To make changes,--- update the "config/intermediate_db.yml" configuration file and then run--- `bin/cli schema generate` to regenerate this file.+-- update the configuration files in "migrations/config/schema/" and then run+-- `migrations/bin/cli schema generate` to regenerate this file.CREATE TABLE badge_groupings(@@ -11,6 +11,7 @@ CREATE TABLE badge_groupingsposition INTEGER NOT NULL);+CREATE TABLE badges(original_id NUMERIC NOT NULL PRIMARY KEY,@@ -35,6 +36,7 @@ CREATE TABLE badges"trigger" INTEGER);+CREATE TABLE categories(original_id NUMERIC NOT NULL PRIMARY KEY,@@ -86,6 +88,7 @@ CREATE TABLE categoriesuser_id NUMERIC NOT NULL);+CREATE TABLE category_custom_fields(category_id NUMERIC NOT NULL,@@ -94,13 +97,15 @@ CREATE TABLE category_custom_fieldsPRIMARY KEY (category_id, name));+CREATE TABLE category_moderation_groups(- category_id NUMERIC,- group_id NUMERIC,+ category_id NUMERIC NOT NULL,+ group_id NUMERIC NOT NULL,PRIMARY KEY (category_id, group_id));+CREATE TABLE category_users(category_id NUMERIC NOT NULL,@@ -110,6 +115,7 @@ CREATE TABLE category_usersPRIMARY KEY (category_id, user_id));+CREATE TABLE group_users(group_id NUMERIC NOT NULL,@@ -120,6 +126,7 @@ CREATE TABLE group_usersPRIMARY KEY (group_id, user_id));+CREATE TABLE "groups"(original_id NUMERIC NOT NULL PRIMARY KEY,@@ -149,13 +156,32 @@ CREATE TABLE "groups"visibility_level INTEGER);++CREATE TABLE log_entries+(+ created_at DATETIME NOT NULL,+ details JSON_TEXT,+ exception TEXT,+ message TEXT NOT NULL,+ type ENUM_TEXT NOT NULL+);++CREATE TABLE muted_users(- created_at DATETIME,+ user_id NUMERIC NOT NULL,muted_user_id NUMERIC NOT NULL,- user_id NUMERIC NOT NULL+ created_at DATETIME,+ PRIMARY KEY (user_id, muted_user_id)+);+++CREATE TABLE permalink_normalizations+(+ normalization TEXT NOT NULL PRIMARY KEY);+CREATE TABLE site_settings(name TEXT NOT NULL PRIMARY KEY,@@ -164,6 +190,7 @@ CREATE TABLE site_settingsvalue TEXT);+CREATE TABLE tag_group_memberships(tag_group_id NUMERIC NOT NULL,@@ -172,15 +199,17 @@ CREATE TABLE tag_group_membershipsPRIMARY KEY (tag_group_id, tag_id));+CREATE TABLE tag_group_permissions(- group_id NUMERIC NOT NULL,- permission_type INTEGER,tag_group_id NUMERIC NOT NULL,+ group_id NUMERIC NOT NULL,+ permission_type INTEGER NOT NULL,created_at DATETIME,PRIMARY KEY (tag_group_id, group_id, permission_type));+CREATE TABLE tag_groups(original_id NUMERIC NOT NULL PRIMARY KEY,@@ -190,6 +219,14 @@ CREATE TABLE tag_groupsparent_tag_id NUMERIC);++CREATE TABLE tag_synonyms+(+ synonym_tag_id NUMERIC NOT NULL PRIMARY KEY,+ target_tag_id NUMERIC NOT NULL+);++CREATE TABLE tag_users(tag_id NUMERIC NOT NULL,@@ -199,6 +236,7 @@ CREATE TABLE tag_usersPRIMARY KEY (tag_id, user_id));+CREATE TABLE tags(original_id NUMERIC NOT NULL PRIMARY KEY,@@ -209,13 +247,15 @@ CREATE TABLE tagsslug TEXT NOT NULL);+CREATE TABLE topic_allowed_groups(- group_id NUMERIC NOT NULL,topic_id NUMERIC NOT NULL,+ group_id NUMERIC NOT NULL,PRIMARY KEY (topic_id, group_id));+CREATE TABLE topic_allowed_users(topic_id NUMERIC NOT NULL,@@ -224,14 +264,16 @@ CREATE TABLE topic_allowed_usersPRIMARY KEY (topic_id, user_id));+CREATE TABLE topic_tags(- tag_id NUMERIC NOT NULL,topic_id NUMERIC NOT NULL,+ tag_id NUMERIC NOT NULL,created_at DATETIME,PRIMARY KEY (topic_id, tag_id));+CREATE TABLE topic_users(topic_id NUMERIC NOT NULL,@@ -249,6 +291,7 @@ CREATE TABLE topic_usersPRIMARY KEY (topic_id, user_id));+CREATE TABLE topics(original_id NUMERIC NOT NULL PRIMARY KEY,@@ -273,12 +316,26 @@ CREATE TABLE topicsvisible BOOLEAN);-CREATE INDEX index_topics_on_archetype ON topics (archetype);+CREATE INDEX idx_topics_archetype ON topics (archetype);++CREATE TABLE uploads+(+ id TEXT NOT NULL PRIMARY KEY,+ data BLOB,+ description TEXT,+ filename TEXT NOT NULL,+ origin TEXT,+ path TEXT,+ type ENUM_INTEGER,+ url TEXT,+ user_id NUMERIC+);+CREATE TABLE user_associated_accounts(+ user_id NUMERIC NOT NULL,provider_name TEXT NOT NULL,- user_id NUMERIC,created_at DATETIME,info JSON_TEXT,last_used DATETIME,@@ -286,24 +343,27 @@ CREATE TABLE user_associated_accountsPRIMARY KEY (user_id, provider_name));+CREATE TABLE user_custom_fields(- name TEXT NOT NULL,user_id NUMERIC NOT NULL,- value TEXT,+ name TEXT NOT NULL,+ value TEXT NOT NULL,created_at DATETIME,PRIMARY KEY (user_id, name, value));+CREATE TABLE user_emails(- email TEXT NOT NULL,user_id NUMERIC NOT NULL,+ email TEXT NOT NULL,created_at DATETIME,"primary" BOOLEAN,PRIMARY KEY (user_id, email));+CREATE TABLE user_field_options(user_field_id NUMERIC NOT NULL,@@ -312,17 +372,19 @@ CREATE TABLE user_field_optionsPRIMARY KEY (user_field_id, value));+CREATE TABLE user_field_values(- created_at DATETIME,+ user_id NUMERIC NOT NULL,field_id NUMERIC NOT NULL,+ value TEXT NOT NULL,+ created_at DATETIME,is_multiselect_field BOOLEAN,- user_id NUMERIC NOT NULL,- value TEXT+ PRIMARY KEY (user_id, field_id, value));-CREATE UNIQUE INDEX user_field_values_multiselect_index ON user_field_values (user_id, field_id, value) WHERE is_multiselect_field = TRUE;-CREATE UNIQUE INDEX user_field_values_not_multiselect_index ON user_field_values (user_id, field_id) WHERE is_multiselect_field = FALSE;+CREATE UNIQUE INDEX idx_unique_user_field_values_multiselect ON user_field_values (user_id, field_id, value) WHERE is_multiselect_field = TRUE;+CREATE UNIQUE INDEX idx_unique_user_field_values_not_multiselect ON user_field_values (user_id, field_id) WHERE is_multiselect_field = FALSE;CREATE TABLE user_fields(@@ -342,78 +404,71 @@ CREATE TABLE user_fieldsshow_on_user_card BOOLEAN);+CREATE TABLE user_options(- user_id NUMERIC NOT NULL PRIMARY KEY,- ai_search_discoveries BOOLEAN,- allow_private_messages BOOLEAN,- auto_image_caption BOOLEAN,- auto_track_topics_after_msecs INTEGER,- automatically_unpin_topics BOOLEAN,- bookmark_auto_delete_preference INTEGER,- chat_email_frequency INTEGER,- chat_enabled BOOLEAN,- chat_header_indicator_preference INTEGER,- chat_quick_reaction_type INTEGER,- chat_quick_reactions_custom TEXT,- chat_send_shortcut INTEGER,- chat_separate_sidebar_mode INTEGER,- chat_sound TEXT,- color_scheme_id NUMERIC,- composition_mode INTEGER,- dark_scheme_id NUMERIC,- default_calendar INTEGER,- digest_after_minutes INTEGER,- discourse_rewind_dismissed_at DATETIME,- discourse_rewind_enabled BOOLEAN,- discourse_rewind_share_publicly BOOLEAN,- dismissed_channel_retention_reminder BOOLEAN,- dismissed_dm_retention_reminder BOOLEAN,- dynamic_favicon BOOLEAN,- email_digests BOOLEAN,- email_in_reply_to BOOLEAN,- email_level INTEGER,- email_messages_level INTEGER,- email_previous_replies INTEGER,- enable_allowed_pm_users BOOLEAN,- enable_defer BOOLEAN,- enable_markdown_monospace_font BOOLEAN,- enable_quoting BOOLEAN,- enable_smart_lists BOOLEAN,- external_links_in_new_tab BOOLEAN,- hide_presence BOOLEAN,- hide_profile BOOLEAN,- hide_profile_and_presence BOOLEAN,- homepage_id NUMERIC,- ignore_channel_wide_mention BOOLEAN,- include_tl0_in_digests BOOLEAN,- interface_color_mode INTEGER,- last_redirected_to_top_at DATETIME,- like_notification_frequency INTEGER,- mailing_list_mode BOOLEAN,- mailing_list_mode_frequency INTEGER,- new_topic_duration_minutes INTEGER,- notification_level_when_assigned INTEGER,- notification_level_when_replying INTEGER,- notify_on_linked_posts BOOLEAN,- oldest_search_log_date DATETIME,- only_chat_push_notifications BOOLEAN,- policy_email_frequency INTEGER,- seen_popups INTEGER,- show_thread_title_prompts BOOLEAN,- sidebar_link_to_filtered_list BOOLEAN,- sidebar_show_count_of_new_items BOOLEAN,- skip_new_user_tips BOOLEAN,- text_size_key INTEGER,- text_size_seq INTEGER,- theme_ids INTEGER,- theme_key_seq INTEGER,- timezone TEXT,- title_count_mode_key INTEGER,- topics_unread_when_closed BOOLEAN,- watched_precedence_over_muted BOOLEAN+ user_id NUMERIC NOT NULL PRIMARY KEY,+ allow_private_messages BOOLEAN,+ auto_track_topics_after_msecs INTEGER,+ automatically_unpin_topics BOOLEAN,+ bookmark_auto_delete_preference INTEGER,+ color_scheme_id NUMERIC,+ composition_mode INTEGER,+ dark_scheme_id NUMERIC,+ default_calendar INTEGER,+ digest_after_minutes INTEGER,+ dynamic_favicon BOOLEAN,+ email_digests BOOLEAN,+ email_in_reply_to BOOLEAN,+ email_level INTEGER,+ email_messages_level INTEGER,+ email_previous_replies INTEGER,+ enable_allowed_pm_users BOOLEAN,+ enable_defer BOOLEAN,+ enable_markdown_monospace_font BOOLEAN,+ enable_quoting BOOLEAN,+ enable_smart_lists BOOLEAN,+ external_links_in_new_tab BOOLEAN,+ hide_presence BOOLEAN,+ hide_profile BOOLEAN,+ hide_profile_and_presence BOOLEAN,+ homepage_id NUMERIC,+ include_tl0_in_digests BOOLEAN,+ interface_color_mode INTEGER,+ last_redirected_to_top_at DATETIME,+ like_notification_frequency INTEGER,+ mailing_list_mode BOOLEAN,+ mailing_list_mode_frequency INTEGER,+ new_topic_duration_minutes INTEGER,+ notification_level_when_replying INTEGER,+ notify_on_linked_posts BOOLEAN,+ oldest_search_log_date DATETIME,+ seen_popups INTEGER,+ sidebar_link_to_filtered_list BOOLEAN,+ sidebar_show_count_of_new_items BOOLEAN,+ skip_new_user_tips BOOLEAN,+ text_size_key INTEGER,+ text_size_seq INTEGER,+ theme_ids INTEGER,+ theme_key_seq INTEGER,+ timezone TEXT,+ title_count_mode_key INTEGER,+ topics_unread_when_closed BOOLEAN,+ watched_precedence_over_muted BOOLEAN+);+++CREATE TABLE user_suspensions+(+ user_id NUMERIC NOT NULL,+ suspended_at DATETIME NOT NULL,+ reason TEXT,+ suspended_by_id NUMERIC,+ suspended_till DATETIME,+ PRIMARY KEY (user_id, suspended_at));+CREATE TABLE users(original_id NUMERIC NOT NULL PRIMARY KEY,@@ -446,3 +501,4 @@ CREATE TABLE usersviews INTEGER);+diff --git a/migrations/docs/.gitkeep b/migrations/docs/.gitkeepdeleted file mode 100644index e69de29bb2d..00000000000diff --git a/migrations/docs/schema-configuration.md b/migrations/docs/schema-configuration.mdnew file mode 100644index 00000000000..2aa10a9354a--- /dev/null+++ b/migrations/docs/schema-configuration.md@@ -0,0 +1,385 @@+# Schema Configuration DSL++The schema DSL defines the structure of a database used during migrations. It maps source Discourse+tables to a schema, letting you control which columns to include, rename columns, override types,+add synthetic columns, and define enums.++Config files live in `migrations/config/schema/<database>/` (e.g. `intermediate_db`).++## File layout++```+migrations/config/schema/intermediate_db/+ config.rb # Output paths and namespaces+ conventions.rb # Global column conventions (renames, type overrides)+ ignored.rb # Tables and plugins to exclude+ tables/ # One file per table+ users.rb+ topics.rb+ ...+ enums/ # One file per enum+ upload_type.rb+ ...+```++## CLI commands++| Command | Description |+|----------------------------------------|------------------------------------------------------------|+| `schema add TABLE` | Create a config file for a new table |+| `schema validate` | Validate config against the database |+| `schema diff` | Show differences between config and database |+| `schema generate` | Generate SQL schema, Ruby models, and enum files |+| `schema list` | List configured tables and enums, plus ignored table count |+| `schema ignore TABLE [--reason "..."]` | Add a table to `ignored.rb` |+| `schema refresh-plugins` | Regenerate the plugin manifest |++All commands accept `--db NAME` (default: `intermediate_db`).++## Table configuration++Each table has its own file in `tables/`. The basic structure:++```ruby+# frozen_string_literal: true++Migrations::Database::Schema.table :users do+ include_all+end+```++### Column inclusion strategy++Every source-backed table must specify a column inclusion strategy. There are four approaches:++#### `include_all`++Include every column from the source table. Simplest starting point.++```ruby+Migrations::Database::Schema.table :users do+ include_all+end+```++#### `include`++Include only specific columns. Remaining columns must be explicitly passed to `ignore` — the+validator requires every database column to be accounted for, so new columns are never silently+excluded.++```ruby+Migrations::Database::Schema.table :users do+ include :id, :username, :email, :created_at+ ignore :admin, :moderator, reason: "Not needed"+end+```++#### `include!`++Include columns that are globally ignored (via conventions) or auto-ignored (via plugins). Regular+`include` will produce a validation error for such columns; use `include!` to explicitly override.++```ruby+Migrations::Database::Schema.table :users do+ include :id, :username+ include! :updated_at # override global ignore from conventions+end+```++#### `ignore`++Exclude specific columns; all others are included (implies `include_all`). You should provide a+reason.++```ruby+Migrations::Database::Schema.table :topics do+ ignore :bumped_at, :excerpt, :fancy_title, reason: "Calculated columns"+end+```++### Column options++Use `column` to set options on an included source column:++```ruby+Migrations::Database::Schema.table :users do+ include_all++ column :username, required: true+ column :bio, max_length: 3000+ column :name, rename_to: :display_name+ column :trust_level, type: :numeric+end+```++Available options:++- `type:` - Override the column type (`:text`, `:numeric`, `:boolean`, `:datetime`, `:blob`)+- `required:` - Mark the column as NOT NULL (`true` or `false`)+- `max_length:` - Set a maximum length constraint+- `rename_to:` - Rename the column in the schema++Block syntax is also supported:++```ruby+column :id do+ rename_to :original_id+ type :numeric+end+```++### Adding columns++Use `add_column` for columns that don't exist in the source table:++```ruby+Migrations::Database::Schema.table :uploads do+ synthetic!++ add_column :id, :text+ add_column :filename, :text, required: true+ add_column :type, :text, enum: :upload_type+end+```++Options:++- `required:` - Mark as NOT NULL (default: `false`)+- `enum:` - Reference a defined enum for validation++### Primary keys++Override the primary key when it differs from the source:++```ruby+Migrations::Database::Schema.table :user_field_values do+ copy_structure_from :user_custom_fields+ primary_key :user_id, :field_id, :value+end+```++Single-column primary keys detected from the source are used automatically.++### Source table++#### `copy_structure_from`++Use a different database table as the column source. The resolver reads the actual database columns+from the specified table — it does not copy another table's DSL configuration.++```ruby+Migrations::Database::Schema.table :user_field_values do+ copy_structure_from :user_custom_fields+ # columns are read from user_custom_fields in the database+end+```++#### `synthetic!`++The table has no source table. Only `add_column` is allowed.++```ruby+Migrations::Database::Schema.table :uploads do+ synthetic!++ add_column :id, :text+ add_column :filename, :text, required: true+end+```++### Indexes++Use `index` or `unique_index` to define indexes on one or more columns:++```ruby+index :user_id, :topic_id+unique_index :username+unique_index %i[user_id field_id], where: "value IS NOT NULL"+index :status, name: :idx_custom_name+```++Options:++- `name:` - Override the index name (default: auto-generated from table and column names)+- `where:` - Add a partial index condition (SQL expression)++Column names are required (one or more). They must reference columns that are included, added, or+renamed in the table configuration.++### Constraints++Use `check` to define a check constraint. Both arguments are required.++```ruby+check :positive_score, "score >= 0"+```++Arguments:++- First: constraint name (symbol or string)+- Second: SQL condition (string)++### Plugin support++Columns from plugins listed in `ignored.rb` are always auto-ignored automatically. Use+`ignore_plugin_columns!` for non-ignored plugins whose columns you don't want in the intermediate+schema.++Auto-ignore columns from all non-ignored plugins:++```ruby+Migrations::Database::Schema.table :users do+ include_all+ ignore_plugin_columns!+end+```++Auto-ignore columns from specific plugins only:++```ruby+Migrations::Database::Schema.table :users do+ include_all+ ignore_plugin_columns! :polls, :discourse_ai+end+```++### Model mode++Controls how `schema generate` handles the Ruby model file for this table. There are three modes:++**Default** (no `model` declaration) — the model file is fully regenerated on every run. Any manual+edits will be overwritten.++**`model :extended`** — the model file is regenerated, but custom code between the marker comments+is preserved:++```ruby+model :extended+```++The generated file will contain a section like this:++```ruby+ # -- custom code --+# your custom methods and logic here+# -- end custom code --+```++Code between the markers survives regeneration. Code outside the markers is overwritten.++**`model :manual`** — the model file is not generated at all. Use this when you need full control+and will write the model yourself.++```ruby+model :manual+```++## Conventions++Global column conventions apply across all tables. Defined in `conventions.rb`:++```ruby+Migrations::Database::Schema.conventions do+ # Exact column name match+ column :id do+ rename_to :original_id+ type :numeric+ end++ column :created_at do+ required false+ end++ # Regex pattern match+ columns_matching(/.*upload.*_id$/) { type :text }+ columns_matching(/.*_id$/) { type :numeric }++ # Globally ignored columns (excluded from all tables)+ ignore_columns :updated_at+end+```++Convention methods:++- `column :name` - Match a specific column name, then set `rename_to`, `type`, `required`+- `columns_matching /pattern/` - Match columns by regex pattern+- `ignore_columns :col1, :col2` - Globally ignore columns across all tables++Conventions are applied during schema resolution. Per-table `column` options take precedence.++## Enums++Enums define named value sets. Defined in `enums/`. All values in an enum must be the same type —+either all integers or all strings.++Integer enum:++```ruby+Migrations::Database::Schema.enum :visibility do+ value :public, 0+ value :private, 1+ value :restricted, 2+end+```++String enum:++```ruby+Migrations::Database::Schema.enum :color do+ value :red, "red"+ value :green, "green"+ value :blue, "blue"+end+```++From a Ruby constant (must return a Hash or Array):++```ruby+Migrations::Database::Schema.enum :upload_type do+ source { ::UploadCreator::TYPES_TO_CROP }+end+```++## Ignored tables++Tables and plugins to exclude entirely. Defined in `ignored.rb`:++```ruby+Migrations::Database::Schema.ignored do+ # Ignore all tables and columns from a plugin+ plugin :chat, "Not migrated yet"++ # Ignore specific tables (reason is optional)+ table :user_actions, "Not needed"+ table :drafts+ tables :notifications, :bookmarks, reason: "Not needed"+end+```++## Output configuration++Controls where `schema generate` writes the SQL schema file, Ruby models, and enum modules. Defined+in `config.rb`:++```ruby+Migrations::Database::Schema.configure do+ output do+ schema_file "db/intermediate_db_schema/100-base-schema.sql"++ models_directory "lib/database/intermediate_db"+ models_namespace "Migrations::Database::IntermediateDB"++ enums_directory "lib/database/intermediate_db/enums"+ enums_namespace "Migrations::Database::IntermediateDB::Enums"+ end+end+```++## Workflow++1. **Add** a new table: `schema add users`+2. **Edit** the generated file in `tables/users.rb`+3. **Validate** your config: `schema validate`+4. **Check differences**: `schema diff`+5. **Generate** the schema, models, and enums: `schema generate`diff --git a/migrations/lib/cli/exception_handler.rb b/migrations/lib/cli/exception_handler.rbindex 40efe8746b2..23fb6cfe532 100644--- a/migrations/lib/cli/exception_handler.rb+++ b/migrations/lib/cli/exception_handler.rb@@ -8,15 +8,17 @@ module Migrationsrescue ClassFilter::UnknownClassNamesError => ehandle_unknown_class_names_error(e)exit(1)+ rescue Migrations::Database::Schema::ConfigError,+ Migrations::Database::Schema::GenerationError => e+ puts e.message.red+ exit(1)rescue => eputs "An error occurred: #{e.message}".redputs e.backtrace.join("\n")exit(1)end- private-- def self.handle_unknown_class_names_error(error)+ private_class_method def self.handle_unknown_class_names_error(error)all_suggestions_found = trueerror.missing_names.each do |missing_name|diff --git a/migrations/lib/cli/schema_sub_command.rb b/migrations/lib/cli/schema_sub_command.rbindex a285214c2e9..06b6f41c1fe 100644--- a/migrations/lib/cli/schema_sub_command.rb+++ b/migrations/lib/cli/schema_sub_command.rb@@ -2,119 +2,237 @@module Migrations::CLIclass SchemaSubCommand < Thor+ remove_command :tree+Schema = ::Migrations::Database::Schema- desc "generate", "Generates the database schema"- method_option :db, type: :string, default: "intermediate_db", desc: "Name of the database"- def generate- db = options[:db]- config = load_config_file(db)+ class_option :database,+ aliases: %w[--db],+ type: :string,+ default: "intermediate_db",+ desc: "Database configuration to use"- puts "Generating schema for #{db.bold}..."- ::Migrations.load_rails_environment(quiet: true)+ desc "validate", "Validate schema configuration against the database"+ def validate+ load_rails!- validate_config(config)+ database = selected_database- loader = Schema::Loader.new(config[:schema])- schema = loader.load_schema- header = file_header(db)+ errors = Schema.validate(database:)+ print_validation_errors(errors)++ puts "✓ Schema valid".green+ end- write_db_schema(config, header, schema)- write_db_models(config, header, schema)- write_enums(config, header, schema)+ desc "generate", "Generate SQL schema, Ruby models, and enum files"+ def generate+ load_rails!- validate_schema(db)+ database = selected_database+ resolved = Schema.generate(database:)- puts "Done"+ puts+ table_count = resolved.tables.size+ enum_count = resolved.enums.size+ tables_str = "#{table_count} #{"table".pluralize(table_count)}"+ enums_str = "#{enum_count} #{"enum".pluralize(enum_count)}"+ puts "✓ Generated #{tables_str}, #{enums_str}".greenend- private+ desc "list", "List configured tables and enums, plus ignored table counts"+ def list+ load_rails!+ database = selected_database+ Schema.ensure_ready!(database:)- def validate_config(config)- validator = Schema::ConfigValidator.new- validator.validate(config)+ tables = Schema.tables+ ignored = Schema.ignored_tables+ effective_ignored = Schema.effective_ignored_table_names(database:)+ enums = Schema.enums- if validator.has_errors?- validator.errors.each { |error| print_error(error) }- exit(2)- end+ puts "Configured tables (#{tables.size}):"+ tables.keys.sort.each { |t| puts " #{t}" }+ puts++ puts "Enums (#{enums.size}):"+ enums.keys.sort.each { |e| puts " #{e}" }+ puts++ explicit_ignored_count = ignored ? ignored.table_names.size : 0+ effective_ignored_count = effective_ignored.size+ ignored_plugin_count = ignored ? ignored.ignored_plugin_names.size : 0++ puts "Ignored tables: #{explicit_ignored_count} explicit, #{effective_ignored_count} effective"+ puts "Ignored plugins: #{ignored_plugin_count}"end- def write_db_schema(config, header, schema)- schema_file_path = File.expand_path(config.dig(:output, :schema_file), ::Migrations.root_path)+ desc "ignore TABLE", "Add a table to ignored.rb"+ method_option :reason, type: :string, desc: "Optional reason for ignoring the table"+ def ignore(table_name)+ load_rails!- File.open(schema_file_path, "w") do |schema_file|- writer = Schema::TableWriter.new(schema_file)- writer.output_file_header(header)+ database = selected_database+ Schema.ignore_table(table_name, reason: options[:reason], database:)+ puts "✓ Added #{table_name} to ignored.rb".green+ end- schema.tables.each { |table| writer.output_table(table) }- end+ desc "diff", "Show differences between configuration and database"+ method_option :verbose, type: :boolean, default: false, desc: "Show auto-ignored plugin columns"+ def diff+ load_rails!++ database = selected_database+ result = Schema.diff(database:)+ display_diff(result, verbose: options[:verbose])+ end++ desc "add TABLE", "Create a config file for a new table"+ def add(table_name)+ load_rails!++ database = selected_database+ path = Schema.add_table(table_name, database:)+ puts "✓ Created #{path}".green+ puts+ puts "Next steps:"+ puts " 1. Edit the file to configure columns"+ puts " 2. Run 'migrations/bin/cli schema validate'"end- def write_db_models(config, header, schema)- model_namespace = config.dig(:output, :models_namespace)- enum_namespace = config.dig(:output, :enums_namespace)- writer = Schema::ModelWriter.new(model_namespace, enum_namespace, header)- models_path = File.expand_path(config.dig(:output, :models_directory), ::Migrations.root_path)+ desc "refresh-plugins", "Regenerate the plugin manifest"+ method_option :force, type: :boolean, default: false, desc: "Force regeneration"+ def refresh_plugins+ load_rails!- schema.tables.each do |table|- model_file_path = File.join(models_path, Schema::ModelWriter.filename_for(table))- File.open(model_file_path, "w") { |model_file| writer.output_table(table, model_file) }- end+ database = selected_database+ Schema.ensure_ready!(database:, refresh_manifest: false)++ manifest = Schema.plugin_manifest- Schema.format_ruby_files(models_path)+ if options[:force] || !manifest.fresh? || manifest.incomplete?+ puts "Detecting plugin tables and columns..."+ manifest.regenerate!+ if manifest.incomplete?+ failed_plugins = manifest.failed_plugins.join(", ").presence || "(unknown)"+ puts "Plugin manifest updated with warnings (failed plugins: #{failed_plugins})"+ else+ puts "✓ Plugin manifest updated".green+ end+ puts " Tables: #{manifest.table_count}"+ puts " Columns: #{manifest.column_count}"+ puts " Plugins: #{manifest.all_plugin_names.join(", ")}"+ else+ puts "Plugin manifest is up to date"+ puts " Use --force to regenerate"+ endend- def write_enums(config, header, schema)- writer = Schema::EnumWriter.new(config.dig(:output, :enums_namespace), header)- enums_path = File.expand_path(config.dig(:output, :enums_directory), ::Migrations.root_path)+ private- schema.enums.each do |enum|- enum_file_path = File.join(enums_path, Schema::EnumWriter.filename_for(enum))- File.open(enum_file_path, "w") { |enum_file| writer.output_enum(enum, enum_file) }+ def load_rails!+ ::Migrations.load_rails_environment(quiet: true)+ end++ def validate_database_option!(database)+ unless File.directory?(Schema.schema_root_path)+ raise(+ Schema::ConfigError,+ "Schema configuration directory not found: #{Schema.schema_root_path}",+ )end- Schema.format_ruby_files(enums_path)+ available = Schema.available_databases+ return if available.include?(database)++ raise(+ Schema::ConfigError,+ "Unknown database '#{database}'. Available: #{available.join(", ")}",+ )end- def relative_config_path(db)- File.join("config", "#{db}.yml")+ def selected_database+ database = options[:database].to_s+ validate_database_option!(database)+ databaseend- def file_header(db)- <<~HEADER- This file is auto-generated from the IntermediateDB schema. To make changes,- update the "#{relative_config_path(db)}" configuration file and then run- `bin/cli schema generate` to regenerate this file.- HEADER+ def print_validation_errors(errors)+ return if errors.empty?++ errors.each { |e| puts "✗ #{e}".red }+ puts+ error_count = errors.size+ puts "#{error_count} #{"error".pluralize(error_count)}"+ exit 1end- def load_config_file(db)- config_path = File.join(::Migrations.root_path, relative_config_path(db))+ def display_diff(result, verbose: false)+ sections = []- if !File.exist?(config_path)- print_error("Configuration file for #{db} wasn't found at '#{config_path}'")- exit 1+ if result.unconfigured_tables.any?+ lines = ["Unconfigured tables (add to tables/ or ignored.rb):".bold]+ result.unconfigured_tables.each do |t|+ plugin_info = t.plugin ? " [#{t.plugin}]" : ""+ lines << " + #{t.name}#{plugin_info}".green+ end+ sections << lines.join("\n")end- YAML.load_file(config_path, symbolize_names: true)- end+ if result.missing_tables.any?+ lines = ["Missing tables (configured but not in database):".bold]+ result.missing_tables.each { |t| lines << " - #{t.name}".red }+ sections << lines.join("\n")+ end- def validate_schema(type)- Tempfile.create do |tempfile|- begin- ::Migrations::Database.migrate(- tempfile,- migrations_path: ::Migrations::Database.schema_path(type),- )- rescue Extralite::SQLError => e- print_error("Invalid schema: #{e.message}")+ if result.stale_ignored_tables.any?+ lines = ["Stale ignored tables (no longer in database):".bold]+ result.stale_ignored_tables.each { |t| lines << " ~ #{t.name}".yellow }+ sections << lines.join("\n")+ end++ table_diffs = filter_table_diffs(result.table_diffs, verbose:)++ if table_diffs.any?+ lines = ["Column differences:".bold]+ table_diffs.each do |table_diff|+ lines << " #{table_diff.table_name}:".bold++ table_diff.unconfigured_columns.each do |c|+ plugin_info = c.plugin ? " [#{c.plugin}]" : ""+ lines << " + #{c.name}#{plugin_info}".green+ end++ table_diff.missing_columns.each { |c| lines << " - #{c.name}".red }+ table_diff.stale_ignored_columns.each do |c|+ lines << " ~ #{c.name} (ignored but gone)".yellow+ end++ if verbose+ table_diff.auto_ignored_columns.each do |c|+ lines << " #{c.name} [#{c.plugin}] (auto-ignored from plugin)".cyan+ end+ endend+ sections << lines.join("\n")+ end++ if sections.any?+ puts sections.join("\n\n")+ puts+ puts "Suggested actions:".bold+ puts " migrations/bin/cli schema add <table>"+ puts " migrations/bin/cli schema ignore <table> [--reason \"...\"]"+ else+ puts "✓ No differences found".greenendend- def print_error(message)- $stderr.puts "ERROR: ".red + message+ def filter_table_diffs(table_diffs, verbose:)+ return table_diffs if verbose++ table_diffs.select do |td|+ td.unconfigured_columns.any? || td.missing_columns.any? || td.stale_ignored_columns.any?+ endendendenddiff --git a/migrations/lib/database/intermediate_db/badge.rb b/migrations/lib/database/intermediate_db/badge.rbindex 1fa47e2e2cb..49a09d3dc79 100644--- a/migrations/lib/database/intermediate_db/badge.rb+++ b/migrations/lib/database/intermediate_db/badge.rb@@ -1,8 +1,8 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule Badgediff --git a/migrations/lib/database/intermediate_db/badge_grouping.rb b/migrations/lib/database/intermediate_db/badge_grouping.rbindex 57cca36fbce..d38988eecbd 100644--- a/migrations/lib/database/intermediate_db/badge_grouping.rb+++ b/migrations/lib/database/intermediate_db/badge_grouping.rb@@ -1,8 +1,8 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule BadgeGroupingdiff --git a/migrations/lib/database/intermediate_db/category.rb b/migrations/lib/database/intermediate_db/category.rbindex b71eb85e019..2c7206b820f 100644--- a/migrations/lib/database/intermediate_db/category.rb+++ b/migrations/lib/database/intermediate_db/category.rb@@ -1,8 +1,8 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule Categorydiff --git a/migrations/lib/database/intermediate_db/category_custom_field.rb b/migrations/lib/database/intermediate_db/category_custom_field.rbindex 12a8cee77b4..957ad29d9ba 100644--- a/migrations/lib/database/intermediate_db/category_custom_field.rb+++ b/migrations/lib/database/intermediate_db/category_custom_field.rb@@ -1,8 +1,8 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule CategoryCustomFielddiff --git a/migrations/lib/database/intermediate_db/category_moderation_group.rb b/migrations/lib/database/intermediate_db/category_moderation_group.rbindex 9ca29c65bbd..f52ca8a2d1e 100644--- a/migrations/lib/database/intermediate_db/category_moderation_group.rb+++ b/migrations/lib/database/intermediate_db/category_moderation_group.rb@@ -1,8 +1,8 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule CategoryModerationGroup@@ -19,8 +19,8 @@ module Migrations::Database::IntermediateDB# Creates a new `category_moderation_groups` record in the IntermediateDB.#- # @param category_id [Integer, String, nil]- # @param group_id [Integer, String, nil]+ # @param category_id [Integer, String]+ # @param group_id [Integer, String]## @return [void]def self.create(category_id:, group_id:)diff --git a/migrations/lib/database/intermediate_db/category_user.rb b/migrations/lib/database/intermediate_db/category_user.rbindex e4319a23504..230e0c85dec 100644--- a/migrations/lib/database/intermediate_db/category_user.rb+++ b/migrations/lib/database/intermediate_db/category_user.rb@@ -1,8 +1,8 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule CategoryUserdiff --git a/migrations/lib/database/intermediate_db/enums/log_entry_type.rb b/migrations/lib/database/intermediate_db/enums/log_entry_type.rbnew file mode 100644index 00000000000..851f76999d4--- /dev/null+++ b/migrations/lib/database/intermediate_db/enums/log_entry_type.rb@@ -0,0 +1,15 @@+# frozen_string_literal: true++# This file is auto-generated from the IntermediateDB schema. To make changes,+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.++module Migrations::Database::IntermediateDB::Enums+ module LogEntryType+ extend ::Migrations::Enum++ ERROR = "error"+ INFO = "info"+ WARNING = "warning"+ end+enddiff --git a/migrations/lib/database/intermediate_db/enums/site_setting_datatype.rb b/migrations/lib/database/intermediate_db/enums/site_setting_datatype.rbindex 1521ef80e23..8b719f69e61 100644--- a/migrations/lib/database/intermediate_db/enums/site_setting_datatype.rb+++ b/migrations/lib/database/intermediate_db/enums/site_setting_datatype.rb@@ -1,8 +1,8 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDB::Enumsmodule SiteSettingDatatypediff --git a/migrations/lib/database/intermediate_db/enums/site_setting_import_mode.rb b/migrations/lib/database/intermediate_db/enums/site_setting_import_mode.rbindex aa1f478a2fb..d6ab6039cca 100644--- a/migrations/lib/database/intermediate_db/enums/site_setting_import_mode.rb+++ b/migrations/lib/database/intermediate_db/enums/site_setting_import_mode.rb@@ -1,8 +1,8 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDB::Enumsmodule SiteSettingImportModediff --git a/migrations/lib/database/intermediate_db/enums/upload_type.rb b/migrations/lib/database/intermediate_db/enums/upload_type.rbnew file mode 100644index 00000000000..b17ecc5e52d--- /dev/null+++ b/migrations/lib/database/intermediate_db/enums/upload_type.rb@@ -0,0 +1,16 @@+# frozen_string_literal: true++# This file is auto-generated from the IntermediateDB schema. To make changes,+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.++module Migrations::Database::IntermediateDB::Enums+ module UploadType+ extend ::Migrations::Enum++ AVATAR = 0+ CARD_BACKGROUND = 1+ CUSTOM_EMOJI = 2+ PROFILE_BACKGROUND = 3+ end+enddiff --git a/migrations/lib/database/intermediate_db/group.rb b/migrations/lib/database/intermediate_db/group.rbindex f1f2e4720a5..58ba148e3d6 100644--- a/migrations/lib/database/intermediate_db/group.rb+++ b/migrations/lib/database/intermediate_db/group.rb@@ -1,8 +1,8 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule Groupdiff --git a/migrations/lib/database/intermediate_db/group_user.rb b/migrations/lib/database/intermediate_db/group_user.rbindex 4022c569dd1..0b387ac8bac 100644--- a/migrations/lib/database/intermediate_db/group_user.rb+++ b/migrations/lib/database/intermediate_db/group_user.rb@@ -1,8 +1,8 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule GroupUserdiff --git a/migrations/lib/database/intermediate_db/muted_user.rb b/migrations/lib/database/intermediate_db/muted_user.rbindex 5bd5e15ef32..b4d02948159 100644--- a/migrations/lib/database/intermediate_db/muted_user.rb+++ b/migrations/lib/database/intermediate_db/muted_user.rb@@ -1,16 +1,16 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule MutedUserSQL = <<~SQLINSERT INTO muted_users (- created_at,+ user_id,muted_user_id,- user_id+ created_at)VALUES (?, ?, ?@@ -20,17 +20,17 @@ module Migrations::Database::IntermediateDB# Creates a new `muted_users` record in the IntermediateDB.#- # @param created_at [Time, nil]- # @param muted_user_id [Integer, String]# @param user_id [Integer, String]+ # @param muted_user_id [Integer, String]+ # @param created_at [Time, nil]## @return [void]- def self.create(created_at: nil, muted_user_id:, user_id:)+ def self.create(user_id:, muted_user_id:, created_at: nil)::Migrations::Database::IntermediateDB.insert(SQL,- ::Migrations::Database.format_datetime(created_at),- muted_user_id,user_id,+ muted_user_id,+ ::Migrations::Database.format_datetime(created_at),)endenddiff --git a/migrations/lib/database/intermediate_db/permalink_normalization.rb b/migrations/lib/database/intermediate_db/permalink_normalization.rbindex 1328de0bb9e..8e53383c36e 100644--- a/migrations/lib/database/intermediate_db/permalink_normalization.rb+++ b/migrations/lib/database/intermediate_db/permalink_normalization.rb@@ -1,13 +1,26 @@# frozen_string_literal: true+# This file is auto-generated from the IntermediateDB schema. To make changes,+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.+module Migrations::Database::IntermediateDBmodule PermalinkNormalizationSQL = <<~SQL- INSERT INTO permalink_normalizations (normalization)- VALUES (?)+ INSERT INTO permalink_normalizations (+ normalization+ )+ VALUES (+ ?+ )SQLprivate_constant :SQL+ # Creates a new `permalink_normalizations` record in the IntermediateDB.+ #+ # @param normalization [String]+ #+ # @return [void]def self.create(normalization:)::Migrations::Database::IntermediateDB.insert(SQL, normalization)enddiff --git a/migrations/lib/database/intermediate_db/site_setting.rb b/migrations/lib/database/intermediate_db/site_setting.rbindex 438f0fbc361..a253020d883 100644--- a/migrations/lib/database/intermediate_db/site_setting.rb+++ b/migrations/lib/database/intermediate_db/site_setting.rb@@ -1,8 +1,8 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule SiteSettingdiff --git a/migrations/lib/database/intermediate_db/tag.rb b/migrations/lib/database/intermediate_db/tag.rbindex c6d520f8957..df8c2bdf103 100644--- a/migrations/lib/database/intermediate_db/tag.rb+++ b/migrations/lib/database/intermediate_db/tag.rb@@ -1,8 +1,8 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule Tagdiff --git a/migrations/lib/database/intermediate_db/tag_group.rb b/migrations/lib/database/intermediate_db/tag_group.rbindex c65210e3405..768886597e4 100644--- a/migrations/lib/database/intermediate_db/tag_group.rb+++ b/migrations/lib/database/intermediate_db/tag_group.rb@@ -1,8 +1,8 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule TagGroupdiff --git a/migrations/lib/database/intermediate_db/tag_group_membership.rb b/migrations/lib/database/intermediate_db/tag_group_membership.rbindex 10e46732c53..6968105c34c 100644--- a/migrations/lib/database/intermediate_db/tag_group_membership.rb+++ b/migrations/lib/database/intermediate_db/tag_group_membership.rb@@ -1,8 +1,8 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule TagGroupMembershipdiff --git a/migrations/lib/database/intermediate_db/tag_group_permission.rb b/migrations/lib/database/intermediate_db/tag_group_permission.rbindex 28a74a926dc..e45dd93dff1 100644--- a/migrations/lib/database/intermediate_db/tag_group_permission.rb+++ b/migrations/lib/database/intermediate_db/tag_group_permission.rb@@ -1,16 +1,16 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule TagGroupPermissionSQL = <<~SQLINSERT INTO tag_group_permissions (+ tag_group_id,group_id,permission_type,- tag_group_id,created_at)VALUES (@@ -21,18 +21,18 @@ module Migrations::Database::IntermediateDB# Creates a new `tag_group_permissions` record in the IntermediateDB.#- # @param group_id [Integer, String]- # @param permission_type [Integer, nil]# @param tag_group_id [Integer, String]+ # @param group_id [Integer, String]+ # @param permission_type [Integer]# @param created_at [Time, nil]## @return [void]- def self.create(group_id:, permission_type:, tag_group_id:, created_at: nil)+ def self.create(tag_group_id:, group_id:, permission_type:, created_at: nil)::Migrations::Database::IntermediateDB.insert(SQL,+ tag_group_id,group_id,permission_type,- tag_group_id,::Migrations::Database.format_datetime(created_at),)enddiff --git a/migrations/lib/database/intermediate_db/tag_synonym.rb b/migrations/lib/database/intermediate_db/tag_synonym.rbindex 3e0e8e94eb5..9a81b684a55 100644--- a/migrations/lib/database/intermediate_db/tag_synonym.rb+++ b/migrations/lib/database/intermediate_db/tag_synonym.rb@@ -1,13 +1,28 @@# frozen_string_literal: true+# This file is auto-generated from the IntermediateDB schema. To make changes,+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.+module Migrations::Database::IntermediateDBmodule TagSynonymSQL = <<~SQL- INSERT INTO tag_synonyms (synonym_tag_id, target_tag_id)- VALUES (?, ?)+ INSERT INTO tag_synonyms (+ synonym_tag_id,+ target_tag_id+ )+ VALUES (+ ?, ?+ )SQLprivate_constant :SQL+ # Creates a new `tag_synonyms` record in the IntermediateDB.+ #+ # @param synonym_tag_id [Integer, String]+ # @param target_tag_id [Integer, String]+ #+ # @return [void]def self.create(synonym_tag_id:, target_tag_id:)::Migrations::Database::IntermediateDB.insert(SQL, synonym_tag_id, target_tag_id)enddiff --git a/migrations/lib/database/intermediate_db/tag_user.rb b/migrations/lib/database/intermediate_db/tag_user.rbindex 31f579a5935..fcfa45750eb 100644--- a/migrations/lib/database/intermediate_db/tag_user.rb+++ b/migrations/lib/database/intermediate_db/tag_user.rb@@ -1,8 +1,8 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule TagUserdiff --git a/migrations/lib/database/intermediate_db/topic.rb b/migrations/lib/database/intermediate_db/topic.rbindex 4045bb43747..c701d411bc2 100644--- a/migrations/lib/database/intermediate_db/topic.rb+++ b/migrations/lib/database/intermediate_db/topic.rb@@ -1,8 +1,8 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule Topicdiff --git a/migrations/lib/database/intermediate_db/topic_allowed_group.rb b/migrations/lib/database/intermediate_db/topic_allowed_group.rbindex ef3d4f3094f..ac8e4ff6ccd 100644--- a/migrations/lib/database/intermediate_db/topic_allowed_group.rb+++ b/migrations/lib/database/intermediate_db/topic_allowed_group.rb@@ -1,15 +1,15 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule TopicAllowedGroupSQL = <<~SQLINSERT INTO topic_allowed_groups (- group_id,- topic_id+ topic_id,+ group_id)VALUES (?, ?@@ -19,12 +19,12 @@ module Migrations::Database::IntermediateDB# Creates a new `topic_allowed_groups` record in the IntermediateDB.#- # @param group_id [Integer, String]# @param topic_id [Integer, String]+ # @param group_id [Integer, String]## @return [void]- def self.create(group_id:, topic_id:)- ::Migrations::Database::IntermediateDB.insert(SQL, group_id, topic_id)+ def self.create(topic_id:, group_id:)+ ::Migrations::Database::IntermediateDB.insert(SQL, topic_id, group_id)endendenddiff --git a/migrations/lib/database/intermediate_db/topic_allowed_user.rb b/migrations/lib/database/intermediate_db/topic_allowed_user.rbindex 771acf9eeb3..b2c2683bebf 100644--- a/migrations/lib/database/intermediate_db/topic_allowed_user.rb+++ b/migrations/lib/database/intermediate_db/topic_allowed_user.rb@@ -1,8 +1,8 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule TopicAllowedUserdiff --git a/migrations/lib/database/intermediate_db/topic_tag.rb b/migrations/lib/database/intermediate_db/topic_tag.rbindex dfbe71f472a..33da85b1d07 100644--- a/migrations/lib/database/intermediate_db/topic_tag.rb+++ b/migrations/lib/database/intermediate_db/topic_tag.rb@@ -1,15 +1,15 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule TopicTagSQL = <<~SQLINSERT INTO topic_tags (- tag_id,topic_id,+ tag_id,created_at)VALUES (@@ -20,16 +20,16 @@ module Migrations::Database::IntermediateDB# Creates a new `topic_tags` record in the IntermediateDB.#- # @param tag_id [Integer, String]# @param topic_id [Integer, String]+ # @param tag_id [Integer, String]# @param created_at [Time, nil]## @return [void]- def self.create(tag_id:, topic_id:, created_at: nil)+ def self.create(topic_id:, tag_id:, created_at: nil)::Migrations::Database::IntermediateDB.insert(SQL,- tag_id,topic_id,+ tag_id,::Migrations::Database.format_datetime(created_at),)enddiff --git a/migrations/lib/database/intermediate_db/topic_user.rb b/migrations/lib/database/intermediate_db/topic_user.rbindex 9cae543b302..4f96718bbfc 100644--- a/migrations/lib/database/intermediate_db/topic_user.rb+++ b/migrations/lib/database/intermediate_db/topic_user.rb@@ -1,8 +1,8 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule TopicUserdiff --git a/migrations/lib/database/intermediate_db/user.rb b/migrations/lib/database/intermediate_db/user.rbindex 333dccb6f21..7c8c0c4af01 100644--- a/migrations/lib/database/intermediate_db/user.rb+++ b/migrations/lib/database/intermediate_db/user.rb@@ -1,8 +1,8 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule Userdiff --git a/migrations/lib/database/intermediate_db/user_associated_account.rb b/migrations/lib/database/intermediate_db/user_associated_account.rbindex d25b0f46bfc..d696370f734 100644--- a/migrations/lib/database/intermediate_db/user_associated_account.rb+++ b/migrations/lib/database/intermediate_db/user_associated_account.rb@@ -1,15 +1,15 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule UserAssociatedAccountSQL = <<~SQLINSERT INTO user_associated_accounts (- provider_name,user_id,+ provider_name,created_at,info,last_used,@@ -23,8 +23,8 @@ module Migrations::Database::IntermediateDB# Creates a new `user_associated_accounts` record in the IntermediateDB.#+ # @param user_id [Integer, String]# @param provider_name [String]- # @param user_id [Integer, String, nil]# @param created_at [Time, nil]# @param info [Object, nil]# @param last_used [Time, nil]@@ -32,8 +32,8 @@ module Migrations::Database::IntermediateDB## @return [void]def self.create(- provider_name:,user_id:,+ provider_name:,created_at: nil,info: nil,last_used: nil,@@ -41,8 +41,8 @@ module Migrations::Database::IntermediateDB)::Migrations::Database::IntermediateDB.insert(SQL,- provider_name,user_id,+ provider_name,::Migrations::Database.format_datetime(created_at),::Migrations::Database.to_json(info),::Migrations::Database.format_datetime(last_used),diff --git a/migrations/lib/database/intermediate_db/user_custom_field.rb b/migrations/lib/database/intermediate_db/user_custom_field.rbindex 5c9e466e9f2..665615db957 100644--- a/migrations/lib/database/intermediate_db/user_custom_field.rb+++ b/migrations/lib/database/intermediate_db/user_custom_field.rb@@ -1,15 +1,15 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule UserCustomFieldSQL = <<~SQLINSERT INTO user_custom_fields (- name,user_id,+ name,value,created_at)@@ -21,17 +21,17 @@ module Migrations::Database::IntermediateDB# Creates a new `user_custom_fields` record in the IntermediateDB.#- # @param name [String]# @param user_id [Integer, String]- # @param value [String, nil]+ # @param name [String]+ # @param value [String]# @param created_at [Time, nil]## @return [void]- def self.create(name:, user_id:, value:, created_at: nil)+ def self.create(user_id:, name:, value:, created_at: nil)::Migrations::Database::IntermediateDB.insert(SQL,- name,user_id,+ name,value,::Migrations::Database.format_datetime(created_at),)diff --git a/migrations/lib/database/intermediate_db/user_email.rb b/migrations/lib/database/intermediate_db/user_email.rbindex b9e696065e1..be69027f6ff 100644--- a/migrations/lib/database/intermediate_db/user_email.rb+++ b/migrations/lib/database/intermediate_db/user_email.rb@@ -1,15 +1,15 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule UserEmailSQL = <<~SQLINSERT INTO user_emails (- email,user_id,+ email,created_at,"primary")@@ -21,17 +21,17 @@ module Migrations::Database::IntermediateDB# Creates a new `user_emails` record in the IntermediateDB.#- # @param email [String]# @param user_id [Integer, String]+ # @param email [String]# @param created_at [Time, nil]# @param primary [Boolean, nil]## @return [void]- def self.create(email:, user_id:, created_at: nil, primary: nil)+ def self.create(user_id:, email:, created_at: nil, primary: nil)::Migrations::Database::IntermediateDB.insert(SQL,- email,user_id,+ email,::Migrations::Database.format_datetime(created_at),::Migrations::Database.format_boolean(primary),)diff --git a/migrations/lib/database/intermediate_db/user_field.rb b/migrations/lib/database/intermediate_db/user_field.rbindex 898213ff18a..5583deeab1f 100644--- a/migrations/lib/database/intermediate_db/user_field.rb+++ b/migrations/lib/database/intermediate_db/user_field.rb@@ -1,8 +1,8 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule UserFielddiff --git a/migrations/lib/database/intermediate_db/user_field_option.rb b/migrations/lib/database/intermediate_db/user_field_option.rbindex bc3fe6c9db7..8e9c8c3015e 100644--- a/migrations/lib/database/intermediate_db/user_field_option.rb+++ b/migrations/lib/database/intermediate_db/user_field_option.rb@@ -1,8 +1,8 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule UserFieldOptiondiff --git a/migrations/lib/database/intermediate_db/user_field_value.rb b/migrations/lib/database/intermediate_db/user_field_value.rbindex bcf5216cf41..88bbf9bc509 100644--- a/migrations/lib/database/intermediate_db/user_field_value.rb+++ b/migrations/lib/database/intermediate_db/user_field_value.rb@@ -1,18 +1,18 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule UserFieldValueSQL = <<~SQLINSERT INTO user_field_values (- created_at,- field_id,- is_multiselect_field,user_id,- value+ field_id,+ value,+ created_at,+ is_multiselect_field)VALUES (?, ?, ?, ?, ?@@ -22,21 +22,21 @@ module Migrations::Database::IntermediateDB# Creates a new `user_field_values` record in the IntermediateDB.#- # @param created_at [Time, nil]+ # @param user_id [Integer, String]# @param field_id [Integer, String]+ # @param value [String]+ # @param created_at [Time, nil]# @param is_multiselect_field [Boolean, nil]- # @param user_id [Integer, String]- # @param value [String, nil]## @return [void]- def self.create(created_at: nil, field_id:, is_multiselect_field: nil, user_id:, value: nil)+ def self.create(user_id:, field_id:, value:, created_at: nil, is_multiselect_field: nil)::Migrations::Database::IntermediateDB.insert(SQL,- ::Migrations::Database.format_datetime(created_at),- field_id,- ::Migrations::Database.format_boolean(is_multiselect_field),user_id,+ field_id,value,+ ::Migrations::Database.format_datetime(created_at),+ ::Migrations::Database.format_boolean(is_multiselect_field),)endenddiff --git a/migrations/lib/database/intermediate_db/user_option.rb b/migrations/lib/database/intermediate_db/user_option.rbindex 73f519f1df2..2bc7ec3e478 100644--- a/migrations/lib/database/intermediate_db/user_option.rb+++ b/migrations/lib/database/intermediate_db/user_option.rb@@ -1,38 +1,23 @@# frozen_string_literal: true# This file is auto-generated from the IntermediateDB schema. To make changes,-# update the "config/intermediate_db.yml" configuration file and then run-# `bin/cli schema generate` to regenerate this file.+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.module Migrations::Database::IntermediateDBmodule UserOptionSQL = <<~SQLINSERT INTO user_options (user_id,- ai_search_discoveries,allow_private_messages,- auto_image_caption,auto_track_topics_after_msecs,automatically_unpin_topics,bookmark_auto_delete_preference,- chat_email_frequency,- chat_enabled,- chat_header_indicator_preference,- chat_quick_reaction_type,- chat_quick_reactions_custom,- chat_send_shortcut,- chat_separate_sidebar_mode,- chat_sound,color_scheme_id,composition_mode,dark_scheme_id,default_calendar,digest_after_minutes,- discourse_rewind_dismissed_at,- discourse_rewind_enabled,- discourse_rewind_share_publicly,- dismissed_channel_retention_reminder,- dismissed_dm_retention_reminder,dynamic_favicon,email_digests,email_in_reply_to,@@ -49,7 +34,6 @@ module Migrations::Database::IntermediateDBhide_profile,hide_profile_and_presence,homepage_id,- ignore_channel_wide_mention,include_tl0_in_digests,interface_color_mode,last_redirected_to_top_at,@@ -57,14 +41,10 @@ module Migrations::Database::IntermediateDBmailing_list_mode,mailing_list_mode_frequency,new_topic_duration_minutes,- notification_level_when_assigned,notification_level_when_replying,notify_on_linked_posts,oldest_search_log_date,- only_chat_push_notifications,- policy_email_frequency,seen_popups,- show_thread_title_prompts,sidebar_link_to_filtered_list,sidebar_show_count_of_new_items,skip_new_user_tips,@@ -79,110 +59,74 @@ module Migrations::Database::IntermediateDB)VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,- ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,- ?, ?, ?, ?, ?, ?, ?, ?+ ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)SQLprivate_constant :SQL# Creates a new `user_options` record in the IntermediateDB.#- # @param user_id [Integer, String]- # @param ai_search_discoveries [Boolean, nil]- # @param allow_private_messages [Boolean, nil]- # @param auto_image_caption [Boolean, nil]- # @param auto_track_topics_after_msecs [Integer, nil]- # @param automatically_unpin_topics [Boolean, nil]- # @param bookmark_auto_delete_preference [Integer, nil]- # @param chat_email_frequency [Integer, nil]- # @param chat_enabled [Boolean, nil]- # @param chat_header_indicator_preference [Integer, nil]- # @param chat_quick_reaction_type [Integer, nil]- # @param chat_quick_reactions_custom [String, nil]- # @param chat_send_shortcut [Integer, nil]- # @param chat_separate_sidebar_mode [Integer, nil]- # @param chat_sound [String, nil]- # @param color_scheme_id [Integer, String, nil]- # @param composition_mode [Integer, nil]- # @param dark_scheme_id [Integer, String, nil]- # @param default_calendar [Integer, nil]- # @param digest_after_minutes [Integer, nil]- # @param discourse_rewind_dismissed_at [Time, nil]- # @param discourse_rewind_enabled [Boolean, nil]- # @param discourse_rewind_share_publicly [Boolean, nil]- # @param dismissed_channel_retention_reminder [Boolean, nil]- # @param dismissed_dm_retention_reminder [Boolean, nil]- # @param dynamic_favicon [Boolean, nil]- # @param email_digests [Boolean, nil]- # @param email_in_reply_to [Boolean, nil]- # @param email_level [Integer, nil]- # @param email_messages_level [Integer, nil]- # @param email_previous_replies [Integer, nil]- # @param enable_allowed_pm_users [Boolean, nil]- # @param enable_defer [Boolean, nil]- # @param enable_markdown_monospace_font [Boolean, nil]- # @param enable_quoting [Boolean, nil]- # @param enable_smart_lists [Boolean, nil]- # @param external_links_in_new_tab [Boolean, nil]- # @param hide_presence [Boolean, nil]- # @param hide_profile [Boolean, nil]- # @param hide_profile_and_presence [Boolean, nil]- # @param homepage_id [Integer, String, nil]- # @param ignore_channel_wide_mention [Boolean, nil]- # @param include_tl0_in_digests [Boolean, nil]- # @param interface_color_mode [Integer, nil]- # @param last_redirected_to_top_at [Time, nil]- # @param like_notification_frequency [Integer, nil]- # @param mailing_list_mode [Boolean, nil]- # @param mailing_list_mode_frequency [Integer, nil]- # @param new_topic_duration_minutes [Integer, nil]- # @param notification_level_when_assigned [Integer, nil]- # @param notification_level_when_replying [Integer, nil]- # @param notify_on_linked_posts [Boolean, nil]- # @param oldest_search_log_date [Time, nil]- # @param only_chat_push_notifications [Boolean, nil]- # @param policy_email_frequency [Integer, nil]- # @param seen_popups [Integer, nil]- # @param show_thread_title_prompts [Boolean, nil]- # @param sidebar_link_to_filtered_list [Boolean, nil]- # @param sidebar_show_count_of_new_items [Boolean, nil]- # @param skip_new_user_tips [Boolean, nil]- # @param text_size_key [Integer, nil]- # @param text_size_seq [Integer, nil]- # @param theme_ids [Integer, nil]- # @param theme_key_seq [Integer, nil]- # @param timezone [String, nil]- # @param title_count_mode_key [Integer, nil]- # @param topics_unread_when_closed [Boolean, nil]- # @param watched_precedence_over_muted [Boolean, nil]+ # @param user_id [Integer, String]+ # @param allow_private_messages [Boolean, nil]+ # @param auto_track_topics_after_msecs [Integer, nil]+ # @param automatically_unpin_topics [Boolean, nil]+ # @param bookmark_auto_delete_preference [Integer, nil]+ # @param color_scheme_id [Integer, String, nil]+ # @param composition_mode [Integer, nil]+ # @param dark_scheme_id [Integer, String, nil]+ # @param default_calendar [Integer, nil]+ # @param digest_after_minutes [Integer, nil]+ # @param dynamic_favicon [Boolean, nil]+ # @param email_digests [Boolean, nil]+ # @param email_in_reply_to [Boolean, nil]+ # @param email_level [Integer, nil]+ # @param email_messages_level [Integer, nil]+ # @param email_previous_replies [Integer, nil]+ # @param enable_allowed_pm_users [Boolean, nil]+ # @param enable_defer [Boolean, nil]+ # @param enable_markdown_monospace_font [Boolean, nil]+ # @param enable_quoting [Boolean, nil]+ # @param enable_smart_lists [Boolean, nil]+ # @param external_links_in_new_tab [Boolean, nil]+ # @param hide_presence [Boolean, nil]+ # @param hide_profile [Boolean, nil]+ # @param hide_profile_and_presence [Boolean, nil]+ # @param homepage_id [Integer, String, nil]+ # @param include_tl0_in_digests [Boolean, nil]+ # @param interface_color_mode [Integer, nil]+ # @param last_redirected_to_top_at [Time, nil]+ # @param like_notification_frequency [Integer, nil]+ # @param mailing_list_mode [Boolean, nil]+ # @param mailing_list_mode_frequency [Integer, nil]+ # @param new_topic_duration_minutes [Integer, nil]+ # @param notification_level_when_replying [Integer, nil]+ # @param notify_on_linked_posts [Boolean, nil]+ # @param oldest_search_log_date [Time, nil]+ # @param seen_popups [Integer, nil]+ # @param sidebar_link_to_filtered_list [Boolean, nil]+ # @param sidebar_show_count_of_new_items [Boolean, nil]+ # @param skip_new_user_tips [Boolean, nil]+ # @param text_size_key [Integer, nil]+ # @param text_size_seq [Integer, nil]+ # @param theme_ids [Integer, nil]+ # @param theme_key_seq [Integer, nil]+ # @param timezone [String, nil]+ # @param title_count_mode_key [Integer, nil]+ # @param topics_unread_when_closed [Boolean, nil]+ # @param watched_precedence_over_muted [Boolean, nil]## @return [void]def self.create(user_id:,- ai_search_discoveries: nil,allow_private_messages: nil,- auto_image_caption: nil,auto_track_topics_after_msecs: nil,automatically_unpin_topics: nil,bookmark_auto_delete_preference: nil,- chat_email_frequency: nil,- chat_enabled: nil,- chat_header_indicator_preference: nil,- chat_quick_reaction_type: nil,- chat_quick_reactions_custom: nil,- chat_send_shortcut: nil,- chat_separate_sidebar_mode: nil,- chat_sound: nil,color_scheme_id: nil,composition_mode: nil,dark_scheme_id: nil,default_calendar: nil,digest_after_minutes: nil,- discourse_rewind_dismissed_at: nil,- discourse_rewind_enabled: nil,- discourse_rewind_share_publicly: nil,- dismissed_channel_retention_reminder: nil,- dismissed_dm_retention_reminder: nil,dynamic_favicon: nil,email_digests: nil,email_in_reply_to: nil,@@ -199,7 +143,6 @@ module Migrations::Database::IntermediateDBhide_profile: nil,hide_profile_and_presence: nil,homepage_id: nil,- ignore_channel_wide_mention: nil,include_tl0_in_digests: nil,interface_color_mode: nil,last_redirected_to_top_at: nil,@@ -207,14 +150,10 @@ module Migrations::Database::IntermediateDBmailing_list_mode: nil,mailing_list_mode_frequency: nil,new_topic_duration_minutes: nil,- notification_level_when_assigned: nil,notification_level_when_replying: nil,notify_on_linked_posts: nil,oldest_search_log_date: nil,- only_chat_push_notifications: nil,- policy_email_frequency: nil,seen_popups: nil,- show_thread_title_prompts: nil,sidebar_link_to_filtered_list: nil,sidebar_show_count_of_new_items: nil,skip_new_user_tips: nil,@@ -230,30 +169,15 @@ module Migrations::Database::IntermediateDB::Migrations::Database::IntermediateDB.insert(SQL,user_id,- ::Migrations::Database.format_boolean(ai_search_discoveries),::Migrations::Database.format_boolean(allow_private_messages),- ::Migrations::Database.format_boolean(auto_image_caption),auto_track_topics_after_msecs,::Migrations::Database.format_boolean(automatically_unpin_topics),bookmark_auto_delete_preference,- chat_email_frequency,- ::Migrations::Database.format_boolean(chat_enabled),- chat_header_indicator_preference,- chat_quick_reaction_type,- chat_quick_reactions_custom,- chat_send_shortcut,- chat_separate_sidebar_mode,- chat_sound,color_scheme_id,composition_mode,dark_scheme_id,default_calendar,digest_after_minutes,- ::Migrations::Database.format_datetime(discourse_rewind_dismissed_at),- ::Migrations::Database.format_boolean(discourse_rewind_enabled),- ::Migrations::Database.format_boolean(discourse_rewind_share_publicly),- ::Migrations::Database.format_boolean(dismissed_channel_retention_reminder),- ::Migrations::Database.format_boolean(dismissed_dm_retention_reminder),::Migrations::Database.format_boolean(dynamic_favicon),::Migrations::Database.format_boolean(email_digests),::Migrations::Database.format_boolean(email_in_reply_to),@@ -270,7 +194,6 @@ module Migrations::Database::IntermediateDB::Migrations::Database.format_boolean(hide_profile),::Migrations::Database.format_boolean(hide_profile_and_presence),homepage_id,- ::Migrations::Database.format_boolean(ignore_channel_wide_mention),::Migrations::Database.format_boolean(include_tl0_in_digests),interface_color_mode,::Migrations::Database.format_datetime(last_redirected_to_top_at),@@ -278,14 +201,10 @@ module Migrations::Database::IntermediateDB::Migrations::Database.format_boolean(mailing_list_mode),mailing_list_mode_frequency,new_topic_duration_minutes,- notification_level_when_assigned,notification_level_when_replying,::Migrations::Database.format_boolean(notify_on_linked_posts),::Migrations::Database.format_datetime(oldest_search_log_date),- ::Migrations::Database.format_boolean(only_chat_push_notifications),- policy_email_frequency,seen_popups,- ::Migrations::Database.format_boolean(show_thread_title_prompts),::Migrations::Database.format_boolean(sidebar_link_to_filtered_list),::Migrations::Database.format_boolean(sidebar_show_count_of_new_items),::Migrations::Database.format_boolean(skip_new_user_tips),diff --git a/migrations/lib/database/intermediate_db/user_suspension.rb b/migrations/lib/database/intermediate_db/user_suspension.rbindex edf5e087e60..c17184021e6 100644--- a/migrations/lib/database/intermediate_db/user_suspension.rb+++ b/migrations/lib/database/intermediate_db/user_suspension.rb@@ -1,14 +1,18 @@# frozen_string_literal: true+# This file is auto-generated from the IntermediateDB schema. To make changes,+# update the configuration files in "migrations/config/schema/" and then run+# `migrations/bin/cli schema generate` to regenerate this file.+module Migrations::Database::IntermediateDBmodule UserSuspensionSQL = <<~SQLINSERT INTO user_suspensions (user_id,suspended_at,- suspended_till,+ reason,suspended_by_id,- reason+ suspended_till)VALUES (?, ?, ?, ?, ?@@ -16,14 +20,23 @@ module Migrations::Database::IntermediateDBSQLprivate_constant :SQL- def self.create(user_id:, suspended_at:, suspended_till: nil, suspended_by_id: nil, reason: nil)+ # Creates a new `user_suspensions` record in the IntermediateDB.+ #+ # @param user_id [Integer, String]+ # @param suspended_at [Time]+ # @param reason [String, nil]+ # @param suspended_by_id [Integer, String, nil]+ # @param suspended_till [Time, nil]+ #+ # @return [void]+ def self.create(user_id:, suspended_at:, reason: nil, suspended_by_id: nil, suspended_till: nil)::Migrations::Database::IntermediateDB.insert(SQL,user_id,::Migrations::Database.format_datetime(suspended_at),- ::Migrations::Database.format_datetime(suspended_till),- suspended_by_id,reason,+ suspended_by_id,+ ::Migrations::Database.format_datetime(suspended_till),)endenddiff --git a/migrations/lib/database/schema.rb b/migrations/lib/database/schema.rbindex b13d58e91f2..cd29b83b732 100644--- a/migrations/lib/database/schema.rb+++ b/migrations/lib/database/schema.rb@@ -3,10 +3,19 @@module Migrations::Databasemodule SchemaDefinition = Data.define(:tables, :enums)+ PreflightResult = Data.define(:resolved, :errors)TableDefinition =- Data.define(:name, :columns, :indexes, :primary_key_column_names, :constraints) do+ Data.define(+ :name,+ :columns,+ :indexes,+ :primary_key_column_names,+ :constraints,+ :model_mode,+ ) dodef sorted_columns- columns.sort_by { |c| [c.is_primary_key ? 0 : 1, c.name] }+ pk_position = primary_key_column_names.each_with_index.to_h+ columns.sort_by { |c| [c.is_primary_key ? 0 : 1, pk_position.fetch(c.name, 0), c.name] }endendColumnDefinition = Data.define(:name, :datatype, :nullable, :max_length, :is_primary_key, :enum)@@ -17,187 +26,200 @@ module Migrations::Databaseclass ConfigError < StandardErrorend- SQLITE_KEYWORDS = %w[- abort- action- add- after- all- alter- always- analyze- and- as- asc- attach- autoincrement- before- begin- between- by- cascade- case- cast- check- collate- column- commit- conflict- constraint- create- cross- current- current_date- current_time- current_timestamp- database- default- deferrable- deferred- delete- desc- detach- distinct- do- drop- each+ class GenerationError < StandardError+ end++ # --- DSL Registration Methods ---++ def self.configure(&block)+ builder = DSL::ConfigBuilder.new+ builder.instance_eval(&block)+ registry.register_config(builder.build)+ end++ def self.conventions(&block)+ builder = DSL::ConventionsBuilder.new+ builder.instance_eval(&block)+ registry.register_conventions(builder.build)+ end++ def self.table(name, &block)+ builder = DSL::TableBuilder.new(name)+ if block+ builder.instance_eval(&block)else+ builder.include_allend- escape- except- exclude- exclusive- exists- explain- fail- filter- first- following- for- foreign- from- full- generated- glob- group- groups- having- if- ignore- immediate- in- index- indexed- initially- inner- insert- instead- intersect- into- is- isnull- join- key- last- left- like- limit- match- materialized- natural- no- not- nothing- notnull- null- nulls- of- offset- on- or- order- others- outer- over- partition- plan- pragma- preceding- primary- query- raise- range- recursive- references- regexp- reindex- release- rename- replace- restrict- returning- right- rollback- row- rows- savepoint- select- set- table- temp- temporary- then- ties- to- transaction- trigger- unbounded- union- unique- update- using- vacuum- values- view- virtual- when- where- window- with- without- ]-- def self.escape_identifier(identifier)- if SQLITE_KEYWORDS.include?(identifier)- %Q("#{identifier}")- else- identifier+ registry.register_table(name, builder.build)+ end++ def self.enum(name, &block)+ builder = DSL::EnumBuilder.new(name)+ builder.instance_eval(&block)+ registry.register_enum(name, builder.build)+ end++ def self.ignored(&block)+ builder = DSL::IgnoredBuilder.new+ builder.instance_eval(&block)+ registry.register_ignored(builder.build)+ end++ # --- Accessor Methods ---++ def self.tables+ registry.tables+ end++ def self.find_table(name)+ registry.table(name)+ end++ def self.enums+ registry.enums+ end++ def self.config+ registry.config+ end++ def self.conventions_config+ registry.conventions_config+ end++ def self.ignored_tables+ registry.ignored_tables+ end++ def self.effective_ignored_table_names(database: :intermediate_db)+ ensure_ready!(database:)+ DSL::ColumnScope.new(self).ignored_table_name_set+ end++ def self.plugin_manifest+ @plugin_manifest ||=+ DSL::PluginManifest.new(manifest_path:, plugins_path: File.join(Rails.root, "plugins"))+ end++ # --- Validation, Resolution & Generation ---++ def self.preflight(database: :intermediate_db)+ ensure_ready!(database:)++ errors = DSL::Validator.new(self).validate+ return PreflightResult.new(resolved: nil, errors:) if errors.any?++ resolved = DSL::SchemaResolver.new(self).resolve+ errors = DSL::ResolvedSchemaValidator.new(resolved).validate++ PreflightResult.new(resolved:, errors:)+ end++ def self.validate(database: :intermediate_db)+ preflight(database:).errors+ end++ def self.generate(database: :intermediate_db)+ ensure_ready!(database:)+ DSL::Generator.new(self, database:).generate+ end++ def self.diff(database: :intermediate_db)+ ensure_ready!(database:)+ DSL::Differ.new(self).diff+ end++ def self.add_table(table_name, database: :intermediate_db)+ ensure_ready!(database:)+ DSL::Scaffolder.new(self, table_name, database:).scaffold!+ end++ def self.ignore_table(table_name, reason: nil, database: :intermediate_db)+ ensure_ready!(database:)++ raise ConfigError, "Table '#{table_name}' is already configured" if find_table(table_name)++ ActiveRecord::Base.with_connection do |connection|+ if connection.tables.exclude?(table_name)+ raise ConfigError, "Table '#{table_name}' does not exist in the database"+ end+ end++ DSL::IgnoredFileEditor.new(config_path(database)).add_table(table_name, reason:)+ end++ # --- Lifecycle Methods ---++ def self.ensure_ready!(database: :intermediate_db, refresh_manifest: true)+ db_key = database.to_sym+ path = config_path(database)++ unless File.directory?(schema_root_path)+ raise ConfigError, "Schema configuration directory not found: #{schema_root_path}"+ end++ unless File.directory?(path)+ available = available_databases.join(", ")+ raise ConfigError, "Unknown database '#{database}'. Available: #{available}"end++ return if @ready == db_key++ reset!+ begin+ DSL::Loader.new(path).load!+ registry.freeze!+ refresh_plugin_manifest! if refresh_manifest+ @ready = db_key+ rescue StandardError+ reset!+ raise+ end+ end++ def self.schema_root_path+ File.join(Migrations.root_path, "config", "schema")end- def self.to_singular_classname(snake_case_string)- snake_case_string.downcase.singularize.camelize+ def self.config_path(database = :intermediate_db)+ File.join(schema_root_path, database.to_s)end- def self.to_const_name(name)- name.parameterize.underscore.upcase+ private_class_method def self.manifest_path+ File.join(Migrations.root_path, "config", "schema", "plugin_manifest.yml")end- def self.format_ruby_files(path)- glob_pattern = File.join(path, "*.rb")+ private_class_method def self.refresh_plugin_manifest!+ manifest = plugin_manifest+ return if manifest.fresh?++ $stdout.write("Plugin manifest outdated, regenerating... ")+ manifest.regenerate!+ if manifest.incomplete?+ failed_plugins = manifest.failed_plugins.join(", ").presence || "(unknown)"+ puts "Detected plugin changes, but some plugin migrations failed: #{failed_plugins}"+ else+ puts "Detected #{manifest.table_count} plugin tables, #{manifest.column_count} plugin columns."+ end+ rescue StandardError => e+ raise ConfigError, "Skipped — #{e.message} (use 'schema refresh-plugins --force' to retry)"+ end++ def self.available_databases+ dir = schema_root_path+ return [] unless File.directory?(dir)++ Dir.children(dir).select { |d| File.directory?(File.join(dir, d)) }.sort+ end++ def self.reset!+ @registry = nil+ @ready = nil+ @plugin_manifest = nil+ end- system(- "bundle",- "exec",- "stree",- "write",- glob_pattern,- exception: true,- out: File::NULL,- err: File::NULL,- )- rescue StandardError- raise "Failed to run `bundle exec stree write '#{glob_pattern}'`"+ private_class_method def self.registry+ @registry ||= DSL::Registry.newendendenddiff --git a/migrations/lib/database/schema/config_validator.rb b/migrations/lib/database/schema/config_validator.rbdeleted file mode 100644index 17ffaf436aa..00000000000--- a/migrations/lib/database/schema/config_validator.rb+++ /dev/null@@ -1,46 +0,0 @@-# frozen_string_literal: true--module Migrations::Database::Schema- class ConfigValidator- attr_reader :errors-- def initialize- @errors = []- end-- def validate(config)- @errors.clear-- validate_with_json_schema(config)- return self if has_errors?-- validate_output_config(config)- validate_schema_config(config)- validate_plugins(config)-- self- end-- def has_errors?- @errors.any?- end-- private-- def validate_with_json_schema(config)- Validation::JsonSchemaValidator.new(config, @errors).validate- end-- def validate_output_config(config)- Validation::OutputConfigValidator.new(config, @errors).validate- end-- def validate_schema_config(config)- Validation::SchemaConfigValidator.new(config, @errors).validate- end-- def validate_plugins(config)- Validation::PluginConfigValidator.new(config, @errors).validate- end- end-enddiff --git a/migrations/lib/database/schema/dsl.rb b/migrations/lib/database/schema/dsl.rbnew file mode 100644index 00000000000..7bb44da1a6d--- /dev/null+++ b/migrations/lib/database/schema/dsl.rb@@ -0,0 +1,4 @@+# frozen_string_literal: true++module Migrations::Database::Schema::DSL+enddiff --git a/migrations/lib/database/schema/dsl/column_scope.rb b/migrations/lib/database/schema/dsl/column_scope.rbnew file mode 100644index 00000000000..700f9627570--- /dev/null+++ b/migrations/lib/database/schema/dsl/column_scope.rb@@ -0,0 +1,94 @@+# frozen_string_literal: true++module Migrations::Database::Schema::DSL+ class ColumnScope+ def initialize(schema)+ @schema = schema+ end++ def ignored_table_name_set+ ignored = @schema.ignored_tables+ return Set.new if ignored.nil?++ names = ignored.table_names++ manifest = @schema.plugin_manifest+ if manifest.available?+ ignored.ignored_plugin_names.each do |plugin_name|+ manifest.tables_for_plugin(plugin_name).each { |t| names << t }+ end+ end++ names+ end++ def globally_ignored_columns+ @globally_ignored_columns ||=+ begin+ conventions = @schema.conventions_config+ return Set.new if conventions.nil?+ conventions.ignored_columns.to_set+ end+ end++ def effective_column_names(table_def, db_column_names)+ forced = table_def.forced_column_names&.to_set || Set.new++ if table_def.included_column_names+ names = table_def.included_column_names.to_set+ else+ ignored = table_def.ignored_column_names.to_set+ auto_ignored = (globally_ignored_columns | plugin_ignored_column_names(table_def)) - forced+ names = db_column_names - ignored - auto_ignored+ end++ added = table_def.added_columns.map(&:name)+ names + added.to_set+ end++ def plugin_ignored_column_names(table_def)+ names = Set.new+ each_plugin_ignored_column(table_def) { |col, _| names << col }+ names+ end++ def each_plugin_ignored_column(table_def, &block)+ return if table_def.source_table_name.nil?++ ignored = @schema.ignored_tables+ return if ignored.nil?++ manifest = @schema.plugin_manifest+ return if !manifest.available?++ table_name = table_def.source_table_name++ yield_columns_from_ignored_plugins(manifest, ignored, table_name, &block)+ yield_columns_from_table_plugins(manifest, ignored, table_def, table_name, &block)+ end++ private++ def yield_columns_from_ignored_plugins(manifest, ignored, table_name, &)+ ignored.ignored_plugin_names.each do |plugin_name|+ manifest+ .columns_for_plugin(plugin_name, table: table_name)+ .each { |col| yield col, plugin_name }+ end+ end++ def yield_columns_from_table_plugins(manifest, ignored, table_def, table_name, &)+ return if !table_def.ignore_plugin_columns?++ plugin_filter = table_def.ignore_plugin_names&.to_set++ manifest.all_plugin_names.each do |plugin_name|+ next if ignored.plugin_ignored?(plugin_name)+ next if plugin_filter && plugin_filter.exclude?(plugin_name)+ manifest+ .columns_for_plugin(plugin_name, table: table_name)+ .each { |col| yield col, plugin_name }+ end+ end+ end+enddiff --git a/migrations/lib/database/schema/dsl/config_builder.rb b/migrations/lib/database/schema/dsl/config_builder.rbnew file mode 100644index 00000000000..a01abefbff8--- /dev/null+++ b/migrations/lib/database/schema/dsl/config_builder.rb@@ -0,0 +1,88 @@+# frozen_string_literal: true++module Migrations::Database::Schema::DSL+ OutputConfig =+ Data.define(+ :schema_file,+ :models_directory,+ :models_namespace,+ :enums_directory,+ :enums_namespace,+ )++ Configuration = Data.define(:output_config)++ class ConfigBuilder+ def initialize+ @output_config = nil+ end++ def output(&block)+ builder = OutputBuilder.new+ builder.instance_eval(&block)+ @output_config = builder.build+ end++ def build+ unless @output_config+ raise Migrations::Database::Schema::ConfigError,+ "Configuration must include an `output` block."+ end+ Configuration.new(output_config: @output_config)+ end+ end++ class OutputBuilder+ def initialize+ @schema_file = nil+ @models_directory = nil+ @models_namespace = nil+ @enums_directory = nil+ @enums_namespace = nil+ end++ def schema_file(value)+ @schema_file = value+ end++ def models_directory(value)+ @models_directory = value+ end++ def models_namespace(value)+ @models_namespace = value+ end++ def enums_directory(value)+ @enums_directory = value+ end++ def enums_namespace(value)+ @enums_namespace = value+ end++ def build+ missing_fields = []+ missing_fields << "schema_file" if @schema_file.blank?+ missing_fields << "models_directory" if @models_directory.blank?+ missing_fields << "models_namespace" if @models_namespace.blank?+ missing_fields << "enums_directory" if @enums_directory.blank?+ missing_fields << "enums_namespace" if @enums_namespace.blank?++ if missing_fields.any?+ raise(+ Migrations::Database::Schema::ConfigError,+ "Output configuration missing required fields: #{missing_fields.join(", ")}.",+ )+ end++ OutputConfig.new(+ schema_file: @schema_file,+ models_directory: @models_directory,+ models_namespace: @models_namespace,+ enums_directory: @enums_directory,+ enums_namespace: @enums_namespace,+ )+ end+ end+enddiff --git a/migrations/lib/database/schema/dsl/conventions_builder.rb b/migrations/lib/database/schema/dsl/conventions_builder.rbnew file mode 100644index 00000000000..1a26d439629--- /dev/null+++ b/migrations/lib/database/schema/dsl/conventions_builder.rb@@ -0,0 +1,92 @@+# frozen_string_literal: true++module Migrations::Database::Schema::DSL+ Convention = Data.define(:name, :pattern, :rename_to, :type_override, :required)++ ConventionsConfig =+ Data.define(:conventions, :ignored_columns) do+ def effective_name(column_name)+ convention = convention_for(column_name)+ return convention.rename_to if convention&.rename_to+ column_name.to_s+ end++ def convention_for(column_name)+ column_name = column_name.to_s+ # Exact name match first, then regex pattern match+ conventions.find { |c| c.name == column_name } ||+ conventions.find { |c| c.pattern&.match?(column_name) }+ end++ def required?(column_name)+ convention_for(column_name)&.required == true+ end++ def ignored_column?(column_name)+ ignored_columns.include?(column_name.to_s)+ end+ end++ class ConventionsBuilder+ def initialize+ @conventions = []+ @ignored_columns = []+ end++ def column(name, &block)+ builder = ConventionEntryBuilder.new(name: name.to_s)+ builder.instance_eval(&block)+ @conventions << builder.build+ end++ def columns_matching(pattern, &block)+ pattern = Regexp.new(pattern) unless pattern.is_a?(Regexp)+ builder = ConventionEntryBuilder.new(pattern:)+ builder.instance_eval(&block)+ @conventions << builder.build+ end++ def ignore_columns(*names)+ @ignored_columns.concat(names.flatten.map(&:to_s))+ end++ def build+ ConventionsConfig.new(+ conventions: @conventions.freeze,+ ignored_columns: @ignored_columns.freeze,+ )+ end+ end++ class ConventionEntryBuilder+ def initialize(name: nil, pattern: nil)+ @name = name+ @pattern = pattern+ @rename_to = nil+ @type_override = nil+ @required = nil+ end++ def rename_to(value)+ @rename_to = value.to_s+ end++ def type(value)+ @type_override = value.to_s+ end++ def required(value = true)+ @required = value+ end++ def build+ Convention.new(+ name: @name,+ pattern: @pattern,+ rename_to: @rename_to,+ type_override: @type_override,+ required: @required,+ )+ end+ end+enddiff --git a/migrations/lib/database/schema/dsl/differ.rb b/migrations/lib/database/schema/dsl/differ.rbnew file mode 100644index 00000000000..4821bcbe997--- /dev/null+++ b/migrations/lib/database/schema/dsl/differ.rb@@ -0,0 +1,166 @@+# frozen_string_literal: true++module Migrations::Database::Schema::DSL+ TableInfo = Data.define(:name, :plugin)+ ColumnInfo = Data.define(:name, :plugin)+ TableDiff =+ Data.define(+ :table_name,+ :unconfigured_columns,+ :missing_columns,+ :stale_ignored_columns,+ :auto_ignored_columns,+ )+ DiffResult =+ Data.define(:unconfigured_tables, :missing_tables, :stale_ignored_tables, :table_diffs)++ class Differ+ def initialize(schema_module)+ @schema = schema_module+ end++ def diff+ ActiveRecord::Base.with_connection do |connection|+ @db = connection+ @db_table_names = @db.tables.to_set+ @scope = ColumnScope.new(@schema)++ unconfigured_tables = find_unconfigured_tables+ missing_tables = find_missing_tables+ stale_ignored = find_stale_ignored_tables+ table_diffs = find_table_diffs++ DiffResult.new(+ unconfigured_tables:,+ missing_tables:,+ stale_ignored_tables: stale_ignored,+ table_diffs:,+ )+ end+ end++ private++ def find_unconfigured_tables+ configured = @schema.tables.each_value.filter_map(&:source_table_name).to_set+ ignored = @scope.ignored_table_name_set++ unconfigured = @db_table_names - configured - ignored+ unconfigured.sort.map { |name| TableInfo.new(name:, plugin: plugin_for_table(name)) }+ end++ def find_missing_tables+ missing = []++ @schema.tables.each_value do |table_def|+ next if table_def.source_table_name.nil?++ source = table_def.source_table_name+ if @db_table_names.exclude?(source)+ missing << TableInfo.new(name: table_def.name, plugin: nil)+ end+ end++ missing.sort_by(&:name)+ end++ def find_stale_ignored_tables+ ignored = @schema.ignored_tables+ return [] if ignored.nil?++ stale = []+ ignored.entries.each do |entry|+ if @db_table_names.exclude?(entry.name)+ stale << TableInfo.new(name: entry.name, plugin: nil)+ end+ end++ stale.sort_by(&:name)+ end++ def find_table_diffs+ diffs = []++ @schema.tables.each_value do |table_def|+ next if table_def.source_table_name.nil?++ source = table_def.source_table_name+ next if @db_table_names.exclude?(source)++ table_diff = diff_table(table_def, source)+ diffs << table_diff if table_diff+ end++ diffs.sort_by(&:table_name)+ end++ def diff_table(table_def, source_table)+ @table_def = table_def+ @db_column_names = @db.columns(source_table).map(&:name).to_set+ @source_table = source_table++ configured_columns = @scope.effective_column_names(@table_def, @db_column_names)++ auto_ignored = find_auto_ignored_columns+ unconfigured = find_unconfigured_columns(configured_columns, auto_ignored)+ missing = find_missing_columns+ stale = find_stale_ignored_columns++ has_changes = unconfigured.any? || missing.any? || stale.any? || auto_ignored.any?+ return nil if !has_changes++ TableDiff.new(+ table_name: @table_def.name,+ unconfigured_columns: unconfigured,+ missing_columns: missing,+ stale_ignored_columns: stale,+ auto_ignored_columns: auto_ignored,+ )+ end++ def find_unconfigured_columns(configured_columns, auto_ignored)+ ignored = @table_def.ignored_column_names.to_set+ globally_ignored = @scope.globally_ignored_columns+ auto_ignored_names = auto_ignored.map(&:name).to_set++ unconfigured =+ @db_column_names - configured_columns - ignored - globally_ignored - auto_ignored_names+ unconfigured.sort.map do |name|+ ColumnInfo.new(name:, plugin: plugin_for_column(@source_table, name))+ end+ end++ def find_missing_columns+ return [] if @table_def.included_column_names.nil?++ missing = @table_def.included_column_names.to_set - @db_column_names+ missing.sort.map { |name| ColumnInfo.new(name:, plugin: nil) }+ end++ def find_stale_ignored_columns+ stale = []+ @table_def.ignored_columns_map.each_key do |col_name|+ stale << ColumnInfo.new(name: col_name, plugin: nil) if @db_column_names.exclude?(col_name)+ end+ stale.sort_by(&:name)+ end++ def find_auto_ignored_columns+ auto_ignored = []+ @scope.each_plugin_ignored_column(@table_def) do |col, plugin|+ auto_ignored << ColumnInfo.new(name: col, plugin:)+ end+ auto_ignored.uniq(&:name).sort_by(&:name)+ end++ def plugin_for_table(name)+ manifest = @schema.plugin_manifest+ manifest.available? ? manifest.plugin_for_table(name) : nil+ end++ def plugin_for_column(table_name, col_name)+ manifest = @schema.plugin_manifest+ manifest.available? ? manifest.plugin_for_column(table_name, col_name) : nil+ end+ end+enddiff --git a/migrations/lib/database/schema/dsl/enum_builder.rb b/migrations/lib/database/schema/dsl/enum_builder.rbnew file mode 100644index 00000000000..c837409e858--- /dev/null+++ b/migrations/lib/database/schema/dsl/enum_builder.rb@@ -0,0 +1,78 @@+# frozen_string_literal: true++module Migrations::Database::Schema::DSL+ EnumDef = Data.define(:name, :values, :datatype)++ class EnumBuilder+ def initialize(name)+ @name = name.to_s+ @values = {}+ @source_block = nil+ end++ def value(name, val)+ @values[name.to_s] = val+ end++ def source(&block)+ @source_block = block+ end++ def build+ values = resolve_values+ if values.empty?+ raise Migrations::Database::Schema::ConfigError,+ "Enum :#{@name} must define at least one value or a source."+ end+ datatype = validate_and_infer_datatype(values)+ EnumDef.new(name: @name, values: values.freeze, datatype:)+ end++ private++ def resolve_values+ if @source_block+ evaluate_source+ else+ @values+ end+ end++ def evaluate_source+ result = @source_block.call+ case result+ when Hash+ result.transform_keys(&:to_s)+ when Array+ result.each_with_index.to_h { |k, i| [k.to_s, i] }+ else+ raise Migrations::Database::Schema::ConfigError,+ "Enum :#{@name} source must return a Hash or Array, got #{result.class}."+ end+ rescue Migrations::Database::Schema::ConfigError+ raise+ rescue StandardError => e+ raise Migrations::Database::Schema::ConfigError,+ "Enum :#{@name} failed to evaluate source: #{e.message}"+ end++ def validate_and_infer_datatype(values)+ types = values.values.map(&:class).uniq++ if types.size > 1+ raise Migrations::Database::Schema::ConfigError,+ "Enum :#{@name} values must all be Strings or all Integers"+ end++ type = types.first+ if type == String+ :text+ elsif type == Integer+ :integer+ else+ raise Migrations::Database::Schema::ConfigError,+ "Enum :#{@name} values must be Strings or Integers, got #{types.first}"+ end+ end+ end+enddiff --git a/migrations/lib/database/schema/dsl/generator.rb b/migrations/lib/database/schema/dsl/generator.rbnew file mode 100644index 00000000000..631f90e877f--- /dev/null+++ b/migrations/lib/database/schema/dsl/generator.rb@@ -0,0 +1,147 @@+# frozen_string_literal: true++require "prism"++module Migrations::Database::Schema::DSL+ class Generator+ CUSTOM_CODE_START = "# -- custom code --"+ CUSTOM_CODE_END = "# -- end custom code --"++ def initialize(schema_module, database: :intermediate_db)+ @schema = schema_module+ @database = database+ @output_config = schema_module.config.output_config+ end++ def generate+ preflight = @schema.preflight(database: @database)+ validate!(preflight.errors)+ resolved = preflight.resolved+ generate_sql(resolved)+ generate_enums(resolved)+ generate_models(resolved)+ format_ruby_files!+ resolved+ end++ private++ def validate!(errors)+ return if errors.empty?++ message = "Schema validation failed with #{errors.size} #{"error".pluralize(errors.size)}:\n"+ message += errors.map { |e| " - #{e}" }.join("\n")+ raise Migrations::Database::Schema::GenerationError, message+ end++ def generate_sql(resolved)+ schema_file = expand_path(@output_config.schema_file)+ FileUtils.mkdir_p(File.dirname(schema_file))++ File.open(schema_file, "w") do |f|+ writer = Migrations::Database::Schema::TableWriter.new(f)+ writer.output_file_header(file_header)+ resolved.tables.each { |table| writer.output_table(table) }+ end+ end++ def generate_enums(resolved)+ enums_dir = expand_path(@output_config.enums_directory)+ FileUtils.mkdir_p(enums_dir)++ writer =+ Migrations::Database::Schema::EnumWriter.new(@output_config.enums_namespace, file_header)++ resolved.enums.each do |enum|+ filename = Migrations::Database::Schema::EnumWriter.filename_for(enum)+ path = File.join(enums_dir, filename)+ File.open(path, "w") { |f| writer.output_enum(enum, f) }+ end+ end++ def generate_models(resolved)+ models_dir = expand_path(@output_config.models_directory)+ FileUtils.mkdir_p(models_dir)++ writer =+ Migrations::Database::Schema::ModelWriter.new(+ @output_config.models_namespace,+ @output_config.enums_namespace,+ file_header,+ )++ resolved.tables.each do |table|+ filename = Migrations::Database::Schema::ModelWriter.filename_for(table)+ path = File.join(models_dir, filename)++ case table.model_mode+ when :manual+ next+ when :extended+ custom_code = extract_custom_code(path)+ File.open(path, "w") { |f| writer.output_table(table, f, custom_code:) }+ else+ File.open(path, "w") { |f| writer.output_table(table, f) }+ end+ end+ end++ def extract_custom_code(path)+ return nil unless File.exist?(path)++ content = File.read(path)+ result = Prism.parse(content)++ if !result.success?+ errors = result.errors.map { |e| " - #{e.message} (line #{e.location.start_line})" }+ raise Migrations::Database::Schema::GenerationError,+ "Failed to parse '#{path}':\n#{errors.join("\n")}"+ end++ comments = result.comments+ start_comment = comments.find { |comment| comment.slice == CUSTOM_CODE_START }+ return nil unless start_comment++ end_comment =+ comments.find do |comment|+ comment.slice == CUSTOM_CODE_END &&+ comment.location.start_offset > start_comment.location.start_offset+ end+ return nil unless end_comment++ start_byte = start_comment.location.start_offset+ line_end = content.b.index("\n", start_byte)+ start_offset = line_end ? line_end + 1 : content.bytesize+ content.byteslice(start_offset...end_comment.location.start_offset)&.presence+ end++ def format_ruby_files!+ format_directory(@output_config.models_directory)+ format_directory(@output_config.enums_directory)+ end++ def format_directory(relative_path)+ return unless relative_path+ path = expand_path(relative_path)+ Migrations::Database::Schema::Helpers.format_ruby_files(path)+ rescue StandardError+ # formatting is best-effort; generation still succeeds+ end++ def expand_path(relative_path)+ File.expand_path(relative_path, Migrations.root_path)+ end++ def file_header+ @file_header ||=+ begin+ db_label = Migrations::Database::Schema::Helpers.db_label(@output_config.models_namespace)+ <<~HEADER+ This file is auto-generated from the #{db_label} schema. To make changes,+ update the configuration files in "migrations/config/schema/" and then run+ `migrations/bin/cli schema generate` to regenerate this file.+ HEADER+ end+ end+ end+enddiff --git a/migrations/lib/database/schema/dsl/ignored_builder.rb b/migrations/lib/database/schema/dsl/ignored_builder.rbnew file mode 100644index 00000000000..5ede90c793c--- /dev/null+++ b/migrations/lib/database/schema/dsl/ignored_builder.rb@@ -0,0 +1,76 @@+# frozen_string_literal: true++module Migrations::Database::Schema::DSL+ IgnoredEntry = Data.define(:name, :reason)+ IgnoredPluginEntry = Data.define(:name, :reason)++ IgnoredConfig =+ Data.define(:entries, :plugin_entries) do+ def table_names+ entries.map(&:name).to_set+ end++ def reason_for(name)+ entries.find { |e| e.name == name.to_s }&.reason+ end++ def ignored_plugin_names+ plugin_entries.map(&:name)+ end++ def plugin_ignored?(name)+ normalized = ::Migrations::Database::Schema::Helpers.normalize_plugin_name(name)+ plugin_entries.any? { |e| e.name == normalized }+ end+ end++ class IgnoredBuilder+ def initialize+ @entries = []+ @plugin_entries = []+ @entry_names = {}+ @plugin_entry_names = {}+ end++ def table(name, reason = nil)+ add_table_entry(name, reason)+ end++ def tables(*names, reason: nil)+ names.flatten.each { |name| add_table_entry(name, reason) }+ end++ def plugin(name, reason)+ if reason.nil? || reason.strip.empty?+ raise Migrations::Database::Schema::ConfigError,+ "Ignored plugin :#{name} must have a reason."+ end++ normalized_name = ::Migrations::Database::Schema::Helpers.normalize_plugin_name(name)+ if @plugin_entry_names.key?(normalized_name)+ raise Migrations::Database::Schema::ConfigError,+ "Ignored plugin :#{normalized_name} is already declared."+ end++ @plugin_entry_names[normalized_name] = true+ @plugin_entries << IgnoredPluginEntry.new(name: normalized_name, reason:)+ end++ def build+ IgnoredConfig.new(entries: @entries.freeze, plugin_entries: @plugin_entries.freeze)+ end++ private++ def add_table_entry(name, reason)+ normalized_name = name.to_s+ if @entry_names.key?(normalized_name)+ raise Migrations::Database::Schema::ConfigError,+ "Ignored table :#{normalized_name} is already declared."+ end++ @entry_names[normalized_name] = true+ @entries << IgnoredEntry.new(name: normalized_name, reason:)+ end+ end+enddiff --git a/migrations/lib/database/schema/dsl/ignored_file_editor.rb b/migrations/lib/database/schema/dsl/ignored_file_editor.rbnew file mode 100644index 00000000000..5f710e18017--- /dev/null+++ b/migrations/lib/database/schema/dsl/ignored_file_editor.rb@@ -0,0 +1,160 @@+# frozen_string_literal: true++require "prism"++module Migrations::Database::Schema::DSL+ class IgnoredFileEditor+ def initialize(config_path)+ @ignored_path = File.join(config_path, "ignored.rb")+ end++ def add_table(table_name, reason: nil)+ table_name = table_name.to_s++ if !/\A[a-z0-9_]+\z/.match?(table_name)+ raise(+ Migrations::Database::Schema::ConfigError,+ "Invalid table name '#{table_name}'. Use lowercase letters, numbers, and underscores.",+ )+ end++ if !File.exist?(@ignored_path)+ raise Migrations::Database::Schema::ConfigError, "ignored.rb not found at #{@ignored_path}"+ end++ content = File.read(@ignored_path)+ ignored_block = parse_ignored_block(content)++ if ignored_block[:table_names].include?(table_name)+ raise Migrations::Database::Schema::ConfigError, "Table #{table_name} is already ignored"+ end++ content =+ if reason.nil? && ignored_block[:last_tables_group]+ append_to_tables_group(content, ignored_block[:last_tables_group], table_name)+ else+ insert_standalone_entry(content, ignored_block[:end_offset], table_name, reason)+ end++ File.write(@ignored_path, content)+ format_file!+ end++ private++ def append_to_tables_group(content, tables_group, table_name)+ all_names = (tables_group[:names] + [table_name]).sort+ replacement = "tables " + all_names.map { |n| ":#{n}" }.join(", ")+ replacement += ", #{tables_group[:keyword_source]}" if tables_group[:keyword_source]++ content.byteslice(0, tables_group[:start_offset]) + replacement ++ content.byteslice(tables_group[:end_offset]..)+ end++ def insert_standalone_entry(content, end_offset, table_name, reason)+ entry =+ (+ if reason.present?+ "\n table :#{table_name}, #{reason.inspect}\n"+ else+ "\n table :#{table_name}\n"+ end+ )++ content.byteslice(0, end_offset) + entry + content.byteslice(end_offset..)+ end++ def parse_ignored_block(content)+ result = Prism.parse(content)+ if !result.success?+ details = result.errors.map(&:message).join(", ")+ raise Migrations::Database::Schema::ConfigError,+ "Could not parse #{@ignored_path}: #{details}"+ end++ declaration = find_ignored_declaration(result.value)+ if declaration.nil?+ raise Migrations::Database::Schema::ConfigError,+ "Could not find `Migrations::Database::Schema.ignored do ... end` in #{@ignored_path}"+ end++ last_tables = find_last_tables_group(declaration.block)++ {+ end_offset: declaration.block.closing_loc.start_offset,+ table_names: extract_all_table_names(declaration.block),+ last_tables_group:+ last_tables &&+ {+ start_offset: last_tables.location.start_offset,+ end_offset: last_tables.location.end_offset,+ names: table_names_from(last_tables),+ keyword_source: trailing_keyword_source(last_tables, content),+ },+ }+ end++ def find_ignored_declaration(node)+ return node if node.is_a?(Prism::CallNode) && ignored_declaration?(node)++ node.compact_child_nodes.each do |child|+ found = find_ignored_declaration(child)+ return found if found+ end++ nil+ end++ def ignored_declaration?(node)+ return false unless node.message.to_s == "ignored"+ return false unless node.block++ receiver = node.receiver+ return false unless receiver.is_a?(Prism::ConstantPathNode)++ receiver.full_name.to_s.sub(/\A::/, "") == "Migrations::Database::Schema"+ end++ def find_last_tables_group(block_node)+ body = block_node&.body+ return nil unless body.is_a?(Prism::StatementsNode)++ body.body.select { |s| s.is_a?(Prism::CallNode) && s.message.to_s == "tables" }.last+ end++ def table_names_from(call_node)+ args = call_node.arguments&.arguments || []+ args.filter_map { |arg| arg.unescaped if arg.is_a?(Prism::SymbolNode) }+ end++ def trailing_keyword_source(call_node, content)+ args = call_node.arguments&.arguments || []+ keyword_hash = args.find { |arg| arg.is_a?(Prism::KeywordHashNode) }+ return nil unless keyword_hash++ content.byteslice(keyword_hash.location.start_offset...keyword_hash.location.end_offset)+ end++ def extract_all_table_names(block_node)+ names = Set.new+ body = block_node&.body+ return names unless body.is_a?(Prism::StatementsNode)++ body.body.each do |statement|+ next unless statement.is_a?(Prism::CallNode)++ message = statement.message.to_s+ next unless message == "table" || message == "tables"++ args = statement.arguments&.arguments || []+ args.each { |arg| names << arg.unescaped if arg.is_a?(Prism::SymbolNode) }+ end++ names+ end++ def format_file!+ Migrations::Database::Schema::Helpers.format_ruby_file(@ignored_path)+ end+ end+enddiff --git a/migrations/lib/database/schema/dsl/loader.rb b/migrations/lib/database/schema/dsl/loader.rbnew file mode 100644index 00000000000..266296ec905--- /dev/null+++ b/migrations/lib/database/schema/dsl/loader.rb@@ -0,0 +1,56 @@+# frozen_string_literal: true++module Migrations::Database::Schema::DSL+ class Loader+ def initialize(config_path)+ @config_path = config_path+ end++ def load!+ validate_config_path!+ load_file("config.rb")+ load_file("conventions.rb", optional: true)+ load_file("ignored.rb", optional: true)+ load_enum_files+ load_table_files+ end++ private++ def validate_config_path!+ if !File.directory?(@config_path)+ raise Migrations::Database::Schema::ConfigError,+ "Schema config directory not found: #{@config_path}"+ end+ end++ def load_file(filename, optional: false)+ path = File.join(@config_path, filename)+ if File.exist?(path)+ load_with_error_handling(path)+ elsif !optional+ raise Migrations::Database::Schema::ConfigError, "Required config file not found: #{path}"+ end+ end++ def load_enum_files+ dir = File.join(@config_path, "enums")+ return unless File.directory?(dir)+ Dir[File.join(dir, "*.rb")].sort.each { |path| load_with_error_handling(path) }+ end++ def load_table_files+ dir = File.join(@config_path, "tables")+ return unless File.directory?(dir)+ Dir[File.join(dir, "*.rb")].sort.each { |path| load_with_error_handling(path) }+ end++ def load_with_error_handling(path)+ Kernel.load(path)+ rescue Migrations::Database::Schema::ConfigError+ raise+ rescue StandardError => e+ raise Migrations::Database::Schema::ConfigError, "Error loading #{path}: #{e.message}"+ end+ end+enddiff --git a/migrations/lib/database/schema/dsl/plugin_introspector.rb b/migrations/lib/database/schema/dsl/plugin_introspector.rbnew file mode 100644index 00000000000..92b21965ae1--- /dev/null+++ b/migrations/lib/database/schema/dsl/plugin_introspector.rb@@ -0,0 +1,183 @@+# frozen_string_literal: true++require "digest/md5"++module Migrations::Database::Schema::DSL+ class PluginIntrospector+ def self.compute_checksums(plugins_path)+ discover_plugins(plugins_path).transform_values { |paths| checksum_for_paths(paths) }+ end++ private_class_method def self.checksum_for_paths(paths)+ files =+ paths.select { |p| File.directory?(p) }.flat_map { |p| Dir[File.join(p, "*.rb")].sort }.uniq++ return "empty" if files.empty?++ digests = files.map { |f| "#{File.basename(f)}:#{Digest::MD5.file(f).hexdigest}" }+ Digest::MD5.hexdigest(digests.join("\n"))+ end++ def self.discover_plugins(plugins_path)+ plugins = {}++ Dir[File.join(plugins_path, "*")].sort.each do |plugin_dir|+ next if !File.directory?(plugin_dir)++ plugin_name = File.basename(plugin_dir)+ paths = plugin_migration_paths(plugin_dir)+ plugins[plugin_name] = paths if paths.any?+ end++ plugins+ end++ private_class_method def self.plugin_migration_paths(plugin_dir)+ %w[db/migrate db/post_migrate]+ .map { |sub| File.join(plugin_dir, sub) }+ .select { |path| File.directory?(path) }+ end++ def initialize(plugins_path: nil)+ @plugins_path = plugins_path || File.join(Rails.root, "plugins")+ end++ def introspect+ with_temporary_database do |stderr|+ run_core_migrations+ load_plugin_rake_tasks++ plugins = self.class.discover_plugins(@plugins_path)+ plugin_data, failed_plugins = introspect_plugins(plugins, stderr)+ checksums = self.class.compute_checksums(@plugins_path)++ build_result(plugin_data, checksums, failed_plugins)+ end+ end++ private++ def with_temporary_database+ original_config = ActiveRecord::Base.connection_db_config.configuration_hash.dup+ db = TemporaryDb.new++ suppress_output do |_stdout, stderr|+ db.start++ db.with_env do+ ActiveRecord::Base.establish_connection(db.connection_hash)++ yield stderr+ end+ ensure+ ActiveRecord::Base.establish_connection(original_config)+ db.stop+ db.remove+ end+ end++ def introspect_plugins(plugins, stderr)+ plugin_data = {}+ failed_plugins = []++ plugins+ .sort_by(&:first)+ .each do |plugin_name, migration_paths|+ result, failed = introspect_plugin(plugin_name, migration_paths, stderr)+ if failed+ failed_plugins << plugin_name+ stderr.puts(+ " Warning: stopping plugin introspection after '#{plugin_name}' failed to avoid partial manifest data",+ )+ break+ end++ plugin_data[plugin_name] = result if result+ end++ [plugin_data, failed_plugins]+ end++ def introspect_plugin(plugin_name, migration_paths, stderr)+ snapshot_before = snapshot_schema++ begin+ run_plugin_migrations(migration_paths)+ rescue StandardError => e+ stderr.puts " Warning: '#{plugin_name}' migration error: #{e.message}"+ return nil, true+ end++ snapshot_after = snapshot_schema+ [diff_schema(snapshot_before, snapshot_after), false]+ end++ def snapshot_schema+ connection = ActiveRecord::Base.connection+ tables = connection.tables.to_set+ columns = {}++ tables.each { |table| columns[table] = connection.columns(table).map(&:name).to_set }++ { tables:, columns: }+ end++ def diff_schema(snapshot_before, snapshot_after)+ new_tables = snapshot_after[:tables] - snapshot_before[:tables]++ new_columns = {}+ snapshot_after[:columns].each do |table, cols|+ next if new_tables.include?(table)+ added = cols - snapshot_before[:columns].fetch(table, Set.new)+ new_columns[table] = added.sort if added.any?+ end++ return if new_tables.empty? && new_columns.empty?++ { "tables" => new_tables.sort, "columns" => new_columns }+ end++ def build_result(plugin_data, checksums, failed_plugins)+ {+ "plugins" => plugin_data,+ "plugin_checksums" => checksums,+ "failed_plugins" => failed_plugins.sort,+ "incomplete" => failed_plugins.any?,+ }+ end++ def core_migration_paths+ [File.join(Rails.root, "db", "migrate"), File.join(Rails.root, "db", "post_migrate")]+ end++ def run_core_migrations+ paths = core_migration_paths.select { |p| File.directory?(p) }+ ActiveRecord::MigrationContext.new(paths).migrate if paths.any?+ end++ # At least one plugin has a migration that invokes a rake task. Loading+ # all plugin rake files ensures those tasks are defined before migrations+ # run. The :environment task stub prevents errors from rake files that+ # depend on it.+ def load_plugin_rake_tasks+ Rake::Task.define_task(:environment) if !Rake::Task.task_defined?(:environment)+ Dir[File.join(@plugins_path, "*/lib/tasks/**/*.rake")].sort.each { |f| load f }+ end++ def run_plugin_migrations(paths)+ valid_paths = paths.select { |p| File.directory?(p) }+ ActiveRecord::MigrationContext.new(valid_paths).migrate if valid_paths.any?+ end++ def suppress_output+ old_stdout = $stdout+ old_stderr = $stderr+ $stdout = StringIO.new+ $stderr = StringIO.new+ yield old_stdout, old_stderr+ ensure+ $stdout = old_stdout+ $stderr = old_stderr+ end+ end+enddiff --git a/migrations/lib/database/schema/dsl/plugin_manifest.rb b/migrations/lib/database/schema/dsl/plugin_manifest.rbnew file mode 100644index 00000000000..af225a983ea--- /dev/null+++ b/migrations/lib/database/schema/dsl/plugin_manifest.rb@@ -0,0 +1,122 @@+# frozen_string_literal: true++module Migrations::Database::Schema::DSL+ class PluginManifest+ def initialize(manifest_path:, plugins_path: nil)+ @manifest_path = manifest_path+ @plugins_path = plugins_path || File.join(Rails.root, "plugins")++ @data = YAML.safe_load_file(@manifest_path) if available?+ @data ||= empty_data++ @table_to_plugin = nil+ @column_to_plugin = nil+ end++ def fresh?+ return false if !available?++ stored_checksums = @data["plugin_checksums"]+ return false if !stored_checksums++ stored_checksums == PluginIntrospector.compute_checksums(@plugins_path)+ end++ def available?+ File.exist?(@manifest_path)+ end++ def incomplete?+ failed_plugins.any?+ end++ def regenerate!+ introspector = PluginIntrospector.new(plugins_path: @plugins_path)+ result = introspector.introspect++ should_write = !available? || @data != result+ @data = result++ if should_write+ FileUtils.mkdir_p(File.dirname(@manifest_path))+ File.write(@manifest_path, format_yaml(@data))+ end++ @table_to_plugin = nil+ @column_to_plugin = nil+ end++ def plugin_for_table(name)+ build_reverse_indexes+ @table_to_plugin[name.to_s]+ end++ def plugin_for_column(table, col)+ build_reverse_indexes+ @column_to_plugin.dig(table.to_s, col.to_s)+ end++ def all_plugin_names+ plugins = @data["plugins"] || {}+ plugins.keys.sort+ end++ def tables_for_plugin(name)+ @data.dig("plugins", normalize_plugin_name(name), "tables") || []+ end++ def columns_for_plugin(name, table: nil)+ plugin_data = @data.dig("plugins", normalize_plugin_name(name), "columns") || {}+ if table+ plugin_data[table.to_s] || []+ else+ plugin_data+ end+ end++ def table_count+ plugins = @data["plugins"] || {}+ plugins.sum { |_, data| (data["tables"] || []).size }+ end++ def column_count+ plugins = @data["plugins"] || {}+ plugins.sum { |_, data| (data["columns"] || {}).sum { |_, cols| cols.size } }+ end++ def failed_plugins+ Array(@data["failed_plugins"])+ end++ private++ def normalize_plugin_name(name)+ ::Migrations::Database::Schema::Helpers.normalize_plugin_name(name)+ end++ def empty_data+ { "plugins" => {}, "plugin_checksums" => {}, "failed_plugins" => [], "incomplete" => false }+ end++ def format_yaml(data)+ YAML.dump(data, indentation: 2).sub(/\A---\n/, "").gsub(/^(\s*)-/, '\1 -')+ end++ def build_reverse_indexes+ return if @table_to_plugin++ @table_to_plugin = {}+ @column_to_plugin = {}++ plugins = @data["plugins"] || {}+ plugins.each do |plugin_name, plugin_data|+ (plugin_data["tables"] || []).each { |t| @table_to_plugin[t] = plugin_name }++ (plugin_data["columns"] || {}).each do |table, cols|+ @column_to_plugin[table] ||= {}+ cols.each { |col| @column_to_plugin[table][col] = plugin_name }+ end+ end+ end+ end+enddiff --git a/migrations/lib/database/schema/dsl/registry.rb b/migrations/lib/database/schema/dsl/registry.rbnew file mode 100644index 00000000000..088057b3d1e--- /dev/null+++ b/migrations/lib/database/schema/dsl/registry.rb@@ -0,0 +1,91 @@+# frozen_string_literal: true++module Migrations::Database::Schema::DSL+ class Registry+ attr_reader :config, :conventions_config, :ignored_tables++ def initialize+ @tables = {}+ @enums = {}+ @config = nil+ @conventions_config = nil+ @ignored_tables = nil+ @frozen = false+ end++ def register_config(config)+ raise_if_frozen!+ if @config+ raise Migrations::Database::Schema::ConfigError,+ "Configuration already registered. Only one `configure` block is allowed."+ end+ @config = config+ end++ def register_conventions(conventions)+ raise_if_frozen!+ if @conventions_config+ raise Migrations::Database::Schema::ConfigError,+ "Conventions already registered. Only one `conventions` block is allowed."+ end+ @conventions_config = conventions+ end++ def register_table(name, table_def)+ raise_if_frozen!+ name = name.to_s+ if @tables.key?(name)+ raise Migrations::Database::Schema::ConfigError, "Table :#{name} is already registered."+ end+ @tables[name] = table_def+ end++ def register_enum(name, enum_def)+ raise_if_frozen!+ name = name.to_s+ if @enums.key?(name)+ raise Migrations::Database::Schema::ConfigError, "Enum :#{name} is already registered."+ end+ @enums[name] = enum_def+ end++ def register_ignored(ignored)+ raise_if_frozen!+ if @ignored_tables+ raise Migrations::Database::Schema::ConfigError,+ "Ignored tables already registered. Only one `ignored` block is allowed."+ end+ @ignored_tables = ignored+ end++ def tables+ @tables.dup.freeze+ end++ def enums+ @enums.dup.freeze+ end++ def table(name)+ @tables[name.to_s]+ end++ def enum(name)+ @enums[name.to_s]+ end++ def freeze!+ @frozen = true+ self+ end++ private++ def raise_if_frozen!+ if @frozen+ raise Migrations::Database::Schema::ConfigError,+ "Registry is frozen. Cannot modify after loading."+ end+ end+ end+enddiff --git a/migrations/lib/database/schema/dsl/resolved_schema_validator.rb b/migrations/lib/database/schema/dsl/resolved_schema_validator.rbnew file mode 100644index 00000000000..3c3bbaae077--- /dev/null+++ b/migrations/lib/database/schema/dsl/resolved_schema_validator.rb@@ -0,0 +1,100 @@+# frozen_string_literal: true++module Migrations::Database::Schema::DSL+ class ResolvedSchemaValidator+ def initialize(resolved_schema)+ @schema = resolved_schema+ @errors = []+ end++ def validate+ @errors.clear++ @schema.tables.each { |table| validate_table(table) }+ @schema.enums.each { |enum| validate_enum(enum) }++ @errors+ end++ private++ def validate_table(table)+ column_names = table.columns.map(&:name).to_set++ validate_columns(table)+ validate_primary_key(table, column_names)+ validate_indexes(table, column_names)+ validate_constraints(table)+ end++ def validate_columns(table)+ table.columns.each do |column|+ if Migrations::Database::Schema::Helpers::VALID_DATATYPES.exclude?(column.datatype)+ @errors << "Table '#{table.name}': column '#{column.name}' has invalid datatype '#{column.datatype}'"+ end++ @errors << "Table '#{table.name}': column has empty name" if column.name.blank?++ if column.is_primary_key && column.nullable+ @errors << "Table '#{table.name}': primary key column '#{column.name}' should not be nullable"+ end+ end++ duplicates = table.columns.map(&:name).tally.filter_map { |n, c| n if c > 1 }+ if duplicates.any?+ @errors << "Table '#{table.name}': duplicate column names: #{duplicates.join(", ")}"+ end+ end++ def validate_primary_key(table, column_names)+ return unless table.primary_key_column_names++ missing = table.primary_key_column_names.reject { |pk| column_names.include?(pk) }+ if missing.any?+ @errors << "Table '#{table.name}': primary key references missing columns: #{missing.join(", ")}"+ end+ end++ def validate_indexes(table, column_names)+ table.indexes.each do |index|+ missing = index.column_names.reject { |col| column_names.include?(col) }+ if missing.any?+ @errors << "Table '#{table.name}': index '#{index.name}' references missing columns: #{missing.join(", ")}"+ end++ @errors << "Table '#{table.name}': index has empty name" if index.name.blank?+ end++ duplicates = table.indexes.map(&:name).tally.filter_map { |n, c| n if c > 1 }+ if duplicates.any?+ @errors << "Table '#{table.name}': duplicate index names: #{duplicates.join(", ")}"+ end+ end++ def validate_constraints(table)+ table.constraints.each do |constraint|+ @errors << "Table '#{table.name}': constraint has empty name" if constraint.name.blank?++ if constraint.condition.blank?+ @errors << "Table '#{table.name}': constraint '#{constraint.name}' has empty condition"+ end+ end+ end++ def validate_enum(enum)+ @errors << "Enum has empty name" if enum.name.blank?++ @errors << "Enum '#{enum.name}' has no values" if enum.values.empty?++ if %i[integer text].exclude?(enum.datatype)+ @errors << "Enum '#{enum.name}' has invalid datatype '#{enum.datatype}'"+ end++ expected_type = enum.datatype == :integer ? Integer : String+ invalid_values = enum.values.reject { |_, value| value.is_a?(expected_type) }+ if invalid_values.any?+ @errors << "Enum '#{enum.name}' has values that do not match datatype '#{enum.datatype}'"+ end+ end+ end+enddiff --git a/migrations/lib/database/schema/dsl/scaffolder.rb b/migrations/lib/database/schema/dsl/scaffolder.rbnew file mode 100644index 00000000000..933458aee51--- /dev/null+++ b/migrations/lib/database/schema/dsl/scaffolder.rb@@ -0,0 +1,102 @@+# frozen_string_literal: true++module Migrations::Database::Schema::DSL+ class Scaffolder+ DSL_REFERENCE = <<~COMMENT+ # Schema DSL Reference:+ #+ # include_all - Include all source columns (implied by `ignore`)+ # include :col1, :col2 - Include only these columns+ # include! :col1, :col2 - Include columns, overriding global/plugin ignores+ # ignore :col1, :col2, reason: "..." - Ignore columns (implies include_all)+ # primary_key :col1, :col2 - Override primary key+ # column :name, required: true - Set column options (required, type, max_length, rename_to)+ # add_column :name, :type - Add a column not in the source table+ # copy_structure_from :other_table - Use another table as the source+ # synthetic! - Table has no source (only add_column allowed)+ # ignore_plugin_columns! - Auto-ignore columns from all (or specific) non-ignored plugins+ COMMENT++ def initialize(schema_module, table_name, database: :intermediate_db)+ @schema = schema_module+ @table_name = table_name.to_s+ @database = database+ end++ def scaffold!+ ActiveRecord::Base.with_connection do |connection|+ @db = connection++ if @db.tables.exclude?(@table_name)+ raise Migrations::Database::Schema::ConfigError,+ "Table '#{@table_name}' does not exist in the database"+ end++ content = generate_table_file+ write_file(content)+ end+ end++ private++ def generate_table_file+ columns = @db.columns(@table_name)+ primary_keys = @db.primary_keys(@table_name)+ indexes = @db.indexes(@table_name)++ lines = []+ lines << "# frozen_string_literal: true"+ lines << ""+ lines << DSL_REFERENCE.chomp+ lines << "#"+ lines.concat(source_metadata_lines(primary_keys, indexes))+ lines << ""+ lines << "Migrations::Database::Schema.table :#{@table_name} do"++ if primary_keys.size > 1+ pks = primary_keys.map { |pk| ":#{pk}" }.join(", ")+ lines << " primary_key #{pks}"+ lines << ""+ end++ lines << " include_all"+ lines << ""+ lines << " # TODO: Configure columns. Run `schema validate` to check."+ lines << ' # ignore :col1, :col2, reason: "..."'+ lines << "end"+ lines.join("\n") + "\n"+ end++ def source_metadata_lines(primary_keys, indexes)+ lines = []+ lines << "# Source table: #{@table_name}"++ pk_display = primary_keys.any? ? primary_keys.join(", ") : "(none)"+ lines << "# Primary key: #{pk_display}"++ unique_indexes = indexes.select(&:unique)++ if unique_indexes.any?+ lines << "# Unique indexes:"+ unique_indexes.each { |idx| lines << "# #{idx.name} (#{idx.columns.join(", ")})" }+ end++ lines+ end++ def write_file(content)+ tables_dir = File.join(Migrations::Database::Schema.config_path(@database), "tables")+ FileUtils.mkdir_p(tables_dir)++ path = File.join(tables_dir, "#{@table_name}.rb")++ if File.exist?(path)+ raise Migrations::Database::Schema::ConfigError, "Table config already exists at #{path}"+ end++ File.write(path, content)+ Migrations::Database::Schema::Helpers.format_ruby_file(path)+ path+ end+ end+enddiff --git a/migrations/lib/database/schema/dsl/schema_resolver.rb b/migrations/lib/database/schema/dsl/schema_resolver.rbnew file mode 100644index 00000000000..90c1acc4a64--- /dev/null+++ b/migrations/lib/database/schema/dsl/schema_resolver.rb@@ -0,0 +1,219 @@+# frozen_string_literal: true++module Migrations::Database::Schema::DSL+ class SchemaResolver+ def initialize(schema_module)+ @schema = schema_module+ @conventions = schema_module.conventions_config+ @enums_by_name = schema_module.enums.transform_values { |e| resolve_enum(e) }+ @scope = ColumnScope.new(@schema)+ end++ def resolve+ tables = resolve_tables+ enums = @enums_by_name.values++ Migrations::Database::Schema::Definition.new(tables:, enums:)+ end++ private++ def resolve_tables+ ActiveRecord::Base.with_connection do |connection|+ @db = connection+ @schema.tables.map { |_name, table_def| resolve_table(table_def) }+ end+ end++ def resolve_table(table_def)+ if table_def.source_table_name+ source_table = table_def.source_table_name+ db_columns = @db.columns(source_table).index_by(&:name)+ db_primary_keys = @db.primary_keys(source_table)+ else+ db_columns = {}+ db_primary_keys = []+ end++ primary_key_columns = table_def.primary_key_columns || db_primary_keys++ columns = resolve_included_columns(table_def, db_columns, primary_key_columns)+ columns += resolve_added_columns(table_def, primary_key_columns)++ indexes = resolve_indexes(table_def)+ constraints = resolve_constraints(table_def)++ # Map PK column names through renames, preserving the original order+ resolved_pk_names = resolve_primary_key_names(primary_key_columns, table_def, columns)++ Migrations::Database::Schema::TableDefinition.new(+ name: table_def.name,+ columns:,+ indexes:,+ primary_key_column_names: resolved_pk_names,+ constraints:,+ model_mode: table_def.model_mode,+ )+ end++ def resolve_included_columns(table_def, db_columns, primary_key_columns)+ column_names = determine_included_columns(table_def, db_columns)++ column_names.filter_map do |col_name|+ db_col = db_columns[col_name]+ next if db_col.nil?++ options = table_def.column_options_for(col_name)++ resolve_column(+ db_col,+ primary_key_columns:,+ type_override: options&.type,+ required_override: options&.required,+ max_length_override: options&.max_length,+ rename_to_override: options&.rename_to,+ )+ end+ end++ def determine_included_columns(table_def, db_columns)+ if table_def.included_column_names+ table_def.included_column_names+ else+ # nil means "all DB columns" minus globally ignored, per-table ignored, and plugin ignored+ all_names = db_columns.keys+ ignored = table_def.ignored_column_names.to_set+ globally_ignored = @conventions ? @conventions.ignored_columns.to_set : Set.new+ plugin_ignored = @scope.plugin_ignored_column_names(table_def)+ forced = table_def.forced_column_names&.to_set || Set.new+ all_names.reject do |n|+ ignored.include?(n) ||+ ((globally_ignored.include?(n) || plugin_ignored.include?(n)) && forced.exclude?(n))+ end+ end+ end++ def resolve_column(+ db_col,+ primary_key_columns:,+ type_override: nil,+ required_override: nil,+ max_length_override: nil,+ rename_to_override: nil+ )+ col_name = db_col.name+ convention = @conventions&.convention_for(col_name)++ effective_name = rename_to_override || convention&.rename_to || col_name++ raw_type = type_override || convention&.type_override || db_col.type++ datatype = normalize_datatype(raw_type)++ enum = nil+ if type_override && @enums_by_name.key?(type_override)+ enum = @enums_by_name[type_override]+ datatype = enum.datatype+ end++ required = required_override.nil? ? convention&.required : required_override++ # Columns with defaults are treated as nullable because converters+ # don't need to supply a value — the DB default will apply.+ nullable = required.nil? ? db_col.null || db_col.default.present? : !required++ max_length = (max_length_override || db_col.limit if datatype == :text)++ is_primary_key = primary_key_columns.include?(col_name)+ nullable = false if is_primary_key++ Migrations::Database::Schema::ColumnDefinition.new(+ name: effective_name,+ datatype:,+ nullable:,+ max_length:,+ is_primary_key:,+ enum:,+ )+ end++ def resolve_added_columns(table_def, primary_key_columns)+ table_def.added_columns.map do |added_col|+ effective_name = added_col.name++ enum = added_col.enum ? @enums_by_name[added_col.enum] : nil+ datatype = enum ? enum.datatype : added_col.type++ is_pk = primary_key_columns.include?(effective_name)++ Migrations::Database::Schema::ColumnDefinition.new(+ name: effective_name,+ datatype: normalize_datatype(datatype),+ nullable: is_pk ? false : !added_col.required,+ max_length: nil,+ is_primary_key: is_pk,+ enum:,+ )+ end+ end++ def resolve_primary_key_names(primary_key_columns, table_def, resolved_columns)+ resolved_column_names = resolved_columns.map(&:name).to_set++ primary_key_columns.filter_map do |pk_col|+ if table_def.source_table_name+ options = table_def.column_options_for(pk_col)+ rename_to = options&.rename_to+ resolved_name = rename_to || @conventions&.effective_name(pk_col) || pk_col+ else+ resolved_name = pk_col+ end+ resolved_name if resolved_column_names.include?(resolved_name)+ end+ end++ def resolve_indexes(table_def)+ table_def.indexes.map do |idx|+ resolved_columns =+ idx.column_names.map { |col_name| resolve_index_column_name(table_def, col_name) }++ Migrations::Database::Schema::IndexDefinition.new(+ name: idx.name,+ column_names: resolved_columns,+ unique: idx.unique,+ condition: idx.condition,+ )+ end+ end++ def resolve_index_column_name(table_def, col_name)+ return col_name if table_def.source_table_name.nil?++ options = table_def.column_options_for(col_name)+ options&.rename_to || @conventions&.effective_name(col_name) || col_name+ end++ def resolve_constraints(table_def)+ table_def.constraints.map do |constraint|+ Migrations::Database::Schema::ConstraintDefinition.new(+ name: constraint.name,+ type: constraint.type,+ condition: constraint.condition,+ )+ end+ end++ def resolve_enum(enum_def)+ Migrations::Database::Schema::EnumDefinition.new(+ name: enum_def.name,+ values: enum_def.values,+ datatype: enum_def.datatype,+ )+ end++ def normalize_datatype(type)+ type = type.to_sym+ Migrations::Database::Schema::Helpers::DATATYPE_ALIASES.fetch(type, type)+ end+ end+enddiff --git a/migrations/lib/database/schema/dsl/table_builder.rb b/migrations/lib/database/schema/dsl/table_builder.rbnew file mode 100644index 00000000000..86dd6077f19--- /dev/null+++ b/migrations/lib/database/schema/dsl/table_builder.rb@@ -0,0 +1,230 @@+# frozen_string_literal: true++module Migrations::Database::Schema::DSL+ ColumnOptions = Data.define(:type, :required, :max_length, :rename_to)+ AddedColumn = Data.define(:name, :type, :required, :enum)+ IndexConfig = Data.define(:column_names, :name, :unique, :condition)+ ConstraintConfig = Data.define(:name, :type, :condition)++ TableDef =+ Data.define(+ :name,+ :source_table_name,+ :primary_key_columns,+ :included_column_names,+ :forced_column_names,+ :column_options,+ :added_columns,+ :indexes,+ :constraints,+ :ignored_columns_map,+ :ignore_plugin_columns,+ :ignore_plugin_names,+ :model_mode,+ ) do+ def column_options_for(col)+ column_options[col.to_s]+ end++ def ignored_column_names+ ignored_columns_map.keys+ end++ def ignore_reason_for(col)+ ignored_columns_map[col.to_s]+ end++ def ignore_plugin_columns?+ ignore_plugin_columns+ end+ end++ VALID_MODEL_MODES = %i[extended manual].freeze++ class TableBuilder+ def initialize(name)+ @name = name.to_s+ @source_table_name = @name+ @primary_key_cols = nil+ @included_columns = nil+ @include_all = false+ @column_options = {}+ @added_columns = []+ @indexes = []+ @constraints = []+ @ignored_columns = {}+ @forced_columns = []+ @ignore_plugin_columns = false+ @ignore_plugin_names = nil+ @model_mode = nil+ end++ def copy_structure_from(table)+ @source_table_name = table.to_s+ end++ def synthetic!+ @source_table_name = nil+ end++ def primary_key(*cols)+ @primary_key_cols = cols.flatten.map(&:to_s)+ end++ def include(*cols)+ @included_columns ||= []+ @included_columns.concat(cols.flatten.map(&:to_s))+ end++ def include_all+ @include_all = true+ end++ def include!(*cols)+ @forced_columns.concat(cols.flatten.map(&:to_s))+ end++ def column(name, type = nil, **opts, &block)+ name = name.to_s+ if block+ builder = ColumnOptionsBuilder.new+ builder.instance_eval(&block)+ @column_options[name] = builder.build+ else+ @column_options[name] = ColumnOptions.new(+ type: type&.to_s || opts[:type]&.to_s,+ required: opts[:required],+ max_length: opts[:max_length],+ rename_to: opts[:rename_to]&.to_s,+ )+ end+ end++ def add_column(name, type, required: false, enum: nil)+ enum = enum.to_s if enum+ @added_columns << AddedColumn.new(name: name.to_s, type: type.to_s, required:, enum:)+ end++ def ignore(*cols, reason: nil)+ cols.flatten.each { |col| @ignored_columns[col.to_s] = reason }+ end++ def index(*cols, name: nil, unique: false, where: nil)+ cols = cols.flatten.map(&:to_s)+ index_name = name || generate_index_name(cols, unique)+ @indexes << IndexConfig.new(+ column_names: cols,+ name: index_name.to_s,+ unique:,+ condition: where,+ )+ end++ def unique_index(*cols, name: nil, where: nil)+ index(*cols, name:, unique: true, where:)+ end++ def check(name, condition)+ @constraints << ConstraintConfig.new(name: name.to_s, type: :check, condition:)+ end++ def ignore_plugin_columns!(*plugin_names)+ @ignore_plugin_columns = true+ @ignore_plugin_names =+ plugin_names.flatten.map do |name|+ ::Migrations::Database::Schema::Helpers.normalize_plugin_name(name)+ end if plugin_names.any?+ end++ def model(mode)+ mode = mode.to_sym+ if VALID_MODEL_MODES.exclude?(mode)+ raise Migrations::Database::Schema::ConfigError,+ "Invalid model mode :#{mode} for table :#{@name}. Valid: #{VALID_MODEL_MODES.join(", ")}"+ end+ @model_mode = mode+ end++ def build+ if @source_table_name && @included_columns.nil? && !@include_all && @ignored_columns.empty?+ raise Migrations::Database::Schema::ConfigError,+ "Table :#{@name} must use `include_all`, `include`, or `ignore` to specify which columns to include"+ end++ if @source_table_name.nil? && (@included_columns || @include_all)+ raise Migrations::Database::Schema::ConfigError,+ "Table :#{@name} is synthetic and cannot use `include` or `include_all`"+ end++ if @included_columns && @ignored_columns.any?+ overlap = @included_columns & @ignored_columns.keys+ if overlap.any?+ raise Migrations::Database::Schema::ConfigError,+ "Table :#{@name} has columns that are both included and ignored: #{overlap.join(", ")}"+ end+ end++ if @included_columns && @include_all+ raise Migrations::Database::Schema::ConfigError,+ "Table :#{@name} cannot use both `include` and `include_all`"+ end++ TableDef.new(+ name: @name,+ source_table_name: @source_table_name,+ primary_key_columns: @primary_key_cols,+ included_column_names: @included_columns,+ forced_column_names: @forced_columns.empty? ? nil : @forced_columns.freeze,+ column_options: @column_options.freeze,+ added_columns: @added_columns.freeze,+ indexes: @indexes.freeze,+ constraints: @constraints.freeze,+ ignored_columns_map: @ignored_columns.freeze,+ ignore_plugin_columns: @ignore_plugin_columns,+ ignore_plugin_names: @ignore_plugin_names&.freeze,+ model_mode: @model_mode,+ )+ end++ private++ def generate_index_name(cols, unique)+ prefix = unique ? "idx_unique" : "idx"+ "#{prefix}_#{@name}_#{cols.join("_")}"+ end+ end++ class ColumnOptionsBuilder+ def initialize+ @type = nil+ @required = nil+ @max_length = nil+ @rename_to = nil+ end++ def type(value)+ @type = value.to_s+ end++ def required(value = true)+ @required = value+ end++ def max_length(value)+ @max_length = value+ end++ def rename_to(value)+ @rename_to = value.to_s+ end++ def build+ ColumnOptions.new(+ type: @type,+ required: @required,+ max_length: @max_length,+ rename_to: @rename_to,+ )+ end+ end+enddiff --git a/migrations/lib/database/schema/dsl/validator.rb b/migrations/lib/database/schema/dsl/validator.rbnew file mode 100644index 00000000000..f11dd7d8a11--- /dev/null+++ b/migrations/lib/database/schema/dsl/validator.rb@@ -0,0 +1,262 @@+# frozen_string_literal: true++module Migrations::Database::Schema::DSL+ class Validator+ def initialize(schema_module)+ @schema = schema_module+ @errors = []+ end++ def validate+ ActiveRecord::Base.with_connection do |connection|+ @db = connection+ @db_table_names = @db.tables.to_set+ @scope = ColumnScope.new(@schema)++ validate_configured_and_ignored_tables+ validate_tables+ validate_enums+ validate_stale_ignored_tables+ end++ @errors.freeze+ end++ private++ def validate_configured_and_ignored_tables+ ignored = @schema.ignored_tables+ return if ignored.nil?++ ignored_names = ignored.table_names++ # Synthetic tables don't use a DB source, so they can share a name with an ignored DB table+ configured_names =+ @schema.tables.each_value.filter_map { |t| t.name if t.source_table_name }.to_set++ overlap = configured_names & ignored_names+ overlap.sort.each { |name| @errors << "Table '#{name}' is both configured and ignored" }+ end++ def validate_tables+ configured_table_names = @schema.tables.each_value.filter_map(&:source_table_name).to_set+ ignored_table_names = @scope.ignored_table_name_set++ # Check for unconfigured tables in database+ unconfigured = @db_table_names - configured_table_names - ignored_table_names+ if unconfigured.any?+ @errors << "Tables exist in database but are not configured or ignored: #{sort_and_join(unconfigured)}"+ end++ # Validate each configured table+ @schema.tables.each_value { |table_def| validate_table(table_def) }+ end++ def validate_table(table_def)+ @table_def = table_def++ validate_ignored_plugin_source++ if @table_def.source_table_name+ source_table = @table_def.source_table_name++ if @db_table_names.exclude?(source_table)+ @errors << "Table '#{@table_def.name}': source table '#{source_table}' does not exist in database"+ return+ end++ @db_column_names = @db.columns(source_table).map(&:name).to_set+ @db_primary_keys = @db.primary_keys(source_table).map(&:to_s)+ else+ @db_column_names = Set.new+ @db_primary_keys = []+ end++ validate_included_columns+ validate_include_overrides+ validate_column_options+ validate_added_columns+ validate_ignored_columns+ validate_unconfigured_columns+ validate_primary_key_columns+ validate_index_columns+ validate_enum_references+ end++ def validate_included_columns+ return if @table_def.included_column_names.nil?++ missing = @table_def.included_column_names.to_set - @db_column_names+ if missing.any?+ @errors << "Table '#{@table_def.name}': included columns do not exist in database: #{sort_and_join(missing)}"+ end+ end++ def validate_include_overrides+ return if @table_def.included_column_names.nil?++ forced = @table_def.forced_column_names&.to_set || Set.new+ globally_ignored = @scope.globally_ignored_columns+ auto_ignored = auto_ignored_column_names++ @table_def.included_column_names.each do |col_name|+ next if forced.include?(col_name)++ if globally_ignored.include?(col_name)+ @errors << "Table '#{@table_def.name}': included column '#{col_name}' is globally ignored — use `include!` to override"+ elsif auto_ignored.include?(col_name)+ @errors << "Table '#{@table_def.name}': included column '#{col_name}' is auto-ignored by a plugin — use `include!` to override"+ end+ end+ end++ def validate_column_options+ configured_columns = @scope.effective_column_names(@table_def, @db_column_names)++ @table_def.column_options.each_key do |col_name|+ if @db_column_names.exclude?(col_name)+ @errors << "Table '#{@table_def.name}': column option for '#{col_name}' " \+ "references a column that does not exist in database"+ elsif configured_columns.exclude?(col_name)+ @errors << "Table '#{@table_def.name}': column option for '#{col_name}' " \+ "references an excluded column"+ end+ end+ end++ def validate_added_columns+ @table_def.added_columns.each do |added_col|+ if @db_column_names.include?(added_col.name)+ @errors << "Table '#{@table_def.name}': added column '#{added_col.name}' already exists in database"+ end++ if added_col.enum && [email protected]?(added_col.enum)+ @errors << "Table '#{@table_def.name}': added column '#{added_col.name}' references unknown enum '#{added_col.enum}'"+ end+ end+ end++ def validate_ignored_columns+ @table_def.ignored_columns_map.each_key do |col_name|+ if @db_column_names.exclude?(col_name)+ @errors << "Table '#{@table_def.name}': ignored column '#{col_name}' does not exist in database (stale ignore)"+ end+ end+ end++ def validate_index_columns+ configured_columns = @scope.effective_column_names(@table_def, @db_column_names)++ seen_names = {}+ @table_def.indexes.each do |idx|+ missing = idx.column_names.to_set - configured_columns+ if missing.any?+ index_message =+ "Table '#{@table_def.name}': index '#{idx.name}' " \+ "references columns not in configuration: #{sort_and_join(missing)}"+ @errors << index_message+ end++ name = idx.name+ if seen_names.key?(name)+ @errors << "Table '#{@table_def.name}': duplicate index name '#{name}'"+ else+ seen_names[name] = true+ end+ end+ end++ def validate_unconfigured_columns+ return if @table_def.source_table_name.nil?++ configured_columns = @scope.effective_column_names(@table_def, @db_column_names)+ ignored_columns = @table_def.ignored_column_names.to_set+ unconfigured =+ @db_column_names - configured_columns - ignored_columns - @scope.globally_ignored_columns -+ auto_ignored_column_names++ if unconfigured.any?+ @errors << "Table '#{@table_def.name}': database columns are not configured or ignored: #{sort_and_join(unconfigured)}"+ end+ end++ def validate_primary_key_columns+ configured_primary_keys = @table_def.primary_key_columns || @db_primary_keys+ return if configured_primary_keys.empty?++ if @table_def.source_table_name+ added_names = @table_def.added_columns.map(&:name).to_set+ missing_in_db = configured_primary_keys.to_set - @db_column_names - added_names+ if missing_in_db.any?+ pk_message =+ "Table '#{@table_def.name}': primary key references columns " \+ "that do not exist in database: #{sort_and_join(missing_in_db)}"+ @errors << pk_message+ end+ end++ configured_columns = @scope.effective_column_names(@table_def, @db_column_names)+ missing_in_config = configured_primary_keys.to_set - configured_columns+ if missing_in_config.any?+ @errors << "Table '#{@table_def.name}': primary key columns are not configured: #{sort_and_join(missing_in_config)}"+ end+ end++ def validate_enum_references+ @table_def.column_options.each do |col_name, options|+ type = options.type+ next if type.nil?+ next if known_type_override?(type)+ next if @schema.enums.key?(type)++ @errors << "Table '#{@table_def.name}': column '#{col_name}' type '#{type}' references unknown enum"+ end+ end++ def validate_ignored_plugin_source+ return if @table_def.source_table_name.nil?++ manifest = @schema.plugin_manifest+ return if !manifest.available?++ ignored = @schema.ignored_tables+ return if ignored.nil?++ plugin = manifest.plugin_for_table(@table_def.source_table_name)+ return if plugin.nil?++ if ignored.plugin_ignored?(plugin)+ @errors << "Table '#{@table_def.name}': source table '#{@table_def.source_table_name}' belongs to ignored plugin '#{plugin}'"+ end+ end++ def validate_enums+ @schema.enums.each_value do |enum_def|+ @errors << "Enum '#{enum_def.name}': has no values" if enum_def.values.empty?+ end+ end++ def validate_stale_ignored_tables+ ignored = @schema.ignored_tables+ return if ignored.nil?++ ignored.entries.each do |entry|+ if @db_table_names.exclude?(entry.name)+ @errors << "Ignored table '#{entry.name}' does not exist in database (stale ignore)"+ end+ end+ end++ def auto_ignored_column_names+ @scope.plugin_ignored_column_names(@table_def)+ end++ def sort_and_join(values)+ values.sort.join(", ")+ end++ def known_type_override?(type)+ Migrations::Database::Schema::Helpers::VALID_TYPE_OVERRIDES.include?(type)+ end+ end+enddiff --git a/migrations/lib/database/schema/enum_resolver.rb b/migrations/lib/database/schema/enum_resolver.rbdeleted file mode 100644index cb0e66b19d7..00000000000--- a/migrations/lib/database/schema/enum_resolver.rb+++ /dev/null@@ -1,72 +0,0 @@-# frozen_string_literal: true--module Migrations::Database::Schema- class EnumResolver- class EnumSourceError < StandardError- end-- def initialize(config)- @config = config || {}- end-- def resolve- @config.map do |name, entry|- name = name.to_s- values_hash = resolve_values(entry)- datatype = values_hash.values.first.is_a?(String) ? :text : :integer- EnumDefinition.new(name:, values: values_hash, datatype:)- end- end-- private-- def resolve_values(entry)- if entry.key?(:values)- normalize_values(entry[:values])- elsif entry.key?(:strings)- normalize_strings(entry[:strings])- elsif entry.key?(:source)- fetch_source(entry[:source])- else- raise EnumSourceError, "Enum must define :values, :strings, or :source"- end- end-- def normalize_values(values)- case values- when Array- values.each_with_index.to_h { |k, i| [k.to_s, i] }- when Hash- values.transform_keys(&:to_s)- else- raise EnumSourceError, "Invalid :values format: #{values.inspect}"- end- end-- def normalize_strings(values)- case values- when Array- values.to_h { |k| [k.to_s, k.to_s] }- when Hash- values.transform_keys(&:to_s).transform_values(&:to_s)- else- raise EnumSourceError, "Invalid :strings format: #{values.inspect}"- end- end-- def fetch_source(source_code)- values = eval(source_code, TOPLEVEL_BINDING) # rubocop:disable Security/Eval-- case values- when Hash- values.transform_keys(&:to_s)- when Array- values.each_with_index.to_h { |k, i| [k.to_s, i] }- else- raise EnumSourceError, "Eval #{source_code} must return Hash or Array"- end- rescue StandardError => e- raise EnumSourceError, "Failed to evaluate source #{source_code}: #{e.message}"- end- end-enddiff --git a/migrations/lib/database/schema/enum_writer.rb b/migrations/lib/database/schema/enum_writer.rbindex 4b67f0dc0ff..f7700531e1c 100644--- a/migrations/lib/database/schema/enum_writer.rb+++ b/migrations/lib/database/schema/enum_writer.rb@@ -1,8 +1,5 @@# frozen_string_literal: true-require "rake"-require "syntax_tree/rake_tasks"-module Migrations::Database::Schemaclass EnumWriterdef initialize(namespace, header)@@ -15,7 +12,7 @@ module Migrations::Database::Schemaenddef output_enum(enum, output_stream)- module_name = ::Migrations::Database::Schema.to_singular_classname(enum.name)+ module_name = ::Migrations::Database::Schema::Helpers.to_singular_classname(enum.name)output_stream.puts "# frozen_string_literal: true"output_stream.puts@@ -37,7 +34,7 @@ module Migrations::Database::Schema.sort_by { |_k, v| v }.map do |name, value|value = %Q|"#{value}"| if value.is_a?(String)- " #{::Migrations::Database::Schema.to_const_name(name)} = #{value}"+ " #{::Migrations::Database::Schema::Helpers.to_const_name(name)} = #{value}"end.join("\n")enddiff --git a/migrations/lib/database/schema/global_config.rb b/migrations/lib/database/schema/global_config.rbdeleted file mode 100644index 9d8ef793e7c..00000000000--- a/migrations/lib/database/schema/global_config.rb+++ /dev/null@@ -1,67 +0,0 @@-# frozen_string_literal: true--module Migrations::Database::Schema- class GlobalConfig- attr_reader :excluded_column_names, :modified_columns-- def initialize(schema_config)- @schema_config = schema_config- @excluded_table_names = load_globally_excluded_table_names.freeze- @excluded_column_names = load_globally_excluded_column_names.freeze- @modified_columns = load_globally_modified_columns.freeze- end-- def excluded_table_name?(table_name)- @excluded_table_names.include?(table_name)- end-- def modified_name(column_name)- if (modified_column = find_modified_column(column_name))- modified_column[:rename_to]- end- end-- def modified_datatype(column_name)- if (modified_column = find_modified_column(column_name))- modified_column[:datatype]- end- end-- def modified_nullable(column_name)- if (modified_column = find_modified_column(column_name))- modified_column[:nullable]- end- end-- private-- def find_modified_column(column_name)- @modified_columns.find { |column| column[:name] == column_name } ||- @modified_columns.find { |column| column[:name_regex]&.match?(column_name) }- end-- def load_globally_excluded_table_names- table_names = @schema_config.dig(:global, :tables, :exclude)- table_names.presence&.to_set || Set.new- end-- def load_globally_excluded_column_names- column_names = @schema_config.dig(:global, :columns, :exclude)- column_names.presence || []- end-- def load_globally_modified_columns- modified_columns = @schema_config.dig(:global, :columns, :modify)- return {} if modified_columns.blank?-- modified_columns.map do |column|- if column[:name_regex]- column[:name_regex_original] = column[:name_regex]- column[:name_regex] = Regexp.new(column[:name_regex])- end- column[:datatype] = column[:datatype]&.to_sym- column- end- end- end-enddiff --git a/migrations/lib/database/schema/helpers.rb b/migrations/lib/database/schema/helpers.rbnew file mode 100644index 00000000000..cf68cbab342--- /dev/null+++ b/migrations/lib/database/schema/helpers.rb@@ -0,0 +1,217 @@+# frozen_string_literal: true++module Migrations::Database::Schema+ module Helpers+ # Canonical datatypes after normalization (used in resolved schema validation)+ VALID_DATATYPES = %i[blob boolean date datetime float inet integer json numeric text].freeze++ # Pre-normalization aliases that map to a canonical datatype+ DATATYPE_ALIASES = {+ binary: :blob,+ string: :text,+ enum: :text,+ uuid: :text,+ jsonb: :json,+ }.freeze++ # All types accepted as DSL type overrides (pre-normalization)+ VALID_TYPE_OVERRIDES =+ (VALID_DATATYPES.map(&:to_s) + DATATYPE_ALIASES.keys.map(&:to_s)).to_set.freeze++ SQLITE_KEYWORDS = %w[+ abort+ action+ add+ after+ all+ alter+ always+ analyze+ and+ as+ asc+ attach+ autoincrement+ before+ begin+ between+ by+ cascade+ case+ cast+ check+ collate+ column+ commit+ conflict+ constraint+ create+ cross+ current+ current_date+ current_time+ current_timestamp+ database+ default+ deferrable+ deferred+ delete+ desc+ detach+ distinct+ do+ drop+ each+ else+ end+ escape+ except+ exclude+ exclusive+ exists+ explain+ fail+ filter+ first+ following+ for+ foreign+ from+ full+ generated+ glob+ group+ groups+ having+ if+ ignore+ immediate+ in+ index+ indexed+ initially+ inner+ insert+ instead+ intersect+ into+ is+ isnull+ join+ key+ last+ left+ like+ limit+ match+ materialized+ natural+ no+ not+ nothing+ notnull+ null+ nulls+ of+ offset+ on+ or+ order+ others+ outer+ over+ partition+ plan+ pragma+ preceding+ primary+ query+ raise+ range+ recursive+ references+ regexp+ reindex+ release+ rename+ replace+ restrict+ returning+ right+ rollback+ row+ rows+ savepoint+ select+ set+ table+ temp+ temporary+ then+ ties+ to+ transaction+ trigger+ unbounded+ union+ unique+ update+ using+ vacuum+ values+ view+ virtual+ when+ where+ window+ with+ without+ ].freeze+ private_constant :SQLITE_KEYWORDS++ def self.escape_identifier(identifier)+ if SQLITE_KEYWORDS.include?(identifier)+ %Q("#{identifier}")+ else+ identifier+ end+ end++ def self.to_singular_classname(snake_case_string)+ snake_case_string.downcase.singularize.camelize+ end++ def self.to_const_name(name)+ name.parameterize.underscore.upcase+ end++ def self.db_label(namespace)+ namespace.split("::").last+ end++ def self.format_ruby_file(path)+ system(+ "bundle",+ "exec",+ "stree",+ "write",+ path,+ exception: true,+ out: File::NULL,+ err: File::NULL,+ )+ rescue StandardError+ raise "Failed to run `bundle exec stree write '#{path}'`"+ end++ def self.format_ruby_files(directory)+ format_ruby_file(File.join(directory, "*.rb"))+ end++ # Plugin directory names use hyphens (discourse-ai), but Ruby symbols+ # use underscores (discourse_ai). Normalize for manifest lookups.+ def self.normalize_plugin_name(name)+ name.to_s.tr("_", "-")+ end+ end+enddiff --git a/migrations/lib/database/schema/loader.rb b/migrations/lib/database/schema/loader.rbdeleted file mode 100644index 54872c23726..00000000000--- a/migrations/lib/database/schema/loader.rb+++ /dev/null@@ -1,170 +0,0 @@-# frozen_string_literal: true--module Migrations::Database::Schema- class Loader- def initialize(schema_config)- @schema_config = schema_config- @global = GlobalConfig.new(@schema_config)- end-- def load_schema- enums = load_enums- tables = load_tables- Definition.new(tables:, enums:)- end-- private-- def load_enums- enums = EnumResolver.new(@schema_config[:enums]).resolve- @enums_by_name = enums.index_by(&:name)- enums- end-- def load_tables- @db = ActiveRecord::Base.lease_connection-- tables = []- existing_table_names = @db.tables.to_set-- @schema_config[:tables].sort.each do |table_name, config|- table_name = table_name.to_s-- if config[:copy_of].present?- table_alias = table_name- table_name = config[:copy_of]- else- next if @global.excluded_table_name?(table_name)- end-- if existing_table_names.include?(table_name)- tables << table(table_name, config, table_alias)- end- end-- @db = nil- ActiveRecord::Base.release_connection-- tables- end-- def table(table_name, config, table_alias = nil)- primary_key_column_names =- config[:primary_key_column_names].presence || @db.primary_keys(table_name)-- columns =- filtered_columns_of(table_name, config).map do |column|- modified_column = modified_column_for(column, config)- enum = @enums_by_name[modified_column[:enum]] if modified_column&.key?(:enum)-- ColumnDefinition.new(- name: name_for(column),- datatype: datatype_for(column, modified_column, enum),- nullable: nullable_for(column, modified_column),- max_length: column.type == :text ? column.limit : nil,- is_primary_key: primary_key_column_names.include?(column.name),- enum:,- )- end + added_columns(config, primary_key_column_names)-- TableDefinition.new(- table_alias || table_name,- columns,- indexes(config),- primary_key_column_names,- constraints(config),- )- end-- def filtered_columns_of(table_name, config)- columns_by_name = @db.columns(table_name).index_by(&:name)- globally_excluded_columns = @global.excluded_column_names-- if (included_columns = config.dig(:columns, :include))- modified_columns = config.dig(:columns, :modify)&.map { |c| c[:name] }- included_columns = included_columns + modified_columns if modified_columns- globally_excluded_columns -= included_columns- columns_by_name.slice!(*included_columns)- elsif (excluded_columns = config.dig(:columns, :exclude))- columns_by_name.except!(*excluded_columns)- end-- columns_by_name.except!(*globally_excluded_columns)- columns_by_name.values- end-- def added_columns(config, primary_key_column_names)- columns = config.dig(:columns, :add) || []- columns.map do |column|- enum = @enums_by_name[column[:enum]] if column[:enum]- datatype = enum ? enum.datatype : column[:datatype].to_sym-- ColumnDefinition.new(- name: column[:name],- datatype:,- nullable: column.fetch(:nullable, true),- max_length: datatype == :text ? column[:max_length] : nil,- is_primary_key: primary_key_column_names.include?(column[:name]),- enum:,- )- end- end-- def name_for(column)- @global.modified_name(column.name) || column.name- end-- def datatype_for(column, modified_column, enum)- datatype = enum.datatype if enum- datatype ||= modified_column[:datatype]&.to_sym if modified_column- datatype ||= @global.modified_datatype(column.name) || column.type-- case datatype- when :binary- :blob- when :string, :enum, :uuid- :text- when :jsonb- :json- when :boolean, :date, :datetime, :float, :inet, :integer, :numeric, :json, :text- datatype- else- raise "Unknown datatype: #{datatype}"- end- end-- def modified_column_for(column, config)- config.dig(:columns, :modify)&.find { |col| col[:name] == column.name }- end-- def nullable_for(column, modified_column)- return modified_column[:nullable] if modified_column&.key?(:nullable)-- global_nullable = @global.modified_nullable(column.name)- return global_nullable unless global_nullable.nil?-- column.null || column.default.present?- end-- def indexes(config)- config[:indexes]&.map do |index|- IndexDefinition.new(- name: index[:name],- column_names: Array.wrap(index[:columns]),- unique: index.fetch(:unique, false),- condition: index[:condition],- )- end- end-- def constraints(config)- config[:constraints]&.map do |constraint|- ConstraintDefinition.new(- name: constraint[:name],- type: constraint.fetch(:type, :check).to_sym,- condition: constraint[:condition],- )- end- end- end-enddiff --git a/migrations/lib/database/schema/model_writer.rb b/migrations/lib/database/schema/model_writer.rbindex fd7b90b9a82..6a003b74bb3 100644--- a/migrations/lib/database/schema/model_writer.rb+++ b/migrations/lib/database/schema/model_writer.rb@@ -1,8 +1,5 @@# frozen_string_literal: true-require "rake"-require "syntax_tree/rake_tasks"-module Migrations::Database::Schemaclass ModelWriterdef initialize(model_namespace, enum_namespace, header)@@ -15,8 +12,8 @@ module Migrations::Database::Schema"#{table.name.singularize}.rb"end- def output_table(table, output_stream)- module_name = ::Migrations::Database::Schema.to_singular_classname(table.name)+ def output_table(table, output_stream, custom_code: nil)+ module_name = ::Migrations::Database::Schema::Helpers.to_singular_classname(table.name)columns = table.sorted_columnsoutput_stream.puts "# frozen_string_literal: true"@@ -35,11 +32,19 @@ module Migrations::Database::Schemaoutput_stream.puts " SQL"output_stream.puts " private_constant :SQL"output_stream.puts++ if table.model_mode == :extended+ output_stream.puts " # -- custom code --"+ output_stream.puts custom_code if custom_code.present?+ output_stream.puts " # -- end custom code --"+ output_stream.puts+ end+output_stream.puts method_documentation(table.name, columns)output_stream.puts " def self.create("output_stream.puts method_parameters(columns)output_stream.puts " )"- output_stream.puts " ::Migrations::Database::IntermediateDB.insert("+ output_stream.puts " ::#{@model_namespace}.insert("output_stream.puts " SQL,"output_stream.puts insertion_arguments(columns)output_stream.puts " )"@@ -80,47 +85,44 @@ module Migrations::Database::Schemaenddef method_documentation(table_name, columns)- max_column_name_length = columns.map { |c| c.name.length }.max-- documentation = +" # Creates a new `#{table_name}` record in the IntermediateDB.\n"- documentation << " #\n"-- param_documentation =- columns.map do |c|- param_name = c.name.ljust(max_column_name_length)- datatypes = datatypes_for_documentation(c)- " # @param #{param_name} [#{datatypes}]"- end-- max_line_length = param_documentation.map(&:length).max- see_documenation = []-- columns.each_with_index do |column, index|- if (enum = column.enum)- enum_module_name = ::Migrations::Database::Schema.to_singular_classname(enum.name)- enum_value_names = enum.values.sort_by { |_k, v| v }.map(&:first)- first_const_name = ::Migrations::Database::Schema.to_const_name(enum_value_names.first)-- enum_documentation =- " # Any constant from #{enum_module_name} (e.g. #{enum_module_name}::#{first_const_name})"+ max_name_length = columns.map { |c| c.name.length }.max+ see_references = []++ param_lines = columns.map { |c| param_line_for(c, max_name_length, see_references) }++ lines = [+ " # Creates a new `#{table_name}` record in the #{Helpers.db_label(@model_namespace)}.",+ " #",+ *param_lines,+ " #",+ " # @return [void]",+ ]++ if see_references.any?+ lines << " #"+ lines.concat(see_references.map { |ref| " # @see #{ref}" })+ end- line = param_documentation[index].ljust(max_line_length)- param_documentation[index] = "#{line}\n#{enum_documentation}"+ lines.join("\n")+ end- see_documenation << "#{@enum_namespace}::#{enum_module_name}"- end- end+ def param_line_for(column, max_name_length, see_references)+ param_name = column.name.ljust(max_name_length)+ datatypes = datatypes_for_documentation(column)+ line = +" # @param #{param_name} [#{datatypes}]"- documentation << param_documentation.join("\n")- documentation << "\n #\n"- documentation << " # @return [void]"+ if (enum = column.enum)+ module_name = ::Migrations::Database::Schema::Helpers.to_singular_classname(enum.name)+ first_const =+ ::Migrations::Database::Schema::Helpers.to_const_name(+ enum.values.min_by { |_k, v| v }.first,+ )- if see_documenation.any?- documentation << "\n #\n"- documentation << see_documenation.map { |see| " # @see #{see}" }.join("\n")+ line << "\n # Any constant from #{module_name} (e.g. #{module_name}::#{first_const})"+ see_references << "#{@enum_namespace}::#{module_name}"end- documentation+ lineenddef datatypes_for_documentation(column)@@ -191,7 +193,7 @@ module Migrations::Database::Schemaenddef escape_identifier(identifier)- ::Migrations::Database::Schema.escape_identifier(identifier)+ ::Migrations::Database::Schema::Helpers.escape_identifier(identifier)endendenddiff --git a/migrations/lib/database/schema/table_writer.rb b/migrations/lib/database/schema/table_writer.rbindex bf50df3f4fd..3cf544c752e 100644--- a/migrations/lib/database/schema/table_writer.rb+++ b/migrations/lib/database/schema/table_writer.rb@@ -88,7 +88,7 @@ module Migrations::Database::Schemaenddef escape_identifier(identifier)- ::Migrations::Database::Schema.escape_identifier(identifier)+ ::Migrations::Database::Schema::Helpers.escape_identifier(identifier)enddef output_indexes(table)@@ -103,7 +103,7 @@ module Migrations::Database::Schema@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.print " WHERE #{index.condition}" if index.condition.present?@output.puts ";"endenddiff --git a/migrations/lib/database/schema/validation/base_validator.rb b/migrations/lib/database/schema/validation/base_validator.rbdeleted file mode 100644index 4635646904d..00000000000--- a/migrations/lib/database/schema/validation/base_validator.rb+++ /dev/null@@ -1,18 +0,0 @@-# frozen_string_literal: true--module Migrations::Database::Schema::Validation- class BaseValidator- def initialize(config, errors, db)- @config = config- @schema_config ||= @config[:schema]- @errors = errors- @db = db- end-- private-- def sort_and_join(values)- values.sort.join(", ")- end- end-enddiff --git a/migrations/lib/database/schema/validation/columns_validator.rb b/migrations/lib/database/schema/validation/columns_validator.rbdeleted file mode 100644index 544799bb475..00000000000--- a/migrations/lib/database/schema/validation/columns_validator.rb+++ /dev/null@@ -1,115 +0,0 @@-# frozen_string_literal: true--module Migrations::Database::Schema::Validation- class ColumnsValidator < BaseValidator- def validate(table_name, copy_of_table_name = nil)- @table_name = table_name- columns = @schema_config[:tables][table_name.to_sym][:columns] || {}-- @existing_column_names = find_existing_column_names(table_name, copy_of_table_name)- @added_column_names = columns[:add]&.map { |column| column[:name] } || []- @included_column_names = columns[:include] || []- @excluded_column_names = columns[:exclude] || []- @modified_column_names = columns[:modify]&.map { |column| column[:name] } || []- @global = ::Migrations::Database::Schema::GlobalConfig.new(@schema_config)-- validated_added_columns- validate_included_columns- validate_excluded_columns- validate_modified_columns- validate_any_columns_configured- end-- private-- def find_existing_column_names(table_name, copy_of_table_name)- table_name = copy_of_table_name if copy_of_table_name.present?- @db.columns(table_name).map { |c| c.name }- end-- def validated_added_columns- if (column_names = @existing_column_names & @added_column_names).any?- @errors << I18n.t(- "schema.validator.tables.added_columns_exist",- table_name: @table_name,- column_names: sort_and_join(column_names),- )- end- end-- def validate_included_columns- if (column_names = @included_column_names - @existing_column_names).any?- @errors << I18n.t(- "schema.validator.tables.included_columns_missing",- table_name: @table_name,- column_names: sort_and_join(column_names),- )- end- end-- def validate_excluded_columns- if (column_names = @excluded_column_names - @existing_column_names).any?- @errors << I18n.t(- "schema.validator.tables.excluded_columns_missing",- table_name: @table_name,- column_names: sort_and_join(column_names),- )- end- end-- def validate_modified_columns- if (column_names = @modified_column_names - @existing_column_names).any?- @errors << I18n.t(- "schema.validator.tables.modified_columns_missing",- table_name: @table_name,- column_names: sort_and_join(column_names),- )- end-- if (column_names = @modified_column_names & @included_column_names).any?- @errors << I18n.t(- "schema.validator.tables.modified_columns_included",- table_name: @table_name,- column_names: sort_and_join(column_names),- )- end-- if (column_names = @modified_column_names & @excluded_column_names).any?- @errors << I18n.t(- "schema.validator.tables.modified_columns_excluded",- table_name: @table_name,- column_names: sort_and_join(column_names),- )- end-- if (column_names = @modified_column_names & @global.excluded_column_names).any?- @errors << I18n.t(- "schema.validator.tables.modified_columns_globally_excluded",- table_name: @table_name,- column_names: sort_and_join(column_names),- )- end- end-- def configured_column_names- if @included_column_names.any?- included_column_names = @included_column_names- modified_column_names = @modified_column_names- else- included_column_names = @existing_column_names - @excluded_column_names- modified_column_names = included_column_names & @modified_column_names- end-- column_names = (included_column_names + modified_column_names).uniq & @existing_column_names- column_names - @global.excluded_column_names + @added_column_names- end-- def validate_any_columns_configured- column_names = configured_column_names-- if column_names.empty?- @errors << I18n.t("schema.validator.tables.no_columns_configured", table_name: @table_name)- end- end- end-enddiff --git a/migrations/lib/database/schema/validation/globally_configured_columns_validator.rb b/migrations/lib/database/schema/validation/globally_configured_columns_validator.rbdeleted file mode 100644index b4120e6dd29..00000000000--- a/migrations/lib/database/schema/validation/globally_configured_columns_validator.rb+++ /dev/null@@ -1,73 +0,0 @@-# frozen_string_literal: true--module Migrations::Database::Schema::Validation- class GloballyConfiguredColumnsValidator < BaseValidator- def initialize(config, errors, db)- super- @global = ::Migrations::Database::Schema::GlobalConfig.new(@schema_config)- end-- def validate- all_column_names = calculate_all_column_names-- validate_excluded_column_names(all_column_names)- validate_modified_columns(all_column_names)- end-- private-- def calculate_all_column_names- existing_table_names = @db.tables-- configured_table_names =- @schema_config[:tables].map do |table_name, table_config|- table_config[:copy_of] || table_name.to_s- end-- globally_excluded_table_names = @schema_config.dig(:global, :tables, :exclude) || []- excluded_table_names = @schema_config.dig(:global, :tables, :exclude) || []-- all_table_names = existing_table_names - globally_excluded_table_names - excluded_table_names- all_table_names = all_table_names.uniq & configured_table_names-- all_table_names.flat_map { |table_name| @db.columns(table_name).map(&:name) }.uniq- end-- def validate_excluded_column_names(all_column_names)- globally_excluded_column_names = @global.excluded_column_names- excluded_missing_column_names = globally_excluded_column_names - all_column_names-- if excluded_missing_column_names.any?- @errors << I18n.t(- "schema.validator.global.excluded_columns_missing",- column_names: sort_and_join(excluded_missing_column_names),- )- end- end-- def validate_modified_columns(all_column_names)- globally_modified_columns = @global.modified_columns-- excluded_missing_columns =- globally_modified_columns.reject do |column|- if column[:name]- all_column_names.include?(column[:name])- elsif column[:name_regex]- all_column_names.any? { |column_name| column[:name_regex]&.match?(column_name) }- else- false- end- end-- if excluded_missing_columns.any?- excluded_missing_column_names =- excluded_missing_columns.map { |column| column[:name_regex_original] || column[:name] }-- @errors << I18n.t(- "schema.validator.global.modified_columns_missing",- column_names: sort_and_join(excluded_missing_column_names),- )- end- end- end-enddiff --git a/migrations/lib/database/schema/validation/globally_excluded_tables_validator.rb b/migrations/lib/database/schema/validation/globally_excluded_tables_validator.rbdeleted file mode 100644index 36459049867..00000000000--- a/migrations/lib/database/schema/validation/globally_excluded_tables_validator.rb+++ /dev/null@@ -1,20 +0,0 @@-# frozen_string_literal: true--module Migrations::Database::Schema::Validation- class GloballyExcludedTablesValidator < BaseValidator- def validate- excluded_table_names = @schema_config.dig(:global, :tables, :exclude)- return if excluded_table_names.blank?-- existing_table_names = @db.tables- missing_table_names = excluded_table_names - existing_table_names-- if missing_table_names.any?- @errors << I18n.t(- "schema.validator.global.excluded_tables_missing",- table_names: sort_and_join(missing_table_names),- )- end- end- end-enddiff --git a/migrations/lib/database/schema/validation/json_schema_validator.rb b/migrations/lib/database/schema/validation/json_schema_validator.rbdeleted file mode 100644index 221ff092816..00000000000--- a/migrations/lib/database/schema/validation/json_schema_validator.rb+++ /dev/null@@ -1,43 +0,0 @@-# frozen_string_literal: true--require "json_schemer"--module Migrations::Database::Schema::Validation- class JsonSchemaValidator- def initialize(config, errors)- @config = config- @errors = errors- end-- def validate- schema = load_json_schema- schemer = ::JSONSchemer.schema(schema)- response = schemer.validate(@config)- response.each { |r| @errors << transform_json_schema_errors(r) }- end-- private-- def load_json_schema- schema_path = File.join(::Migrations.root_path, "config", "json_schemas", "db_schema.json")- JSON.load_file(schema_path)- end-- def transform_json_schema_errors(response)- error_message = response.fetch("error")-- error_message.gsub!(/value at (`.+?`) matches `not` schema/) do- path = $1- schema_pointer = response.fetch("schema_pointer")-- case schema_pointer- in %r{/tables/}- I18n.t("schema.validator.include_exclude_not_allowed", path:)- else- $1- end- end- error_message- end- end-enddiff --git a/migrations/lib/database/schema/validation/output_config_validator.rb b/migrations/lib/database/schema/validation/output_config_validator.rbdeleted file mode 100644index 36213a3b8b7..00000000000--- a/migrations/lib/database/schema/validation/output_config_validator.rb+++ /dev/null@@ -1,46 +0,0 @@-# frozen_string_literal: true--module Migrations::Database::Schema::Validation- class OutputConfigValidator < BaseValidator- def initialize(config, errors)- super(config, errors, nil)- @output_config = config[:output]- end-- def validate- validate_schema_file_directory- validate_models_directory- validate_models_namespace- end-- private-- def validate_schema_file_directory- schema_file_path = File.dirname(@output_config[:schema_file])- schema_file_path = File.expand_path(schema_file_path, ::Migrations.root_path)-- if !Dir.exist?(schema_file_path)- @errors << I18n.t("schema.validator.output.schema_file_directory_not_found")- end- end-- def validate_models_directory- models_directory = File.expand_path(@output_config[:models_directory], ::Migrations.root_path)-- if !Dir.exist?(models_directory)- @errors << I18n.t("schema.validator.output.models_directory_not_found")- end- end-- def validate_models_namespace- existing_namespace =- begin- Object.const_get(@output_config[:models_namespace]).is_a?(Module)- rescue NameError- false- end-- @errors << I18n.t("schema.validator.output.models_namespace_undefined") if !existing_namespace- end- end-enddiff --git a/migrations/lib/database/schema/validation/plugin_config_validator.rb b/migrations/lib/database/schema/validation/plugin_config_validator.rbdeleted file mode 100644index ffaf87eea2b..00000000000--- a/migrations/lib/database/schema/validation/plugin_config_validator.rb+++ /dev/null@@ -1,28 +0,0 @@-# frozen_string_literal: true--module Migrations::Database::Schema::Validation- class PluginConfigValidator < BaseValidator- def initialize(config, errors)- super(config, errors, nil)- end-- def validate- all_plugin_names = Discourse.plugins.map(&:name)- configured_plugin_names = @config[:plugins]-- if (additional_plugins = all_plugin_names - configured_plugin_names).any?- @errors << I18n.t(- "schema.validator.plugins.additional_installed",- plugin_names: sort_and_join(additional_plugins),- )- end-- if (missing_plugins = configured_plugin_names - all_plugin_names).any?- @errors << I18n.t(- "schema.validator.plugins.not_installed",- plugin_names: sort_and_join(missing_plugins),- )- end- end- end-enddiff --git a/migrations/lib/database/schema/validation/schema_config_validator.rb b/migrations/lib/database/schema/validation/schema_config_validator.rbdeleted file mode 100644index 4d252d41cc3..00000000000--- a/migrations/lib/database/schema/validation/schema_config_validator.rb+++ /dev/null@@ -1,18 +0,0 @@-# frozen_string_literal: true--module Migrations::Database::Schema::Validation- class SchemaConfigValidator- def initialize(config, errors)- @config = config- @errors = errors- end-- def validate- ActiveRecord::Base.with_connection do |db|- GloballyExcludedTablesValidator.new(@config, @errors, db).validate- GloballyConfiguredColumnsValidator.new(@config, @errors, db).validate- TablesValidator.new(@config, @errors, db).validate- end- end- end-enddiff --git a/migrations/lib/database/schema/validation/tables_validator.rb b/migrations/lib/database/schema/validation/tables_validator.rbdeleted file mode 100644index 0b99c5ba356..00000000000--- a/migrations/lib/database/schema/validation/tables_validator.rb+++ /dev/null@@ -1,70 +0,0 @@-# frozen_string_literal: true--module Migrations::Database::Schema::Validation- class TablesValidator < BaseValidator- def initialize(config, errors, db)- super-- @existing_table_names = @db.tables- @configured_tables = @schema_config[:tables]- @configured_table_names = @configured_tables.keys.map(&:to_s)- @excluded_table_names = @schema_config.dig(:global, :tables, :exclude) || []- end-- def validate- validate_excluded_tables- validate_unconfigured_tables- validate_copied_tables- validate_columns- end-- private-- def validate_excluded_tables- table_names = @configured_table_names & @excluded_table_names-- if table_names.any?- @errors << I18n.t(- "schema.validator.tables.excluded_tables_configured",- table_names: sort_and_join(table_names),- )- end- end-- def validate_unconfigured_tables- table_names = @existing_table_names - @configured_table_names - @excluded_table_names-- if table_names.any?- @errors << I18n.t(- "schema.validator.tables.not_configured",- table_names: sort_and_join(table_names),- )- end- end-- def validate_copied_tables- @configured_tables.each do |_table_name, table_config|- next unless table_config[:copy_of]-- if !@existing_table_names.include?(table_config[:copy_of])- @errors << I18n.t(- "schema.validator.tables.copy_table_not_found",- table_name: table_config[:copy_of],- )- end- end- end-- def validate_columns- @configured_tables.each do |table_name, table_config|- validator = ColumnsValidator.new(@config, @errors, @db)-- if table_config[:copy_of]- validator.validate(table_name.to_s, table_config[:copy_of])- else- validator.validate(table_name.to_s)- end- end- end- end-enddiff --git a/migrations/lib/importer/steps/base/site_settings.rb b/migrations/lib/importer/steps/base/site_settings.rbindex dd1370e9666..7454928c911 100644--- a/migrations/lib/importer/steps/base/site_settings.rb+++ b/migrations/lib/importer/steps/base/site_settings.rb@@ -18,6 +18,7 @@ module Migrations::Importer::Steps::BaseEnums::SiteSettingDatatype::GROUP_LIST,Enums::SiteSettingDatatype::TAG_GROUP_LIST,Enums::SiteSettingDatatype::TAG_LIST,+ Enums::SiteSettingDatatype::TOPIC,Enums::SiteSettingDatatype::UPLOAD,Enums::SiteSettingDatatype::UPLOADED_IMAGE_LIST,Enums::SiteSettingDatatype::USERNAME,diff --git a/migrations/lib/importer/steps/site_settings_with_dependencies.rb b/migrations/lib/importer/steps/site_settings_with_dependencies.rbindex fd059664620..52896dee04b 100644--- a/migrations/lib/importer/steps/site_settings_with_dependencies.rb+++ b/migrations/lib/importer/steps/site_settings_with_dependencies.rb@@ -5,7 +5,7 @@ module Migrations::Importer::Stepstitle "Importing site settings with dependencies"# TODO Add :emojis as dependency- depends_on :categories, :groups, :tags, :uploads, :users+ depends_on :categories, :groups, :tags, :topics, :uploads, :usersprivate@@ -32,6 +32,8 @@ module Migrations::Importer::Stepsvaluewhen Enums::SiteSettingDatatype::TAG_LISTvalue # TODO Map tag names+ when Enums::SiteSettingDatatype::TOPIC+ map_ids(setting_name, value, MappingType::TOPICS, "topic")when Enums::SiteSettingDatatype::UPLOADmap_ids(setting_name, value, MappingType::UPLOADS, "upload").to_iwhen Enums::SiteSettingDatatype::UPLOADED_IMAGE_LISTdiff --git a/migrations/migrations.rb b/migrations/migrations.rbindex d54ed54f64d..788ed5c9953 100644--- a/migrations/migrations.rb+++ b/migrations/migrations.rb@@ -49,6 +49,7 @@ module Migrationsloader.inflector.inflect({"cli" => "CLI",+ "dsl" => "DSL","id" => "ID","discourse_db" => "DiscourseDB","intermediate_db" => "IntermediateDB",diff --git a/migrations/spec/fixtures/schema/config.rb b/migrations/spec/fixtures/schema/config.rbnew file mode 100644index 00000000000..361e6e03c29--- /dev/null+++ b/migrations/spec/fixtures/schema/config.rb@@ -0,0 +1,11 @@+# frozen_string_literal: true++Migrations::Database::Schema.configure do+ output do+ schema_file "db/test_schema/100-base-schema.sql"+ models_directory "lib/database/test_db"+ models_namespace "Migrations::Database::TestDB"+ enums_directory "lib/database/test_db/enums"+ enums_namespace "Migrations::Database::TestDB::Enums"+ end+enddiff --git a/migrations/spec/fixtures/schema/conventions.rb b/migrations/spec/fixtures/schema/conventions.rbnew file mode 100644index 00000000000..baf664c1cf4--- /dev/null+++ b/migrations/spec/fixtures/schema/conventions.rb@@ -0,0 +1,13 @@+# frozen_string_literal: true++Migrations::Database::Schema.conventions do+ column :id do+ rename_to :original_id+ type :integer+ required+ end++ columns_matching(/_at$/) { type :datetime }++ ignore_columns :created_by, :updated_by+enddiff --git a/migrations/spec/fixtures/schema/enums/visibility.rb b/migrations/spec/fixtures/schema/enums/visibility.rbnew file mode 100644index 00000000000..c0da30e537a--- /dev/null+++ b/migrations/spec/fixtures/schema/enums/visibility.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++Migrations::Database::Schema.enum :visibility do+ value :public, 0+ value :private, 1+ value :restricted, 2+enddiff --git a/migrations/spec/fixtures/schema/ignored.rb b/migrations/spec/fixtures/schema/ignored.rbnew file mode 100644index 00000000000..13f0b8048c0--- /dev/null+++ b/migrations/spec/fixtures/schema/ignored.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++Migrations::Database::Schema.ignored do+ table :schema_migrations, "Rails internal table"+ table :ar_internal_metadata, "Rails internal table"+ tables :temp_data, :old_logs, reason: "Legacy tables no longer in use"+enddiff --git a/migrations/spec/fixtures/schema/tables/users.rb b/migrations/spec/fixtures/schema/tables/users.rbnew file mode 100644index 00000000000..1200081a1f3--- /dev/null+++ b/migrations/spec/fixtures/schema/tables/users.rb@@ -0,0 +1,16 @@+# frozen_string_literal: true++Migrations::Database::Schema.table :users do+ primary_key :id++ include :id, :username, :email, :created_at++ column :email, :text, required: true++ add_column :existing_id, :numeric++ ignore :admin_notes, reason: "Not needed for migration"++ index :username, unique: true+ check :email_format, "email LIKE '%@%'"+enddiff --git a/migrations/spec/lib/cli/schema_sub_command_spec.rb b/migrations/spec/lib/cli/schema_sub_command_spec.rbnew file mode 100644index 00000000000..5b759cf6d7e--- /dev/null+++ b/migrations/spec/lib/cli/schema_sub_command_spec.rb@@ -0,0 +1,266 @@+# frozen_string_literal: true++require "thor"++RSpec.describe Migrations::CLI::SchemaSubCommand do+ let(:command) { described_class.new }+ let(:schema) { Migrations::Database::Schema }++ def stub_command(options = {})+ allow(command).to receive(:load_rails!)+ allow(command).to receive(:puts)+ allow(command).to receive(:options).and_return(+ { database: "intermediate_db", verbose: false, force: false }.merge(options),+ )+ allow(schema).to receive(:available_databases).and_return(%w[intermediate_db])+ end++ def resolved_schema(table_name: "users", enum_name: "visibility")+ table =+ schema::TableDefinition.new(+ name: table_name,+ columns: [+ schema::ColumnDefinition.new(+ name: "id",+ datatype: :integer,+ nullable: false,+ max_length: nil,+ is_primary_key: true,+ enum: nil,+ ),+ ],+ indexes: [],+ primary_key_column_names: ["id"],+ constraints: [],+ model_mode: nil,+ )++ enum =+ schema::EnumDefinition.new(name: enum_name, values: { "public" => 0 }, datatype: :integer)++ schema::Definition.new(tables: [table], enums: [enum])+ end++ def diff_result(+ unconfigured_tables: [],+ missing_tables: [],+ stale_ignored_tables: [],+ table_diffs: []+ )+ Data.define(:unconfigured_tables, :missing_tables, :stale_ignored_tables, :table_diffs).new(+ unconfigured_tables:,+ missing_tables:,+ stale_ignored_tables:,+ table_diffs:,+ )+ end++ def table_info(name, plugin: nil)+ Data.define(:name, :plugin).new(name:, plugin:)+ end++ def column_info(name, plugin: nil)+ Data.define(:name, :plugin).new(name:, plugin:)+ end++ def table_diff(+ table_name:,+ unconfigured_columns: [],+ missing_columns: [],+ stale_ignored_columns: [],+ auto_ignored_columns: []+ )+ Data.define(+ :table_name,+ :unconfigured_columns,+ :missing_columns,+ :stale_ignored_columns,+ :auto_ignored_columns,+ ).new(+ table_name:,+ unconfigured_columns:,+ missing_columns:,+ stale_ignored_columns:,+ auto_ignored_columns:,+ )+ end++ describe "#generate" do+ it "prints a summary for the generated schema" do+ definition = resolved_schema++ stub_command+ allow(schema).to receive(:generate).with(database: "intermediate_db").and_return(definition)++ command.generate++ expect(command).to have_received(:puts).with(a_string_including("Generated 1 table, 1 enum"))+ end++ it "raises when the selected database is unknown" do+ stub_command(database: "archive_db")+ allow(schema).to receive(:available_databases).and_return(%w[intermediate_db])+ allow(schema).to receive(:generate)++ expect { command.generate }.to raise_error(+ schema::ConfigError,+ /Unknown database 'archive_db'/,+ )+ expect(schema).not_to have_received(:generate)+ end+ end++ describe "#ignore" do+ it "allows adding ignored tables without a reason" do+ stub_command+ allow(schema).to receive(:ignore_table).with(+ "users",+ reason: nil,+ database: "intermediate_db",+ )++ command.ignore("users")++ expect(schema).to have_received(:ignore_table).with(+ "users",+ reason: nil,+ database: "intermediate_db",+ )+ end+ end++ describe "#list" do+ it "prints configured tables, enums, and ignored table counts" do+ ignored = double(table_names: Set["z", "a"], ignored_plugin_names: %w[chat])++ stub_command+ allow(schema).to receive(:ensure_ready!).with(database: "intermediate_db")+ allow(schema).to receive(:tables).and_return({ "users" => double, "posts" => double })+ allow(schema).to receive(:enums).and_return({ "visibility" => double, "status" => double })+ allow(schema).to receive(:ignored_tables).and_return(ignored)+ allow(schema).to receive(:effective_ignored_table_names).with(+ database: "intermediate_db",+ ).and_return(Set["z", "a", "chat_messages"])++ command.list++ expect(command).to have_received(:puts).with("Configured tables (2):")+ expect(command).to have_received(:puts).with(" posts")+ expect(command).to have_received(:puts).with(" users")+ expect(command).to have_received(:puts).with("Enums (2):")+ expect(command).to have_received(:puts).with(" status")+ expect(command).to have_received(:puts).with(" visibility")+ expect(command).to have_received(:puts).with("Ignored tables: 2 explicit, 3 effective")+ expect(command).to have_received(:puts).with("Ignored plugins: 1")+ end+ end++ describe "#refresh_plugins" do+ it "reports incomplete manifest regeneration" do+ manifest = instance_double(Migrations::Database::Schema::DSL::PluginManifest)+ allow(manifest).to receive(:fresh?).and_return(false)+ allow(manifest).to receive(:regenerate!)+ allow(manifest).to receive(:incomplete?).and_return(true)+ allow(manifest).to receive(:failed_plugins).and_return(%w[chat])+ allow(manifest).to receive(:table_count).and_return(1)+ allow(manifest).to receive(:column_count).and_return(2)+ allow(manifest).to receive(:all_plugin_names).and_return(%w[chat])++ stub_command+ allow(command).to receive(:options).and_return(+ { database: "intermediate_db", force: false, verbose: false },+ )++ allow(schema).to receive(:ensure_ready!).with(+ database: "intermediate_db",+ refresh_manifest: false,+ )+ allow(schema).to receive(:plugin_manifest).and_return(manifest)++ command.refresh_plugins++ expect(command).to have_received(:puts).with(+ "Plugin manifest updated with warnings (failed plugins: chat)",+ )+ end+ end++ describe "#diff" do+ it "prints the detailed diff and suggestions" do+ result =+ diff_result(+ unconfigured_tables: [table_info("chat_channels", plugin: "chat")],+ missing_tables: [table_info("users")],+ stale_ignored_tables: [table_info("legacy_users")],+ table_diffs: [+ table_diff(+ table_name: "topics",+ unconfigured_columns: [column_info("chat_enabled", plugin: "chat")],+ missing_columns: [column_info("title")],+ stale_ignored_columns: [column_info("old_column")],+ auto_ignored_columns: [column_info("poll_enabled", plugin: "poll")],+ ),+ ],+ )++ stub_command(verbose: true)+ allow(schema).to receive(:diff).with(database: "intermediate_db").and_return(result)++ command.diff++ expect(command).to have_received(:puts).with(+ a_string_including(+ "Unconfigured tables",+ "+ chat_channels [chat]",+ "Missing tables",+ "- users",+ "Stale ignored tables",+ "~ legacy_users",+ "Column differences",+ "topics:",+ "+ chat_enabled [chat]",+ "- title",+ "~ old_column (ignored but gone)",+ "poll_enabled [poll] (auto-ignored from plugin)",+ ),+ )+ expect(command).to have_received(:puts).with(a_string_including("Suggested actions:"))+ end++ it "prints a no-differences message when the schema matches the database" do+ stub_command+ allow(schema).to receive(:diff).with(database: "intermediate_db").and_return(diff_result)++ command.diff++ expect(command).to have_received(:puts).with(a_string_including("No differences found"))+ end+ end++ describe "#add" do+ it "prints the created file path and next steps" do+ stub_command+ allow(schema).to receive(:add_table).with("users", database: "intermediate_db").and_return(+ "/tmp/users.rb",+ )++ command.add("users")++ expect(command).to have_received(:puts).with(a_string_including("Created /tmp/users.rb"))+ expect(command).to have_received(:puts).with("Next steps:")+ expect(command).to have_received(:puts).with(" 1. Edit the file to configure columns")+ expect(command).to have_received(:puts).with(" 2. Run 'migrations/bin/cli schema validate'")+ end+ end++ describe "#validate" do+ it "treats resolved schema errors as validation failures" do+ stub_command+ allow(schema).to receive(:validate).with(database: "intermediate_db").and_return(+ ["resolved schema problem"],+ )++ expect { command.validate }.to raise_error(SystemExit)+ end+ end+enddiff --git a/migrations/spec/lib/database/schema/dsl/config_builder_spec.rb b/migrations/spec/lib/database/schema/dsl/config_builder_spec.rbnew file mode 100644index 00000000000..27c95ed470a--- /dev/null+++ b/migrations/spec/lib/database/schema/dsl/config_builder_spec.rb@@ -0,0 +1,76 @@+# frozen_string_literal: true++RSpec.describe Migrations::Database::Schema::DSL::ConfigBuilder do+ after { Migrations::Database::Schema.reset! }++ describe "Schema.configure" do+ it "registers output configuration" do+ Migrations::Database::Schema.configure do+ output do+ schema_file "db/schema.sql"+ models_directory "lib/models"+ models_namespace "Test::Models"+ enums_directory "lib/enums"+ enums_namespace "Test::Enums"+ end+ end++ config = Migrations::Database::Schema.config+ expect(config).to be_a(Migrations::Database::Schema::DSL::Configuration)++ output = config.output_config+ expect(output.schema_file).to eq("db/schema.sql")+ expect(output.models_directory).to eq("lib/models")+ expect(output.models_namespace).to eq("Test::Models")+ expect(output.enums_directory).to eq("lib/enums")+ expect(output.enums_namespace).to eq("Test::Enums")+ end++ it "raises when output block is missing" do+ expect do Migrations::Database::Schema.configure {} end.to raise_error(+ Migrations::Database::Schema::ConfigError,+ /output/,+ )+ end++ it "raises when schema_file is missing" do+ expect do Migrations::Database::Schema.configure { output {} } end.to raise_error(+ Migrations::Database::Schema::ConfigError,+ /schema_file/,+ )+ end++ it "raises when other output fields are missing" do+ expect do+ Migrations::Database::Schema.configure { output { schema_file "db/schema.sql" } }+ end.to raise_error(+ Migrations::Database::Schema::ConfigError,+ /models_directory.*models_namespace.*enums_directory.*enums_namespace/,+ )+ end++ it "raises on duplicate configure calls" do+ Migrations::Database::Schema.configure do+ output do+ schema_file "db/schema.sql"+ models_directory "lib/models"+ models_namespace "Test::Models"+ enums_directory "lib/enums"+ enums_namespace "Test::Enums"+ end+ end++ expect do+ Migrations::Database::Schema.configure do+ output do+ schema_file "db/other.sql"+ models_directory "lib/models"+ models_namespace "Test::Models"+ enums_directory "lib/enums"+ enums_namespace "Test::Enums"+ end+ end+ end.to raise_error(Migrations::Database::Schema::ConfigError, /already registered/)+ end+ end+enddiff --git a/migrations/spec/lib/database/schema/dsl/conventions_builder_spec.rb b/migrations/spec/lib/database/schema/dsl/conventions_builder_spec.rbnew file mode 100644index 00000000000..ed1254045da--- /dev/null+++ b/migrations/spec/lib/database/schema/dsl/conventions_builder_spec.rb@@ -0,0 +1,29 @@+# frozen_string_literal: true++RSpec.describe Migrations::Database::Schema::DSL::ConventionsBuilder do+ after { Migrations::Database::Schema.reset! }++ describe "Schema.conventions" do+ it "matches exact names before regex patterns" do+ Migrations::Database::Schema.conventions do+ column :id do+ rename_to :original_id+ end++ columns_matching(/^id/) { rename_to :other_name }+ end++ conventions = Migrations::Database::Schema.conventions_config+ expect(conventions.effective_name("id")).to eq("original_id")+ end++ it "tracks ignored columns" do+ Migrations::Database::Schema.conventions { ignore_columns :created_by, :updated_by }++ conventions = Migrations::Database::Schema.conventions_config+ expect(conventions.ignored_column?("created_by")).to be true+ expect(conventions.ignored_column?("updated_by")).to be true+ expect(conventions.ignored_column?("username")).to be false+ end+ end+enddiff --git a/migrations/spec/lib/database/schema/dsl/differ_spec.rb b/migrations/spec/lib/database/schema/dsl/differ_spec.rbnew file mode 100644index 00000000000..b10d35e43ef--- /dev/null+++ b/migrations/spec/lib/database/schema/dsl/differ_spec.rb@@ -0,0 +1,430 @@+# frozen_string_literal: true++RSpec.describe Migrations::Database::Schema::DSL::Differ do+ after { Migrations::Database::Schema.reset! }++ let(:connection) { instance_double(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) }++ def mock_db_columns(names)+ names.map do |name|+ col = instance_double(ActiveRecord::ConnectionAdapters::Column)+ allow(col).to receive(:name).and_return(name.to_s)+ col+ end+ end++ def stub_database(connection, db_tables:, table_columns: {})+ allow(ActiveRecord::Base).to receive(:with_connection).and_yield(connection)+ allow(connection).to receive(:tables).and_return(db_tables.map(&:to_s))++ table_columns.each do |table_name, columns|+ allow(connection).to receive(:columns).with(table_name.to_s).and_return(+ mock_db_columns(columns),+ )+ end+ end++ def stub_plugin_manifest_unavailable+ manifest = instance_double(Migrations::Database::Schema::DSL::PluginManifest)+ allow(manifest).to receive(:fresh?).and_return(false)+ allow(manifest).to receive(:available?).and_return(false)+ allow(Migrations::Database::Schema).to receive(:plugin_manifest).and_return(manifest)+ end++ describe "#diff" do+ it "returns empty result when everything matches" do+ stub_plugin_manifest_unavailable++ Migrations::Database::Schema.table(:users) { include :id, :username }++ stub_database(connection, db_tables: %i[users], table_columns: { users: %i[id username] })++ result = described_class.new(Migrations::Database::Schema).diff++ expect(result.unconfigured_tables).to be_empty+ expect(result.missing_tables).to be_empty+ expect(result.stale_ignored_tables).to be_empty+ expect(result.table_diffs).to be_empty+ end++ it "detects unconfigured tables" do+ stub_plugin_manifest_unavailable++ Migrations::Database::Schema.table(:users) { include :id }++ stub_database(+ connection,+ db_tables: %i[users posts comments],+ table_columns: {+ users: %i[id],+ },+ )++ result = described_class.new(Migrations::Database::Schema).diff++ names = result.unconfigured_tables.map(&:name)+ expect(names).to contain_exactly("comments", "posts")+ end++ it "does not report copy_structure_from source tables as unknown" do+ stub_plugin_manifest_unavailable++ Migrations::Database::Schema.table :user_archive do+ copy_structure_from :users+ include :id+ end++ stub_database(connection, db_tables: %i[users], table_columns: { users: %i[id] })++ result = described_class.new(Migrations::Database::Schema).diff++ expect(result.unconfigured_tables).to be_empty+ end++ it "excludes ignored tables from unknown" do+ stub_plugin_manifest_unavailable++ Migrations::Database::Schema.table(:users) { include :id }+ Migrations::Database::Schema.ignored { table :posts, "not needed" }++ stub_database(+ connection,+ db_tables: %i[users posts comments],+ table_columns: {+ users: %i[id],+ },+ )++ result = described_class.new(Migrations::Database::Schema).diff++ names = result.unconfigured_tables.map(&:name)+ expect(names).to contain_exactly("comments")+ end++ it "detects missing tables" do+ stub_plugin_manifest_unavailable++ Migrations::Database::Schema.table(:users) { include :id }+ Migrations::Database::Schema.table(:posts) { include :id }++ stub_database(connection, db_tables: %i[users], table_columns: { users: %i[id] })++ result = described_class.new(Migrations::Database::Schema).diff++ names = result.missing_tables.map(&:name)+ expect(names).to contain_exactly("posts")+ end++ it "does not report synthetic tables as missing" do+ stub_plugin_manifest_unavailable++ Migrations::Database::Schema.table(:log_entries) do+ synthetic!+ add_column :created_at, :datetime+ end++ stub_database(connection, db_tables: [])++ result = described_class.new(Migrations::Database::Schema).diff++ expect(result.missing_tables).to be_empty+ end++ it "detects stale ignored tables" do+ stub_plugin_manifest_unavailable++ Migrations::Database::Schema.ignored do+ table :old_table, "removed"+ table :users, "still exists"+ end++ stub_database(connection, db_tables: %i[users])++ result = described_class.new(Migrations::Database::Schema).diff++ names = result.stale_ignored_tables.map(&:name)+ expect(names).to contain_exactly("old_table")+ end++ it "detects unconfigured columns" do+ stub_plugin_manifest_unavailable++ Migrations::Database::Schema.table(:users) { include :id, :username }++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: %i[id username email created_at],+ },+ )++ result = described_class.new(Migrations::Database::Schema).diff++ expect(result.table_diffs.size).to eq(1)+ diff = result.table_diffs.first+ expect(diff.table_name).to eq("users")+ names = diff.unconfigured_columns.map(&:name)+ expect(names).to contain_exactly("created_at", "email")+ end++ it "uses source table name when attributing unconfigured columns for copied tables" do+ manifest = instance_double(Migrations::Database::Schema::DSL::PluginManifest)+ allow(manifest).to receive(:available?).and_return(true)+ allow(manifest).to receive(:plugin_for_table).and_return(nil)+ allow(manifest).to receive(:plugin_for_column).with("users", "chat_enabled").and_return(+ "chat",+ )+ allow(Migrations::Database::Schema).to receive(:plugin_manifest).and_return(manifest)++ Migrations::Database::Schema.table :user_archive do+ copy_structure_from :users+ include :id, :username+ end++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: %i[id username chat_enabled],+ },+ )++ result = described_class.new(Migrations::Database::Schema).diff++ expect(result.table_diffs.size).to eq(1)+ diff = result.table_diffs.first+ expect(diff.table_name).to eq("user_archive")+ expect(diff.unconfigured_columns.map(&:name)).to eq(["chat_enabled"])+ expect(diff.unconfigured_columns.first.plugin).to eq("chat")+ end++ it "detects missing columns" do+ stub_plugin_manifest_unavailable++ Migrations::Database::Schema.table(:users) { include :id, :username, :bio }++ stub_database(connection, db_tables: %i[users], table_columns: { users: %i[id username] })++ result = described_class.new(Migrations::Database::Schema).diff++ expect(result.table_diffs.size).to eq(1)+ diff = result.table_diffs.first+ names = diff.missing_columns.map(&:name)+ expect(names).to contain_exactly("bio")+ end++ it "detects stale ignored columns" do+ stub_plugin_manifest_unavailable++ Migrations::Database::Schema.table(:users) do+ include :id, :username+ ignore :old_column, reason: "removed in migration"+ end++ stub_database(connection, db_tables: %i[users], table_columns: { users: %i[id username] })++ result = described_class.new(Migrations::Database::Schema).diff++ expect(result.table_diffs.size).to eq(1)+ diff = result.table_diffs.first+ names = diff.stale_ignored_columns.map(&:name)+ expect(names).to contain_exactly("old_column")+ end++ it "accounts for globally ignored columns" do+ stub_plugin_manifest_unavailable++ Migrations::Database::Schema.conventions { ignore_columns :updated_at }+ Migrations::Database::Schema.table(:users) { include :id, :username }++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: %i[id username updated_at],+ },+ )++ result = described_class.new(Migrations::Database::Schema).diff++ expect(result.table_diffs).to be_empty+ end++ it "skips column diff for tables missing from database" do+ stub_plugin_manifest_unavailable++ Migrations::Database::Schema.table(:users) { include :id, :nonexistent_col }++ stub_database(connection, db_tables: [])++ result = described_class.new(Migrations::Database::Schema).diff++ expect(result.table_diffs).to be_empty+ expect(result.missing_tables.size).to eq(1)+ end++ it "excludes tables from ignored plugins" do+ manifest = instance_double(Migrations::Database::Schema::DSL::PluginManifest)+ allow(manifest).to receive(:available?).and_return(true)+ allow(manifest).to receive(:tables_for_plugin).with("chat").and_return(+ %w[chat_channels chat_messages],+ )+ allow(manifest).to receive(:columns_for_plugin).and_return([])+ allow(Migrations::Database::Schema).to receive(:plugin_manifest).and_return(manifest)++ Migrations::Database::Schema.table(:users) { include :id }+ Migrations::Database::Schema.ignored { plugin :chat, "Not migrating" }++ stub_database(+ connection,+ db_tables: %i[users chat_channels chat_messages],+ table_columns: {+ users: %i[id],+ },+ )++ result = described_class.new(Migrations::Database::Schema).diff++ expect(result.unconfigured_tables).to be_empty+ end++ it "auto-ignores columns from ignored plugins without ignore_plugin_columns!" do+ manifest = instance_double(Migrations::Database::Schema::DSL::PluginManifest)+ allow(manifest).to receive(:available?).and_return(true)+ allow(manifest).to receive(:tables_for_plugin).and_return([])+ allow(manifest).to receive(:columns_for_plugin).with("chat", table: "users").and_return(+ %w[chat_enabled chat_sound],+ )+ allow(Migrations::Database::Schema).to receive(:plugin_manifest).and_return(manifest)++ Migrations::Database::Schema.table(:users) { include :id, :username }+ Migrations::Database::Schema.ignored { plugin :chat, "Not migrating" }++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: %i[id username chat_enabled chat_sound],+ },+ )++ result = described_class.new(Migrations::Database::Schema).diff++ expect(result.table_diffs.size).to eq(1)+ diff = result.table_diffs.first+ expect(diff.unconfigured_columns).to be_empty+ expect(diff.auto_ignored_columns.map(&:name)).to contain_exactly("chat_enabled", "chat_sound")+ end++ it "ignore_plugin_columns! additionally ignores columns from non-ignored plugins" do+ manifest = instance_double(Migrations::Database::Schema::DSL::PluginManifest)+ allow(manifest).to receive(:available?).and_return(true)+ allow(manifest).to receive(:tables_for_plugin).and_return([])+ allow(manifest).to receive(:all_plugin_names).and_return(%w[chat polls])+ allow(manifest).to receive(:columns_for_plugin).with("chat", table: "users").and_return(+ %w[chat_enabled],+ )+ allow(manifest).to receive(:columns_for_plugin).with("polls", table: "users").and_return(+ %w[polls_enabled],+ )+ allow(manifest).to receive(:plugin_for_column).and_return(nil)+ allow(Migrations::Database::Schema).to receive(:plugin_manifest).and_return(manifest)++ Migrations::Database::Schema.table(:users) do+ include :id, :username+ ignore_plugin_columns!+ end+ Migrations::Database::Schema.ignored { plugin :chat, "Not migrating" }++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: %i[id username chat_enabled polls_enabled],+ },+ )++ result = described_class.new(Migrations::Database::Schema).diff++ expect(result.table_diffs.size).to eq(1)+ diff = result.table_diffs.first+ expect(diff.unconfigured_columns).to be_empty+ expect(diff.auto_ignored_columns.map(&:name)).to contain_exactly(+ "chat_enabled",+ "polls_enabled",+ )+ end++ it "ignore_plugin_columns! with specific plugin names only ignores those plugins" do+ manifest = instance_double(Migrations::Database::Schema::DSL::PluginManifest)+ allow(manifest).to receive(:available?).and_return(true)+ allow(manifest).to receive(:tables_for_plugin).and_return([])+ allow(manifest).to receive(:all_plugin_names).and_return(%w[polls discourse_ai])+ allow(manifest).to receive(:columns_for_plugin).with("polls", table: "users").and_return(+ %w[polls_enabled],+ )+ allow(manifest).to receive(:columns_for_plugin).with(+ "discourse_ai",+ table: "users",+ ).and_return(%w[ai_summary])+ allow(manifest).to receive(:plugin_for_column).and_return(nil)+ allow(Migrations::Database::Schema).to receive(:plugin_manifest).and_return(manifest)++ Migrations::Database::Schema.table(:users) do+ include :id, :username+ ignore_plugin_columns! :polls+ end+ Migrations::Database::Schema.ignored { table :unused_table, "placeholder" }++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: %i[id username polls_enabled ai_summary],+ },+ )++ result = described_class.new(Migrations::Database::Schema).diff++ expect(result.table_diffs.size).to eq(1)+ diff = result.table_diffs.first+ expect(diff.auto_ignored_columns.map(&:name)).to contain_exactly("polls_enabled")+ expect(diff.unconfigured_columns.map(&:name)).to contain_exactly("ai_summary")+ end++ it "normalizes underscored plugin filters for ignore_plugin_columns!" do+ manifest = instance_double(Migrations::Database::Schema::DSL::PluginManifest)+ allow(manifest).to receive(:available?).and_return(true)+ allow(manifest).to receive(:tables_for_plugin).and_return([])+ allow(manifest).to receive(:all_plugin_names).and_return(%w[discourse-ai])+ allow(manifest).to receive(:columns_for_plugin).with(+ "discourse-ai",+ table: "users",+ ).and_return(%w[ai_summary])+ allow(manifest).to receive(:plugin_for_column).and_return(nil)+ allow(Migrations::Database::Schema).to receive(:plugin_manifest).and_return(manifest)++ Migrations::Database::Schema.table(:users) do+ include :id, :username+ ignore_plugin_columns! :discourse_ai+ end+ Migrations::Database::Schema.ignored { table :unused_table, "placeholder" }++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: %i[id username ai_summary],+ },+ )++ result = described_class.new(Migrations::Database::Schema).diff++ expect(result.table_diffs.size).to eq(1)+ diff = result.table_diffs.first+ expect(diff.auto_ignored_columns.map(&:name)).to contain_exactly("ai_summary")+ expect(diff.unconfigured_columns).to be_empty+ end+ end+enddiff --git a/migrations/spec/lib/database/schema/dsl/enum_builder_spec.rb b/migrations/spec/lib/database/schema/dsl/enum_builder_spec.rbnew file mode 100644index 00000000000..bacf87cbfae--- /dev/null+++ b/migrations/spec/lib/database/schema/dsl/enum_builder_spec.rb@@ -0,0 +1,55 @@+# frozen_string_literal: true++RSpec.describe Migrations::Database::Schema::DSL::EnumBuilder do+ after { Migrations::Database::Schema.reset! }++ describe "Schema.enum" do+ it "registers an enum with integer values" do+ Migrations::Database::Schema.enum :visibility do+ value :public, 0+ value :private, 1+ value :restricted, 2+ end++ enum = Migrations::Database::Schema.enums["visibility"]+ expect(enum.name).to eq("visibility")+ expect(enum.values).to eq({ "public" => 0, "private" => 1, "restricted" => 2 })+ expect(enum.datatype).to eq(:integer)+ end++ it "raises when enum has no values" do+ expect do Migrations::Database::Schema.enum(:empty) {} end.to raise_error(+ Migrations::Database::Schema::ConfigError,+ /at least one value/,+ )+ end++ it "raises on invalid source" do+ expect do+ Migrations::Database::Schema.enum :bad do+ source { raise "boom" }+ end+ end.to raise_error(Migrations::Database::Schema::ConfigError, /failed to evaluate/i)+ end++ it "raises on duplicate enum name" do+ Migrations::Database::Schema.enum(:status) { value :active, 0 }++ expect do+ Migrations::Database::Schema.enum(:status) { value :inactive, 1 }+ end.to raise_error(Migrations::Database::Schema::ConfigError, /already registered/)+ end++ it "raises when enum values mix strings and integers" do+ expect do+ Migrations::Database::Schema.enum :mixed do+ value :a, 0+ value :b, "b"+ end+ end.to raise_error(+ Migrations::Database::Schema::ConfigError,+ /all be Strings or all Integers/,+ )+ end+ end+enddiff --git a/migrations/spec/lib/database/schema/dsl/generator_spec.rb b/migrations/spec/lib/database/schema/dsl/generator_spec.rbnew file mode 100644index 00000000000..66bbb317e76--- /dev/null+++ b/migrations/spec/lib/database/schema/dsl/generator_spec.rb@@ -0,0 +1,261 @@+# frozen_string_literal: true++RSpec.describe Migrations::Database::Schema::DSL::Generator do+ after { Migrations::Database::Schema.reset! }++ def make_table(name, model_mode: nil)+ Migrations::Database::Schema::TableDefinition.new(+ name:,+ columns: [+ Migrations::Database::Schema::ColumnDefinition.new(+ name: "id",+ datatype: :integer,+ nullable: false,+ max_length: nil,+ is_primary_key: true,+ enum: nil,+ ),+ Migrations::Database::Schema::ColumnDefinition.new(+ name: "username",+ datatype: :text,+ nullable: false,+ max_length: nil,+ is_primary_key: false,+ enum: nil,+ ),+ ],+ indexes: [],+ primary_key_column_names: ["id"],+ constraints: [],+ model_mode:,+ )+ end++ let(:resolved_definition) do+ table = make_table("users")++ enum =+ Migrations::Database::Schema::EnumDefinition.new(+ name: "visibility",+ values: {+ "public" => 0,+ "private" => 1,+ },+ datatype: :integer,+ )++ Migrations::Database::Schema::Definition.new(tables: [table], enums: [enum])+ end++ def stub_validation_and_resolution(definition)+ allow(Migrations::Database::Schema).to receive(:preflight).and_return(+ Migrations::Database::Schema::PreflightResult.new(resolved: definition, errors: []),+ )++ allow(Migrations::Database::Schema::Helpers).to receive(:format_ruby_files)+ end++ def configure_output(tmpdir)+ sql_path = File.join(tmpdir, "schema.sql")+ models_path = File.join(tmpdir, "models")+ enums_path = File.join(tmpdir, "enums")++ Migrations::Database::Schema.configure do+ output do+ schema_file sql_path+ models_directory models_path+ models_namespace "Test::Models"+ enums_directory enums_path+ enums_namespace "Test::Enums"+ end+ end++ { sql: sql_path, models: models_path, enums: enums_path }+ end++ describe "#generate" do+ it "raises a generation error when preflight returns validation failures" do+ Dir.mktmpdir do |tmpdir|+ configure_output(tmpdir)++ allow(Migrations::Database::Schema).to receive(:preflight).and_return(+ Migrations::Database::Schema::PreflightResult.new(+ resolved: nil,+ errors: ["Table 'users': bad config"],+ ),+ )++ expect { described_class.new(Migrations::Database::Schema).generate }.to raise_error(+ Migrations::Database::Schema::GenerationError,+ /Schema validation failed with 1 error/,+ )+ end+ end++ it "generates SQL, model, and enum files" do+ Dir.mktmpdir do |tmpdir|+ paths = configure_output(tmpdir)+ stub_validation_and_resolution(resolved_definition)++ generator = described_class.new(Migrations::Database::Schema)+ result = generator.generate++ expect(result).to eq(resolved_definition)+ expect(File.exist?(paths[:sql])).to be true+ expect(Dir.exist?(paths[:models])).to be true+ expect(Dir.exist?(paths[:enums])).to be true++ sql_content = File.read(paths[:sql])+ expect(sql_content).to include("CREATE TABLE users")+ expect(sql_content).to include("id")+ expect(sql_content).to include("username")++ model_files = Dir[File.join(paths[:models], "*.rb")]+ expect(model_files.size).to eq(1)+ expect(File.basename(model_files.first)).to eq("user.rb")++ enum_files = Dir[File.join(paths[:enums], "*.rb")]+ expect(enum_files.size).to eq(1)+ expect(File.basename(enum_files.first)).to eq("visibility.rb")+ end+ end++ it "uses the configured namespace in model insert calls" do+ Dir.mktmpdir do |tmpdir|+ paths = configure_output(tmpdir)+ stub_validation_and_resolution(resolved_definition)++ described_class.new(Migrations::Database::Schema).generate++ model_content = File.read(File.join(paths[:models], "user.rb"))+ expect(model_content).to include("::Test::Models.insert(")+ expect(model_content).not_to include("IntermediateDB.insert(")+ end+ end++ it "derives header label from models_namespace" do+ Dir.mktmpdir do |tmpdir|+ paths = configure_output(tmpdir)+ stub_validation_and_resolution(resolved_definition)++ described_class.new(Migrations::Database::Schema).generate++ model_content = File.read(File.join(paths[:models], "user.rb"))+ expect(model_content).to include("auto-generated from the Models schema")+ end+ end++ it "does not write model file for :manual mode" do+ Dir.mktmpdir do |tmpdir|+ paths = configure_output(tmpdir)++ table = make_table("log_entries", model_mode: :manual)+ definition = Migrations::Database::Schema::Definition.new(tables: [table], enums: [])+ stub_validation_and_resolution(definition)++ described_class.new(Migrations::Database::Schema).generate++ expect(Dir[File.join(paths[:models], "*.rb")]).to be_empty+ end+ end++ it "does not create a model file for :manual mode even when models directory exists" do+ Dir.mktmpdir do |tmpdir|+ paths = configure_output(tmpdir)++ table = make_table("log_entries", model_mode: :manual)+ definition = Migrations::Database::Schema::Definition.new(tables: [table], enums: [])+ stub_validation_and_resolution(definition)++ FileUtils.mkdir_p(paths[:models])++ described_class.new(Migrations::Database::Schema).generate++ expect(File.exist?(File.join(paths[:models], "log_entry.rb"))).to be false+ end+ end++ it "generates model file with custom code markers for :extended mode" do+ Dir.mktmpdir do |tmpdir|+ paths = configure_output(tmpdir)++ table = make_table("uploads", model_mode: :extended)+ definition = Migrations::Database::Schema::Definition.new(tables: [table], enums: [])+ stub_validation_and_resolution(definition)++ described_class.new(Migrations::Database::Schema).generate++ content = File.read(File.join(paths[:models], "upload.rb"))+ expect(content).to include("# -- custom code --")+ expect(content).to include("# -- end custom code --")+ end+ end++ it "preserves custom code between markers on regeneration for :extended mode" do+ Dir.mktmpdir do |tmpdir|+ paths = configure_output(tmpdir)++ table = make_table("uploads", model_mode: :extended)+ definition = Migrations::Database::Schema::Definition.new(tables: [table], enums: [])+ stub_validation_and_resolution(definition)++ described_class.new(Migrations::Database::Schema).generate++ model_path = File.join(paths[:models], "upload.rb")+ original = File.read(model_path)+ custom_method = <<~RUBY.strip+ def self.create_for_file(path:)+ create(id: path.hash)+ end+ RUBY+ updated =+ original.sub(+ "# -- custom code --\n # -- end custom code --",+ "# -- custom code --\n #{custom_method}\n # -- end custom code --",+ )+ File.write(model_path, updated)++ Migrations::Database::Schema.reset!+ configure_output(tmpdir)+ stub_validation_and_resolution(definition)+ described_class.new(Migrations::Database::Schema).generate++ regenerated = File.read(model_path)+ expect(regenerated).to include("# -- custom code --")+ expect(regenerated).to include("def self.create_for_file(path:)")+ expect(regenerated).to include("# -- end custom code --")+ end+ end++ it "raises when an extended model contains invalid Ruby" do+ Dir.mktmpdir do |tmpdir|+ paths = configure_output(tmpdir)++ table = make_table("uploads", model_mode: :extended)+ definition = Migrations::Database::Schema::Definition.new(tables: [table], enums: [])+ stub_validation_and_resolution(definition)++ FileUtils.mkdir_p(paths[:models])+ File.write(File.join(paths[:models], "upload.rb"), "def broken(\n")++ expect { described_class.new(Migrations::Database::Schema).generate }.to raise_error(+ Migrations::Database::Schema::GenerationError,+ /Failed to parse/,+ )+ end+ end++ it "does not include custom code markers for default mode" do+ Dir.mktmpdir do |tmpdir|+ paths = configure_output(tmpdir)+ stub_validation_and_resolution(resolved_definition)++ described_class.new(Migrations::Database::Schema).generate++ content = File.read(File.join(paths[:models], "user.rb"))+ expect(content).not_to include("# -- custom code --")+ expect(content).not_to include("# -- end custom code --")+ end+ end+ end+enddiff --git a/migrations/spec/lib/database/schema/dsl/ignored_builder_spec.rb b/migrations/spec/lib/database/schema/dsl/ignored_builder_spec.rbnew file mode 100644index 00000000000..4d673b83305--- /dev/null+++ b/migrations/spec/lib/database/schema/dsl/ignored_builder_spec.rb@@ -0,0 +1,46 @@+# frozen_string_literal: true++RSpec.describe Migrations::Database::Schema::DSL::IgnoredBuilder do+ after { Migrations::Database::Schema.reset! }++ describe "Schema.ignored" do+ it "registers ignored tables with reasons" do+ Migrations::Database::Schema.ignored do+ table :schema_migrations, "Rails internal table"+ table :ar_internal_metadata, "Rails internal table"+ end++ ignored = Migrations::Database::Schema.ignored_tables+ expect(ignored.table_names).to include("schema_migrations")+ expect(ignored.table_names).to include("ar_internal_metadata")+ expect(ignored.table_names).not_to include("users")+ end++ it "supports batch ignore with shared reason" do+ Migrations::Database::Schema.ignored { tables :temp_data, :old_logs, reason: "Legacy tables" }++ ignored = Migrations::Database::Schema.ignored_tables+ expect(ignored.table_names).to eq(Set["temp_data", "old_logs"])+ expect(ignored.reason_for(:temp_data)).to eq("Legacy tables")+ expect(ignored.reason_for(:old_logs)).to eq("Legacy tables")+ end+ end++ describe "plugin DSL" do+ it "raises when plugin reason is missing" do+ expect do Migrations::Database::Schema.ignored { plugin :chat, "" } end.to raise_error(+ Migrations::Database::Schema::ConfigError,+ /reason/,+ )+ end++ it "normalizes underscored plugin names to hyphenated names" do+ Migrations::Database::Schema.ignored { plugin :discourse_ai, "Not migrating" }++ ignored = Migrations::Database::Schema.ignored_tables+ expect(ignored.ignored_plugin_names).to eq(["discourse-ai"])+ expect(ignored.plugin_ignored?(:discourse_ai)).to be true+ expect(ignored.plugin_ignored?(:"discourse-ai")).to be true+ end+ end+enddiff --git a/migrations/spec/lib/database/schema/dsl/ignored_file_editor_spec.rb b/migrations/spec/lib/database/schema/dsl/ignored_file_editor_spec.rbnew file mode 100644index 00000000000..b51a7f2f2f3--- /dev/null+++ b/migrations/spec/lib/database/schema/dsl/ignored_file_editor_spec.rb@@ -0,0 +1,78 @@+# frozen_string_literal: true++RSpec.describe Migrations::Database::Schema::DSL::IgnoredFileEditor do+ def with_ignored_file(content)+ Dir.mktmpdir do |tmpdir|+ ignored_path = File.join(tmpdir, "ignored.rb")+ File.write(ignored_path, content)+ yield tmpdir, ignored_path+ end+ end++ describe "#add_table" do+ it "preserves an existing tables-group reason when appending a table" do+ with_ignored_file(<<~RUBY) do |tmpdir, ignored_path|+ Migrations::Database::Schema.ignored do+ tables :a, :b, reason: "legacy"+ end+ RUBY+ allow(Migrations::Database::Schema::Helpers).to receive(:format_ruby_file)++ described_class.new(tmpdir).add_table(:c)++ content = File.read(ignored_path)+ expect(content).to include('tables :a, :b, :c, reason: "legacy"')+ end+ end++ it "inserts a standalone table entry when a reason is provided" do+ with_ignored_file(<<~RUBY) do |tmpdir, ignored_path|+ Migrations::Database::Schema.ignored do+ table :a+ end+ RUBY+ allow(Migrations::Database::Schema::Helpers).to receive(:format_ruby_file)++ described_class.new(tmpdir).add_table(:b, reason: "Legacy table")++ content = File.read(ignored_path)+ expect(content).to include('table :b, "Legacy table"')+ end+ end++ it "raises when the table is already ignored" do+ with_ignored_file(<<~RUBY) do |tmpdir, _ignored_path|+ Migrations::Database::Schema.ignored do+ tables :a, :b+ end+ RUBY+ expect { described_class.new(tmpdir).add_table(:a) }.to raise_error(+ Migrations::Database::Schema::ConfigError,+ /already ignored/,+ )+ end+ end++ it "raises when ignored.rb cannot be parsed" do+ with_ignored_file(+ "Migrations::Database::Schema.ignored do\n tables :a,\n",+ ) do |tmpdir, _ignored_path|+ expect { described_class.new(tmpdir).add_table(:b) }.to raise_error(+ Migrations::Database::Schema::ConfigError,+ /Could not parse/,+ )+ end+ end++ it "raises when the ignored block is missing" do+ with_ignored_file(+ "Migrations::Database::Schema.table :users do\nend\n",+ ) do |tmpdir, _ignored_path|+ expect { described_class.new(tmpdir).add_table(:b) }.to raise_error(+ Migrations::Database::Schema::ConfigError,+ /Could not find `Migrations::Database::Schema.ignored do ... end`/,+ )+ end+ end+ end+enddiff --git a/migrations/spec/lib/database/schema/dsl/loader_spec.rb b/migrations/spec/lib/database/schema/dsl/loader_spec.rbnew file mode 100644index 00000000000..eb1389b0e02--- /dev/null+++ b/migrations/spec/lib/database/schema/dsl/loader_spec.rb@@ -0,0 +1,70 @@+# frozen_string_literal: true++RSpec.describe Migrations::Database::Schema::DSL::Loader do+ let(:fixtures_path) { File.join(__dir__, "..", "..", "..", "..", "fixtures", "schema") }++ after { Migrations::Database::Schema.reset! }++ describe "#load!" do+ it "loads all config files from the fixture directory" do+ described_class.new(fixtures_path).load!++ config = Migrations::Database::Schema.config+ expect(config).to be_a(Migrations::Database::Schema::DSL::Configuration)+ expect(config.output_config.schema_file).to eq("db/test_schema/100-base-schema.sql")++ conventions = Migrations::Database::Schema.conventions_config+ expect(conventions).to be_a(Migrations::Database::Schema::DSL::ConventionsConfig)+ expect(conventions.effective_name("id")).to eq("original_id")++ ignored = Migrations::Database::Schema.ignored_tables+ expect(ignored.table_names).to include("schema_migrations")++ enums = Migrations::Database::Schema.enums+ expect(enums["visibility"]).to be_a(Migrations::Database::Schema::DSL::EnumDef)+ expect(enums["visibility"].values["public"]).to eq(0)++ tables = Migrations::Database::Schema.tables+ expect(tables["users"]).to be_a(Migrations::Database::Schema::DSL::TableDef)+ expect(tables["users"].primary_key_columns).to eq(%w[id])+ end++ it "raises when config directory does not exist" do+ expect { described_class.new("/nonexistent/path").load! }.to raise_error(+ Migrations::Database::Schema::ConfigError,+ /not found/,+ )+ end++ it "raises when config.rb is missing" do+ Dir.mktmpdir do |dir|+ expect { described_class.new(dir).load! }.to raise_error(+ Migrations::Database::Schema::ConfigError,+ /config\.rb/,+ )+ end+ end++ it "works without optional files" do+ Dir.mktmpdir do |dir|+ File.write(File.join(dir, "config.rb"), <<~RUBY)+ Migrations::Database::Schema.configure do+ output do+ schema_file "db/schema.sql"+ models_directory "lib/models"+ models_namespace "Test::Models"+ enums_directory "lib/enums"+ enums_namespace "Test::Enums"+ end+ end+ RUBY++ described_class.new(dir).load!++ expect(Migrations::Database::Schema.config).not_to be_nil+ expect(Migrations::Database::Schema.conventions_config).to be_nil+ expect(Migrations::Database::Schema.ignored_tables).to be_nil+ end+ end+ end+enddiff --git a/migrations/spec/lib/database/schema/dsl/plugin_introspector_spec.rb b/migrations/spec/lib/database/schema/dsl/plugin_introspector_spec.rbnew file mode 100644index 00000000000..6854eae3627--- /dev/null+++ b/migrations/spec/lib/database/schema/dsl/plugin_introspector_spec.rb@@ -0,0 +1,126 @@+# frozen_string_literal: true++require "stringio"++RSpec.describe Migrations::Database::Schema::DSL::PluginIntrospector do+ let(:plugins_path) { Dir.mktmpdir("plugins") }++ after { FileUtils.rm_rf(plugins_path) }++ def create_plugin(name, migrations: {})+ plugin_dir = File.join(plugins_path, name)+ migrate_dir = File.join(plugin_dir, "db", "migrate")+ FileUtils.mkdir_p(migrate_dir)++ migrations.each { |filename, content| File.write(File.join(migrate_dir, filename), content) }+ end++ describe ".compute_checksums" do+ it "returns a checksum per plugin" do+ create_plugin("chat", migrations: { "001_create_chat.rb" => "class CreateChat; end" })+ create_plugin("polls", migrations: { "001_create_polls.rb" => "class CreatePolls; end" })++ checksums = described_class.compute_checksums(plugins_path)++ expect(checksums.keys).to contain_exactly("chat", "polls")+ expect(checksums["chat"]).to be_a(String)+ expect(checksums["polls"]).to be_a(String)+ expect(checksums["chat"]).not_to eq(checksums["polls"])+ end++ it "excludes plugins without migration directories" do+ create_plugin("chat", migrations: { "001_create_chat.rb" => "class CreateChat; end" })+ FileUtils.mkdir_p(File.join(plugins_path, "no_migrations"))++ checksums = described_class.compute_checksums(plugins_path)++ expect(checksums.keys).to contain_exactly("chat")+ end++ it "changes when file content changes" do+ create_plugin("chat", migrations: { "001_create_chat.rb" => "class CreateChat; end" })++ checksum_before = described_class.compute_checksums(plugins_path)["chat"]++ File.write(+ File.join(plugins_path, "chat", "db", "migrate", "001_create_chat.rb"),+ "class CreateChatV2; end",+ )++ checksum_after = described_class.compute_checksums(plugins_path)["chat"]++ expect(checksum_before).not_to eq(checksum_after)+ end++ it "changes when a new migration file is added" do+ create_plugin("chat", migrations: { "001_create_chat.rb" => "class CreateChat; end" })++ checksum_before = described_class.compute_checksums(plugins_path)["chat"]++ File.write(+ File.join(plugins_path, "chat", "db", "migrate", "002_add_column.rb"),+ "class AddColumn; end",+ )++ checksum_after = described_class.compute_checksums(plugins_path)["chat"]++ expect(checksum_before).not_to eq(checksum_after)+ end++ it "returns an empty hash when there are no plugins" do+ expect(described_class.compute_checksums(plugins_path)).to eq({})+ end+ end++ describe "#introspect_plugin" do+ it "drops partial schema data when a plugin migration fails" do+ introspector = described_class.allocate+ stderr = StringIO.new++ allow(introspector).to receive(:snapshot_schema).and_return({ tables: Set[], columns: {} })+ allow(introspector).to receive(:run_plugin_migrations).and_raise(StandardError, "boom")+ allow(introspector).to receive(:diff_schema)++ result, failed = introspector.send(:introspect_plugin, "chat", ["unused"], stderr)++ expect(result).to be_nil+ expect(failed).to be true+ expect(introspector).not_to have_received(:diff_schema)+ expect(stderr.string).to include("Warning: 'chat' migration error: boom")+ end+ end++ describe "#introspect_plugins" do+ it "stops after the first failed plugin to avoid partial manifest data" do+ introspector = described_class.allocate+ stderr = StringIO.new+ plugins = {+ "alpha" => ["001_alpha.rb"],+ "broken" => ["001_broken.rb"],+ "omega" => ["001_omega.rb"],+ }++ allow(introspector).to receive(:introspect_plugin).with(+ "alpha",+ ["001_alpha.rb"],+ stderr,+ ).and_return([{ "tables" => ["alpha_table"], "columns" => {} }, false])+ allow(introspector).to receive(:introspect_plugin).with(+ "broken",+ ["001_broken.rb"],+ stderr,+ ).and_return([nil, true])++ plugin_data, failed_plugins = introspector.send(:introspect_plugins, plugins, stderr)++ expect(plugin_data).to eq("alpha" => { "tables" => ["alpha_table"], "columns" => {} })+ expect(failed_plugins).to eq(["broken"])+ expect(stderr.string).to include("stopping plugin introspection after 'broken' failed")+ expect(introspector).not_to have_received(:introspect_plugin).with(+ "omega",+ ["001_omega.rb"],+ stderr,+ )+ end+ end+enddiff --git a/migrations/spec/lib/database/schema/dsl/plugin_manifest_spec.rb b/migrations/spec/lib/database/schema/dsl/plugin_manifest_spec.rbnew file mode 100644index 00000000000..418977149a6--- /dev/null+++ b/migrations/spec/lib/database/schema/dsl/plugin_manifest_spec.rb@@ -0,0 +1,209 @@+# frozen_string_literal: true++RSpec.describe Migrations::Database::Schema::DSL::PluginManifest do+ let(:manifest_dir) { Dir.mktmpdir("manifest") }+ let(:manifest_path) { File.join(manifest_dir, "plugin_manifest.yml") }+ let(:plugins_path) { Dir.mktmpdir("plugins") }++ after do+ FileUtils.rm_rf(manifest_dir)+ FileUtils.rm_rf(plugins_path)+ end++ def write_manifest(data)+ File.write(manifest_path, YAML.dump(data))+ end++ def build_manifest+ described_class.new(manifest_path:, plugins_path:)+ end++ describe "#fresh?" do+ it "returns false when manifest file does not exist" do+ expect(build_manifest.fresh?).to be false+ end++ it "returns true when checksums match" do+ checksums =+ Migrations::Database::Schema::DSL::PluginIntrospector.compute_checksums(plugins_path)++ write_manifest({ "plugins" => {}, "plugin_checksums" => checksums })++ expect(build_manifest.fresh?).to be true+ end+ end++ describe "#plugin_for_table" do+ it "returns the plugin that owns a table" do+ write_manifest(+ {+ "plugins" => {+ "chat" => {+ "tables" => %w[chat_channels chat_messages],+ "columns" => {+ },+ },+ },+ "plugin_checksums" => {+ },+ },+ )++ manifest = build_manifest+ expect(manifest.plugin_for_table("chat_channels")).to eq("chat")+ expect(manifest.plugin_for_table("chat_messages")).to eq("chat")+ expect(manifest.plugin_for_table("users")).to be_nil+ end+ end++ describe "#plugin_for_column" do+ it "returns the plugin that owns a column" do+ write_manifest(+ {+ "plugins" => {+ "chat" => {+ "tables" => [],+ "columns" => {+ "user_options" => %w[chat_enabled chat_sound],+ },+ },+ },+ "plugin_checksums" => {+ },+ },+ )++ manifest = build_manifest+ expect(manifest.plugin_for_column("user_options", "chat_enabled")).to eq("chat")+ expect(manifest.plugin_for_column("user_options", "chat_sound")).to eq("chat")+ expect(manifest.plugin_for_column("user_options", "email")).to be_nil+ expect(manifest.plugin_for_column("other_table", "chat_enabled")).to be_nil+ end+ end++ describe "#tables_for_plugin" do+ it "returns tables for a given plugin" do+ write_manifest(+ {+ "plugins" => {+ "chat" => {+ "tables" => %w[chat_channels chat_messages],+ "columns" => {+ },+ },+ },+ "plugin_checksums" => {+ },+ },+ )++ expect(build_manifest.tables_for_plugin("chat")).to eq(%w[chat_channels chat_messages])+ end++ it "normalizes underscored symbol names to hyphenated manifest keys" do+ write_manifest(+ {+ "plugins" => {+ "discourse-ai" => {+ "tables" => %w[ai_tools ai_personas],+ "columns" => {+ },+ },+ },+ "plugin_checksums" => {+ },+ },+ )++ expect(build_manifest.tables_for_plugin(:discourse_ai)).to eq(%w[ai_tools ai_personas])+ end+ end++ describe "#columns_for_plugin" do+ before do+ write_manifest(+ {+ "plugins" => {+ "chat" => {+ "tables" => [],+ "columns" => {+ "user_options" => %w[chat_enabled chat_sound],+ "users" => %w[chat_status],+ },+ },+ },+ "plugin_checksums" => {+ },+ },+ )+ end++ it "returns all columns for a plugin" do+ cols = build_manifest.columns_for_plugin("chat")+ expect(cols).to eq(+ { "user_options" => %w[chat_enabled chat_sound], "users" => %w[chat_status] },+ )+ end++ it "returns columns for a specific table" do+ cols = build_manifest.columns_for_plugin("chat", table: "user_options")+ expect(cols).to eq(%w[chat_enabled chat_sound])+ end+ end++ describe "#regenerate!" do+ it "writes the manifest file when generated data matches the default empty state" do+ stable_data = {+ "plugins" => {+ },+ "plugin_checksums" => {+ },+ "failed_plugins" => [],+ "incomplete" => false,+ }++ introspector = instance_double(Migrations::Database::Schema::DSL::PluginIntrospector)+ allow(introspector).to receive(:introspect).and_return(stable_data)+ allow(Migrations::Database::Schema::DSL::PluginIntrospector).to receive(:new).and_return(+ introspector,+ )++ manifest = build_manifest+ manifest.regenerate!++ expect(File).to exist(manifest_path)+ expect(described_class.new(manifest_path:, plugins_path:).fresh?).to be true+ end++ it "does not rewrite file when manifest content is unchanged" do+ stable_data = {+ "plugins" => {+ "chat" => {+ "tables" => ["chat_channels"],+ "columns" => {+ "users" => ["chat_enabled"],+ },+ },+ },+ "plugin_checksums" => {+ "chat" => "def",+ },+ "failed_plugins" => [],+ "incomplete" => false,+ }++ write_manifest(stable_data)+ mtime_before = File.mtime(manifest_path)++ introspector = instance_double(Migrations::Database::Schema::DSL::PluginIntrospector)+ allow(introspector).to receive(:introspect).and_return(stable_data)+ allow(Migrations::Database::Schema::DSL::PluginIntrospector).to receive(:new).and_return(+ introspector,+ )++ build_manifest.regenerate!++ expect(File.mtime(manifest_path)).to eq(mtime_before)+ end+ end+enddiff --git a/migrations/spec/lib/database/schema/dsl/resolved_schema_validator_spec.rb b/migrations/spec/lib/database/schema/dsl/resolved_schema_validator_spec.rbnew file mode 100644index 00000000000..9359d2e420f--- /dev/null+++ b/migrations/spec/lib/database/schema/dsl/resolved_schema_validator_spec.rb@@ -0,0 +1,223 @@+# frozen_string_literal: true++RSpec.describe Migrations::Database::Schema::DSL::ResolvedSchemaValidator do+ def build_column(+ name: "col",+ datatype: :text,+ nullable: true,+ max_length: nil,+ is_primary_key: false,+ enum: nil+ )+ Migrations::Database::Schema::ColumnDefinition.new(+ name:,+ datatype:,+ nullable:,+ max_length:,+ is_primary_key:,+ enum:,+ )+ end++ def build_table(+ name: "test_table",+ columns: [],+ indexes: [],+ primary_key_column_names: [],+ constraints: [],+ model_mode: nil+ )+ Migrations::Database::Schema::TableDefinition.new(+ name:,+ columns:,+ indexes:,+ primary_key_column_names:,+ constraints:,+ model_mode:,+ )+ end++ def build_index(name: "idx_test", column_names: [], unique: false, condition: nil)+ Migrations::Database::Schema::IndexDefinition.new(name:, column_names:, unique:, condition:)+ end++ def build_constraint(name: "chk_test", type: :check, condition: "1=1")+ Migrations::Database::Schema::ConstraintDefinition.new(name:, type:, condition:)+ end++ def build_enum(name: "status", values: { "active" => 0 }, datatype: :integer)+ Migrations::Database::Schema::EnumDefinition.new(name:, values:, datatype:)+ end++ def build_schema(tables: [], enums: [])+ Migrations::Database::Schema::Definition.new(tables:, enums:)+ end++ describe "#validate" do+ it "returns no errors for a valid schema" do+ table =+ build_table(+ columns: [+ build_column(name: "id", datatype: :integer, is_primary_key: true, nullable: false),+ ],+ primary_key_column_names: ["id"],+ )+ schema = build_schema(tables: [table])++ errors = described_class.new(schema).validate+ expect(errors).to be_empty+ end++ it "detects invalid datatypes" do+ table = build_table(columns: [build_column(datatype: :invalid_type)])+ schema = build_schema(tables: [table])++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/invalid datatype 'invalid_type'/))+ end++ it "detects empty column names" do+ table = build_table(columns: [build_column(name: "")])+ schema = build_schema(tables: [table])++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/column has empty name/))+ end++ it "detects nullable primary key columns" do+ table =+ build_table(+ columns: [build_column(name: "id", is_primary_key: true, nullable: true)],+ primary_key_column_names: ["id"],+ )+ schema = build_schema(tables: [table])++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/primary key column 'id' should not be nullable/))+ end++ it "detects duplicate column names" do+ table = build_table(columns: [build_column(name: "dup"), build_column(name: "dup")])+ schema = build_schema(tables: [table])++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/duplicate column names: dup/))+ end++ it "detects primary key referencing missing columns" do+ table =+ build_table(+ columns: [build_column(name: "id")],+ primary_key_column_names: %w[id missing_col],+ )+ schema = build_schema(tables: [table])++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/primary key references missing columns: missing_col/))+ end++ it "detects indexes referencing missing columns" do+ table =+ build_table(+ columns: [build_column(name: "id")],+ indexes: [build_index(name: "idx_missing", column_names: ["nonexistent"])],+ )+ schema = build_schema(tables: [table])++ errors = described_class.new(schema).validate+ expect(errors).to include(+ match(/index 'idx_missing' references missing columns: nonexistent/),+ )+ end++ it "detects empty index names" do+ table =+ build_table(+ columns: [build_column(name: "id")],+ indexes: [build_index(name: "", column_names: ["id"])],+ )+ schema = build_schema(tables: [table])++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/index has empty name/))+ end++ it "detects duplicate index names" do+ table =+ build_table(+ columns: [build_column(name: "id")],+ indexes: [+ build_index(name: "idx_dup", column_names: ["id"]),+ build_index(name: "idx_dup", column_names: ["id"]),+ ],+ )+ schema = build_schema(tables: [table])++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/duplicate index names: idx_dup/))+ end++ it "detects empty constraint names" do+ table =+ build_table(columns: [build_column(name: "id")], constraints: [build_constraint(name: "")])+ schema = build_schema(tables: [table])++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/constraint has empty name/))+ end++ it "detects empty constraint conditions" do+ table =+ build_table(+ columns: [build_column(name: "id")],+ constraints: [build_constraint(name: "chk_empty", condition: "")],+ )+ schema = build_schema(tables: [table])++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/constraint 'chk_empty' has empty condition/))+ end++ it "detects empty enum names" do+ enum = build_enum(name: "")+ schema = build_schema(enums: [enum])++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/Enum has empty name/))+ end++ it "detects enums with no values" do+ enum = build_enum(values: {})+ schema = build_schema(enums: [enum])++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/has no values/))+ end++ it "detects enums with invalid datatype" do+ enum = build_enum(datatype: :float)+ schema = build_schema(enums: [enum])++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/invalid datatype 'float'/))+ end++ it "detects enums with values that do not match datatype" do+ enum = build_enum(values: { "active" => "active" }, datatype: :integer)+ schema = build_schema(enums: [enum])++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/do not match datatype 'integer'/))+ end++ it "accepts all valid datatypes" do+ valid_types = %i[blob boolean date datetime float inet integer json numeric text]+ columns = valid_types.map { |t| build_column(name: "col_#{t}", datatype: t) }+ table = build_table(columns:)+ schema = build_schema(tables: [table])++ errors = described_class.new(schema).validate+ expect(errors).to be_empty+ end+ end+enddiff --git a/migrations/spec/lib/database/schema/dsl/scaffolder_spec.rb b/migrations/spec/lib/database/schema/dsl/scaffolder_spec.rbnew file mode 100644index 00000000000..b37925aa23b--- /dev/null+++ b/migrations/spec/lib/database/schema/dsl/scaffolder_spec.rb@@ -0,0 +1,307 @@+# frozen_string_literal: true++RSpec.describe Migrations::Database::Schema::DSL::Scaffolder do+ after { Migrations::Database::Schema.reset! }++ let(:connection) { instance_double(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) }++ def mock_db_columns(columns_hash)+ columns_hash.map do |name, attrs|+ col = instance_double(ActiveRecord::ConnectionAdapters::Column)+ allow(col).to receive_messages(+ name: name.to_s,+ type: attrs[:type] || :text,+ null: attrs.fetch(:null, true),+ default: attrs[:default],+ limit: attrs[:limit],+ )+ col+ end+ end++ def mock_index(name:, columns:, unique: false, where: nil)+ instance_double(+ ActiveRecord::ConnectionAdapters::IndexDefinition,+ name:,+ columns:,+ unique:,+ where:,+ )+ end++ def stub_database(connection, db_tables:, table_columns: {}, primary_keys: {}, indexes: {})+ allow(ActiveRecord::Base).to receive(:with_connection).and_yield(connection)+ allow(connection).to receive(:tables).and_return(db_tables.map(&:to_s))++ table_columns.each do |table_name, columns|+ allow(connection).to receive(:columns).with(table_name.to_s).and_return(+ mock_db_columns(columns),+ )+ end++ primary_keys.each do |table_name, keys|+ allow(connection).to receive(:primary_keys).with(table_name.to_s).and_return(keys)+ end++ indexes.each do |table_name, idx_list|+ allow(connection).to receive(:indexes).with(table_name.to_s).and_return(idx_list)+ end+ end++ def scaffold_table(table_name, database: :intermediate_db, &setup)+ Dir.mktmpdir do |tmpdir|+ config_path = File.join(tmpdir, "schema")+ allow(Migrations::Database::Schema).to receive(:config_path).with(any_args).and_return(+ config_path,+ )++ yield if setup++ schema = Migrations::Database::Schema+ path = described_class.new(schema, table_name, database:).scaffold!+ [path, File.read(path)]+ end+ end++ describe "#scaffold!" do+ it "generates a table config file with DSL reference and include_all" do+ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: {+ id: {+ type: :integer,+ },+ username: {+ type: :text,+ },+ email: {+ type: :text,+ },+ },+ },+ primary_keys: {+ users: ["id"],+ },+ indexes: {+ users: [],+ },+ )++ path, content = scaffold_table(:users)++ expect(path).to end_with("/tables/users.rb")+ expect(content).to include("# Schema DSL Reference:")+ expect(content).to include("table :users do")+ expect(content).to include("include_all")+ expect(content).to include("# TODO: Configure columns.")+ expect(content).not_to include("include :id")+ end++ it "includes source table metadata as comments" do+ stub_database(+ connection,+ db_tables: %i[posts],+ table_columns: {+ posts: {+ id: {+ type: :integer,+ },+ title: {+ type: :text,+ },+ },+ },+ primary_keys: {+ posts: ["id"],+ },+ indexes: {+ posts: [+ mock_index(name: "index_posts_on_slug", columns: %w[slug], unique: true),+ mock_index(name: "index_posts_on_user_id", columns: %w[user_id]),+ mock_index(name: "index_posts_on_topic_id", columns: %w[topic_id]),+ ],+ },+ )++ _path, content = scaffold_table(:posts)++ expect(content).to include("# Source table: posts")+ expect(content).to include("# Primary key: id")+ expect(content).to include("# Unique indexes:\n# index_posts_on_slug (slug)")+ expect(content).not_to include("index_posts_on_user_id")+ expect(content).not_to include("index_posts_on_topic_id")+ end++ it "does not emit active index or unique_index DSL calls" do+ idx = mock_index(name: "idx_users_username", columns: %w[username], unique: true, where: nil)++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: {+ id: {+ type: :integer,+ },+ username: {+ type: :text,+ },+ },+ },+ primary_keys: {+ users: ["id"],+ },+ indexes: {+ users: [idx],+ },+ )++ _path, content = scaffold_table(:users)++ expect(content).not_to match(/^\s+unique_index\b/)+ expect(content).not_to match(/^\s+index\b/)+ expect(content).to include("# Unique indexes:\n# idx_users_username (username)")+ end++ it "writes to the selected database config path" do+ Dir.mktmpdir do |tmpdir|+ allow(Migrations::Database::Schema).to receive(+ :config_path,+ ) do |database = :intermediate_db|+ File.join(tmpdir, "schema", database.to_s)+ end++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: {+ id: {+ type: :integer,+ },+ username: {+ type: :text,+ },+ },+ },+ primary_keys: {+ users: ["id"],+ },+ indexes: {+ users: [],+ },+ )++ schema = Migrations::Database::Schema+ path = described_class.new(schema, :users, database: :archive_db).scaffold!++ expect(path).to include("/schema/archive_db/tables/users.rb")+ expect(+ File.exist?(File.join(tmpdir, "schema", "intermediate_db", "tables", "users.rb")),+ ).to be(false)+ end+ end++ it "includes composite primary keys" do+ stub_database(+ connection,+ db_tables: %i[topic_tags],+ table_columns: {+ topic_tags: {+ topic_id: {+ type: :integer,+ },+ tag_id: {+ type: :integer,+ },+ },+ },+ primary_keys: {+ topic_tags: %w[topic_id tag_id],+ },+ indexes: {+ topic_tags: [],+ },+ )++ _path, content = scaffold_table(:topic_tags)++ expect(content).to include("primary_key :topic_id, :tag_id")+ end++ it "raises when table does not exist" do+ stub_database(connection, db_tables: [])++ schema = Migrations::Database::Schema+ expect { described_class.new(schema, :nonexistent).scaffold! }.to raise_error(+ Migrations::Database::Schema::ConfigError,+ /does not exist/,+ )+ end++ it "raises when config file already exists" do+ Dir.mktmpdir do |tmpdir|+ config_path = File.join(tmpdir, "schema")+ tables_dir = File.join(config_path, "tables")+ FileUtils.mkdir_p(tables_dir)+ File.write(File.join(tables_dir, "users.rb"), "existing")++ allow(Migrations::Database::Schema).to receive(:config_path).with(any_args).and_return(+ config_path,+ )++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: {+ id: {+ type: :integer,+ },+ },+ },+ primary_keys: {+ users: ["id"],+ },+ indexes: {+ users: [],+ },+ )++ schema = Migrations::Database::Schema+ expect { described_class.new(schema, :users).scaffold! }.to raise_error(+ Migrations::Database::Schema::ConfigError,+ /already exists/,+ )+ end+ end++ it "omits non-unique indexes from metadata comments" do+ stub_database(+ connection,+ db_tables: %i[posts],+ table_columns: {+ posts: {+ id: {+ type: :integer,+ },+ },+ },+ primary_keys: {+ posts: ["id"],+ },+ indexes: {+ posts: [+ mock_index(name: "index_posts_on_user_id_and_topic_id", columns: %w[user_id topic_id]),+ ],+ },+ )++ _path, content = scaffold_table(:posts)++ expect(content).not_to include("index_posts_on_user_id_and_topic_id")+ end+ end+enddiff --git a/migrations/spec/lib/database/schema/dsl/schema_resolver_spec.rb b/migrations/spec/lib/database/schema/dsl/schema_resolver_spec.rbnew file mode 100644index 00000000000..249ef21907d--- /dev/null+++ b/migrations/spec/lib/database/schema/dsl/schema_resolver_spec.rb@@ -0,0 +1,546 @@+# frozen_string_literal: true++RSpec.describe Migrations::Database::Schema::DSL::SchemaResolver do+ after { Migrations::Database::Schema.reset! }++ def build_schema(tables: {}, enums: {}, conventions: nil)+ schema = Migrations::Database::Schema++ tables.each { |name, block| schema.table(name, &block) }++ enums.each { |name, block| schema.enum(name, &block) }++ schema.conventions(&conventions) if conventions++ schema+ end++ def mock_db_columns(columns_hash)+ columns_hash.map do |name, attrs|+ col = instance_double(ActiveRecord::ConnectionAdapters::Column)+ allow(col).to receive_messages(+ name: name.to_s,+ type: attrs[:type] || :text,+ null: attrs.fetch(:null, true),+ default: attrs[:default],+ limit: attrs[:limit],+ )+ col+ end+ end++ def stub_database(connection, table_columns: {}, primary_keys: {})+ allow(ActiveRecord::Base).to receive(:with_connection).and_yield(connection)++ table_columns.each do |table_name, columns|+ allow(connection).to receive(:columns).with(table_name.to_s).and_return(+ mock_db_columns(columns),+ )+ end++ primary_keys.each do |table_name, keys|+ allow(connection).to receive(:primary_keys).with(table_name.to_s).and_return(keys)+ end+ end++ let(:connection) { instance_double(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) }++ describe "#resolve" do+ it "resolves a simple table with all DB columns" do+ schema = build_schema(tables: { users: proc { include_all } })++ stub_database(+ connection,+ table_columns: {+ users: {+ id: {+ type: :integer,+ null: false,+ },+ username: {+ type: :text,+ null: false,+ },+ email: {+ type: :text,+ null: true,+ },+ },+ },+ primary_keys: {+ users: ["id"],+ },+ )++ result = described_class.new(schema).resolve++ expect(result).to be_a(Migrations::Database::Schema::Definition)+ expect(result.tables.size).to eq(1)++ table = result.tables.first+ expect(table.name).to eq("users")+ expect(table.columns.size).to eq(3)+ expect(table.primary_key_column_names).to eq(["id"])+ end++ it "resolves only included columns" do+ schema = build_schema(tables: { users: proc { include :id, :username } })++ stub_database(+ connection,+ table_columns: {+ users: {+ id: {+ type: :integer,+ null: false,+ },+ username: {+ type: :text,+ null: false,+ },+ email: {+ type: :text,+ null: true,+ },+ },+ },+ primary_keys: {+ users: ["id"],+ },+ )++ result = described_class.new(schema).resolve+ table = result.tables.first+ expect(table.columns.map(&:name)).to contain_exactly("id", "username")+ end++ it "applies conventions for name and type" do+ schema =+ build_schema(+ tables: {+ users: proc { include :id, :username },+ },+ conventions:+ proc do+ column :id do+ rename_to :original_id+ type :integer+ required+ end+ end,+ )++ stub_database(+ connection,+ table_columns: {+ users: {+ id: {+ type: :integer,+ null: true,+ },+ username: {+ type: :text,+ null: false,+ },+ },+ },+ primary_keys: {+ users: ["id"],+ },+ )++ result = described_class.new(schema).resolve+ table = result.tables.first+ id_col = table.columns.find { |c| c.name == "original_id" }++ expect(id_col).not_to be_nil+ expect(id_col.datatype).to eq(:integer)+ expect(id_col.nullable).to eq(false)+ end++ it "resolves added columns" do+ schema =+ build_schema(+ tables: {+ users:+ proc do+ include :id+ add_column :existing_id, :numeric+ end,+ },+ )++ stub_database(+ connection,+ table_columns: {+ users: {+ id: {+ type: :integer,+ null: false,+ },+ },+ },+ primary_keys: {+ users: ["id"],+ },+ )++ result = described_class.new(schema).resolve+ table = result.tables.first+ added = table.columns.find { |c| c.name == "existing_id" }++ expect(added).not_to be_nil+ expect(added.datatype).to eq(:numeric)+ expect(added.nullable).to eq(true)+ end++ it "resolves indexes with convention-renamed columns" do+ schema =+ build_schema(+ tables: {+ users:+ proc do+ include :id+ index :id, name: :idx_users_id+ end,+ },+ conventions:+ proc do+ column :id do+ rename_to :original_id+ end+ end,+ )++ stub_database(+ connection,+ table_columns: {+ users: {+ id: {+ type: :integer,+ null: false,+ },+ },+ },+ primary_keys: {+ users: ["id"],+ },+ )++ result = described_class.new(schema).resolve+ table = result.tables.first+ idx = table.indexes.first++ expect(idx.name).to eq("idx_users_id")+ expect(idx.column_names).to eq(["original_id"])+ end++ it "resolves constraints" do+ schema =+ build_schema(+ tables: {+ users:+ proc do+ include :id, :email+ check :email_format, "email LIKE '%@%'"+ end,+ },+ )++ stub_database(+ connection,+ table_columns: {+ users: {+ id: {+ type: :integer,+ null: false,+ },+ email: {+ type: :text,+ null: false,+ },+ },+ },+ primary_keys: {+ users: ["id"],+ },+ )++ result = described_class.new(schema).resolve+ table = result.tables.first++ expect(table.constraints.size).to eq(1)+ expect(table.constraints.first.name).to eq("email_format")+ expect(table.constraints.first.condition).to eq("email LIKE '%@%'")+ end++ it "resolves enums" do+ schema =+ build_schema(+ enums: {+ visibility:+ proc do+ value :public, 0+ value :private, 1+ end,+ },+ )++ stub_database(connection, table_columns: {}, primary_keys: {})++ result = described_class.new(schema).resolve++ expect(result.enums.size).to eq(1)+ enum = result.enums.first+ expect(enum.name).to eq("visibility")+ expect(enum.values).to eq({ "public" => 0, "private" => 1 })+ expect(enum.datatype).to eq(:integer)+ end++ it "resolves copy_structure_from by introspecting source table" do+ schema =+ build_schema(+ tables: {+ user_archive:+ proc do+ copy_structure_from :users+ include :id, :username+ end,+ },+ )++ stub_database(+ connection,+ table_columns: {+ users: {+ id: {+ type: :integer,+ null: false,+ },+ username: {+ type: :text,+ null: false,+ },+ },+ },+ primary_keys: {+ users: ["id"],+ },+ )++ result = described_class.new(schema).resolve+ table = result.tables.first++ expect(table.name).to eq("user_archive")+ expect(table.columns.map(&:name)).to contain_exactly("id", "username")+ end++ it "normalizes datatypes" do+ schema = build_schema(tables: { data: proc { include :binary_col, :string_col, :jsonb_col } })++ stub_database(+ connection,+ table_columns: {+ data: {+ binary_col: {+ type: :binary,+ null: true,+ },+ string_col: {+ type: :string,+ null: true,+ },+ jsonb_col: {+ type: :jsonb,+ null: true,+ },+ },+ },+ primary_keys: {+ data: [],+ },+ )++ result = described_class.new(schema).resolve+ table = result.tables.first+ types = table.columns.map { |c| [c.name, c.datatype] }.to_h++ expect(types["binary_col"]).to eq(:blob)+ expect(types["string_col"]).to eq(:text)+ expect(types["jsonb_col"]).to eq(:json)+ end++ it "applies per-table rename_to override" do+ schema =+ build_schema(+ tables: {+ posts:+ proc do+ include :id, :user_id+ column :user_id, rename_to: :author_id+ end,+ },+ )++ stub_database(+ connection,+ table_columns: {+ posts: {+ id: {+ type: :integer,+ null: false,+ },+ user_id: {+ type: :integer,+ null: false,+ },+ },+ },+ primary_keys: {+ posts: ["id"],+ },+ )++ result = described_class.new(schema).resolve+ table = result.tables.first+ col = table.columns.find { |c| c.name == "author_id" }++ expect(col).not_to be_nil+ expect(table.columns.none? { |c| c.name == "user_id" }).to eq(true)+ end++ it "per-table rename_to takes precedence over conventions" do+ schema =+ build_schema(+ tables: {+ posts:+ proc do+ include :id+ column :id, rename_to: :post_original_id+ end,+ },+ conventions:+ proc do+ column :id do+ rename_to :original_id+ end+ end,+ )++ stub_database(+ connection,+ table_columns: {+ posts: {+ id: {+ type: :integer,+ null: false,+ },+ },+ },+ primary_keys: {+ posts: ["id"],+ },+ )++ result = described_class.new(schema).resolve+ table = result.tables.first+ col = table.columns.find { |c| c.name == "post_original_id" }++ expect(col).not_to be_nil+ expect(table.columns.none? { |c| c.name == "original_id" }).to eq(true)+ end++ it "resolves a table with synthetic! using only add_columns" do+ schema =+ build_schema(+ tables: {+ log_entries:+ proc do+ synthetic!+ add_column :created_at, :datetime, required: true+ add_column :type, :text, required: true+ add_column :message, :text, required: true+ add_column :exception, :text+ add_column :details, :json+ end,+ },+ )++ stub_database(connection, table_columns: {}, primary_keys: {})++ result = described_class.new(schema).resolve+ table = result.tables.first++ expect(table.name).to eq("log_entries")+ expect(table.columns.size).to eq(5)+ expect(table.primary_key_column_names).to eq([])++ created_at = table.columns.find { |c| c.name == "created_at" }+ expect(created_at.datatype).to eq(:datetime)+ expect(created_at.nullable).to eq(false)++ details = table.columns.find { |c| c.name == "details" }+ expect(details.datatype).to eq(:json)+ expect(details.nullable).to eq(true)+ end++ it "resolves composite primary keys for nil source tables" do+ schema =+ build_schema(+ tables: {+ user_suspensions:+ proc do+ synthetic!+ primary_key :user_id, :suspended_at+ add_column :user_id, :numeric, required: true+ add_column :suspended_at, :datetime, required: true+ add_column :reason, :text+ end,+ },+ )++ stub_database(connection, table_columns: {}, primary_keys: {})++ result = described_class.new(schema).resolve+ table = result.tables.first++ expect(table.primary_key_column_names).to eq(%w[user_id suspended_at])+ expect(table.columns.find { |c| c.name == "user_id" }.is_primary_key).to eq(true)+ expect(table.columns.find { |c| c.name == "suspended_at" }.is_primary_key).to eq(true)+ expect(table.columns.find { |c| c.name == "reason" }.is_primary_key).to eq(false)+ end++ it "excludes globally ignored columns when no include list specified" do+ schema =+ build_schema(+ tables: {+ users: proc { include_all },+ },+ conventions: proc { ignore_columns :secret_token },+ )++ stub_database(+ connection,+ table_columns: {+ users: {+ id: {+ type: :integer,+ null: false,+ },+ secret_token: {+ type: :text,+ null: true,+ },+ },+ },+ primary_keys: {+ users: ["id"],+ },+ )++ result = described_class.new(schema).resolve+ table = result.tables.first+ expect(table.columns.map(&:name)).to eq(["id"])+ end+ end+enddiff --git a/migrations/spec/lib/database/schema/dsl/table_builder_spec.rb b/migrations/spec/lib/database/schema/dsl/table_builder_spec.rbnew file mode 100644index 00000000000..db6b1aa1664--- /dev/null+++ b/migrations/spec/lib/database/schema/dsl/table_builder_spec.rb@@ -0,0 +1,86 @@+# frozen_string_literal: true++RSpec.describe Migrations::Database::Schema::DSL::TableBuilder do+ after { Migrations::Database::Schema.reset! }++ describe "Schema.table" do+ it "registers a basic table" do+ Migrations::Database::Schema.table :users do+ primary_key :id+ include :id, :username, :email+ end++ table = Migrations::Database::Schema.tables["users"]+ expect(table.name).to eq("users")+ expect(table.source_table_name).to eq("users")+ expect(table.primary_key_columns).to eq(%w[id])+ expect(table.included_column_names).to eq(%w[id username email])+ end++ it "raises when block has no include, include_all, ignore, or synthetic!" do+ expect do+ Migrations::Database::Schema.table :badge_groupings do+ primary_key :id+ end+ end.to raise_error(+ Migrations::Database::Schema::ConfigError,+ /must use `include_all`, `include`, or `ignore`/,+ )+ end++ it "raises on invalid model mode" do+ expect do+ Migrations::Database::Schema.table :users do+ model :invalid+ end+ end.to raise_error(Migrations::Database::Schema::ConfigError, /Invalid model mode :invalid/)+ end++ it "raises when synthetic table uses include or include_all" do+ %i[include include_all].each do |method|+ expect do+ Migrations::Database::Schema.table :"log_#{method}" do+ synthetic!+ send(method, *(:id if method == :include))+ end+ end.to raise_error(+ Migrations::Database::Schema::ConfigError,+ /synthetic and cannot use `include` or `include_all`/,+ )+ end+ end++ it "raises when columns are both included and ignored" do+ expect do+ Migrations::Database::Schema.table :users do+ include :id, :email+ ignore :email, reason: "duplicate"+ end+ end.to raise_error(+ Migrations::Database::Schema::ConfigError,+ /both included and ignored.*email/,+ )+ end++ it "raises when both include and include_all are used" do+ expect do+ Migrations::Database::Schema.table :users do+ include :id+ include_all+ end+ end.to raise_error(+ Migrations::Database::Schema::ConfigError,+ /both `include` and `include_all`/,+ )+ end++ it "raises on duplicate table name" do+ Migrations::Database::Schema.table(:users) { include_all }++ expect do Migrations::Database::Schema.table(:users) { include_all } end.to raise_error(+ Migrations::Database::Schema::ConfigError,+ /already registered/,+ )+ end+ end+enddiff --git a/migrations/spec/lib/database/schema/dsl/validator_spec.rb b/migrations/spec/lib/database/schema/dsl/validator_spec.rbnew file mode 100644index 00000000000..9861d09939d--- /dev/null+++ b/migrations/spec/lib/database/schema/dsl/validator_spec.rb@@ -0,0 +1,733 @@+# frozen_string_literal: true++RSpec.describe Migrations::Database::Schema::DSL::Validator do+ after { Migrations::Database::Schema.reset! }++ def build_schema(tables: {}, enums: {}, conventions: nil, ignored: nil)+ schema = Migrations::Database::Schema++ tables.each { |name, block| schema.table(name, &block) }+ enums.each { |name, block| schema.enum(name, &block) }+ schema.conventions(&conventions) if conventions+ schema.ignored(&ignored) if ignored++ schema+ end++ def mock_db_columns(columns_hash)+ columns_hash.map do |name, attrs|+ col = instance_double(ActiveRecord::ConnectionAdapters::Column)+ allow(col).to receive_messages(+ name: name.to_s,+ type: attrs[:type] || :text,+ null: attrs.fetch(:null, true),+ default: attrs[:default],+ limit: attrs[:limit],+ )+ col+ end+ end++ def stub_database(connection, db_tables: [], table_columns: {}, primary_keys: {})+ allow(ActiveRecord::Base).to receive(:with_connection).and_yield(connection)+ allow(connection).to receive(:tables).and_return(db_tables.map(&:to_s))++ allow(connection).to receive(:primary_keys) do |table_name|+ primary_keys.fetch(table_name.to_sym) { primary_keys.fetch(table_name.to_s, []) }+ end++ table_columns.each do |table_name, columns|+ allow(connection).to receive(:columns).with(table_name.to_s).and_return(+ mock_db_columns(columns),+ )+ end+ end++ let(:connection) { instance_double(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) }++ describe "#validate" do+ it "returns no errors for a valid configuration" do+ schema = build_schema(tables: { users: proc { include :id, :username } })++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: {+ id: {+ type: :integer,+ null: false,+ },+ username: {+ type: :text,+ null: false,+ },+ },+ },+ )++ errors = described_class.new(schema).validate++ expect(errors).to be_an(Array)+ expect(errors).to be_empty+ end++ it "detects unconfigured tables in database" do+ schema = build_schema(tables: { users: proc { include_all } })++ stub_database(+ connection,+ db_tables: %i[users posts comments],+ table_columns: {+ users: {+ id: {+ type: :integer,+ },+ },+ },+ )++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/not configured or ignored.*comments.*posts/m))+ end++ it "does not report ignored tables as unconfigured" do+ schema =+ build_schema(+ tables: {+ users: proc { include_all },+ },+ ignored: proc { table :posts, "not needed" },+ )++ stub_database(+ connection,+ db_tables: %i[users posts],+ table_columns: {+ users: {+ id: {+ type: :integer,+ },+ },+ },+ )++ errors = described_class.new(schema).validate+ expect(errors).to be_empty+ end++ it "detects source table not existing in database" do+ schema = build_schema(tables: { users: proc { include_all } })++ stub_database(connection, db_tables: [], table_columns: {})++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/source table 'users' does not exist/))+ end++ it "detects included columns not existing in database" do+ schema = build_schema(tables: { users: proc { include :id, :nonexistent } })++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: {+ id: {+ type: :integer,+ },+ },+ },+ )++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/included columns do not exist.*nonexistent/))+ end++ it "detects database columns that are not configured or ignored" do+ schema = build_schema(tables: { users: proc { include :id, :username } })++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: {+ id: {+ type: :integer,+ },+ username: {+ type: :text,+ },+ email: {+ type: :text,+ },+ },+ },+ )++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/database columns are not configured or ignored.*email/))+ end++ it "detects column options referencing missing columns" do+ schema =+ build_schema(+ tables: {+ users:+ proc do+ include :id+ column :ghost, :integer+ end,+ },+ )++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: {+ id: {+ type: :integer,+ },+ },+ },+ )++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/column option for 'ghost'.*does not exist/))+ end++ it "detects added columns that already exist in database" do+ schema =+ build_schema(+ tables: {+ users:+ proc do+ include :id+ add_column :username, :text+ end,+ },+ )++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: {+ id: {+ type: :integer,+ },+ username: {+ type: :text,+ },+ },+ },+ )++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/added column 'username' already exists/))+ end++ it "detects added columns referencing unknown enums" do+ schema =+ build_schema(+ tables: {+ users:+ proc do+ include :id+ add_column :status, :integer, enum: :nonexistent_enum+ end,+ },+ )++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: {+ id: {+ type: :integer,+ },+ },+ },+ )++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/references unknown enum 'nonexistent_enum'/))+ end++ it "detects column type overrides referencing unknown enums" do+ schema =+ build_schema(+ tables: {+ users:+ proc do+ include :id, :status+ column :status, :missing_enum+ end,+ },+ )++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: {+ id: {+ type: :integer,+ },+ status: {+ type: :text,+ },+ },+ },+ )++ errors = described_class.new(schema).validate+ expect(errors).to include(+ match(/column 'status' type 'missing_enum' references unknown enum/),+ )+ end++ it "detects stale ignored columns" do+ schema =+ build_schema(+ tables: {+ users:+ proc do+ include :id+ ignore :gone_column, reason: "was removed"+ end,+ },+ )++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: {+ id: {+ type: :integer,+ },+ },+ },+ )++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/ignored column 'gone_column' does not exist.*stale/))+ end++ it "detects stale ignored tables" do+ schema =+ build_schema(+ tables: {+ users: proc { include_all },+ },+ ignored: proc { table :deleted_table, "no longer exists" },+ )++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: {+ id: {+ type: :integer,+ },+ },+ },+ )++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/Ignored table 'deleted_table' does not exist.*stale/))+ end++ it "detects index columns not in configuration" do+ schema =+ build_schema(+ tables: {+ users:+ proc do+ include :id+ index :missing_col, name: :idx_missing+ end,+ },+ )++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: {+ id: {+ type: :integer,+ },+ },+ },+ )++ errors = described_class.new(schema).validate+ expect(errors).to include(+ match(/index 'idx_missing' references columns not in configuration/),+ )+ end++ it "validates copy_structure_from source table" do+ schema =+ build_schema(+ tables: {+ user_archive:+ proc do+ copy_structure_from :users+ include :id+ end,+ },+ )++ stub_database(connection, db_tables: [], table_columns: {})++ errors = described_class.new(schema).validate+ expect(errors).to contain_exactly(match(/source table 'users' does not exist in database/))+ end++ it "does not report copy_structure_from source tables as unconfigured" do+ schema =+ build_schema(+ tables: {+ user_archive:+ proc do+ copy_structure_from :users+ include :id+ end,+ },+ )++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: {+ id: {+ type: :integer,+ },+ },+ },+ primary_keys: {+ users: ["id"],+ },+ )++ errors = described_class.new(schema).validate+ expect(errors).to be_empty+ end++ it "passes when index references added columns" do+ schema =+ build_schema(+ tables: {+ users:+ proc do+ include :id+ add_column :new_col, :text+ index :new_col, name: :idx_new_col+ end,+ },+ )++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: {+ id: {+ type: :integer,+ },+ },+ },+ )++ errors = described_class.new(schema).validate+ expect(errors).to be_empty+ end++ it "detects when primary key columns are not configured" do+ schema = build_schema(tables: { users: proc { include :username } })++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: {+ id: {+ type: :integer,+ null: false,+ },+ username: {+ type: :text,+ null: false,+ },+ },+ },+ primary_keys: {+ users: ["id"],+ },+ )++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/primary key columns are not configured.*id/))+ end++ it "does not report plugin-ignored tables as unconfigured" do+ manifest = instance_double(Migrations::Database::Schema::DSL::PluginManifest)+ allow(manifest).to receive(:available?).and_return(true)+ allow(manifest).to receive(:tables_for_plugin).with("chat").and_return(+ %w[chat_channels chat_messages],+ )+ allow(manifest).to receive(:columns_for_plugin).with("chat", table: "users").and_return([])+ allow(manifest).to receive(:plugin_for_table).with("users").and_return(nil)+ allow(Migrations::Database::Schema).to receive(:plugin_manifest).and_return(manifest)++ schema =+ build_schema(+ tables: {+ users: proc { include_all },+ },+ ignored: proc { plugin :chat, "Not migrating" },+ )++ stub_database(+ connection,+ db_tables: %i[users chat_channels chat_messages],+ table_columns: {+ users: {+ id: {+ type: :integer,+ },+ },+ },+ )++ errors = described_class.new(schema).validate+ expect(errors).to be_empty+ end++ it "detects tables that are both configured and ignored" do+ schema =+ build_schema(+ tables: {+ users: proc { include_all },+ },+ ignored: proc { table :users, "not needed" },+ )++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: {+ id: {+ type: :integer,+ },+ },+ },+ )++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/Table 'users' is both configured and ignored/))+ end++ it "detects source table belonging to an ignored plugin" do+ manifest = instance_double(Migrations::Database::Schema::DSL::PluginManifest)+ allow(manifest).to receive(:available?).and_return(true)+ allow(manifest).to receive(:tables_for_plugin).with("chat").and_return(+ %w[chat_channels chat_messages],+ )+ allow(manifest).to receive(:plugin_for_table).with("chat_channels").and_return("chat")+ allow(manifest).to receive(:columns_for_plugin).and_return([])+ allow(manifest).to receive(:all_plugin_names).and_return(%w[chat])+ allow(Migrations::Database::Schema).to receive(:plugin_manifest).and_return(manifest)++ schema =+ build_schema(+ tables: {+ chat_channels: proc { include_all },+ },+ ignored: proc { plugin :chat, "Not migrating" },+ )++ stub_database(+ connection,+ db_tables: %i[chat_channels chat_messages],+ table_columns: {+ chat_channels: {+ id: {+ type: :integer,+ },+ },+ },+ )++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/belongs to ignored plugin 'chat'/))+ end++ it "normalizes underscored ignored plugin names for plugin-owned source tables" do+ manifest = instance_double(Migrations::Database::Schema::DSL::PluginManifest)+ allow(manifest).to receive(:available?).and_return(true)+ allow(manifest).to receive(:tables_for_plugin).with("discourse-ai").and_return(%w[ai_tools])+ allow(manifest).to receive(:plugin_for_table).with("ai_tools").and_return("discourse-ai")+ allow(manifest).to receive(:columns_for_plugin).and_return([])+ allow(manifest).to receive(:all_plugin_names).and_return(%w[discourse-ai])+ allow(Migrations::Database::Schema).to receive(:plugin_manifest).and_return(manifest)++ schema =+ build_schema(+ tables: {+ ai_tools: proc { include_all },+ },+ ignored: proc { plugin :discourse_ai, "Not migrating" },+ )++ stub_database(+ connection,+ db_tables: %i[ai_tools],+ table_columns: {+ ai_tools: {+ id: {+ type: :integer,+ },+ },+ },+ )++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/belongs to ignored plugin 'discourse-ai'/))+ end++ it "detects column options on excluded columns" do+ schema =+ build_schema(+ tables: {+ users:+ proc do+ include :id+ column :email, :text+ end,+ },+ )++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: {+ id: {+ type: :integer,+ },+ email: {+ type: :text,+ },+ },+ },+ )++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/column option for 'email'.*excluded column/))+ end++ it "detects duplicate index names" do+ schema =+ build_schema(+ tables: {+ users:+ proc do+ include :id, :username, :email+ index :username, name: :idx_users+ index :email, name: :idx_users+ end,+ },+ )++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: {+ id: {+ type: :integer,+ },+ username: {+ type: :text,+ },+ email: {+ type: :text,+ },+ },+ },+ )++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/duplicate index name 'idx_users'/))+ end++ it "detects included columns that are globally ignored without include!" do+ schema =+ build_schema(+ tables: {+ users: proc { include :id, :updated_at },+ },+ conventions: proc { ignore_columns :updated_at },+ )++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: {+ id: {+ type: :integer,+ },+ updated_at: {+ type: :datetime,+ },+ },+ },+ )++ errors = described_class.new(schema).validate+ expect(errors).to include(match(/included column 'updated_at' is globally ignored.*include!/))+ end++ it "allows include! to override globally ignored columns" do+ schema =+ build_schema(+ tables: {+ users:+ proc do+ include :id, :updated_at+ include! :updated_at+ end,+ },+ conventions: proc { ignore_columns :updated_at },+ )++ stub_database(+ connection,+ db_tables: %i[users],+ table_columns: {+ users: {+ id: {+ type: :integer,+ },+ updated_at: {+ type: :datetime,+ },+ },+ },+ )++ errors = described_class.new(schema).validate+ expect(errors).not_to include(match(/globally ignored/))+ end+ end+enddiff --git a/migrations/spec/lib/database/schema/schema_spec.rb b/migrations/spec/lib/database/schema/schema_spec.rbnew file mode 100644index 00000000000..177d9bbd106--- /dev/null+++ b/migrations/spec/lib/database/schema/schema_spec.rb@@ -0,0 +1,196 @@+# frozen_string_literal: true++RSpec.describe Migrations::Database::Schema do+ after { described_class.reset! }++ describe ".validate" do+ it "includes resolved schema validation errors" do+ resolved =+ Migrations::Database::Schema::Definition.new(+ tables: [+ Migrations::Database::Schema::TableDefinition.new(+ name: "users",+ columns: [],+ indexes: [],+ primary_key_column_names: ["missing_id"],+ constraints: [],+ model_mode: nil,+ ),+ ],+ enums: [],+ )+ allow(described_class).to receive(:preflight).with(database: :test_db).and_return(+ described_class::PreflightResult.new(+ resolved:,+ errors: ["Table 'users': primary key references missing columns: missing_id"],+ ),+ )++ errors = described_class.validate(database: :test_db)++ expect(errors).to include(match(/primary key references missing columns: missing_id/))+ end++ it "returns static validation errors without attempting resolution" do+ allow(described_class).to receive(:preflight).with(database: :test_db).and_return(+ described_class::PreflightResult.new(resolved: nil, errors: ["Table 'users': bad config"]),+ )++ errors = described_class.validate(database: :test_db)++ expect(errors).to eq(["Table 'users': bad config"])+ end++ it "runs static validation before resolution in preflight" do+ validator =+ instance_double(+ Migrations::Database::Schema::DSL::Validator,+ validate: ["Table 'users': bad config"],+ )++ allow(described_class).to receive(:ensure_ready!).with(database: :test_db)+ allow(Migrations::Database::Schema::DSL::Validator).to receive(:new).with(+ described_class,+ ).and_return(validator)+ allow(Migrations::Database::Schema::DSL::SchemaResolver).to receive(:new)++ result = described_class.preflight(database: :test_db)++ expect(result.errors).to eq(["Table 'users': bad config"])+ expect(Migrations::Database::Schema::DSL::SchemaResolver).not_to have_received(:new)+ end+ end++ describe ".ignore_table" do+ let(:editor) do+ instance_double(Migrations::Database::Schema::DSL::IgnoredFileEditor, add_table: nil)+ end++ it "adds the table to ignored.rb after validating it" do+ connection =+ instance_double(ActiveRecord::ConnectionAdapters::AbstractAdapter, tables: %w[users])++ allow(described_class).to receive(:ensure_ready!).with(database: :test_db)+ allow(described_class).to receive(:find_table).with("users").and_return(nil)+ allow(described_class).to receive(:config_path).with(:test_db).and_return("/tmp/schema")+ allow(Migrations::Database::Schema::DSL::IgnoredFileEditor).to receive(:new).with(+ "/tmp/schema",+ ).and_return(editor)+ allow(ActiveRecord::Base).to receive(:with_connection).and_yield(connection)++ described_class.ignore_table("users", reason: "legacy", database: :test_db)++ expect(editor).to have_received(:add_table).with("users", reason: "legacy")+ end++ it "rejects ignoring a table that is already configured" do+ allow(described_class).to receive(:ensure_ready!).with(database: :test_db)+ allow(described_class).to receive(:find_table).with("users").and_return(double)++ expect { described_class.ignore_table("users", database: :test_db) }.to raise_error(+ described_class::ConfigError,+ /already configured/,+ )+ end++ it "rejects ignoring a table that does not exist in the database" do+ connection =+ instance_double(ActiveRecord::ConnectionAdapters::AbstractAdapter, tables: %w[posts])++ allow(described_class).to receive(:ensure_ready!).with(database: :test_db)+ allow(described_class).to receive(:find_table).with("users").and_return(nil)+ allow(ActiveRecord::Base).to receive(:with_connection).and_yield(connection)++ expect { described_class.ignore_table("users", database: :test_db) }.to raise_error(+ described_class::ConfigError,+ /does not exist in the database/,+ )+ end+ end++ describe ".ensure_ready!" do+ def write_minimal_config(tmpdir)+ config_dir = File.join(tmpdir, "config", "schema", "test_db")+ tables_dir = File.join(config_dir, "tables")+ FileUtils.mkdir_p(tables_dir)++ File.write(File.join(config_dir, "config.rb"), <<~RUBY)+ Migrations::Database::Schema.configure do+ output do+ schema_file "db/schema.sql"+ models_directory "lib/models"+ models_namespace "Migrations::Database::IntermediateDB"+ enums_directory "lib/enums"+ enums_namespace "Migrations::Database::IntermediateDB::Enums"+ end+ end+ RUBY++ File.write(File.join(tables_dir, "users.rb"), <<~RUBY)+ Migrations::Database::Schema.table :users do+ include_all+ end+ RUBY++ { config_dir:, tables_dir: }+ end++ it "resets partially loaded DSL state when loading fails" do+ Dir.mktmpdir do |tmpdir|+ paths = write_minimal_config(tmpdir)+ tables_dir = paths[:tables_dir]++ File.write(File.join(tables_dir, "broken.rb"), "raise 'boom'\n")++ manifest = instance_double(Migrations::Database::Schema::DSL::PluginManifest, fresh?: true)+ allow(described_class).to receive(:plugin_manifest).and_return(manifest)+ allow(Migrations).to receive(:root_path).and_return(tmpdir)++ expect { described_class.ensure_ready!(database: :test_db) }.to raise_error(+ described_class::ConfigError,+ /Error loading/,+ )++ File.write(File.join(tables_dir, "broken.rb"), <<~RUBY)+ Migrations::Database::Schema.table :posts do+ include_all+ end+ RUBY++ expect { described_class.ensure_ready!(database: :test_db) }.not_to raise_error+ expect(described_class.tables.keys).to contain_exactly("posts", "users")+ end+ end++ it "does not auto-regenerate when checksums are fresh but manifest is incomplete" do+ Dir.mktmpdir do |tmpdir|+ write_minimal_config(tmpdir)++ manifest = instance_double(Migrations::Database::Schema::DSL::PluginManifest, fresh?: true)+ allow(manifest).to receive(:regenerate!)+ allow(described_class).to receive(:plugin_manifest).and_return(manifest)+ allow(Migrations).to receive(:root_path).and_return(tmpdir)++ expect { described_class.ensure_ready!(database: :test_db) }.not_to raise_error+ expect(manifest).not_to have_received(:regenerate!)+ end+ end++ it "raises when stale manifest regeneration fails" do+ Dir.mktmpdir do |tmpdir|+ write_minimal_config(tmpdir)++ manifest = instance_double(Migrations::Database::Schema::DSL::PluginManifest, fresh?: false)+ allow(manifest).to receive(:regenerate!).and_raise(StandardError, "boom")+ allow(described_class).to receive(:plugin_manifest).and_return(manifest)+ allow(Migrations).to receive(:root_path).and_return(tmpdir)+ allow($stdout).to receive(:write)++ expect { described_class.ensure_ready!(database: :test_db) }.to raise_error(+ described_class::ConfigError,+ /Skipped/,+ )+ end+ end+ end+enddiff --git a/migrations/spec/support/fixtures/schema/copy/schema.sql b/migrations/spec/support/fixtures/schema/copy/schema.sqldeleted file mode 100644index e15177cc1e4..00000000000--- a/migrations/spec/support/fixtures/schema/copy/schema.sql+++ /dev/null@@ -1,17 +0,0 @@-CREATE TABLE users-(- id INTEGER NOT NULL PRIMARY KEY,- username TEXT NOT NULL UNIQUE-);--CREATE TABLE config-(- name TEXT NOT NULL PRIMARY KEY,- value TEXT NOT NULL-);--CREATE TABLE uploads-(- id INTEGER NOT NULL PRIMARY KEY,- url TEXT NOT NULL-);