How to Connect Ent to PlanetScale in a Go Application
The MySQL serverless database platform
PlanetScale is a serverless database platform for MySQL. Ent is an ORM framework with a 100% statically typed API. This post will walk you through how to connect Ent to PlanetScale in a Go application.
Pre-requisites
Before beginning, go to the PlanetScale and sign up for Free.
Example Repository
Here is the final codebase on GitHub.
Dependencies
Required dependencies are as follows:
- Go v1.18
- ent/ent
Implementation Overview
The steps of the implementation are as follows:
- Set up ent
- Set up PlanetScale
Set up PlanetScale
After signing up, go to the dashboard and create a new database.
And fill in your database name.
After creating, click a New branch.
Create a branch called staging
.
Now that the database and the branch have been created, our schema and data can be imported.
Set up ent
In order to import our schema to ScalePlanet, let’s set up ent next.
First, introduce the ent package to our application and configure the database.
Let us install it:
$ go get -d entgo.io/ent/cmd/ent
And create a user schema.
$ ent init User
Then, the generated directory should look like this:
ent
├── generate.go
└── schema
└── user.go
Open up the ent/schema/user.go
and add some fields as follows:
And run the generator:
$ go generate ./ent
Database Migration
Now that we’re ready to migrate our schema to PlanetScale. But first, add the viper package for easy access to a shared configuration.
$ go get github.com/spf13/viper
And create the config/config.go
like so:
This will read a YAML file and allows you to use its values entirely across the application. The MySQL configurations should be included in this file.
To access PlanetScale’s database, we need to get their configuration. Go to Settings
and Passwords
and create a new password:
Create a password called staging and copy the connection strings.
Go back to our code and make a config/config.staging.yml
like so:
create the cmd/migration/main.go
and write this:
And add datastore/datastore.go
:
Run the migration by the command:
$ APP_ENV=staging go run ./cmd/migration/main.go
After that, the schema in the staging branch should look like this:
Seeding
Seed data can be created with the Ent package.
First, enable the template feature for codegen.
Create a new file named, ent/entc.go
and add this code:
And then, open up ent/generate.go
file and change the code to:
And add ent/template/mutation_input.tmpl
:
To execute the SQL in Ent, create ent/template/external.tmpl
:
Run codegen:
go generate ./ent
This generates a struct for input and update of each schema. In this case, CreateUserInput
and UpdateUserInput
are generated in the file.
Next, create cmd/seed/user.go
and write this:
This function imports data into the user table using bulk inserts.
Create a cmd/seed/seed.go
and call the User function here:
And create cmd/seed/main.go
:
Run the script:
$ APP_ENV=staging go run ./cmd/seed/main.go
Seed data has been created and the console displays the inserted data as follows:
Promote a branch to production
Now that we’ve created the staging branch and defined our schema. The staging branch is now ready for production, migrate it to the main branch.
Go to the overview section and click the Promote a branch to production
in the main branch.
And switch to the staging branch, and create a deploy request to the main branch.
If the change is okay, hit the Deploy changes.
In the main branch, the schema has been created like so:
Conclusion
We’ve covered how to deploy a database schema through Ent and PlanetScale. The production branch should be changed through deploy requests in PlanetScale so that this workflow allows us to make it safe to deploy schema and update database tables without locking or causing downtime.