MCPcopy Index your code
hub / github.com/bytebase/bytebase / TestSyncSchemaWithTempSchema

Function TestSyncSchemaWithTempSchema

backend/tests/sync_schema_test.go:127–228  ·  view source on GitHub ↗

TestSyncSchemaWithTempSchema tests that schema sync works correctly even when pg_temp schemas exist This reproduces the issue from SUP-3 where users get permission errors on pg_temp schemas

(t *testing.T)

Source from the content-addressed store, hash-verified

125// TestSyncSchemaWithTempSchema tests that schema sync works correctly even when pg_temp schemas exist
126// This reproduces the issue from SUP-3 where users get permission errors on pg_temp schemas
127func TestSyncSchemaWithTempSchema(t *testing.T) {
128 databaseName := "sync_schema_temp"
129
130 t.Parallel()
131 a := require.New(t)
132 ctx := context.Background()
133 ctl := &controller{}
134 ctx, err := ctl.StartServerWithExternalPg(ctx)
135 a.NoError(err)
136 defer ctl.Close(ctx)
137
138 pgContainer, err := getPgContainer(ctx)
139 defer func() {
140 pgContainer.Close(ctx)
141 }()
142 a.NoError(err)
143
144 pgDB := pgContainer.db
145 err = pgDB.Ping()
146 a.NoError(err)
147
148 // Create database and user
149 _, err = pgDB.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %v", databaseName))
150 a.NoError(err)
151 _, err = pgDB.Exec(fmt.Sprintf("CREATE DATABASE %v", databaseName))
152 a.NoError(err)
153
154 // Create a limited user (not superuser) to simulate real-world scenario
155 _, err = pgDB.Exec("DROP USER IF EXISTS testuser")
156 a.NoError(err)
157 _, err = pgDB.Exec("CREATE USER testuser WITH ENCRYPTED PASSWORD 'testpass'")
158 a.NoError(err)
159 _, err = pgDB.Exec(fmt.Sprintf("GRANT ALL PRIVILEGES ON DATABASE %v TO testuser", databaseName))
160 a.NoError(err)
161
162 // Connect to the test database to create temp tables and schemas
163 testDB, err := sql.Open("pgx", fmt.Sprintf("host=%s port=%s user=postgres password=root-password dbname=%s sslmode=disable", pgContainer.host, pgContainer.port, databaseName))
164 a.NoError(err)
165 defer testDB.Close()
166
167 // Create some regular schemas and tables
168 _, err = testDB.Exec(`
169 CREATE SCHEMA app;
170 CREATE TABLE app.users (id INT PRIMARY KEY, name TEXT);
171 GRANT USAGE ON SCHEMA app TO testuser;
172 GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA app TO testuser;
173 `)
174 a.NoError(err)
175
176 // Create temporary tables which will create pg_temp schemas
177 // These temp tables will be in pg_temp_N schemas that testuser doesn't have permission to access
178 _, err = testDB.Exec(`
179 CREATE TEMP TABLE temp_table1 (id INT);
180 CREATE TEMP TABLE temp_table2 (data TEXT);
181 `)
182 a.NoError(err)
183
184 // Verify that pg_temp schemas exist

Callers

nothing calls this directly

Calls 11

CloseMethod · 0.95
getPgContainerFunction · 0.85
generateRandomStringFunction · 0.85
ScanMethod · 0.80
CloseMethod · 0.65
PingMethod · 0.65
OpenMethod · 0.65
CreateInstanceMethod · 0.65
SyncDatabaseMethod · 0.65
GetDatabaseSchemaMethod · 0.65

Tested by

no test coverage detected