-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathdump_integration_test.go
More file actions
619 lines (525 loc) · 18.6 KB
/
dump_integration_test.go
File metadata and controls
619 lines (525 loc) · 18.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
package dump
// Dump Integration Tests
// These comprehensive integration tests verify the entire dump workflow by comparing
// schema representations from two different sources:
// 1. Database inspection (pgdump.sql → database → dump command → schema output)
// 2. Expected output verification (comparing actual vs expected schema dumps)
// This ensures our pgschema output accurately represents the original database schema
import (
"context"
"fmt"
"os"
"strings"
"testing"
"github.com/pgplex/pgschema/ir"
"github.com/pgplex/pgschema/testutil"
)
func TestDumpCommand_Employee(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
runExactMatchTest(t, "employee")
}
func TestDumpCommand_Sakila(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
runExactMatchTest(t, "sakila")
}
func TestDumpCommand_Bytebase(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
runExactMatchTest(t, "bytebase")
}
func TestDumpCommand_TenantSchemas(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
runTenantSchemaTest(t, "tenant")
}
func TestDumpCommand_Issue78ConstraintNotValidAndQuoting(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
runExactMatchTest(t, "issue_78_constraint_not_valid_and_quoting")
}
func TestDumpCommand_Issue80IndexNameQuote(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
runExactMatchTest(t, "issue_80_index_name_quote")
}
func TestDumpCommand_Issue82ViewLogicExpr(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
runExactMatchTest(t, "issue_82_view_logic_expr")
}
func TestDumpCommand_Issue83ExplicitConstraintName(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
runExactMatchTest(t, "issue_83_explicit_constraint_name")
}
func TestDumpCommand_Issue125FunctionDefault(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
runExactMatchTest(t, "issue_125_function_default")
}
func TestDumpCommand_Issue133IndexSort(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
runExactMatchTest(t, "issue_133_index_sort")
}
func TestDumpCommand_Issue183GeneratedColumn(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
runExactMatchTest(t, "issue_183_generated_column")
}
func TestDumpCommand_Issue275TruncatedFunctionGrants(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
runExactMatchTest(t, "issue_275_truncated_function_grants")
}
func TestDumpCommand_Issue252FunctionSchemaQualifier(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
runExactMatchTest(t, "issue_252_function_schema_qualifier")
}
func TestDumpCommand_Issue307ViewDependencyOrder(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
runExactMatchTest(t, "issue_307_view_dependency_order")
}
func TestDumpCommand_Issue320PlpgsqlReservedKeywordType(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
runExactMatchTest(t, "issue_320_plpgsql_reserved_keyword_type")
}
func TestDumpCommand_Issue345ArrayCast(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
runExactMatchTest(t, "issue_345_array_cast")
}
func TestDumpCommand_Issue396CheckConstraintIsNotNull(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
runExactMatchTest(t, "issue_396_check_constraint_is_not_null")
}
func TestDumpCommand_Issue412UniqueNullsNotDistinct(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
runExactMatchTest(t, "issue_412_unique_nulls_not_distinct")
}
func TestDumpCommand_Issue191FunctionProcedureOverload(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
runExactMatchTest(t, "issue_191_function_procedure_overload")
}
func TestDumpCommand_Issue420VarcharArrayLengthModifier(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
runExactMatchTest(t, "issue_420_varchar_array_length_modifier")
}
// Reproduces a bug where a column declared as `name` is dumped as `char[]`.
// The inspector classifies any base type with pg_type.typelem <> 0 as an array,
// but the `name` type has typelem = 18 (the OID of "char") despite not being an array.
func TestDumpCommand_NameTypeNotDumpedAsCharArray(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
embeddedPG := testutil.SetupPostgres(t)
defer embeddedPG.Stop()
conn, host, port, dbname, user, password := testutil.ConnectToPostgres(t, embeddedPG)
defer conn.Close()
_, err := conn.ExecContext(context.Background(), `CREATE TABLE pgschema_name_repro (n name);`)
if err != nil {
t.Fatalf("Failed to create table: %v", err)
}
output, err := ExecuteDump(&DumpConfig{
Host: host,
Port: port,
DB: dbname,
User: user,
Password: password,
Schema: "public",
})
if err != nil {
t.Fatalf("Dump command failed: %v", err)
}
if strings.Contains(output, "char[]") {
t.Errorf("Dump output should not contain char[] for a name column.\nOutput:\n%s", output)
}
if !strings.Contains(output, "n name") {
t.Errorf("Dump output should contain `n name` column declaration.\nOutput:\n%s", output)
}
}
func TestDumpCommand_Issue318CrossSchemaComment(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
// Setup PostgreSQL
embeddedPG := testutil.SetupPostgres(t)
defer embeddedPG.Stop()
// Connect to database
conn, host, port, dbname, user, password := testutil.ConnectToPostgres(t, embeddedPG)
defer conn.Close()
// Read and execute the setup SQL that creates two schemas with same-named tables
setupPath := "../../testdata/dump/issue_318_cross_schema_comment/setup.sql"
setupContent, err := os.ReadFile(setupPath)
if err != nil {
t.Fatalf("Failed to read %s: %v", setupPath, err)
}
_, err = conn.ExecContext(context.Background(), string(setupContent))
if err != nil {
t.Fatalf("Failed to execute setup.sql: %v", err)
}
// Dump each schema and verify comments are correctly attributed
tests := []struct {
schema string
tableComment string
colComment string
}{
{"alpha", "Alpha account table", "Alpha account name"},
{"beta", "Beta account table", "Beta account name"},
}
for _, tc := range tests {
t.Run(tc.schema, func(t *testing.T) {
config := &DumpConfig{
Host: host,
Port: port,
DB: dbname,
User: user,
Password: password,
Schema: tc.schema,
MultiFile: false,
File: "",
}
output, err := ExecuteDump(config)
if err != nil {
t.Fatalf("Dump command failed for schema %s: %v", tc.schema, err)
}
// Verify table comment
expectedTableComment := fmt.Sprintf("COMMENT ON TABLE account IS '%s';", tc.tableComment)
if !strings.Contains(output, expectedTableComment) {
t.Errorf("Schema %s: expected table comment %q not found in output:\n%s", tc.schema, expectedTableComment, output)
}
// Verify column comment
expectedColComment := fmt.Sprintf("COMMENT ON COLUMN account.name IS '%s';", tc.colComment)
if !strings.Contains(output, expectedColComment) {
t.Errorf("Schema %s: expected column comment %q not found in output:\n%s", tc.schema, expectedColComment, output)
}
})
}
}
func TestDumpCommand_Issue307MultiFileViewDependencyOrder(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
// Setup PostgreSQL
embeddedPG := testutil.SetupPostgres(t)
defer embeddedPG.Stop()
// Connect to database
conn, host, port, dbname, user, password := testutil.ConnectToPostgres(t, embeddedPG)
defer conn.Close()
// Read and execute the pgdump.sql file
pgdumpPath := "../../testdata/dump/issue_307_view_dependency_order/pgdump.sql"
pgdumpContent, err := os.ReadFile(pgdumpPath)
if err != nil {
t.Fatalf("Failed to read %s: %v", pgdumpPath, err)
}
// Execute the SQL to create the schema
_, err = conn.ExecContext(context.Background(), string(pgdumpContent))
if err != nil {
t.Fatalf("Failed to execute pgdump.sql: %v", err)
}
// Create temp directory for multi-file output
tmpDir := t.TempDir()
outputPath := tmpDir + "/schema.sql"
// Create dump configuration for multi-file mode
config := &DumpConfig{
Host: host,
Port: port,
DB: dbname,
User: user,
Password: password,
Schema: "public",
MultiFile: true,
File: outputPath,
}
// Execute pgschema dump in multi-file mode
_, err = ExecuteDump(config)
if err != nil {
t.Fatalf("Dump command failed: %v", err)
}
// Read the main schema file to check include order
mainContent, err := os.ReadFile(outputPath)
if err != nil {
t.Fatalf("Failed to read main file: %v", err)
}
// Parse include directives to check view ordering
lines := strings.Split(string(mainContent), "\n")
var viewIncludes []string
for _, line := range lines {
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(trimmed, `\i views/`) {
viewIncludes = append(viewIncludes, trimmed)
}
}
// Verify we have both view includes
if len(viewIncludes) != 2 {
t.Fatalf("Expected 2 view includes, got %d: %v", len(viewIncludes), viewIncludes)
}
// item_summary must come before dashboard because dashboard depends on item_summary
// (even though "dashboard" sorts before "item_summary" alphabetically)
if viewIncludes[0] != `\i views/item_summary.sql` {
t.Errorf("Expected first view include to be item_summary (dependency), got: %s", viewIncludes[0])
}
if viewIncludes[1] != `\i views/dashboard.sql` {
t.Errorf("Expected second view include to be dashboard (depends on item_summary), got: %s", viewIncludes[1])
}
}
func TestDumpCommand_Issue323SupabaseDefaultPrivilegeFilter(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
// Verifies that dump filters out default privileges for roles the current
// user is not a member of. Simulates Supabase where 'postgres' is not a
// superuser and has no membership in 'supabase_admin'.
embeddedPG := testutil.SetupPostgres(t)
defer embeddedPG.Stop()
conn, host, port, dbname, _, _ := testutil.ConnectToPostgres(t, embeddedPG)
defer conn.Close()
ctx := context.Background()
majorVersion, err := testutil.GetMajorVersion(conn)
if err != nil {
t.Fatalf("Failed to detect PostgreSQL version: %v", err)
}
testutil.ShouldSkipTest(t, t.Name(), majorVersion)
// Create system_admin (simulating supabase_admin) and limited_user
// (simulating the connecting postgres user). limited_user is NOT a member
// of system_admin.
_, err = conn.ExecContext(ctx, fmt.Sprintf(`
CREATE ROLE system_admin;
CREATE ROLE app_user;
CREATE ROLE limited_user LOGIN PASSWORD 'limitedpass';
GRANT CONNECT ON DATABASE %s TO limited_user;
SET ROLE system_admin;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO app_user;
RESET ROLE;
`, dbname))
if err != nil {
t.Fatalf("Failed to set up roles and privileges: %v", err)
}
// Dump as limited_user (non-superuser, not a member of system_admin).
output, err := ExecuteDump(&DumpConfig{
Host: host,
Port: port,
DB: dbname,
User: "limited_user",
Password: "limitedpass",
Schema: "public",
})
if err != nil {
t.Fatalf("Dump command failed: %v", err)
}
if strings.Contains(output, "system_admin") {
t.Errorf("Dump as limited_user should not include system_admin's default privileges\nActual output:\n%s", output)
}
}
func runExactMatchTest(t *testing.T, testDataDir string) {
runExactMatchTestWithContext(t, context.Background(), testDataDir)
}
func runExactMatchTestWithContext(t *testing.T, ctx context.Context, testDataDir string) {
// Setup PostgreSQL
embeddedPG := testutil.SetupPostgres(t)
defer embeddedPG.Stop()
// Connect to database
conn, host, port, dbname, user, password := testutil.ConnectToPostgres(t, embeddedPG)
defer conn.Close()
// Detect PostgreSQL version and skip tests if needed
majorVersion, err := testutil.GetMajorVersion(conn)
if err != nil {
t.Fatalf("Failed to detect PostgreSQL version: %v", err)
}
// Check if this test should be skipped for this PostgreSQL version
// If skipped, ShouldSkipTest will call t.Skipf() and stop execution
testutil.ShouldSkipTest(t, t.Name(), majorVersion)
// Read and execute the pgdump.sql file
pgdumpPath := fmt.Sprintf("../../testdata/dump/%s/pgdump.sql", testDataDir)
pgdumpContent, err := os.ReadFile(pgdumpPath)
if err != nil {
t.Fatalf("Failed to read %s: %v", pgdumpPath, err)
}
// Execute the SQL to create the schema
_, err = conn.ExecContext(ctx, string(pgdumpContent))
if err != nil {
t.Fatalf("Failed to execute pgdump.sql: %v", err)
}
// Create dump configuration
config := &DumpConfig{
Host: host,
Port: port,
DB: dbname,
User: user,
Password: password,
Schema: "public",
MultiFile: false,
File: "",
}
// Execute pgschema dump
actualOutput, err := ExecuteDump(config)
if err != nil {
t.Fatalf("Dump command failed: %v", err)
}
// Read expected output
expectedPath := fmt.Sprintf("../../testdata/dump/%s/pgschema.sql", testDataDir)
expectedContent, err := os.ReadFile(expectedPath)
if err != nil {
t.Fatalf("Failed to read %s: %v", expectedPath, err)
}
expectedOutput := string(expectedContent)
// Use shared comparison function
compareSchemaOutputs(t, actualOutput, expectedOutput, testDataDir)
}
func runTenantSchemaTest(t *testing.T, testDataDir string) {
// Setup PostgreSQL
embeddedPG := testutil.SetupPostgres(t)
defer embeddedPG.Stop()
// Connect to database
conn, host, port, dbname, user, password := testutil.ConnectToPostgres(t, embeddedPG)
defer conn.Close()
// Read the tenant SQL that will be loaded into all schemas
tenantSQL, err := os.ReadFile(fmt.Sprintf("../../testdata/dump/%s/tenant.sql", testDataDir))
if err != nil {
t.Fatalf("Failed to read tenant.sql: %v", err)
}
// Load utility functions (if util.sql exists)
utilPath := fmt.Sprintf("../../testdata/dump/%s/util.sql", testDataDir)
if utilSQL, err := os.ReadFile(utilPath); err == nil {
_, err = conn.Exec(string(utilSQL))
if err != nil {
t.Fatalf("Failed to load utility functions from util.sql: %v", err)
}
} else if !os.IsNotExist(err) {
t.Fatalf("Failed to read util.sql: %v", err)
}
// Create two tenant schemas (public already exists)
schemas := []string{"public", "tenant1", "tenant2"}
for _, schema := range schemas[1:] { // Skip public as it already exists
_, err = conn.Exec(fmt.Sprintf("CREATE SCHEMA %s", schema))
if err != nil {
t.Fatalf("Failed to create schema %s: %v", schema, err)
}
}
// Load the tenant SQL into all three schemas
for _, schema := range schemas {
// Set search path to target schema only
quotedSchema := ir.QuoteIdentifier(schema)
_, err = conn.Exec(fmt.Sprintf("SET search_path TO %s", quotedSchema))
if err != nil {
t.Fatalf("Failed to set search path to %s: %v", schema, err)
}
// Execute the SQL - objects will be created in the target schema
_, err = conn.Exec(string(tenantSQL))
if err != nil {
t.Fatalf("Failed to load SQL into schema %s: %v", schema, err)
}
}
// Dump all three schemas using pgschema dump command
var dumps []string
for _, schemaName := range schemas {
// Create dump configuration for this schema
config := &DumpConfig{
Host: host,
Port: port,
DB: dbname,
User: user,
Password: password,
Schema: schemaName,
MultiFile: false,
File: "",
}
// Execute pgschema dump
actualOutput, err := ExecuteDump(config)
if err != nil {
t.Fatalf("Dump command failed for schema %s: %v", schemaName, err)
}
dumps = append(dumps, actualOutput)
}
// Read expected output
expectedBytes, err := os.ReadFile(fmt.Sprintf("../../testdata/dump/%s/pgschema.sql", testDataDir))
if err != nil {
t.Fatalf("Failed to read expected output: %v", err)
}
expected := string(expectedBytes)
// Compare all dumps against expected output
for i, dump := range dumps {
schemaName := schemas[i]
// Use shared comparison function
compareSchemaOutputs(t, dump, expected, fmt.Sprintf("%s_%s", testDataDir, schemaName))
}
// Also compare all dumps with each other - they should be identical
for i := 1; i < len(dumps); i++ {
compareSchemaOutputs(t, dumps[0], dumps[i], fmt.Sprintf("%s_%s_vs_%s", testDataDir, schemas[0], schemas[i]))
}
}
// normalizeSchemaOutput removes version-specific lines for comparison.
// This allows comparing dumps across different PostgreSQL versions.
func normalizeSchemaOutput(output string) string {
lines := strings.Split(output, "\n")
var normalizedLines []string
for _, line := range lines {
// Skip version-related lines
if strings.Contains(line, "-- Dumped by pgschema version") ||
strings.Contains(line, "-- Dumped from database version") {
continue
}
normalizedLines = append(normalizedLines, line)
}
return strings.Join(normalizedLines, "\n")
}
// compareSchemaOutputs compares actual and expected schema outputs
func compareSchemaOutputs(t *testing.T, actualOutput, expectedOutput string, testName string) {
// Normalize both outputs to ignore version differences
normalizedActual := normalizeSchemaOutput(actualOutput)
normalizedExpected := normalizeSchemaOutput(expectedOutput)
// Compare the normalized outputs
if normalizedActual != normalizedExpected {
t.Errorf("Output does not match for %s", testName)
t.Logf("Total lines - Actual: %d, Expected: %d", len(strings.Split(actualOutput, "\n")), len(strings.Split(expectedOutput, "\n")))
// Write actual output to file for debugging only when test fails
actualFilename := fmt.Sprintf("%s_actual.sql", testName)
os.WriteFile(actualFilename, []byte(actualOutput), 0644)
expectedFilename := fmt.Sprintf("%s_expected.sql", testName)
os.WriteFile(expectedFilename, []byte(expectedOutput), 0644)
t.Logf("Outputs written to %s and %s for debugging", actualFilename, expectedFilename)
// Find and show first difference
actualLines := strings.Split(normalizedActual, "\n")
expectedLines := strings.Split(normalizedExpected, "\n")
for i := 0; i < len(actualLines) && i < len(expectedLines); i++ {
if actualLines[i] != expectedLines[i] {
t.Errorf("First difference at line %d:\nActual: %s\nExpected: %s",
i+1, actualLines[i], expectedLines[i])
break
}
}
if len(actualLines) != len(expectedLines) {
t.Errorf("Different number of lines - Actual: %d, Expected: %d",
len(actualLines), len(expectedLines))
}
}
}