Unison: A Datastore migration library for Google Go
Unison is a lightweight library that allows you to version and manage your Datastore migrations. In this blog article we will have a look at how you can install the Unison command line tool and get started migrating entities to Datastore.
Note: At the time of writing this article the library was not compatible with Google Firestore.
Before we begin, the article assumes that you have Google Go installed on your computer. In case you do not have it installed, click here to grab an installer for your operating system.
The go-unison project comprises two packages, viz, unison and unisoner. The first is the library that you will use to migrate your scripts, while the latter is a command line tool that lets you generate new migration scripts.
Let’s start with creating a migration script that migrates some delicious fruits to Datastore. Run the following commands to set up your project.
|
|
The ent/fruit.go
file should contain the serializable Datastore entity structure as follows.
|
|
Now let’s go ahead and install the unisoner command line tool and the unison library.
|
|
We are now ready to create new migration files. When you execute the unisoner command it creates the following to bootstrap unison.
- Migration package: A new package is created in the present working directory. By default the name of this package is migration. But this can be overridden by either setting the environment variable
unison_migration_package
or by passing--migration_package <pkg_name>
while executing the command. Note: if values are received from both, environment variable and the command line param, the latter will be given precedence. - Unison migrations type: A new type gets created within the above package. It is on this type new migrations will be defined. Finally an instance of this type needs to be passed to the unison library for it to work its magic.
Now with the details out of the way let’s create our first migration script.
|
|
Let’s examine the files that were generated by unison.
|
|
The file unison.go contains the migrations type that has been explained above. The other file is where the migration script goes.
Open gcp/fruits.go
in a text editor and the contents should look like the following.
|
|
Each migration file we generate should ideally contain one method defined on UnisonMigrations. These method names are based on the following naming convention Apply<Timestamp>
. It is based on this timestamp that the unison library sorts the order of migrations. A migration once successfully commited will not be run again. Only migrations that have a timestamp greater than the last successully executed migration will be executed.
Since I love eating apples and mangoes, I will migrate these two fruits to Datastore. Let’s make the following changes to gcp/fruits.go
(you can use your favorites here 😉).
|
|
Great! Our first migration script is ready. Now all that remains is to use the unison library to migrate this script. For that let’s make changes to main.go
.
|
|
We are a step away from migrating our first entities to Datastore. Before running the code make sure you have Google application credentials set. More on it here.
After you have set the variable you will be ready to run your shiny new migration script.
|
|
Yay ! We are done migrating our first migration script. Head over to the Google Cloud console to verify the changes.
Note that the migration records themselves are stored as entites of kind UnisonMigrationMeta
.
Now with the first migration script done, let’s create a new script to migrate a few more fruits.
|
|
Edit the newly created gcp/more_fruits.go
file to migrate a few more fruits.
|
|
Running go run .
again should migrate only the new migration script.
|
|
Please note: The project is in it’s early stages. Contributions in terms of bug reports, documentation, and testing are welcome. Do not hesitate to report issues or to raise pull requests.