2020-01-19 17:52:16 +01:00
---
date: "2020-01-19:16:00+02:00"
title: "Migrations"
draft: false
type: "doc"
menu:
sidebar:
parent: "development"
---
# Writing a migrator for Vikunja
It is possible to migrate data from other to-do services to Vikunja.
To make this easier, we have put together a few helpers which are documented on this page.
In general, each migrator implements a migrator interface which is then called from a client.
2021-09-04 21:26:31 +02:00
The interface makes it possible to use helper methods which handle http and focus only on the implementation of the migrator itself.
There are two ways of migrating data from another service:
1. Through the auth-based flow where the user gives you access to their data at the third-party service through an
oauth flow. You can then call the service's api on behalf of your user to get all the data.
The Todoist, Trello and Microsoft To-Do Migrators use this pattern.
2. A file migration where the user uploads a file obtained from some third-party service. In your migrator, you need
to parse the file and create the lists, tasks etc.
The Vikunja File Import uses this pattern.
To differentiate the two, there are two different interfaces you must implement.
2020-01-19 17:52:16 +01:00
2020-09-03 17:34:44 +02:00
{{< table_of_contents > }}
## Structure
2020-01-19 17:52:16 +01:00
All migrator implementations live in their own package in `pkg/modules/migration/<name-of-the-service>` .
When creating a new migrator, you should place all related code inside that module.
2021-09-04 21:26:31 +02:00
## Migrator Interface
2020-01-19 17:52:16 +01:00
The migrator interface is defined as follows:
```go
// Migrator is the basic migrator interface which is shared among all migrators
type Migrator interface {
2021-09-04 21:26:31 +02:00
// 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
2020-01-19 17:52:16 +01:00
// Migrate is the interface used to migrate a user's tasks from another platform to vikunja.
// The user object is the user who's tasks will be migrated.
Migrate(user *models.User) error
// AuthURL returns a url for clients to authenticate against.
// The use case for this are Oauth flows, where the server token should remain hidden and not
// known to the frontend.
AuthURL() string
2021-09-04 21:26:31 +02:00
}
```
## File Migrator Interface
```go
// FileMigrator handles importing Vikunja data from a file. The implementation of it determines the format.
type FileMigrator interface {
2020-01-20 20:48:46 +01:00
// 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
2021-09-04 21:26:31 +02:00
// Migrate is the interface used to migrate a user's tasks, list and other things from a file to vikunja.
// The user object is the user who's tasks will be migrated.
Migrate(user *user.User, file io.ReaderAt, size int64) error
2020-01-19 17:52:16 +01:00
}
```
2020-09-03 17:34:44 +02:00
## Defining http routes
2020-01-19 17:52:16 +01:00
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 ):
2020-01-20 20:48:46 +01:00
The `RegisterRoutes(m)` method registers all routes with the scheme `/[MigratorName]/(auth|migrate|status)` for the
authUrl, Status and Migrate methods.
2020-01-19 17:52:16 +01:00
```go
// This is an example for the Wunderlist migrator
if config.MigrationWunderlistEnable.GetBool() {
2021-09-04 21:26:31 +02:00
wunderlistMigrationHandler := & migrationHandler.MigrationWeb{
2020-01-19 17:52:16 +01:00
MigrationStruct: func() migration.Migrator {
return & wunderlist.Migration{}
},
}
2021-09-04 21:26:31 +02:00
wunderlistMigrationHandler.RegisterRoutes(m)
}
```
And for the file migrator:
```go
vikunjaFileMigrationHandler := & migrationHandler.FileMigratorWeb{
MigrationStruct: func() migration.FileMigrator {
return & vikunja_file.FileMigrator{}
},
2020-01-19 17:52:16 +01:00
}
2021-09-04 21:26:31 +02:00
vikunjaFileMigrationHandler.RegisterRoutes(m)
2020-01-19 17:52:16 +01:00
```
2021-07-14 00:25:12 +02:00
You should also document the routes with [swagger annotations ]({{< ref "swagger-docs.md" >}} ).
2020-01-19 17:52:16 +01:00
2020-09-03 17:34:44 +02:00
## Insertion helper method
2020-01-19 17:52:16 +01:00
There is a method available in the `migration` package which takes a fully nested Vikunja structure and creates it with all relations.
This means you start by adding a namespace, then add lists inside of that namespace, then tasks in the lists and so on.
2021-09-04 21:26:31 +02:00
The root structure must be present as `[]*models.NamespaceWithListsAndTasks` . It allows to represent all of Vikunja's
hierachie as a single data structure.
2020-01-19 17:52:16 +01:00
Then call the method like so:
```go
fullVikunjaHierachie, err := convertWunderlistToVikunja(wContent)
if err != nil {
return
}
err = migration.InsertFromStructure(fullVikunjaHierachie, user)
```
2020-09-03 17:34:44 +02:00
## Configuration
2020-01-19 17:52:16 +01:00
2021-09-04 21:26:31 +02:00
If your migrator is an oauth-based one, you should add at least an option to enable or disable it.
2020-01-19 17:52:16 +01:00
Chances are, you'll need some more options for things like client ID and secret
(if the other service uses oAuth as an authentication flow).
The easiest way to implement an on/off switch is to check whether your migration service is enabled or not when
2021-09-04 21:26:31 +02:00
registering the routes, and then simply don't registering the routes in case it is disabled.
File based migrators can always be enabled.
2020-01-19 17:52:16 +01:00
2020-09-03 17:34:44 +02:00
### Making the migrator public in `/info`
2020-01-19 17:52:16 +01:00
You should make your migrator available in the `/info` endpoint so that frontends can display options to enable them or not.
2021-09-04 21:26:31 +02:00
To do this, add an entry to the `AvailableMigrators` field in `pkg/routes/api/v1/info.go` .