{"record":{"id":"e133dd651f8dadf4","repo":"gofr-dev/gofr","slug":"sql-w","errorCode":null,"errorMessage":"sql: %w","messagePattern":"sql: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/gofr/migration/sql.go","lineNumber":88,"sourceCode":"\nfunc (d sqlMigrator) checkAndCreateMigrationTable(c *container.Container) error {\n\tif _, err := c.SQL.Exec(createSQLGoFrMigrationsTable); err != nil {\n\t\treturn err\n\t}\n\n\tif _, err := c.SQL.Exec(createSQLGoFrMigrationLocksTable); err != nil {\n\t\treturn err\n\t}\n\n\treturn d.migrator.checkAndCreateMigrationTable(c)\n}\n\nfunc (d sqlMigrator) getLastMigration(c *container.Container) (int64, error) {\n\tvar lastMigration int64\n\n\terr := c.SQL.QueryRowContext(context.Background(), getLastSQLGoFrMigration).Scan(&lastMigration)\n\tif err != nil {\n\t\treturn -1, fmt.Errorf(\"sql: %w\", err)\n\t}\n\n\tc.Debugf(\"SQL last migration fetched value is: %v\", lastMigration)\n\n\tlm2, err := d.migrator.getLastMigration(c)\n\tif err != nil {\n\t\treturn -1, err\n\t}\n\n\treturn max(lastMigration, lm2), nil\n}\n\nfunc (d sqlMigrator) commitMigration(c *container.Container, data transactionData) error {\n\tif data.UsedDatasources[dsSQL] {\n\t\tdialect := c.SQL.Dialect()\n\n\t\tswitch dialect {\n\t\tcase mysql, sqlite:","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/gofr-dev/gofr/blob/187eb24962502e91f1fee856230670958b66e89c/pkg/gofr/migration/sql.go#L70-L106","documentation":"This error wraps any failure that occurs while querying the migrations table via SQL in the GoFr migration framework. getLastMigration issues `SELECT ... FROM gofr_migrations` (getLastSQLGoFrMigration) through the container's SQL datasource and scans the result into an int64. If the query or scan fails (missing table, connection failure, NULL value), the underlying driver error is wrapped with the `sql:` prefix so callers can identify the SQL migration stage.","triggerScenarios":"Calling Migration.Run/Migrate with a SQL migrator when c.SQL.QueryRowContext(...).Scan(&lastMigration) fails: database unreachable, credentials wrong, gofr_migrations table absent, or the scanned column is NULL/non-numeric.","commonSituations":"Fresh database where migrations table was never created; SQL server down or wrong DSN in config; network/firewall blocking the DB; a migration row with NULL version value.","solutions":["Verify the database is reachable and credentials/DSN in the container config are correct by testing a simple query.","Run the migration setup so the gofr_migrations table exists (checkAndCreateMigrationTable runs before getLastMigration; confirm it succeeded).","Inspect the wrapped driver error (%w) for the root cause (e.g. 'connection refused', 'no such table').","Check network/firewall/proxy between the app and the SQL server."],"exampleFix":"// before: migrations run against a fresh DB with no table and DB may be down\napp.Migrate()\n// after: pre-check connectivity and let migrations create the table\nif err := sqlDB.Ping(); err != nil {\n\tlog.Fatalf(\"database not reachable: %v\", err)\n}\napp.Migrate()","handlingStrategy":"try-catch","validationCode":"// Go: check SQL connectivity before running migrations\nif err := c.SQL.Ping(); err != nil {\n\treturn fmt.Errorf(\"sql unavailable before migration: %w\", err)\n}","typeGuard":"var sqlDB *sql.DB\ndb, ok := c.SQL.(*sql.DB)\nif !ok || db == nil {\n\treturn errors.New(\"SQL datasource not configured\")\n}","tryCatchPattern":"last, err := migrator.getLastMigration(c)\nif err != nil {\n\tvar pathErr *net.OpError\n\tif errors.As(err, &pathErr) {\n\t\t// retry / fail fast with connection context\n\t}\n\treturn fmt.Errorf(\"migration state lookup failed: %w\", err)\n}","preventionTips":["Ping the SQL datasource during app startup before calling Migrate.","Keep migrations self-contained: let checkAndCreateMigrationTable create the table on every environment.","Log the wrapped inner error, not just the outer message, when diagnosing.","Ensure the DB user has SELECT on gofr_migrations."],"tags":["sql","migration","database","gofr"],"backgroundTag":"sql-query-failed","analyzedSha":"187eb24962502e91f1fee856230670958b66e89c","analyzedAt":"2026-09-01T20:34:54.554Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}