A few months ago the Ent project announced
the Schema Import Initiative, its goal is to help support
many use cases for generating Ent schemas from external resources. Today, I'm happy to share a project I’ve been working
on: entimport - an importent (pun intended) command line tool designed to create Ent schemas from existing SQL
databases. This is a feature that has been requested by the community for some time, so I hope many people find it
useful. It can help ease the transition of an existing setup from another language or ORM to Ent. It can also help with
use cases where you would like to access the same data from different platforms (such as to automatically sync between
The first version supports both MySQL and PostgreSQL databases, with some limitations described below. Support for other relational databases such as SQLite is in the works.
To give you an idea of how
entimport works, I want to share a quick example of end to end usage with a MySQL database.
On a high-level, this is what we’re going to do:
- Create a Database and Schema - we want to show how
entimportcan generate an Ent schema for an existing database. We will first create a database, then define some tables in it that we can import into Ent.
- Initialize an Ent Project - we will use the Ent CLI to create the needed directory structure and an Ent schema generation script.
entimportagainst our demo database - next, we will import the database schema that we’ve created into our Ent project.
- Explain how to use Ent with our generated schemas.
Let's get started.
We’re going to start by creating a database. The way I prefer to do it is to use
a Docker container. We will use a
docker-compose which will automatically pass
all needed parameters to the MySQL container.
Start the project in a new directory called
entimport-example. Create a file named
docker-compose.yaml and paste the
following content inside:
This file contains the service configuration for a MySQL docker container. Run it with the following command:
Next, we will create a simple schema. For this example we will use a relation between two entities:
Connect to the database using MySQL shell, you can do it with the following command:
Make sure you run it from the root project directory
Let's validate that we've created the tables mentioned above, in your MySQL shell, run:
We should see two tables:
Now that we've created our database, and a baseline schema to demonstrate our example, we need to create a Go project with Ent. In this phase I will explain how to do it. Since eventually we would like to use our imported schema, we need to create the Ent directory structure.
Initialize a new Go project inside a directory called
Run Ent Init:
The project should look like this:
OK, now the fun begins! We are finally ready to install
entimport and see it in action.
Let’s start by running
entimport will be downloaded and the command will print:
We are now ready to import our MySQL schema to Ent!
We will do it with the following command:
This command will import all tables in our schema, you can also limit to specific tables using
Like many unix tools,
entimport doesn't print anything on a successful run. To verify that it ran properly, we will
check the file system, and more specifically
Let’s see what this gives us - remember that we had two schemas: the
users schema and the
cars schema with a one to
many relationship. Let’s see how
entimportsuccessfully created entities and their relation!
So far looks good, now let’s actually try them out. First we must generate the Ent schema. We do it because Ent is a schema first ORM that generates Go code for interacting with different databases.
To run the Ent code generation:
Let's see our
Let’s run a quick example to verify that our schema works:
Create a file named
example.go in the root of the project, with the following content:
This part of the example can be found here
Let's try to add a user, write the following code at the end of the file:
This should output:
# User created: User(id=1, age=33, name=Zeev, last_name=Manilovich)
Let's check with the database if the user was really added
Great! now let's play a little more with Ent and add some relations, add the following code at the end of
make sure you add
"entimport-example/ent/user"to the import() declaration
This part of the example can be found here
go run example.go.
After Running the code above, the database should hold a user with 2 cars in a O2M relation.
Since we want to keep the database in sync, we want
entimport to be able to change the schema after the database was
changed. Let's see how it works.
Run the following SQL code to add a
phone column with a
unique index to the
The table should look like this:
Now let's run
entimport again to get the latest schema from our database:
We can see that the
user.go file was changed:
Now we can run
go generate ./ent again and use the new schema to add a
phone to the User entity.
As mentioned above this initial version supports MySQL and PostgreSQL databases.
It also supports all types of SQL relations. I have plans to further upgrade the tool and add features such as missing PostgreSQL fields, default values, and more.
In this post, I presented
entimport, a tool that was anticipated and requested many times by the Ent community. I
showed an example of how to use it with Ent. This tool is another addition to Ent schema import tools, which are
designed to make the integration of ent even easier. For discussion and
support, open an issue. The full example can be
found in here. I hope you found this blog post useful!