|
| 1 | +// Copyright 2025 Dolthub, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package _go |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/dolthub/go-mysql-server/sql" |
| 23 | + "github.com/jackc/pgx/v5" |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | + |
| 27 | + dserver "github.com/dolthub/doltgresql/server" |
| 28 | + "github.com/dolthub/doltgresql/servercfg" |
| 29 | + "github.com/dolthub/doltgresql/servercfg/cfgdetails" |
| 30 | +) |
| 31 | + |
| 32 | +// TestCreateSchemaWithNonExistentDatabase tests that connecting to a non-existent |
| 33 | +// database fails with the appropriate error at the connection level. |
| 34 | +// This is the PostgreSQL-compliant behavior where database validation happens |
| 35 | +// during connection establishment. |
| 36 | +// |
| 37 | +// NOTE: This test cannot use RunScripts because RunScripts auto-creates the |
| 38 | +// database before running assertions. We need to test the connection-level |
| 39 | +// failure when connecting to a non-existent database. |
| 40 | +// |
| 41 | +// See: https://github.com/dolthub/doltgresql/issues/1863 |
| 42 | +func TestCreateSchemaWithNonExistentDatabase(t *testing.T) { |
| 43 | + port, err := sql.GetEmptyPort() |
| 44 | + require.NoError(t, err) |
| 45 | + |
| 46 | + // Start server using the same pattern as CreateServerWithPort |
| 47 | + controller, err := dserver.RunInMemory( |
| 48 | + &servercfg.DoltgresConfig{ |
| 49 | + DoltgresConfig: cfgdetails.DoltgresConfig{ |
| 50 | + ListenerConfig: &cfgdetails.DoltgresListenerConfig{ |
| 51 | + PortNumber: &port, |
| 52 | + HostStr: &serverHost, |
| 53 | + }, |
| 54 | + LogLevelStr: &testServerLogLevel, |
| 55 | + }, |
| 56 | + }, dserver.NewListener, |
| 57 | + ) |
| 58 | + require.NoError(t, err) |
| 59 | + defer func() { |
| 60 | + controller.Stop() |
| 61 | + require.NoError(t, controller.WaitForStop()) |
| 62 | + }() |
| 63 | + |
| 64 | + ctx := context.Background() |
| 65 | + |
| 66 | + t.Run( |
| 67 | + "connection to non-existent database fails", func(t *testing.T) { |
| 68 | + // Attempt to connect to a database that doesn't exist |
| 69 | + connStr := fmt.Sprintf( |
| 70 | + "postgres://postgres:password@%s:%d/nonexistent_db?sslmode=disable", |
| 71 | + serverHost, |
| 72 | + port, |
| 73 | + ) |
| 74 | + _, err := pgx.Connect(ctx, connStr) |
| 75 | + |
| 76 | + require.Error(t, err, "connection should fail when database doesn't exist") |
| 77 | + assert.Contains( |
| 78 | + t, err.Error(), "does not exist", |
| 79 | + "expected 'does not exist' error, got: %v", err, |
| 80 | + ) |
| 81 | + }, |
| 82 | + ) |
| 83 | + |
| 84 | + t.Run( |
| 85 | + "connection to existing database succeeds", func(t *testing.T) { |
| 86 | + // Verify that connecting to the default "postgres" database works |
| 87 | + connStr := fmt.Sprintf("postgres://postgres:password@%s:%d/postgres?sslmode=disable", serverHost, port) |
| 88 | + conn, err := pgx.Connect(ctx, connStr) |
| 89 | + require.NoError(t, err) |
| 90 | + defer conn.Close(ctx) |
| 91 | + |
| 92 | + // Verify we can create a schema on the valid database |
| 93 | + _, err = conn.Exec(ctx, "CREATE SCHEMA test_schema_1863") |
| 94 | + require.NoError(t, err) |
| 95 | + |
| 96 | + // Verify the schema was created |
| 97 | + rows, err := conn.Query( |
| 98 | + ctx, |
| 99 | + "SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'test_schema_1863'", |
| 100 | + ) |
| 101 | + require.NoError(t, err) |
| 102 | + defer rows.Close() |
| 103 | + |
| 104 | + require.True(t, rows.Next(), "expected to find test_schema_1863") |
| 105 | + }, |
| 106 | + ) |
| 107 | +} |
0 commit comments