-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MAJOR MIGRATION] Initial structure for a concept of "datastores"
**Caution**: This is a major migration that changes how files are tracked within the database. Although tested to ensure things won't go wrong, there is the possibility of something going haywire. It is recommended to run the following database commands before upgrading: CREATE TABLE media_backup AS TABLE media; CREATE TABLE thumbnails_backup AS TABLE thumbnails; In the event you need to roll back, run the following: DROP TABLE media; DROP TABLE thumbnails; CREATE TABLE media AS TABLE media_backup; CREATE TABLE thumbnails AS TABLE thumbnails_backup; DELETE FROM gomigrate WHERE migration_id = 7; This will cause any files uploaded between upgrading and rolling back to be lost. NOTE: The migration will take a while and runs on the first access of a file. If you have a lot of files, this could take a while. ----- Fixes #106 Step towards #47
- Loading branch information
Showing
20 changed files
with
478 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
ALTER TABLE media DROP COLUMN datastore_id; | ||
ALTER TABLE thumbnails DROP COLUMN datastore_id; | ||
DROP INDEX datastores_index; | ||
DROP TABLE datastores; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CREATE TABLE IF NOT EXISTS datastores ( | ||
datastore_id TEXT NOT NULL, | ||
ds_type TEXT NOT NULL, | ||
uri TEXT NOT NULL | ||
); | ||
CREATE UNIQUE INDEX IF NOT EXISTS datastores_index ON datastores (datastore_id); | ||
|
||
ALTER TABLE media ADD COLUMN datastore_id TEXT NOT NULL DEFAULT ''; | ||
ALTER TABLE thumbnails ADD COLUMN datastore_id TEXT NOT NULL DEFAULT ''; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/github.com/turt2live/matrix-media-repo/storage/ds_utils.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package storage | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"errors" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/turt2live/matrix-media-repo/storage/stores" | ||
"github.com/turt2live/matrix-media-repo/types" | ||
"github.com/turt2live/matrix-media-repo/util" | ||
) | ||
|
||
func GetOrCreateDatastore(ctx context.Context, log *logrus.Entry, basePath string) (*types.Datastore, error) { | ||
logrus.Info("Init DS lookup") | ||
mediaService := GetDatabase().GetMediaStore(ctx, log) | ||
|
||
return getOrCreateDatastoreWithMediaService(mediaService, basePath) | ||
} | ||
|
||
func getOrCreateDatastoreWithMediaService(mediaService *stores.MediaStore, basePath string) (*types.Datastore, error) { | ||
datastore, err := mediaService.GetDatastoreByUri(basePath) | ||
if err != nil && err == sql.ErrNoRows { | ||
id, err2 := util.GenerateRandomString(32) | ||
if err2 != nil { | ||
logrus.Error("Error generating datastore ID for base path ", basePath, ": ", err) | ||
return nil, err2 | ||
} | ||
datastore = &types.Datastore{ | ||
DatastoreId: id, | ||
Type: "file", | ||
Uri: basePath, | ||
} | ||
err2 = mediaService.InsertDatastore(datastore) | ||
if err2 != nil { | ||
logrus.Error("Error creating datastore for base path ", basePath, ": ", err) | ||
return nil, err2 | ||
} | ||
} else if err != nil { | ||
logrus.Error("Error getting datastore for base path ", basePath, ": ", err) | ||
return nil, err | ||
} | ||
|
||
return datastore, nil | ||
} | ||
|
||
func ResolveMediaLocation(ctx context.Context, log *logrus.Entry, datastoreId string, location string) (string, error) { | ||
svc := GetDatabase().GetMediaStore(ctx, log) | ||
ds, err := svc.GetDatastore(datastoreId) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if ds.Type != "file" { | ||
return "", errors.New("unrecognized datastore type: " + ds.Type) | ||
} | ||
|
||
return ds.ResolveFilePath(location), nil | ||
} |
Oops, something went wrong.