feat(writer): 🎉 Enhance SQL execution logging and add statement type detection
All checks were successful
CI / Test (1.24) (push) Successful in -26m21s
CI / Test (1.25) (push) Successful in -26m15s
CI / Build (push) Successful in -26m39s
CI / Lint (push) Successful in -26m29s
Release / Build and Release (push) Successful in -26m28s
Integration Tests / Integration Tests (push) Successful in -26m11s
All checks were successful
CI / Test (1.24) (push) Successful in -26m21s
CI / Test (1.25) (push) Successful in -26m15s
CI / Build (push) Successful in -26m39s
CI / Lint (push) Successful in -26m29s
Release / Build and Release (push) Successful in -26m28s
Integration Tests / Integration Tests (push) Successful in -26m11s
* Log statement type during execution for better debugging * Introduce detectStatementType function to categorize SQL statements * Update unique constraint naming convention in tests
This commit is contained in:
@@ -603,8 +603,10 @@ func (r *Reader) parseColumn(line, tableName, schemaName string) (*models.Column
|
||||
column.Default = strings.Trim(defaultVal, "'\"")
|
||||
} else if attr == "unique" {
|
||||
// Create a unique constraint
|
||||
// Clean table name by removing leading underscores to avoid double underscores
|
||||
cleanTableName := strings.TrimLeft(tableName, "_")
|
||||
uniqueConstraint := models.InitConstraint(
|
||||
fmt.Sprintf("uq_%s_%s", tableName, columnName),
|
||||
fmt.Sprintf("ukey_%s_%s", cleanTableName, columnName),
|
||||
models.UniqueConstraint,
|
||||
)
|
||||
uniqueConstraint.Schema = schemaName
|
||||
|
||||
@@ -809,14 +809,14 @@ func TestConstraintNaming(t *testing.T) {
|
||||
t.Fatal("Posts table not found")
|
||||
}
|
||||
|
||||
// Test unique constraint naming: uq_table_column
|
||||
if _, exists := usersTable.Constraints["uq_users_email"]; !exists {
|
||||
t.Error("Expected unique constraint 'uq_users_email' not found")
|
||||
// Test unique constraint naming: ukey_table_column
|
||||
if _, exists := usersTable.Constraints["ukey_users_email"]; !exists {
|
||||
t.Error("Expected unique constraint 'ukey_users_email' not found")
|
||||
t.Logf("Available constraints: %v", getKeys(usersTable.Constraints))
|
||||
}
|
||||
|
||||
if _, exists := postsTable.Constraints["uq_posts_slug"]; !exists {
|
||||
t.Error("Expected unique constraint 'uq_posts_slug' not found")
|
||||
if _, exists := postsTable.Constraints["ukey_posts_slug"]; !exists {
|
||||
t.Error("Expected unique constraint 'ukey_posts_slug' not found")
|
||||
t.Logf("Available constraints: %v", getKeys(postsTable.Constraints))
|
||||
}
|
||||
|
||||
|
||||
@@ -1410,7 +1410,8 @@ func (w *Writer) executeDatabaseSQL(db *models.Database, connString string) erro
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, "Executing statement %d/%d...\n", i+1, len(statements))
|
||||
stmtType := detectStatementType(stmtTrimmed)
|
||||
fmt.Fprintf(os.Stderr, "Executing statement %d/%d [%s]...\n", i+1, len(statements), stmtType)
|
||||
|
||||
_, execErr := conn.Exec(ctx, stmt)
|
||||
if execErr != nil {
|
||||
@@ -1545,6 +1546,91 @@ func getCurrentTimestamp() string {
|
||||
return time.Now().Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
// detectStatementType detects the type of SQL statement for logging
|
||||
func detectStatementType(stmt string) string {
|
||||
upperStmt := strings.ToUpper(stmt)
|
||||
|
||||
// Check for DO blocks (used for conditional DDL)
|
||||
if strings.HasPrefix(upperStmt, "DO $$") || strings.HasPrefix(upperStmt, "DO $") {
|
||||
// Look inside the DO block for the actual operation
|
||||
if strings.Contains(upperStmt, "ALTER TABLE") && strings.Contains(upperStmt, "ADD CONSTRAINT") {
|
||||
if strings.Contains(upperStmt, "UNIQUE") {
|
||||
return "ADD UNIQUE CONSTRAINT"
|
||||
} else if strings.Contains(upperStmt, "FOREIGN KEY") {
|
||||
return "ADD FOREIGN KEY"
|
||||
} else if strings.Contains(upperStmt, "PRIMARY KEY") {
|
||||
return "ADD PRIMARY KEY"
|
||||
} else if strings.Contains(upperStmt, "CHECK") {
|
||||
return "ADD CHECK CONSTRAINT"
|
||||
}
|
||||
return "ADD CONSTRAINT"
|
||||
}
|
||||
if strings.Contains(upperStmt, "ALTER TABLE") && strings.Contains(upperStmt, "ADD COLUMN") {
|
||||
return "ADD COLUMN"
|
||||
}
|
||||
if strings.Contains(upperStmt, "DROP CONSTRAINT") {
|
||||
return "DROP CONSTRAINT"
|
||||
}
|
||||
return "DO BLOCK"
|
||||
}
|
||||
|
||||
// Direct DDL statements
|
||||
if strings.HasPrefix(upperStmt, "CREATE SCHEMA") {
|
||||
return "CREATE SCHEMA"
|
||||
}
|
||||
if strings.HasPrefix(upperStmt, "CREATE SEQUENCE") {
|
||||
return "CREATE SEQUENCE"
|
||||
}
|
||||
if strings.HasPrefix(upperStmt, "CREATE TABLE") {
|
||||
return "CREATE TABLE"
|
||||
}
|
||||
if strings.HasPrefix(upperStmt, "CREATE INDEX") {
|
||||
return "CREATE INDEX"
|
||||
}
|
||||
if strings.HasPrefix(upperStmt, "CREATE UNIQUE INDEX") {
|
||||
return "CREATE UNIQUE INDEX"
|
||||
}
|
||||
if strings.HasPrefix(upperStmt, "ALTER TABLE") {
|
||||
if strings.Contains(upperStmt, "ADD CONSTRAINT") {
|
||||
if strings.Contains(upperStmt, "FOREIGN KEY") {
|
||||
return "ADD FOREIGN KEY"
|
||||
} else if strings.Contains(upperStmt, "PRIMARY KEY") {
|
||||
return "ADD PRIMARY KEY"
|
||||
} else if strings.Contains(upperStmt, "UNIQUE") {
|
||||
return "ADD UNIQUE CONSTRAINT"
|
||||
} else if strings.Contains(upperStmt, "CHECK") {
|
||||
return "ADD CHECK CONSTRAINT"
|
||||
}
|
||||
return "ADD CONSTRAINT"
|
||||
}
|
||||
if strings.Contains(upperStmt, "ADD COLUMN") {
|
||||
return "ADD COLUMN"
|
||||
}
|
||||
if strings.Contains(upperStmt, "DROP CONSTRAINT") {
|
||||
return "DROP CONSTRAINT"
|
||||
}
|
||||
if strings.Contains(upperStmt, "ALTER COLUMN") {
|
||||
return "ALTER COLUMN"
|
||||
}
|
||||
return "ALTER TABLE"
|
||||
}
|
||||
if strings.HasPrefix(upperStmt, "COMMENT ON TABLE") {
|
||||
return "COMMENT ON TABLE"
|
||||
}
|
||||
if strings.HasPrefix(upperStmt, "COMMENT ON COLUMN") {
|
||||
return "COMMENT ON COLUMN"
|
||||
}
|
||||
if strings.HasPrefix(upperStmt, "DROP TABLE") {
|
||||
return "DROP TABLE"
|
||||
}
|
||||
if strings.HasPrefix(upperStmt, "DROP INDEX") {
|
||||
return "DROP INDEX"
|
||||
}
|
||||
|
||||
// Default
|
||||
return "SQL"
|
||||
}
|
||||
|
||||
// quoteIdentifier wraps an identifier in double quotes if necessary
|
||||
// This is needed for identifiers that start with numbers or contain special characters
|
||||
func quoteIdentifier(s string) string {
|
||||
|
||||
Reference in New Issue
Block a user