MCPcopy Index your code
hub / github.com/riverqueue/river / testTxSchemaForDatabaseAndMigrationLines

Function testTxSchemaForDatabaseAndMigrationLines

riverdbtest/riverdbtest.go:598–668  ·  view source on GitHub ↗
(ctx context.Context, tb testing.TB, driver riverdriver.Driver[TTx], opts *TestTxOpts)

Source from the content-addressed store, hash-verified

596)
597
598func testTxSchemaForDatabaseAndMigrationLines[TTx any](ctx context.Context, tb testing.TB, driver riverdriver.Driver[TTx], opts *TestTxOpts) string {
599 tb.Helper()
600
601 if opts == nil {
602 opts = &TestTxOpts{}
603 }
604
605 lines := driver.GetMigrationDefaultLines()
606 if opts.lines != nil {
607 lines = opts.lines
608 }
609
610 var databaseAndLinesKey string
611 var schema string
612
613 if !opts.DisableSchemaSharing {
614 // Transaction schemas must be managed by database and which migration lines
615 // were run within them, which is determined by the included driver. i.e. A
616 // schema with no migrations obviously cannot be reused for a test expecting
617 // the `main` migration line. An SQLite schema can't be reused for Postgres.
618 //
619 // linesKey acts as key specific to this migrations set for testTxSchemas.
620 slices.Sort(lines)
621 databaseAndLinesKey = strings.Join(append([]string{driver.DatabaseName()}, lines...), ",")
622
623 testTxSchemasMu.RLock()
624 schema := testTxSchemas[databaseAndLinesKey]
625 testTxSchemasMu.RUnlock()
626
627 if schema != "" {
628 return schema
629 }
630
631 testTxSchemasMu.Lock()
632 defer testTxSchemasMu.Unlock()
633
634 // Check for a schema once more in case there was a race to acquire the
635 // mutex lock and another TestTx invocation did it first.
636 if schema = testTxSchemas[databaseAndLinesKey]; schema != "" {
637 return schema
638 }
639 }
640
641 // If called from a transaction helper like `TestTxPgx`, skip one more frame
642 // for purposes of schema naming.
643 skipExtraFrames := 2
644 if opts.IsTestTxHelper {
645 skipExtraFrames += 2
646 }
647
648 schema = TestSchema(ctx, tb, driver, &TestSchemaOpts{
649 // If test transactions are being shared (opts.DisableSharing = false)
650 // then reserve the shared schemas exclusively for TestTx. Otherwise,
651 // allow them to be put back in the pool for use by other test
652 // transactions with opts.DisableSharing = true or other TestSchema
653 // invocations.
654 DisableReuse: !opts.DisableSchemaSharing,
655

Callers 1

TestTxFunction · 0.85

Calls 4

TestSchemaFunction · 0.85
HelperMethod · 0.65
DatabaseNameMethod · 0.65

Tested by 1

TestTxFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…