From 1123c948f9060b063d971b4880e4f3db2f780e96 Mon Sep 17 00:00:00 2001 From: Martin Weinelt Date: Thu, 10 Aug 2017 22:47:42 +0200 Subject: [PATCH 1/2] magneticod: add mysql support --- src/magneticod/persistence.go | 52 +++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/src/magneticod/persistence.go b/src/magneticod/persistence.go index ee53de2..9b33f58 100644 --- a/src/magneticod/persistence.go +++ b/src/magneticod/persistence.go @@ -5,6 +5,7 @@ import ( "database/sql" "net/url" + _ "github.com/go-sql-driver/mysql" _ "github.com/mattn/go-sqlite3" "go.uber.org/zap" @@ -14,22 +15,20 @@ import ( "os" ) - type engineType uint8 const ( - SQLITE engineType = 0 - POSTGRESQL = 1 + SQLITE engineType = 0 + POSTGRESQL = 1 + MYSQL = 2 ) - type Database struct { - database *sql.DB - engine engineType + database *sql.DB + engine engineType newTorrents chan bittorrent.Metadata } - // NewDatabase creates a new Database. // // url either starts with "sqlite:" or "postgresql:" @@ -66,6 +65,10 @@ func NewDatabase(rawurl string) (*Database, error) { db.engine = POSTGRESQL db.database, err = sql.Open("postgresql", rawurl) + case "mysql": + db.engine = MYSQL + db.database, err = sql.Open("mysql", rawurl) + default: return nil, fmt.Errorf("unknown URI scheme (or malformed URI)!") } @@ -88,7 +91,6 @@ func NewDatabase(rawurl string) (*Database, error) { return &db, nil } - // AddNewTorrent adds a new torrent to the *queue* to be flushed to the persistent database. func (db *Database) AddNewTorrent(torrent bittorrent.Metadata) error { for { @@ -106,7 +108,6 @@ func (db *Database) AddNewTorrent(torrent bittorrent.Metadata) error { } } - func (db *Database) flushNewTorrents() error { tx, err := db.database.Begin() if err != nil { @@ -154,13 +155,11 @@ func (db *Database) flushNewTorrents() error { return nil } - func (db *Database) Close() { // Be careful to not to get into an infinite loop. =) db.database.Close() } - func (db *Database) setupDatabase() error { switch db.engine { case SQLITE: @@ -169,6 +168,9 @@ func (db *Database) setupDatabase() error { case POSTGRESQL: zap.L().Fatal("setupDatabase() is not implemented for PostgreSQL yet!") + case MYSQL: + return setupMySQLDatabase(db.database) + default: zap.L().Sugar().Fatalf("Unknown database engine value %d! (programmer error)", db.engine) } @@ -176,7 +178,6 @@ func (db *Database) setupDatabase() error { return nil } - func setupSqliteDatabase(database *sql.DB) error { // Enable Write-Ahead Logging for SQLite as "WAL provides more concurrency as readers do not // block writers and a writer does not block readers. Reading and writing can proceed @@ -226,3 +227,30 @@ func setupSqliteDatabase(database *sql.DB) error { return nil } + +func setupMySQLDatabase(database *sql.DB) error { + _, err := database.Exec( + `CREATE TABLE IF NOT EXISTS torrents (" + id INTEGER PRIMARY KEY AUTO_INCREMENT, + info_hash BINARY(20) NOT NULL UNIQUE, + name VARCHAR(255) NOT NULL, + total_size BIGINT UNSIGNED NOT NULL, + discovered_on INTEGER UNSIGNED NOT NULL + ); + + ALTER TABLE torrents ADD INDEX info_hash_index (info_hash); + + CREATE TABLE IF NOT EXISTS files ( + id INTEGER PRIMARY KEY AUTO_INCREMENT, + torrent_id INTEGER REFERENCES torrents (id) ON DELETE CASCADE ON UPDATE RESTRICT, + size BIGINT NOT NULL, + path TEXT NOT NULL + );`, + ) + + if err != nil { + return err + } + + return nil +} From 90fad55722130ab219af249b7729699fd82224b4 Mon Sep 17 00:00:00 2001 From: Martin Weinelt Date: Sun, 13 Aug 2017 02:39:44 +0200 Subject: [PATCH 2/2] magneticod: use mysql in strict mode, increase name column to 1024 characters --- src/magneticod/persistence.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/magneticod/persistence.go b/src/magneticod/persistence.go index 9b33f58..4cae745 100644 --- a/src/magneticod/persistence.go +++ b/src/magneticod/persistence.go @@ -229,11 +229,17 @@ func setupSqliteDatabase(database *sql.DB) error { } func setupMySQLDatabase(database *sql.DB) error { - _, err := database.Exec( + // Set strict mode to prevent silent truncation + _, err := database.Exec(`SET SESSION SQL_MODE = 'STRICT_ALL_TABLES';`) + if err != nil { + return err + } + + _, err = database.Exec( `CREATE TABLE IF NOT EXISTS torrents (" id INTEGER PRIMARY KEY AUTO_INCREMENT, info_hash BINARY(20) NOT NULL UNIQUE, - name VARCHAR(255) NOT NULL, + name VARCHAR(1024) NOT NULL, total_size BIGINT UNSIGNED NOT NULL, discovered_on INTEGER UNSIGNED NOT NULL );