Skip to content

Commit

Permalink
SQL bug fix + removed "pCloud deleted" state from database
Browse files Browse the repository at this point in the history
  • Loading branch information
seborama committed Dec 27, 2020
1 parent e17eedc commit 8772978
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 117 deletions.
17 changes: 14 additions & 3 deletions tracker/db/migrations/sqlite3.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
package migrations

// SQLite3 holds the migrations for the sqlite3-based schema.
// TODO: missing a concept of replica_id which would uniquely identify a cloud or a local
// replica beyond the current type + device_id which is not sufficient. For instance,
// there could be 2 local replicas with the same device ID (they would differ by root
// path)
var SQLite3 = []string{
`
BEGIN;
CREATE TABLE IF NOT EXISTS "sync" (
"type" VARCHAR,
"device_id" VARCHAR,
"status" VARCHAR,
PRIMARY KEY ("type", "device_id")
);
CREATE TABLE IF NOT EXISTS "filesystem" (
"type" VARCHAR,
"version" CHAR(1),
"version" VARCHAR,
"device_id" VARCHAR,
"entry_id" VARCHAR,
"is_folder" BOOL DEFAULT FALSE,
"is_deleted" BOOL DEFAULT FALSE,
"deleted_file_id" VARCHAR NULL,
"path" VARCHAR NOT NULL,
"name" VARCHAR NOT NULL,
"parent_folder_id" VARCHAR NOT NULL,
Expand Down
31 changes: 6 additions & 25 deletions tracker/db/sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,16 @@ type Version string

const (
// VersionPrevious is the previous version of file system entries in the database.
VersionPrevious Version = "P"
VersionPrevious Version = "Previous"
// VersionNew is the newer version of file system entries in the database.
VersionNew Version = "N"
VersionNew Version = "New"
)

// FSEntry is a set of details about an entry (folder or file) in the file system.
type FSEntry struct {
DeviceID string // for cloud, this could be used to distinguish multiple accounts on the same cloud provider
EntryID uint64
IsFolder bool
IsDeleted bool
DeletedFileID uint64
Path string
Name string
ParentFolderID uint64
Expand Down Expand Up @@ -103,15 +101,13 @@ func (s *SQLite3) AddNewFileSystemEntries(ctx context.Context, fsType FSType, op
_, err = tx.ExecContext(
ctx,
`INSERT INTO "filesystem"
(type, version, device_id, entry_id, is_folder, is_deleted, deleted_file_id, path, name, parent_folder_id, created, modified, size, hash)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
(type, version, device_id, entry_id, is_folder, path, name, parent_folder_id, created, modified, size, hash)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
fsType,
VersionNew,
entry.DeviceID,
fmt.Sprintf("%d", entry.EntryID),
entry.IsFolder,
entry.IsDeleted,
fmt.Sprintf("%d", entry.DeletedFileID), // TODO: needed?
entry.Path,
entry.Name,
fmt.Sprintf("%d", entry.ParentFolderID),
Expand Down Expand Up @@ -149,8 +145,6 @@ func (s *SQLite3) getFileSystemEntries(ctx context.Context, fsType FSType, versi
device_id,
entry_id,
is_folder,
is_deleted,
deleted_file_id,
path,
name,
parent_folder_id,
Expand All @@ -177,8 +171,6 @@ func (s *SQLite3) getFileSystemEntries(ctx context.Context, fsType FSType, versi
&entry.DeviceID,
&entry.EntryID,
&entry.IsFolder,
&entry.IsDeleted,
&entry.DeletedFileID,
&entry.Path,
&entry.Name,
&entry.ParentFolderID,
Expand Down Expand Up @@ -242,20 +234,17 @@ func (s *SQLite3) GetPCloudMutations(ctx context.Context) ([]FSMutation, error)
ctx,
`WITH previous AS (SELECT * FROM filesystem
WHERE version = '`+string(VersionPrevious)+`'
AND is_deleted = false),
AND type = '`+string(PCloudFileSystem)+`'),
new AS (SELECT * FROM filesystem
WHERE version = '`+string(VersionNew)+`'
AND is_deleted = false
AND type = '`+string(PCloudFileSystem)+`')
AND type = '`+string(PCloudFileSystem)+`')
SELECT
'`+string(MutationTypeDeleted)+`',
previous.version,
previous.device_id,
previous.entry_id,
previous.is_folder,
previous.is_deleted,
previous.deleted_file_id,
previous.path,
previous.name,
previous.parent_folder_id,
Expand All @@ -274,8 +263,6 @@ func (s *SQLite3) GetPCloudMutations(ctx context.Context) ([]FSMutation, error)
new.device_id,
new.entry_id,
new.is_folder,
new.is_deleted,
new.deleted_file_id,
new.path,
new.name,
new.parent_folder_id,
Expand All @@ -294,8 +281,6 @@ func (s *SQLite3) GetPCloudMutations(ctx context.Context) ([]FSMutation, error)
new.device_id,
new.entry_id,
new.is_folder,
new.is_deleted,
new.deleted_file_id,
new.path,
new.name,
new.parent_folder_id,
Expand All @@ -318,8 +303,6 @@ func (s *SQLite3) GetPCloudMutations(ctx context.Context) ([]FSMutation, error)
new.device_id,
new.entry_id,
new.is_folder,
new.is_deleted,
new.deleted_file_id,
new.path,
new.name,
new.parent_folder_id,
Expand Down Expand Up @@ -347,8 +330,6 @@ func (s *SQLite3) GetPCloudMutations(ctx context.Context) ([]FSMutation, error)
&fsMutation.DeviceID,
&fsMutation.EntryID,
&fsMutation.IsFolder,
&fsMutation.IsDeleted,
&fsMutation.DeletedFileID,
&fsMutation.Path,
&fsMutation.Name,
&fsMutation.ParentFolderID,
Expand Down
10 changes: 8 additions & 2 deletions tracker/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ func (t *Tracker) ListLatestPCloudContents(ctx context.Context, opts ...Options)
return errors.New("cannot list pCloud drive contents: no data")
}

if lf.Metadata.IsDeleted {
return errors.New("cannot list pCloud drive contents: root folder is reportedly deleted")
}

err = func() error {
fsEntriesCh, errCh := t.store.AddNewFileSystemEntries(ctx, db.PCloudFileSystem, db.WithEntriesChSize(cfg.entriesChSize))
var entries stack
Expand All @@ -95,6 +99,10 @@ func (t *Tracker) ListLatestPCloudContents(ctx context.Context, opts ...Options)
entryID := entry.FileID
if entry.IsFolder {
for _, e := range entry.Contents {
if e.IsDeleted {
continue
}

e.Path = filepath.Join(entry.Path, entry.Name)
entries.add(e)
}
Expand All @@ -107,8 +115,6 @@ func (t *Tracker) ListLatestPCloudContents(ctx context.Context, opts ...Options)
fsEntry := db.FSEntry{
EntryID: entryID,
IsFolder: entry.IsFolder,
IsDeleted: entry.IsDeleted,
DeletedFileID: entry.DeletedFileID,
Path: entry.Path,
Name: entry.Name,
ParentFolderID: entry.ParentFolderID,
Expand Down
Loading

0 comments on commit 8772978

Please sign in to comment.