...

Source file src/github.com/cybertec-postgresql/pgwatch/v5/internal/cmdopts/cmdconfig_integration_test.go

Documentation: github.com/cybertec-postgresql/pgwatch/v5/internal/cmdopts

     1  package cmdopts
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/cybertec-postgresql/pgwatch/v5/internal/metrics"
    10  	"github.com/cybertec-postgresql/pgwatch/v5/internal/sinks"
    11  	"github.com/cybertec-postgresql/pgwatch/v5/internal/sources"
    12  	"github.com/cybertec-postgresql/pgwatch/v5/internal/testutil"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  // TestConfigUpgrade_VerifyNoCircularDependency tests that config upgrade can be run even when the schema
    18  // needs migrations, proving there's no circular dependency
    19  func TestConfigUpgrade_VerifyNoCircularDependency(t *testing.T) {
    20  	if testing.Short() {
    21  		t.Skip("Skipping integration test")
    22  	}
    23  
    24  	// Create a PostgreSQL container with empty database (no schema)
    25  	pgContainer, tearDown, err := testutil.SetupPostgresContainerWithInitScripts()
    26  	require.NoError(t, err)
    27  	defer tearDown()
    28  
    29  	connStr, err := pgContainer.ConnectionString(testutil.TestContext)
    30  	require.NoError(t, err)
    31  
    32  	ctx := context.Background()
    33  
    34  	// Test with only metrics and sinks (sources reader doesn't implement Migrator)
    35  	opts := &Options{
    36  		Metrics:      metrics.CmdOpts{Metrics: connStr},
    37  		Sinks:        sinks.CmdOpts{Sinks: []string{connStr}, RetentionInterval: "30 days", BatchingDelay: time.Second, PartitionInterval: "1 week", MaintenanceInterval: "12 hours"},
    38  		OutputWriter: io.Discard,
    39  	}
    40  
    41  	// Config upgrade should work on metrics and sinks
    42  	cmd := ConfigUpgradeCommand{owner: opts}
    43  	err = cmd.Execute(nil)
    44  	assert.NoError(t, err)
    45  	assert.Equal(t, ExitCodeOK, opts.ExitCode)
    46  
    47  	// After successful upgrade, InitConfigReaders should succeed
    48  	opts2 := &Options{
    49  		Metrics: metrics.CmdOpts{Metrics: connStr},
    50  		Sources: sources.CmdOpts{Sources: connStr, Refresh: 120, MaxParallelConnectionsPerDb: 1},
    51  		Sinks:   sinks.CmdOpts{Sinks: []string{connStr}},
    52  	}
    53  	err = opts2.InitConfigReaders(ctx)
    54  	assert.NoError(t, err)
    55  	assert.NotNil(t, opts2.MetricsReaderWriter)
    56  	assert.NotNil(t, opts2.SourcesReaderWriter)
    57  }
    58