0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-07 13:19:19 +08:00
discourse/config/initializers/000-active_record.rb
Alan Guo Xiang Tan af075e5fb8
FIX: Load PostgreSQL structures in a single transaction (#42012)
When Active Record migrates a brand-new Discourse database, Discourse
  invokes `DatabaseTasks.migrate`, which can initialize the database from
  `db/structure.sql`. That file clears the session `search_path` and
  restores it near the end. With PgBouncer transaction pooling, `psql`'s
  autocommit statements can run on different PostgreSQL backends, leaving
  one pooled backend with an empty search path and causing later
  migrations or seeds to fail intermittently with "no schema has been
  selected" or missing-relation errors.

  This commit configures PostgreSQL structure loads with
  `--single-transaction`, using Active Record's existing structure-loader
  flags to keep the restore on one PgBouncer backend and roll it back
  atomically on error.
2026-07-27 09:51:34 +08:00

17 lines
674 B
Ruby
Vendored

# frozen_string_literal: true
# Concurrent transactional structure loads exhaust PostgreSQL's shared lock-tracking memory.
if !Discourse.is_parallel_test?
ActiveRecord::Tasks::DatabaseTasks.structure_load_flags = { postgresql: "--single-transaction" }
end
# No ActiveRecord for these models. Avoids the "unknown OID" warnings.
# We need it in core, because plugins are not loaded in core tests,
# but the tables are likely still present in the test database.
ActiveRecord.schema_cache_ignored_tables.push(
"ai_topics_embeddings",
"ai_posts_embeddings",
"ai_user_embeddings",
"ai_document_fragments_embeddings",
/\Adiscourse_workflows_data_table_\d+_rows\z/,
)