From 8c33e24e92883925b1b139b29a7d2656d528be0a Mon Sep 17 00:00:00 2001 From: konrad Date: Mon, 20 Jan 2020 19:48:46 +0000 Subject: [PATCH] Migration Improvements (#122) Update swagger docs Update docs Let the wunderlist migrator use the registerRoutes function Add migration status table Add migration status Co-authored-by: kolaente Reviewed-on: https://kolaente.dev/vikunja/api/pulls/122 --- docs/content/doc/development/migration.md | 9 ++- pkg/cmd/cmd.go | 5 ++ pkg/migration/20200120201756.go | 47 ++++++++++++++++ pkg/modules/migration/db.go | 49 ++++++++++++++++ pkg/modules/migration/handler/handler.go | 30 ++++++++++ pkg/modules/migration/migration_status.go | 49 ++++++++++++++++ pkg/modules/migration/migrator.go | 3 + .../migration/wunderlist/wunderlist.go | 13 +++++ pkg/routes/routes.go | 3 +- pkg/swagger/docs.go | 56 ++++++++++++++++++- pkg/swagger/swagger.json | 54 ++++++++++++++++++ pkg/swagger/swagger.yaml | 36 ++++++++++++ 12 files changed, 349 insertions(+), 5 deletions(-) create mode 100644 pkg/migration/20200120201756.go create mode 100644 pkg/modules/migration/db.go create mode 100644 pkg/modules/migration/migration_status.go diff --git a/docs/content/doc/development/migration.md b/docs/content/doc/development/migration.md index 460107bff8..8868fe2e71 100644 --- a/docs/content/doc/development/migration.md +++ b/docs/content/doc/development/migration.md @@ -35,6 +35,9 @@ type Migrator interface { // The use case for this are Oauth flows, where the server token should remain hidden and not // known to the frontend. AuthURL() string + // Name holds the name of the migration. + // This is used to show the name to users and to keep track of users who already migrated. + Name() string } ``` @@ -43,6 +46,9 @@ type Migrator interface { Once your migrator implements the migration interface, it becomes possible to use the helper http handlers. Their usage is very similar to the [general web handler](https://kolaente.dev/vikunja/web#user-content-defining-routes-using-the-standard-web-handler): +The `RegisterRoutes(m)` method registers all routes with the scheme `/[MigratorName]/(auth|migrate|status)` for the +authUrl, Status and Migrate methods. + ```go // This is an example for the Wunderlist migrator if config.MigrationWunderlistEnable.GetBool() { @@ -51,8 +57,7 @@ if config.MigrationWunderlistEnable.GetBool() { return &wunderlist.Migration{} }, } - m.GET("/wunderlist/auth", wunderlistMigrationHandler.AuthURL) - m.POST("/wunderlist/migrate", wunderlistMigrationHandler.Migrate) + wunderlistMigrationHandler.RegisterRoutes(m) } ``` diff --git a/pkg/cmd/cmd.go b/pkg/cmd/cmd.go index 0e791fa1d3..62d70f8ad0 100644 --- a/pkg/cmd/cmd.go +++ b/pkg/cmd/cmd.go @@ -23,6 +23,7 @@ import ( "code.vikunja.io/api/pkg/mail" "code.vikunja.io/api/pkg/migration" "code.vikunja.io/api/pkg/models" + migrator "code.vikunja.io/api/pkg/modules/migration" "code.vikunja.io/api/pkg/red" "fmt" "github.com/spf13/cobra" @@ -79,6 +80,10 @@ func initialize() { if err != nil { log.Fatal(err.Error()) } + err = migrator.InitDB() + if err != nil { + log.Fatal(err.Error()) + } // Initialize the files handler files.InitFileHandler() diff --git a/pkg/migration/20200120201756.go b/pkg/migration/20200120201756.go new file mode 100644 index 0000000000..1f43b43b7f --- /dev/null +++ b/pkg/migration/20200120201756.go @@ -0,0 +1,47 @@ +// Vikunja is a todo-list application to facilitate your life. +// Copyright 2018-2020 Vikunja and contributors. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package migration + +import ( + "github.com/go-xorm/xorm" + "src.techknowlogick.com/xormigrate" +) + +type status20200120201756 struct { + ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"` + UserID int64 `xorm:"int(11) not null" json:"user_id"` + MigratorName string `xorm:"varchar(255)" json:"migrator_name"` + CreatedUnix int64 `xorm:"created not null"` +} + +func (s status20200120201756) TableName() string { + return "migration_status" +} + +func init() { + migrations = append(migrations, &xormigrate.Migration{ + ID: "20200120201756", + Description: "Add migration status table", + Migrate: func(tx *xorm.Engine) error { + return tx.Sync2(status20200120201756{}) + }, + Rollback: func(tx *xorm.Engine) error { + return dropTableColum(tx, "migration_status", "index") + }, + }) + +} diff --git a/pkg/modules/migration/db.go b/pkg/modules/migration/db.go new file mode 100644 index 0000000000..814ef67713 --- /dev/null +++ b/pkg/modules/migration/db.go @@ -0,0 +1,49 @@ +// Vikunja is a todo-list application to facilitate your life. +// Copyright 2018-2020 Vikunja and contributors. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package migration + +import ( + "code.vikunja.io/api/pkg/config" + "code.vikunja.io/api/pkg/db" + "code.vikunja.io/api/pkg/log" + "github.com/go-xorm/xorm" +) + +var x *xorm.Engine + +// InitDB sets up the database connection to use in this module +func InitDB() (err error) { + x, err = db.CreateDBEngine() + if err != nil { + log.Criticalf("Could not connect to db: %v", err.Error()) + return + } + + // Cache + if config.CacheEnabled.GetBool() && config.CacheType.GetString() == "redis" { + db.RegisterTableStructsForCache(GetTables()) + } + + return nil +} + +// GetTables returns all structs which are also a table. +func GetTables() []interface{} { + return []interface{}{ + &Status{}, + } +} diff --git a/pkg/modules/migration/handler/handler.go b/pkg/modules/migration/handler/handler.go index 4efef3adb9..805e93747c 100644 --- a/pkg/modules/migration/handler/handler.go +++ b/pkg/modules/migration/handler/handler.go @@ -34,6 +34,14 @@ type AuthURL struct { URL string `json:"url"` } +// RegisterRoutes registers all routes for migration +func (mw *MigrationWeb) RegisterRoutes(g *echo.Group) { + ms := mw.MigrationStruct() + g.GET("/"+ms.Name()+"/auth", mw.AuthURL) + g.GET("/"+ms.Name()+"/status", mw.Status) + g.POST("/"+ms.Name()+"/migrate", mw.Migrate) +} + // AuthURL is the web handler to get the auth url func (mw *MigrationWeb) AuthURL(c echo.Context) error { ms := mw.MigrationStruct() @@ -62,5 +70,27 @@ func (mw *MigrationWeb) Migrate(c echo.Context) error { return handler.HandleHTTPError(err, c) } + err = migration.SetMigrationStatus(ms, user) + if err != nil { + return handler.HandleHTTPError(err, c) + } + return c.JSON(http.StatusOK, models.Message{Message: "Everything was migrated successfully."}) } + +// Status returns whether or not a user has already done this migration +func (mw *MigrationWeb) Status(c echo.Context) error { + ms := mw.MigrationStruct() + + user, err := models.GetCurrentUser(c) + if err != nil { + return handler.HandleHTTPError(err, c) + } + + status, err := migration.GetMigrationStatus(ms, user) + if err != nil { + return handler.HandleHTTPError(err, c) + } + + return c.JSON(http.StatusOK, status) +} diff --git a/pkg/modules/migration/migration_status.go b/pkg/modules/migration/migration_status.go new file mode 100644 index 0000000000..4ec8bec883 --- /dev/null +++ b/pkg/modules/migration/migration_status.go @@ -0,0 +1,49 @@ +// Vikunja is a todo-list application to facilitate your life. +// Copyright 2018-2020 Vikunja and contributors. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package migration + +import "code.vikunja.io/api/pkg/models" + +// Status represents this migration status +type Status struct { + ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"` + UserID int64 `xorm:"int(11) not null" json:"-"` + MigratorName string `xorm:"varchar(255)" json:"migrator_name"` + CreatedUnix int64 `xorm:"created not null" json:"time_unix"` +} + +// TableName holds the table name for the migration status table +func (s *Status) TableName() string { + return "migration_status" +} + +// SetMigrationStatus sets the migration status for a user +func SetMigrationStatus(m Migrator, u *models.User) (err error) { + status := &Status{ + UserID: u.ID, + MigratorName: m.Name(), + } + _, err = x.Insert(status) + return +} + +// GetMigrationStatus returns the migration status for a migration and a user +func GetMigrationStatus(m Migrator, u *models.User) (status *Status, err error) { + status = &Status{} + _, err = x.Where("user_id = ? and migrator_name = ?", u.ID, m.Name()).Desc("id").Get(status) + return +} diff --git a/pkg/modules/migration/migrator.go b/pkg/modules/migration/migrator.go index bac7dd7160..8ea27391ae 100644 --- a/pkg/modules/migration/migrator.go +++ b/pkg/modules/migration/migrator.go @@ -28,4 +28,7 @@ type Migrator interface { // The use case for this are Oauth flows, where the server token should remain hidden and not // known to the frontend. AuthURL() string + // Name holds the name of the migration. + // This is used to show the name to users and to keep track of users who already migrated. + Name() string } diff --git a/pkg/modules/migration/wunderlist/wunderlist.go b/pkg/modules/migration/wunderlist/wunderlist.go index 3bdbce3d52..3930146b27 100644 --- a/pkg/modules/migration/wunderlist/wunderlist.go +++ b/pkg/modules/migration/wunderlist/wunderlist.go @@ -480,3 +480,16 @@ func (w *Migration) AuthURL() string { config.MigrationWunderlistRedirectURL.GetString() + "&state=" + utils.MakeRandomString(32) } + +// Name is used to get the name of the wunderlist migration +// @Summary Get migration status +// @Description Returns if the current user already did the migation or not. This is useful to show a confirmation message in the frontend if the user is trying to do the same migration again. +// @tags migration +// @Produce json +// @Security JWTKeyAuth +// @Success 200 {object} migration.Status "The migration status" +// @Failure 500 {object} models.Message "Internal server error" +// @Router /migration/wunderlist/status [get] +func (w *Migration) Name() string { + return "wunderlist" +} diff --git a/pkg/routes/routes.go b/pkg/routes/routes.go index 68c1d8c3c6..fdb6705df2 100644 --- a/pkg/routes/routes.go +++ b/pkg/routes/routes.go @@ -386,8 +386,7 @@ func registerAPIRoutes(a *echo.Group) { return &wunderlist.Migration{} }, } - m.GET("/wunderlist/auth", wunderlistMigrationHandler.AuthURL) - m.POST("/wunderlist/migrate", wunderlistMigrationHandler.Migrate) + wunderlistMigrationHandler.RegisterRoutes(m) } } diff --git a/pkg/swagger/docs.go b/pkg/swagger/docs.go index 5b10b2ed6d..b7dc4f9594 100644 --- a/pkg/swagger/docs.go +++ b/pkg/swagger/docs.go @@ -1,6 +1,6 @@ // GENERATED BY THE COMMAND ABOVE; DO NOT EDIT // This file was generated by swaggo/swag at -// 2020-01-19 16:18:04.294790395 +0100 CET m=+0.176548843 +// 2020-01-20 20:27:54.414478978 +0100 CET m=+0.171577588 package swagger @@ -1692,6 +1692,37 @@ var doc = `{ } } }, + "/migration/wunderlist/status": { + "get": { + "security": [ + { + "JWTKeyAuth": [] + } + ], + "description": "Returns if the current user already did the migation or not. This is useful to show a confirmation message in the frontend if the user is trying to do the same migration again.", + "produces": [ + "application/json" + ], + "tags": [ + "migration" + ], + "summary": "Get migration status", + "responses": { + "200": { + "description": "The migration status", + "schema": { + "$ref": "#/definitions/migration.Status" + } + }, + "500": { + "description": "Internal server error", + "schema": { + "$ref": "#/definitions/models.Message" + } + } + } + } + }, "/namespace/{id}": { "post": { "security": [ @@ -4548,6 +4579,20 @@ var doc = `{ } } }, + "migration.Status": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "migrator_name": { + "type": "string" + }, + "time_unix": { + "type": "integer" + } + } + }, "models.APIUserPassword": { "type": "object", "properties": { @@ -5596,6 +5641,12 @@ var doc = `{ "v1.vikunjaInfos": { "type": "object", "properties": { + "available_migrators": { + "type": "array", + "items": { + "type": "string" + } + }, "frontend_url": { "type": "string" }, @@ -5608,6 +5659,9 @@ var doc = `{ "motd": { "type": "string" }, + "registration_enabled": { + "type": "boolean" + }, "version": { "type": "string" } diff --git a/pkg/swagger/swagger.json b/pkg/swagger/swagger.json index 49d341ef90..295522af8f 100644 --- a/pkg/swagger/swagger.json +++ b/pkg/swagger/swagger.json @@ -1674,6 +1674,37 @@ } } }, + "/migration/wunderlist/status": { + "get": { + "security": [ + { + "JWTKeyAuth": [] + } + ], + "description": "Returns if the current user already did the migation or not. This is useful to show a confirmation message in the frontend if the user is trying to do the same migration again.", + "produces": [ + "application/json" + ], + "tags": [ + "migration" + ], + "summary": "Get migration status", + "responses": { + "200": { + "description": "The migration status", + "schema": { + "$ref": "#/definitions/migration.Status" + } + }, + "500": { + "description": "Internal server error", + "schema": { + "$ref": "#/definitions/models.Message" + } + } + } + } + }, "/namespace/{id}": { "post": { "security": [ @@ -4529,6 +4560,20 @@ } } }, + "migration.Status": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "migrator_name": { + "type": "string" + }, + "time_unix": { + "type": "integer" + } + } + }, "models.APIUserPassword": { "type": "object", "properties": { @@ -5577,6 +5622,12 @@ "v1.vikunjaInfos": { "type": "object", "properties": { + "available_migrators": { + "type": "array", + "items": { + "type": "string" + } + }, "frontend_url": { "type": "string" }, @@ -5589,6 +5640,9 @@ "motd": { "type": "string" }, + "registration_enabled": { + "type": "boolean" + }, "version": { "type": "string" } diff --git a/pkg/swagger/swagger.yaml b/pkg/swagger/swagger.yaml index 6f59419c7c..5e0206d3f1 100644 --- a/pkg/swagger/swagger.yaml +++ b/pkg/swagger/swagger.yaml @@ -18,6 +18,15 @@ definitions: url: type: string type: object + migration.Status: + properties: + id: + type: integer + migrator_name: + type: string + time_unix: + type: integer + type: object models.APIUserPassword: properties: email: @@ -854,6 +863,10 @@ definitions: type: object v1.vikunjaInfos: properties: + available_migrators: + items: + type: string + type: array frontend_url: type: string link_sharing_enabled: @@ -862,6 +875,8 @@ definitions: type: string motd: type: string + registration_enabled: + type: boolean version: type: string type: object @@ -1997,6 +2012,27 @@ paths: summary: Migrate all lists, tasks etc. from wunderlist tags: - migration + /migration/wunderlist/status: + get: + description: Returns if the current user already did the migation or not. This + is useful to show a confirmation message in the frontend if the user is trying + to do the same migration again. + produces: + - application/json + responses: + "200": + description: The migration status + schema: + $ref: '#/definitions/migration.Status' + "500": + description: Internal server error + schema: + $ref: '#/definitions/models.Message' + security: + - JWTKeyAuth: [] + summary: Get migration status + tags: + - migration /namespace/{id}: post: consumes: