feat(main): add silent mode to suppress output messages
* implement --silent flag to control output verbosity * update error handling to restore stderr after silent mode * enhance progress reporting in PostgreSQL reader
This commit is contained in:
@@ -34,11 +34,14 @@ func (r *Reader) ReadDatabase() (*models.Database, error) {
|
||||
return nil, fmt.Errorf("connection string is required")
|
||||
}
|
||||
|
||||
// Connect to the database
|
||||
// Connect to the database. This can take noticeable time across a slow network,
|
||||
// so report it before the driver starts the connection attempt.
|
||||
r.progress("Connecting to PostgreSQL...")
|
||||
if err := r.connect(); err != nil {
|
||||
return nil, fmt.Errorf("failed to connect: %w", err)
|
||||
}
|
||||
defer r.close()
|
||||
r.progress("Connected. Reading database metadata...")
|
||||
|
||||
// Get database name from connection
|
||||
var dbName string
|
||||
@@ -60,34 +63,42 @@ func (r *Reader) ReadDatabase() (*models.Database, error) {
|
||||
}
|
||||
|
||||
// Query all schemas
|
||||
r.progress("Discovering schemas...")
|
||||
schemas, err := r.querySchemas()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to query schemas: %w", err)
|
||||
}
|
||||
|
||||
// Process each schema
|
||||
for _, schema := range schemas {
|
||||
for schemaIndex, schema := range schemas {
|
||||
r.progress(fmt.Sprintf("Reading schema %q (%d/%d): tables...", schema.Name, schemaIndex+1, len(schemas)))
|
||||
// Query tables for this schema
|
||||
tables, err := r.queryTables(schema.Name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to query tables for schema %s: %w", schema.Name, err)
|
||||
}
|
||||
schema.Tables = tables
|
||||
r.progress(fmt.Sprintf("Reading schema %q: found %d table(s).", schema.Name, len(tables)))
|
||||
|
||||
r.progress(fmt.Sprintf("Reading schema %q: views...", schema.Name))
|
||||
// Query views for this schema
|
||||
views, err := r.queryViews(schema.Name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to query views for schema %s: %w", schema.Name, err)
|
||||
}
|
||||
schema.Views = views
|
||||
r.progress(fmt.Sprintf("Reading schema %q: found %d view(s).", schema.Name, len(views)))
|
||||
|
||||
r.progress(fmt.Sprintf("Reading schema %q: sequences...", schema.Name))
|
||||
// Query sequences for this schema
|
||||
sequences, err := r.querySequences(schema.Name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to query sequences for schema %s: %w", schema.Name, err)
|
||||
}
|
||||
schema.Sequences = sequences
|
||||
r.progress(fmt.Sprintf("Reading schema %q: found %d sequence(s).", schema.Name, len(sequences)))
|
||||
|
||||
r.progress(fmt.Sprintf("Reading schema %q: extensions...", schema.Name))
|
||||
// Query extensions installed into this schema
|
||||
extensions, err := r.queryExtensions(schema.Name)
|
||||
if err != nil {
|
||||
@@ -99,12 +110,15 @@ func (r *Reader) ReadDatabase() (*models.Database, error) {
|
||||
}
|
||||
schema.Metadata["extensions"] = extensions
|
||||
}
|
||||
r.progress(fmt.Sprintf("Reading schema %q: found %d extension(s).", schema.Name, len(extensions)))
|
||||
|
||||
r.progress(fmt.Sprintf("Reading schema %q: columns...", schema.Name))
|
||||
// Query columns for tables and views
|
||||
columnsMap, err := r.queryColumns(schema.Name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to query columns for schema %s: %w", schema.Name, err)
|
||||
}
|
||||
r.progress(fmt.Sprintf("Reading schema %q: found %d column(s).", schema.Name, countColumns(columnsMap)))
|
||||
|
||||
// Populate table columns
|
||||
for _, table := range schema.Tables {
|
||||
@@ -122,11 +136,13 @@ func (r *Reader) ReadDatabase() (*models.Database, error) {
|
||||
}
|
||||
}
|
||||
|
||||
r.progress(fmt.Sprintf("Reading schema %q: primary keys...", schema.Name))
|
||||
// Query primary keys
|
||||
primaryKeys, err := r.queryPrimaryKeys(schema.Name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to query primary keys for schema %s: %w", schema.Name, err)
|
||||
}
|
||||
r.progress(fmt.Sprintf("Reading schema %q: found %d primary key(s).", schema.Name, len(primaryKeys)))
|
||||
|
||||
// Apply primary keys to tables
|
||||
for _, table := range schema.Tables {
|
||||
@@ -143,11 +159,13 @@ func (r *Reader) ReadDatabase() (*models.Database, error) {
|
||||
}
|
||||
}
|
||||
|
||||
r.progress(fmt.Sprintf("Reading schema %q: foreign keys...", schema.Name))
|
||||
// Query foreign keys
|
||||
foreignKeys, err := r.queryForeignKeys(schema.Name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to query foreign keys for schema %s: %w", schema.Name, err)
|
||||
}
|
||||
r.progress(fmt.Sprintf("Reading schema %q: found %d foreign key(s).", schema.Name, countConstraints(foreignKeys)))
|
||||
|
||||
// Apply foreign keys to tables
|
||||
for _, table := range schema.Tables {
|
||||
@@ -161,11 +179,13 @@ func (r *Reader) ReadDatabase() (*models.Database, error) {
|
||||
}
|
||||
}
|
||||
|
||||
r.progress(fmt.Sprintf("Reading schema %q: unique constraints...", schema.Name))
|
||||
// Query unique constraints
|
||||
uniqueConstraints, err := r.queryUniqueConstraints(schema.Name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to query unique constraints for schema %s: %w", schema.Name, err)
|
||||
}
|
||||
r.progress(fmt.Sprintf("Reading schema %q: found %d unique constraint(s).", schema.Name, countConstraints(uniqueConstraints)))
|
||||
|
||||
// Apply unique constraints to tables
|
||||
for _, table := range schema.Tables {
|
||||
@@ -177,11 +197,13 @@ func (r *Reader) ReadDatabase() (*models.Database, error) {
|
||||
}
|
||||
}
|
||||
|
||||
r.progress(fmt.Sprintf("Reading schema %q: check constraints...", schema.Name))
|
||||
// Query check constraints
|
||||
checkConstraints, err := r.queryCheckConstraints(schema.Name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to query check constraints for schema %s: %w", schema.Name, err)
|
||||
}
|
||||
r.progress(fmt.Sprintf("Reading schema %q: found %d check constraint(s).", schema.Name, countConstraints(checkConstraints)))
|
||||
|
||||
// Apply check constraints to tables
|
||||
for _, table := range schema.Tables {
|
||||
@@ -193,11 +215,13 @@ func (r *Reader) ReadDatabase() (*models.Database, error) {
|
||||
}
|
||||
}
|
||||
|
||||
r.progress(fmt.Sprintf("Reading schema %q: indexes...", schema.Name))
|
||||
// Query indexes
|
||||
indexes, err := r.queryIndexes(schema.Name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to query indexes for schema %s: %w", schema.Name, err)
|
||||
}
|
||||
r.progress(fmt.Sprintf("Reading schema %q: found %d index(es).", schema.Name, countIndexes(indexes)))
|
||||
|
||||
// Apply indexes to tables
|
||||
for _, table := range schema.Tables {
|
||||
@@ -226,10 +250,41 @@ func (r *Reader) ReadDatabase() (*models.Database, error) {
|
||||
// Add schema to database
|
||||
db.Schemas = append(db.Schemas, schema)
|
||||
}
|
||||
r.progress("PostgreSQL schema read complete.")
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func (r *Reader) progress(message string) {
|
||||
if r.options.Progress != nil {
|
||||
r.options.Progress(message)
|
||||
}
|
||||
}
|
||||
|
||||
func countColumns(columns map[string]map[string]*models.Column) int {
|
||||
total := 0
|
||||
for _, tableColumns := range columns {
|
||||
total += len(tableColumns)
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
func countConstraints(constraints map[string][]*models.Constraint) int {
|
||||
total := 0
|
||||
for _, tableConstraints := range constraints {
|
||||
total += len(tableConstraints)
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
func countIndexes(indexes map[string][]*models.Index) int {
|
||||
total := 0
|
||||
for _, tableIndexes := range indexes {
|
||||
total += len(tableIndexes)
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
// ReadSchema reads a single schema (returns the first schema from the database)
|
||||
func (r *Reader) ReadSchema() (*models.Schema, error) {
|
||||
db, err := r.ReadDatabase()
|
||||
|
||||
@@ -32,6 +32,10 @@ type ReaderOptions struct {
|
||||
// fail on an unknown namespace or key instead of preserving them silently.
|
||||
StrictDirectives bool
|
||||
|
||||
// Progress receives human-readable status updates while a reader is working.
|
||||
// It is optional so library users can opt in without coupling readers to a UI.
|
||||
Progress func(string)
|
||||
|
||||
// Additional options can be added here as needed
|
||||
Metadata map[string]interface{}
|
||||
}
|
||||
|
||||
@@ -1980,7 +1980,8 @@ func (w *Writer) executeDatabaseSQL(db *models.Database, connString string) erro
|
||||
Errors: make([]ExecutionError, 0),
|
||||
}
|
||||
|
||||
// Generate SQL statements
|
||||
// Generating a large schema can take time before any statement is executed.
|
||||
fmt.Fprintln(os.Stderr, " → Generating PostgreSQL statements...")
|
||||
statements, err := w.GenerateDatabaseStatements(db)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to generate SQL statements: %w", err)
|
||||
@@ -1989,12 +1990,14 @@ func (w *Writer) executeDatabaseSQL(db *models.Database, connString string) erro
|
||||
w.executionReport.TotalStatements = len(statements)
|
||||
|
||||
// Connect to database
|
||||
fmt.Fprintln(os.Stderr, " → Connecting to PostgreSQL output database...")
|
||||
ctx := context.Background()
|
||||
conn, err := pgsql.Connect(ctx, connString, "writer-pgsql")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to connect to database: %w", err)
|
||||
}
|
||||
defer conn.Close(ctx)
|
||||
fmt.Fprintln(os.Stderr, " → Connected. Executing statements...")
|
||||
|
||||
// Track schemas and tables
|
||||
schemaMap := make(map[string]*SchemaReport)
|
||||
|
||||
Reference in New Issue
Block a user