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

Implement SQLite #3

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions .idea/sqldialects.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions etc/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"LocalRoot": "uploadTest",
"DriveRoot": "root",
"DbPath": "queue.db",
"SQLitePath": ":memory:",
"LocalTime": "America/Los_Angeles",
"UploadMaxWorkers": 3
}
31 changes: 30 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,42 @@
package main

import (
"flag"
"log"
"net/http"
"sync"
)

func main() {

flag.Parse()

switch mode := flag.Arg(0) ; mode {
case "daemonV1":
daemonV1()
case "migrateToV2":
migrateToV2()
case "scanGDrive":
scanGDrive()
default:
flag.PrintDefaults()
}

}

func scanGDrive() {
loadSettings()
getLocTime()
setupSQL()
getDrive()

}

func migrateToV2() {

}

func daemonV1() {
loadSettings()
getLocTime()
setupBolt()
Expand All @@ -31,5 +61,4 @@ func main() {
}()

wg.Wait()

}
79 changes: 79 additions & 0 deletions migration/20190219200159_initial_tables.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package migration

import (
"database/sql"
"github.com/pressly/goose"
)

func init() {
goose.AddMigration(Up20190219200159, Down20190219200159)
}

func Up20190219200159(tx *sql.Tx) error {
// This code is executed when the migration is applied.
_, err := tx.Exec(`
CREATE TABLE localFile (
id INTEGER PRIMARY KEY,
parentID INTEGER,
fileType TEXT NOT NULL,
name TEXT NOT NULL,
md5 TEXT NOT NULL,
gDriveFileID TEXT,
uploadStatus TEXT NOT NULL DEFAULT "unknown",
updatedAt TEXT NOT NULL,
deleted INTEGER NOT NULL DEFAULT 0
);
`)
if err != nil {
return err
}
_, err = tx.Exec(`
CREATE TABLE localFileMetric (
localFileID INTEGER NOT NULL,
foundAt TEXT,
queuedAt TEXT,
uploadStartedAt TEXT,
uploadEndedAt TEXT,
uploadRetries INTEGER NOT NULL DEFAULT 0
);
`)
if err != nil {
return err
}
_, err = tx.Exec(`
CREATE TABLE gDriveFile (
id TEXT PRIMARY KEY,
parentID TEXT,
fileType TEXT NOT NULL,
name TEXT NOT NULL,
md5 TEXT NOT NULL,
localFileID INTEGER,
updatedAt TEXT NOT NULL,
deleted INTEGER NOT NULL DEFAULT 0
);
`)
if err != nil {
return err
}
_, err = tx.Exec(`
CREATE TABLE gDriveFileMetric (
gDriveFileID TEXT,
foundAt TEXT
);
`)
if err != nil {
return err
}
return nil
}

func Down20190219200159(tx *sql.Tx) error {
// This code is executed when the migration is rolled back.
tx.Exec(`
DROP TABLE localFile;
DROP TABLE localFileMetric;
DROP TABLE gDriveFile;
DROP TABLE gDriveFileMetric;
`)
return nil
}
12 changes: 0 additions & 12 deletions paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,4 @@ func makeSortKey(path *Path) string {
return path.ModTime.Format("20060102")
}

var locTime *time.Location

func getLocTime() *time.Location {
if locTime != nil {
return locTime
}
loc, err := time.LoadLocation(settings.LocalTime)
if err != nil {
log.Fatalf("Unable to local time location: %v", err)
}
locTime = loc
return locTime
}
36 changes: 36 additions & 0 deletions schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
_ "github.com/pjankovsky/gdriver-go/migration"
"github.com/pressly/goose"
"log"
)

func setupSQL() {
db := getDB()

err :=goose.SetDialect("sqlite3")
if err != nil {
log.Fatalf("Unable to set goose dialect: %v", err)
}

err = goose.Up(db.DB, "migration")
if err != nil {
log.Fatalf("Unable to run goose migrations: %v", err)
}
}

var cachedDB *sqlx.DB

func getDB() *sqlx.DB {
if cachedDB == nil {
db, err := sqlx.Connect("sqlite3", settings.SQLitePath)
if err != nil {
log.Fatalf("Unable to connect to sqlite3 db: %v", err)
}
cachedDB = db
}
return cachedDB
}
32 changes: 32 additions & 0 deletions schema/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package schema

// LocalFileID : DB autoinc ID
type LocalFileID uint64

// GDriveFileID : string ID from Google Drive
type GDriveFileID string

// MD5 : byte slice for MD5 hash
type MD5 [32]byte

// FileType : designate if the file is a Directory or File
type FileType string

// enum options for FileType
const (
FileTypeFile FileType = "F"
FileTypeDir FileType = "D"
)

// UploadStatus : the status of the upload
type UploadStatus string

// enum options for UploadStatus
const (
UploadStatusUnknown UploadStatus = "unknown"
UploadStatusError UploadStatus = "error"
UploadStatusReady UploadStatus = "ready"
UploadStatusPending UploadStatus = "pending"
UploadStatusInProgress UploadStatus = "inprogress"
UploadStatusDone UploadStatus = "done"
)
21 changes: 21 additions & 0 deletions schema/gdrive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package schema

import "time"

// GDriveFileRow is for db table gDriveFile
type GDriveFileRow struct {
id GDriveFileID
parentID GDriveFileID
fileType FileType
name string
md5 MD5
localFileID LocalFileID
updatedAt time.Time
deleted bool
}

// GDriveFileMetricRow is for db table gDriveFileMetric
type GDriveFileMetricRow struct {
gDriveFileID GDriveFileID
foundAt time.Time
}
26 changes: 26 additions & 0 deletions schema/local.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package schema

import "time"

// LocalFileRow is for db table localFile
type LocalFileRow struct {
id LocalFileID
parentID LocalFileID
fileType FileType
name string
md5 MD5
gDriveFileID GDriveFileID
uploadStatus UploadStatus
updatedAt time.Time
deleted bool
}

// LocalFileMetricRow is for db table localFileMetric
type LocalFileMetricRow struct {
localFileID LocalFileID
foundAt time.Time
queuedAt time.Time
uploadStartedAt time.Time
uploadEndedAt time.Time
uploadRetries int
}
17 changes: 17 additions & 0 deletions settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"os"
"path"
"time"
)

type Settings struct {
Expand All @@ -19,6 +20,7 @@ type Settings struct {
DbPath string
LocalTime string
UploadMaxWorkers int
SQLitePath string
}

var settings Settings
Expand Down Expand Up @@ -48,6 +50,21 @@ func loadSettings() {
log.Printf(" -- LocalRoot: %v", settings.LocalRoot)
log.Printf(" -- DriveRoot: %v", settings.DriveRoot)
log.Printf(" -- DbPath: %v", settings.DbPath)
log.Printf(" -- SQLitePath: %v", settings.SQLitePath)
log.Printf(" -- LocalTime: %v", settings.LocalTime)
log.Printf(" -- UploadMaxWorkers: %v", settings.UploadMaxWorkers)
}

var locTime *time.Location

func getLocTime() *time.Location {
if locTime != nil {
return locTime
}
loc, err := time.LoadLocation(settings.LocalTime)
if err != nil {
log.Fatalf("Unable to local time location: %v", err)
}
locTime = loc
return locTime
}