Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a3e45c206d |
@@ -603,8 +603,10 @@ func (r *Reader) parseColumn(line, tableName, schemaName string) (*models.Column
|
|||||||
column.Default = strings.Trim(defaultVal, "'\"")
|
column.Default = strings.Trim(defaultVal, "'\"")
|
||||||
} else if attr == "unique" {
|
} else if attr == "unique" {
|
||||||
// Create a unique constraint
|
// Create a unique constraint
|
||||||
|
// Clean table name by removing leading underscores to avoid double underscores
|
||||||
|
cleanTableName := strings.TrimLeft(tableName, "_")
|
||||||
uniqueConstraint := models.InitConstraint(
|
uniqueConstraint := models.InitConstraint(
|
||||||
fmt.Sprintf("uq_%s_%s", tableName, columnName),
|
fmt.Sprintf("ukey_%s_%s", cleanTableName, columnName),
|
||||||
models.UniqueConstraint,
|
models.UniqueConstraint,
|
||||||
)
|
)
|
||||||
uniqueConstraint.Schema = schemaName
|
uniqueConstraint.Schema = schemaName
|
||||||
|
|||||||
@@ -809,14 +809,14 @@ func TestConstraintNaming(t *testing.T) {
|
|||||||
t.Fatal("Posts table not found")
|
t.Fatal("Posts table not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test unique constraint naming: uq_table_column
|
// Test unique constraint naming: ukey_table_column
|
||||||
if _, exists := usersTable.Constraints["uq_users_email"]; !exists {
|
if _, exists := usersTable.Constraints["ukey_users_email"]; !exists {
|
||||||
t.Error("Expected unique constraint 'uq_users_email' not found")
|
t.Error("Expected unique constraint 'ukey_users_email' not found")
|
||||||
t.Logf("Available constraints: %v", getKeys(usersTable.Constraints))
|
t.Logf("Available constraints: %v", getKeys(usersTable.Constraints))
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, exists := postsTable.Constraints["uq_posts_slug"]; !exists {
|
if _, exists := postsTable.Constraints["ukey_posts_slug"]; !exists {
|
||||||
t.Error("Expected unique constraint 'uq_posts_slug' not found")
|
t.Error("Expected unique constraint 'ukey_posts_slug' not found")
|
||||||
t.Logf("Available constraints: %v", getKeys(postsTable.Constraints))
|
t.Logf("Available constraints: %v", getKeys(postsTable.Constraints))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1410,7 +1410,8 @@ func (w *Writer) executeDatabaseSQL(db *models.Database, connString string) erro
|
|||||||
continue
|
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)
|
_, execErr := conn.Exec(ctx, stmt)
|
||||||
if execErr != nil {
|
if execErr != nil {
|
||||||
@@ -1545,6 +1546,91 @@ func getCurrentTimestamp() string {
|
|||||||
return time.Now().Format("2006-01-02 15:04:05")
|
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
|
// quoteIdentifier wraps an identifier in double quotes if necessary
|
||||||
// This is needed for identifiers that start with numbers or contain special characters
|
// This is needed for identifiers that start with numbers or contain special characters
|
||||||
func quoteIdentifier(s string) string {
|
func quoteIdentifier(s string) string {
|
||||||
|
|||||||
Reference in New Issue
Block a user