From 6c5fdbc42bcea9df23745c0890bde40b3cac64ba Mon Sep 17 00:00:00 2001 From: louis Date: Thu, 23 Jan 2025 10:20:40 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A5=20Remove=20Bolt=20DB=20Migration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/db.go | 42 ------ cmd/root.go | 1 - docs/migration.md | 25 ---- go.mod | 4 - go.sum | 19 --- internal/legacy/legacy.go | 5 - internal/legacy/message.go | 36 ----- internal/legacy/migration.go | 214 ------------------------------ internal/legacy/migration_test.go | 206 ---------------------------- internal/legacy/setting.go | 7 - internal/legacy/storage.go | 64 --------- internal/legacy/ticker.go | 48 ------- internal/legacy/upload.go | 13 -- internal/legacy/user.go | 13 -- mkdocs.yml | 1 - 15 files changed, 698 deletions(-) delete mode 100644 cmd/db.go delete mode 100644 docs/migration.md delete mode 100644 internal/legacy/legacy.go delete mode 100644 internal/legacy/message.go delete mode 100644 internal/legacy/migration.go delete mode 100644 internal/legacy/migration_test.go delete mode 100644 internal/legacy/setting.go delete mode 100644 internal/legacy/storage.go delete mode 100644 internal/legacy/ticker.go delete mode 100644 internal/legacy/upload.go delete mode 100644 internal/legacy/user.go diff --git a/cmd/db.go b/cmd/db.go deleted file mode 100644 index 275d15a4..00000000 --- a/cmd/db.go +++ /dev/null @@ -1,42 +0,0 @@ -package cmd - -import ( - "github.com/asdine/storm" - "github.com/spf13/cobra" - "github.com/systemli/ticker/internal/legacy" -) - -var ( - stormPath string - - dbCmd = &cobra.Command{ - Use: "db", - Short: "Manage the database", - Long: "Commands for managing the database.", - Args: cobra.ExactArgs(1), - } - - dbMigrateCmd = &cobra.Command{ - Use: "migrate", - Short: "Migrate the database from BoltDB to SQL", - Run: func(cmd *cobra.Command, args []string) { - oldDb, err := storm.Open(stormPath) - if err != nil { - log.WithError(err).Fatal("Unable to open the old database") - } - defer oldDb.Close() - - legacyStorage := legacy.NewLegacyStorage(oldDb) - migration := legacy.NewMigration(legacyStorage, store) - if err := migration.Do(); err != nil { - log.WithError(err).Fatal("Unable to migrate the database") - } - }, - } -) - -func init() { - dbCmd.AddCommand(dbMigrateCmd) - - dbMigrateCmd.Flags().StringVar(&stormPath, "storm.path", "", "path to the old db file") -} diff --git a/cmd/root.go b/cmd/root.go index 23f1841f..6e6fd144 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -60,7 +60,6 @@ func initConfig() { func Execute() { rootCmd.AddCommand(runCmd) - rootCmd.AddCommand(dbCmd) rootCmd.AddCommand(userCmd) rootCmd.AddCommand(versionCmd) diff --git a/docs/migration.md b/docs/migration.md deleted file mode 100644 index dd6d03ba..00000000 --- a/docs/migration.md +++ /dev/null @@ -1,25 +0,0 @@ -# Migration - -!!! Important - - In version 2.0.0 the database backend was changed from [Storm](https://github.com/asdine/storm) to [Gorm](https://gorm.io/). This means you have to migrate - your database. The following steps will guide you through the process. - -## Migrate from Storm to Gorm - -!!! Information - - Ensure you have the latest version of the ticker (`>= 2.0.0`) service installed. - -1. Stop the ticker service -2. Configure the new database backend in `config.yml` - - database: - type: "postgres" # postgres, mysql, sqlite - dsn: "host=localhost port=5432 user=ticker dbname=ticker password=ticker sslmode=disable" - -3. Start the migration - - ticker db migrate --storm.path /path/to/storm.db - -4. Start the ticker service diff --git a/go.mod b/go.mod index d516ed67..bf0e2592 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ toolchain go1.22.2 require ( github.com/appleboy/gin-jwt/v2 v2.10.1 - github.com/asdine/storm v2.1.2+incompatible github.com/bluesky-social/indigo v0.0.0-20240425170844-efe2ce5ca2e1 github.com/disintegration/imaging v1.6.2 github.com/gin-contrib/cors v1.7.3 @@ -33,7 +32,6 @@ require ( ) require ( - github.com/Sereal/Sereal/Go/sereal v0.0.0-20231009093132-b9187f1a92c6 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/bytedance/sonic v1.12.6 // indirect github.com/bytedance/sonic/loader v0.2.1 // indirect @@ -107,9 +105,7 @@ require ( github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.12 // indirect - github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect github.com/whyrusleeping/cbor-gen v0.1.1-0.20240311221002-68b9f235c302 // indirect - go.etcd.io/bbolt v1.3.7 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 // indirect go.opentelemetry.io/otel v1.21.0 // indirect go.opentelemetry.io/otel/metric v1.21.0 // indirect diff --git a/go.sum b/go.sum index dbec596e..8505ad6c 100644 --- a/go.sum +++ b/go.sum @@ -1,14 +1,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/DataDog/zstd v1.5.2 h1:vUG4lAyuPCXO0TLbXvPv7EB7cNK1QV/luu55UHLrrn8= -github.com/DataDog/zstd v1.5.2/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw= -github.com/Sereal/Sereal/Go/sereal v0.0.0-20231009093132-b9187f1a92c6 h1:5kUcJJAKWWI82Xnp/CaU0eu5hLlHkmm9acjowSkwCd0= -github.com/Sereal/Sereal/Go/sereal v0.0.0-20231009093132-b9187f1a92c6/go.mod h1:JwrycNnC8+sZPDyzM3MQ86LvaGzSpfxg885KOOwFRW4= github.com/appleboy/gin-jwt/v2 v2.10.1 h1:I68+9qGsgHDx8omd65MKhYXF7Qz5LtdFFTsB/kSU4z0= github.com/appleboy/gin-jwt/v2 v2.10.1/go.mod h1:xuzn4aNUwqwR3+j+jbL6MhryiRKinUL1SJ7WUfB33vU= github.com/appleboy/gofight/v2 v2.1.2 h1:VOy3jow4vIK8BRQJoC/I9muxyYlJ2yb9ht2hZoS3rf4= github.com/appleboy/gofight/v2 v2.1.2/go.mod h1:frW+U1QZEdDgixycTj4CygQ48yLTUhplt43+Wczp3rw= -github.com/asdine/storm v2.1.2+incompatible h1:dczuIkyqwY2LrtXPz8ixMrU/OFgZp71kbKTHGrXYt/Q= -github.com/asdine/storm v2.1.2+incompatible/go.mod h1:RarYDc9hq1UPLImuiXK3BIWPJLdIygvV3PsInK0FbVQ= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= @@ -38,8 +32,6 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dchest/siphash v1.2.3/go.mod h1:0NvQU092bT0ipiFN++/rXm69QG9tVxLAlQHIXMPAkHc= -github.com/dgryski/go-ddmin v0.0.0-20210904190556-96a6d69f1034/go.mod h1:zz4KxBkcXUWKjIcrc+uphJ1gPh/t18ymGm3PmQ+VGTk= github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c= github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4= github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= @@ -87,12 +79,7 @@ github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69 github.com/golang-jwt/jwt/v4 v4.5.1 h1:JdqV9zKUdtaa9gdPlywC3aeoEsR681PlKC+4F5gQgeo= github.com/golang-jwt/jwt/v4 v4.5.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= -github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= -github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM= -github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= @@ -301,8 +288,6 @@ github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZ github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= github.com/urfave/cli v1.22.10/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= -github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI= -github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= github.com/warpfork/go-wish v0.0.0-20220906213052-39a1cc7a02d0 h1:GDDkbFiaK8jsSDJfjId/PEGEShv6ugrt4kYsC5UIDaQ= github.com/warpfork/go-wish v0.0.0-20220906213052-39a1cc7a02d0/go.mod h1:x6AKhvSSexNrVSrViXSHUEbICjmGXhtgABaHIySUSGw= github.com/whyrusleeping/cbor-gen v0.1.1-0.20240311221002-68b9f235c302 h1:MhInbXe4SzcImAKktUvWBCWZgcw6MYf5NfumTj1BhAw= @@ -313,8 +298,6 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -go.etcd.io/bbolt v1.3.7 h1:j+zJOnnEjF/kyHlDDgGnVL/AIqIJPq8UoB2GSNfkUfQ= -go.etcd.io/bbolt v1.3.7/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 h1:aFJWCqJMNjENlcleuuOkGAPH82y0yULBScfXcIEdS24= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1/go.mod h1:sEGXWArGqc3tVa+ekntsN65DmVbVeW+7lTKTjZF3/Fo= go.opentelemetry.io/otel v1.21.0 h1:hzLeKBZEL7Okw2mGzZ0cc4k/A7Fta0uoPgaJCr8fsFc= @@ -449,8 +432,6 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 h1:+cNy6SZtPcJQH3LJVLOSmiC7MMxXNOb3PU/VUEz+EhU= golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90= -google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= -google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= google.golang.org/protobuf v1.34.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= diff --git a/internal/legacy/legacy.go b/internal/legacy/legacy.go deleted file mode 100644 index 322d877c..00000000 --- a/internal/legacy/legacy.go +++ /dev/null @@ -1,5 +0,0 @@ -package legacy - -import "github.com/sirupsen/logrus" - -var log = logrus.WithField("package", "legacy") diff --git a/internal/legacy/message.go b/internal/legacy/message.go deleted file mode 100644 index d9dfda93..00000000 --- a/internal/legacy/message.go +++ /dev/null @@ -1,36 +0,0 @@ -package legacy - -import ( - "time" - - tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" - "github.com/mattn/go-mastodon" - geojson "github.com/paulmach/go.geojson" -) - -type Message struct { - ID int `storm:"id,increment"` - CreationDate time.Time `storm:"index"` - Ticker int `storm:"index"` - Text string - Attachments []Attachment - GeoInformation geojson.FeatureCollection - Tweet Tweet - Telegram TelegramMeta - Mastodon mastodon.Status -} - -type Attachment struct { - UUID string - Extension string - ContentType string -} - -type Tweet struct { - ID string - UserName string -} - -type TelegramMeta struct { - Messages []tgbotapi.Message -} diff --git a/internal/legacy/migration.go b/internal/legacy/migration.go deleted file mode 100644 index 5aab0603..00000000 --- a/internal/legacy/migration.go +++ /dev/null @@ -1,214 +0,0 @@ -package legacy - -import ( - "time" - - "github.com/systemli/ticker/internal/storage" -) - -type Migration struct { - oldStorage *LegacyStorage - newStorage *storage.SqlStorage -} - -func NewMigration(oldStorage *LegacyStorage, newStorage *storage.SqlStorage) *Migration { - return &Migration{ - oldStorage: oldStorage, - newStorage: newStorage, - } -} - -func (m *Migration) Do() error { - tickers, err := m.oldStorage.FindTickers() - if err != nil { - log.WithError(err).Error("Unable to find tickers") - } - - for _, oldTicker := range tickers { - ticker := storage.Ticker{ - ID: oldTicker.ID, - CreatedAt: oldTicker.CreationDate, - UpdatedAt: time.Now(), - Domain: oldTicker.Domain, - Title: oldTicker.Title, - Description: oldTicker.Description, - Active: oldTicker.Active, - Information: storage.TickerInformation{ - Author: oldTicker.Information.Author, - URL: oldTicker.Information.URL, - Email: oldTicker.Information.Email, - Twitter: oldTicker.Information.Twitter, - Facebook: oldTicker.Information.Facebook, - Telegram: oldTicker.Information.Telegram, - }, - Telegram: storage.TickerTelegram{ - CreatedAt: oldTicker.CreationDate, - UpdatedAt: time.Now(), - Active: oldTicker.Telegram.Active, - ChannelName: oldTicker.Telegram.ChannelName, - }, - Mastodon: storage.TickerMastodon{ - CreatedAt: oldTicker.CreationDate, - UpdatedAt: time.Now(), - Active: oldTicker.Mastodon.Active, - Server: oldTicker.Mastodon.Server, - Token: oldTicker.Mastodon.Token, - Secret: oldTicker.Mastodon.Secret, - AccessToken: oldTicker.Mastodon.AccessToken, - User: storage.MastodonUser{ - Username: oldTicker.Mastodon.User.Username, - Avatar: oldTicker.Mastodon.User.Avatar, - DisplayName: oldTicker.Mastodon.User.DisplayName, - }, - }, - Location: storage.TickerLocation{ - Lat: oldTicker.Location.Lat, - Lon: oldTicker.Location.Lon, - }, - } - - if err := m.newStorage.DB.Create(&ticker).Error; err != nil { - log.WithError(err).WithField("ticker_id", ticker.ID).Error("Unable to save ticker") - } - - messages, err := m.oldStorage.FindMessageByTickerID(oldTicker.ID) - if err != nil { - log.WithError(err).WithField("ticker_id", oldTicker.ID).Error("Unable to find messages for ticker") - continue - } - - for _, oldMessage := range messages { - attachments := make([]storage.Attachment, 0) - for _, oldAttachment := range oldMessage.Attachments { - attachment := storage.Attachment{ - CreatedAt: oldMessage.CreationDate, - UpdatedAt: oldMessage.CreationDate, - MessageID: oldMessage.ID, - UUID: oldAttachment.UUID, - Extension: oldAttachment.Extension, - ContentType: oldAttachment.ContentType, - } - attachments = append(attachments, attachment) - } - - message := storage.Message{ - ID: oldMessage.ID, - CreatedAt: oldMessage.CreationDate, - UpdatedAt: time.Now(), - TickerID: oldMessage.Ticker, - Text: oldMessage.Text, - Attachments: attachments, - GeoInformation: oldMessage.GeoInformation, - Telegram: storage.TelegramMeta{ - Messages: oldMessage.Telegram.Messages, - }, - Mastodon: storage.MastodonMeta{ - ID: string(oldMessage.Mastodon.ID), - URI: oldMessage.Mastodon.URI, - URL: oldMessage.Mastodon.URL, - }, - } - - if err := m.newStorage.DB.Create(&message).Error; err != nil { - log.WithError(err).WithField("message_id", message.ID).Error("Unable to save message") - continue - } - } - - } - - users, err := m.oldStorage.FindUsers() - if err != nil { - log.WithError(err).Error("Unable to find users") - } - - for _, oldUser := range users { - user := storage.User{ - ID: oldUser.ID, - CreatedAt: oldUser.CreationDate, - UpdatedAt: time.Now(), - Email: oldUser.Email, - EncryptedPassword: oldUser.EncryptedPassword, - IsSuperAdmin: oldUser.IsSuperAdmin, - } - - if err := m.newStorage.DB.Create(&user).Error; err != nil { - log.WithError(err).WithField("user_id", user.ID).Error("Unable to save user") - continue - } - - for _, tickerID := range oldUser.Tickers { - ticker, err := m.newStorage.FindTickerByID(tickerID) - if err != nil { - log.WithError(err).WithField("ticker_id", tickerID).Warn("Unable to find ticker") - continue - } - err = m.newStorage.AddTickerUser(&ticker, &user) - if err != nil { - log.WithError(err).WithField("ticker_id", tickerID).WithField("user_id", user.ID).Error("Unable to add ticker to user") - continue - } - } - } - - uploads, err := m.oldStorage.FindUploads() - if err != nil { - log.WithError(err).Error("Unable to find uploads") - } - - for _, oldUpload := range uploads { - upload := storage.Upload{ - ID: oldUpload.ID, - CreatedAt: oldUpload.CreationDate, - UpdatedAt: time.Now(), - TickerID: oldUpload.TickerID, - UUID: oldUpload.UUID, - Path: oldUpload.Path, - Extension: oldUpload.Extension, - ContentType: oldUpload.ContentType, - } - - if err := m.newStorage.DB.Create(&upload).Error; err != nil { - log.WithError(err).WithField("upload_id", upload.ID).Error("Unable to save upload") - continue - } - } - - refreshIntervalSetting, err := m.oldStorage.FindSetting("refresh_interval") - if err != nil { - log.WithError(err).Warn("Unable to find refresh_interval setting") - } else { - var value int - switch refreshIntervalSetting.Value.(type) { - case float64: - value = int(refreshIntervalSetting.Value.(float64)) - case int: - value = refreshIntervalSetting.Value.(int) - } - err = m.newStorage.SaveRefreshIntervalSettings(storage.RefreshIntervalSettings{ - RefreshInterval: value, - }) - if err != nil { - log.WithError(err).Error("Unable to save refresh_interval setting") - } - } - - inactiveSettings, err := m.oldStorage.FindSetting("inactive_settings") - if err != nil { - log.WithError(err).Warn("Unable to find inactive_settings setting") - } else { - err = m.newStorage.SaveInactiveSettings(storage.InactiveSettings{ - Headline: inactiveSettings.Value.(map[string]interface{})["headline"].(string), - SubHeadline: inactiveSettings.Value.(map[string]interface{})["sub_headline"].(string), - Description: inactiveSettings.Value.(map[string]interface{})["description"].(string), - Author: inactiveSettings.Value.(map[string]interface{})["author"].(string), - Email: inactiveSettings.Value.(map[string]interface{})["email"].(string), - Twitter: inactiveSettings.Value.(map[string]interface{})["twitter"].(string), - }) - if err != nil { - log.WithError(err).Error("Unable to save inactive_settings setting") - } - } - - return nil -} diff --git a/internal/legacy/migration_test.go b/internal/legacy/migration_test.go deleted file mode 100644 index a6ec4743..00000000 --- a/internal/legacy/migration_test.go +++ /dev/null @@ -1,206 +0,0 @@ -package legacy - -import ( - "os" - "testing" - "time" - - "github.com/asdine/storm" - "github.com/stretchr/testify/suite" - "github.com/systemli/ticker/internal/storage" - "gorm.io/driver/sqlite" - "gorm.io/gorm" -) - -type MigrationTestSuite struct { - oldDb *storm.DB - newDb *gorm.DB - migration *Migration - err error - - ticker Ticker - message Message - attachment Attachment - adminUser User - user User - upload Upload - refreshIntervalSetting Setting - inactiveSettingsSetting Setting - - suite.Suite -} - -func (s *MigrationTestSuite) SetupTest() { - s.oldDb, s.err = storm.Open("storm.db", storm.BoltOptions(0600, nil)) - s.NoError(s.err) - - s.newDb, s.err = gorm.Open(sqlite.Open("file:testdatabase?mode=memory&cache=shared"), &gorm.Config{}) - s.NoError(s.err) - s.NoError(storage.MigrateDB(s.newDb)) - - oldStorage := NewLegacyStorage(s.oldDb) - newStorage := storage.NewSqlStorage(s.newDb, "testdata/uploads") - s.migration = NewMigration(oldStorage, newStorage) - - s.ticker = Ticker{ - ID: 161, - CreationDate: time.Now(), - Active: true, - Title: "Ticker", - Domain: "ticker.org", - Description: "Ticker description", - Information: Information{ - Author: "Author", - }, - Telegram: Telegram{ - Active: true, - }, - Mastodon: Mastodon{ - Active: true, - }, - } - - s.attachment = Attachment{ - UUID: "uuid", - Extension: "png", - ContentType: "image/png", - } - - s.message = Message{ - ID: 1, - CreationDate: time.Now(), - Ticker: 161, - Text: "Message", - Attachments: []Attachment{s.attachment}, - } - - s.adminUser = User{ - ID: 1, - Email: "admin@systemli.org", - EncryptedPassword: "notempty", - CreationDate: time.Now(), - IsSuperAdmin: true, - } - - s.user = User{ - ID: 2, - Email: "user@systemli.org", - EncryptedPassword: "notempty", - CreationDate: time.Now(), - IsSuperAdmin: false, - Tickers: []int{161}, - } - - s.upload = Upload{ - ID: 1, - CreationDate: time.Now(), - TickerID: 161, - UUID: "uuid", - Extension: "png", - ContentType: "image/png", - Path: "testdata/uploads", - } - - s.refreshIntervalSetting = Setting{ - ID: 1, - Name: "refresh_interval", - Value: 10000, - } - - s.inactiveSettingsSetting = Setting{ - ID: 2, - Name: "inactive_settings", - Value: map[string]string{ - "headline": "Headline", - "sub_headline": "Subheadline", - "description": "Description", - "author": "Author", - "email": "Email", - "twitter": "Twitter", - }, - } - - s.NoError(s.oldDb.Save(&s.ticker)) - s.NoError(s.oldDb.Save(&s.message)) - s.NoError(s.oldDb.Save(&s.adminUser)) - s.NoError(s.oldDb.Save(&s.user)) - s.NoError(s.oldDb.Save(&s.upload)) - s.NoError(s.oldDb.Save(&s.refreshIntervalSetting)) - s.NoError(s.oldDb.Save(&s.inactiveSettingsSetting)) -} - -func (s *MigrationTestSuite) TearDownTest() { - s.NoError(s.oldDb.Close()) - s.NoError(os.Remove("storm.db")) -} - -func (s *MigrationTestSuite) TestDo() { - s.err = s.migration.Do() - s.NoError(s.err) - - var tickers []storage.Ticker - s.NoError(s.newDb.Find(&tickers).Error) - s.Equal(1, len(tickers)) - s.Equal(s.ticker.ID, tickers[0].ID) - s.Equal(s.ticker.Active, tickers[0].Active) - - var telegram storage.TickerTelegram - s.NoError(s.newDb.First(&telegram).Error) - s.Equal(s.ticker.Telegram.Active, telegram.Active) - - var mastodon storage.TickerMastodon - s.NoError(s.newDb.First(&mastodon).Error) - s.Equal(s.ticker.Mastodon.Active, mastodon.Active) - - var users []storage.User - s.NoError(s.newDb.Find(&users).Error) - s.Equal(2, len(users)) - s.Equal(s.adminUser.ID, users[0].ID) - s.Equal(s.adminUser.Email, users[0].Email) - s.Equal(s.adminUser.IsSuperAdmin, users[0].IsSuperAdmin) - s.Equal(s.user.ID, users[1].ID) - s.Equal(s.user.Email, users[1].Email) - s.Equal(s.user.IsSuperAdmin, users[1].IsSuperAdmin) - - var tickersUsers []storage.User - s.NoError(s.newDb.Model(&tickers[0]).Association("Users").Find(&tickersUsers)) - s.Equal(1, len(tickersUsers)) - s.Equal(s.user.ID, tickersUsers[0].ID) - s.Equal(s.user.Email, tickersUsers[0].Email) - - var messages []storage.Message - s.NoError(s.newDb.Find(&messages).Error) - s.Equal(1, len(messages)) - s.Equal(s.message.ID, messages[0].ID) - s.Equal(s.message.Ticker, messages[0].TickerID) - s.Equal(s.message.Text, messages[0].Text) - - var attachments []storage.Attachment - s.NoError(s.newDb.Find(&attachments).Error) - s.Equal(1, len(attachments)) - s.Equal(s.attachment.UUID, attachments[0].UUID) - s.Equal(s.attachment.Extension, attachments[0].Extension) - s.Equal(s.attachment.ContentType, attachments[0].ContentType) - - var uploads []storage.Upload - s.NoError(s.newDb.Find(&uploads).Error) - s.Equal(1, len(uploads)) - s.Equal(s.upload.ID, uploads[0].ID) - s.Equal(s.upload.TickerID, uploads[0].TickerID) - s.Equal(s.upload.UUID, uploads[0].UUID) - s.Equal(s.upload.Extension, uploads[0].Extension) - s.Equal(s.upload.ContentType, uploads[0].ContentType) - s.Equal(s.upload.Path, uploads[0].Path) - - var settings []storage.Setting - s.NoError(s.newDb.Find(&settings).Error) - s.Equal(2, len(settings)) - s.Equal(s.refreshIntervalSetting.Name, settings[0].Name) - s.Equal(`{"refreshInterval":10000}`, settings[0].Value) - s.Equal(s.inactiveSettingsSetting.Name, settings[1].Name) - s.Equal(`{"headline":"Headline","subHeadline":"Subheadline","description":"Description","author":"Author","email":"Email","homepage":"","twitter":"Twitter"}`, settings[1].Value) -} - -func TestMigrationTestSuite(t *testing.T) { - suite.Run(t, new(MigrationTestSuite)) -} diff --git a/internal/legacy/setting.go b/internal/legacy/setting.go deleted file mode 100644 index 2c1a99e8..00000000 --- a/internal/legacy/setting.go +++ /dev/null @@ -1,7 +0,0 @@ -package legacy - -type Setting struct { - ID int `storm:"id,increment"` - Name string `storm:"unique"` - Value interface{} -} diff --git a/internal/legacy/storage.go b/internal/legacy/storage.go deleted file mode 100644 index cd7f9c0a..00000000 --- a/internal/legacy/storage.go +++ /dev/null @@ -1,64 +0,0 @@ -package legacy - -import ( - "github.com/asdine/storm" - "github.com/asdine/storm/q" -) - -type LegacyStorage struct { - db *storm.DB -} - -func NewLegacyStorage(db *storm.DB) *LegacyStorage { - return &LegacyStorage{db: db} -} - -func (s *LegacyStorage) FindTickers() ([]Ticker, error) { - tickers := make([]Ticker, 0) - err := s.db.Select().Reverse().Find(&tickers) - if err != nil && err.Error() != "not found" { - return tickers, err - } - - return tickers, nil -} - -func (s *LegacyStorage) FindUsers() ([]User, error) { - users := make([]User, 0) - err := s.db.Select().Reverse().Find(&users) - if err != nil && err.Error() != "not found" { - return users, err - } - - return users, nil -} - -func (s *LegacyStorage) FindUploads() ([]Upload, error) { - uploads := make([]Upload, 0) - err := s.db.Select().Reverse().Find(&uploads) - if err != nil && err.Error() != "not found" { - return uploads, err - } - - return uploads, nil -} - -func (s *LegacyStorage) FindMessageByTickerID(id int) ([]Message, error) { - messages := make([]Message, 0) - err := s.db.Select(q.Eq("Ticker", id)).Reverse().Find(&messages) - if err != nil && err.Error() == "not found" { - return messages, nil - } - - return messages, err -} - -func (s *LegacyStorage) FindSetting(name string) (Setting, error) { - var setting Setting - err := s.db.One("Name", name, &setting) - if err != nil && err.Error() == "not found" { - return setting, err - } - - return setting, nil -} diff --git a/internal/legacy/ticker.go b/internal/legacy/ticker.go deleted file mode 100644 index dadd1039..00000000 --- a/internal/legacy/ticker.go +++ /dev/null @@ -1,48 +0,0 @@ -package legacy - -import ( - "time" - - "github.com/mattn/go-mastodon" -) - -type Ticker struct { - ID int `storm:"id,increment"` - CreationDate time.Time `storm:"index"` - Domain string `storm:"unique"` - Title string - Description string - Active bool - Information Information - Telegram Telegram - Mastodon Mastodon - Location Location -} - -type Information struct { - Author string - URL string - Email string - Twitter string - Facebook string - Telegram string -} - -type Telegram struct { - Active bool `json:"active"` - ChannelName string `json:"channel_name"` -} - -type Mastodon struct { - Active bool `json:"active"` - Server string `json:"server"` - Token string `json:"token"` - Secret string `json:"secret"` - AccessToken string `json:"access_token"` - User mastodon.Account -} - -type Location struct { - Lat float64 - Lon float64 -} diff --git a/internal/legacy/upload.go b/internal/legacy/upload.go deleted file mode 100644 index 49f174b1..00000000 --- a/internal/legacy/upload.go +++ /dev/null @@ -1,13 +0,0 @@ -package legacy - -import "time" - -type Upload struct { - ID int `storm:"id,increment"` - UUID string `storm:"index,unique"` - CreationDate time.Time `storm:"index"` - TickerID int `storm:"index"` - Path string - Extension string - ContentType string -} diff --git a/internal/legacy/user.go b/internal/legacy/user.go deleted file mode 100644 index dd02de66..00000000 --- a/internal/legacy/user.go +++ /dev/null @@ -1,13 +0,0 @@ -package legacy - -import "time" - -type User struct { - ID int `storm:"id,increment"` - CreationDate time.Time `storm:"index"` - Email string `storm:"unique"` - Role string - EncryptedPassword string - IsSuperAdmin bool - Tickers []int -} diff --git a/mkdocs.yml b/mkdocs.yml index e50cfd9f..a4d0871b 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -6,7 +6,6 @@ nav: - Home: index.md - Configuration: configuration.md - API: api.md - - Migration: migration.md - Quick Install Guide: quick-install.md theme: