Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove database on schema creation failure #837

Merged
merged 2 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions internal/users/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ func New(dbDir string) (*Manager, error) {
log.Debugf(context.Background(), "Creating new SQLite database at %v", dbPath)
_, err = db.Exec(createSchema)
if err != nil {
// Remove the database file if we failed to create the schema, to avoid that authd tries to use a broken
// database on the next start.
if removeErr := os.Remove(dbPath); removeErr != nil {
log.Warningf(context.Background(), "Failed to remove database file after failed schema creation: %v", removeErr)
}
return nil, fmt.Errorf("failed to create schema: %w", err)
}
}
Expand Down
20 changes: 20 additions & 0 deletions internal/users/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/stretchr/testify/require"
"github.com/ubuntu/authd/internal/fileutils"
"github.com/ubuntu/authd/internal/testutils/golden"
"github.com/ubuntu/authd/internal/users/db"
"github.com/ubuntu/authd/log"
Expand Down Expand Up @@ -91,6 +92,25 @@ func TestNew(t *testing.T) {
}
}

func TestDatabaseRemovedWhenSchemaCreationFails(t *testing.T) {
// Don't run this test in parallel because it writes a global variable (via db.SetCreateSchemaQuery)
origQuery := db.GetCreateSchemaQuery()
db.SetCreateSchemaQuery("invalid query")
t.Cleanup(func() {
db.SetCreateSchemaQuery(origQuery)
})

dbDir := t.TempDir()
dbDestPath := filepath.Join(dbDir, db.Z_ForTests_DBName())

_, err := db.New(dbDir)
require.Error(t, err, "New should return an error when schema creation fails")

exists, err := fileutils.FileExists(dbDestPath)
require.NoError(t, err, "Failed to check if database file exists")
require.False(t, exists, "Database file should not exist after failed schema creation")
}

func TestUpdateUserEntry(t *testing.T) {
t.Parallel()

Expand Down
10 changes: 10 additions & 0 deletions internal/users/db/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,13 @@ package db
func (m *Manager) Path() string {
return m.path
}

// GetCreateSchemaQuery exposes the query to create the schema for testing.
func GetCreateSchemaQuery() string {
return createSchema
}

// SetCreateSchemaQuery sets the query to create the schema for testing.
func SetCreateSchemaQuery(query string) {
createSchema = query
}
Loading