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

Fix snapshot generation of deleted database #409

Merged
merged 1 commit into from
Oct 3, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions cmd/litefs/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,45 @@ func TestSingleNode_BackupClient(t *testing.T) {
waitForBackupSync(t, cmd0)
})

// Ensure LiteFS can send a deleted database to backup.
t.Run("InitiateSnapshot/Empty", func(t *testing.T) {
cmd0 := newMountCommand(t, t.TempDir(), nil)
cmd0.Config.Data.Retention = 1 * time.Microsecond
cmd0.Config.Backup = main.BackupConfig{Type: "file", Path: t.TempDir(), Delay: 1 * time.Millisecond}
runMountCommand(t, cmd0)

// Create a simple table and delete the database.
db := testingutil.OpenSQLDB(t, filepath.Join(cmd0.Config.FUSE.Dir, "db"))
if _, err := db.Exec(`CREATE TABLE t (x)`); err != nil {
t.Fatal(err)
} else if err := db.Close(); err != nil {
t.Fatal(err)
} else if err := os.Remove(filepath.Join(cmd0.Config.FUSE.Dir, "db")); err != nil {
t.Fatal(err)
}

// Enforce retention to remove initial LTX file.
time.Sleep(cmd0.Config.Data.Retention)
if err := cmd0.Store.EnforceRetention(context.Background()); err != nil {
t.Fatal(err)
}

// Sync to backup.
if err := cmd0.Store.SyncBackup(context.Background()); err != nil {
t.Fatal(err)
}

// Recreate database
db = testingutil.OpenSQLDB(t, filepath.Join(cmd0.Config.FUSE.Dir, "db"))
if _, err := db.Exec(`CREATE TABLE v (y)`); err != nil {
t.Fatal(err)
}
if err := cmd0.Store.SyncBackup(context.Background()); err != nil {
t.Fatal(err)
}
waitForBackupSync(t, cmd0)
})

// Ensure LiteFS can restore a database from backup if it doesn't exist locally.
t.Run("RestoreFromBackup/NoLocalDatabase", func(t *testing.T) {
backupDir := t.TempDir()
Expand Down
9 changes: 5 additions & 4 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3380,12 +3380,13 @@ func (db *DB) WriteSnapshotTo(ctx context.Context, dst io.Writer) (header ltx.He
// Log transaction ID for the snapshot.
log.Printf("writing snapshot %q @ %s", db.name, pos.TXID.String())

// Open database file.
dbFile, err := db.os.Open("WRITESNAPSHOT:DB", db.DatabasePath())
if err != nil {
// Open database file. File may not exist if the database has been deleted.
var dbFile *os.File
if dbFile, err = db.os.Open("WRITESNAPSHOT:DB", db.DatabasePath()); err != nil && !os.IsNotExist(err) {
return header, trailer, fmt.Errorf("open database file: %w", err)
} else if err == nil {
defer func() { _ = dbFile.Close() }()
}
defer func() { _ = dbFile.Close() }()

// Open WAL file if we have overriding WAL frames.
var walFile *os.File
Expand Down