entimport is a tool for creating Ent schemas from existing SQL databases.

Related tags

entimport
Overview

entimport

entimport is a tool for creating Ent schemas from existing SQL databases. Currently, MySQL and PostgreSQL are supported. The tool can import to ent schema any number of tables, including relations between them.

Installation

Setup A Go Environment

If your project directory is outside GOPATH or you are not familiar with GOPATH, setup a Go module project as follows:

go mod init <project>

Install ent

go install entgo.io/ent/cmd/ent

After installing ent codegen tool, you should have it in your PATH. If you don't find it your path, you can also run: go run entgo.io/ent/cmd/ent

Create Schema Directory

Go to the root directory of your project, and run:

ent init

The command above will create /ent/schema/ directory and the file inside /ent/generate.go

Importing a Schema

Installing and running entimport

go run ariga.io/entimport/cmd/entimport
  • For example, importing a MySQL schema with users table:
go run ariga.io/entimport/cmd/entimport -dialect mysql -dsn "root:[email protected](localhost:3308)/test" -tables "users"

The command above will write a valid ent schema into the directory specified (or the default ./ent/schema):

.
├── generate.go
└── schema
    └── user.go

1 directory, 2 files

Code Generation:

In order to generate ent files from the produced schemas, run:

go run -mod=mod entgo.io/ent/cmd/ent generate ./schema

# OR `ent` init:

go generate ./ent

If you are not yet familiar with ent, you can also follow the quick start guide.

Usage

entimport  -h
Usage of ./entimport:
  -dialect string
        database dialect (default "mysql")
  -dsn string
        data source name (connection information)
  -schema-path string
        output path for ent schema (default "./ent/schema")
  -tables value
        comma-separated list of tables to inspect (all if empty)

Examples:

  1. Import ent schema from Postgres database

Note: add search_path=foo if you use non public schema.

go run ariga.io/entimport/cmd/entimport -dialect postgres -dsn "host=localhost port=5434 user=postgres dbname=test password=pass sslmode=disable" 
  1. Import ent schema from MySQL database
go run ariga.io/entimport/cmd/entimport -dialect mysql -dsn "root:[email protected](localhost:3308)/test"
  1. Import only specific tables:

Note: When importing specific tables:
if the table is a join table, you must also provide referenced tables.
If the table is only one part of a relation, the other part won't be imported unless specified.
If the -tables flags is omitted all tables in current database schema will be imported

go run ariga.io/entimport/cmd/entimport -dialect postgres -dsn "..." -tables "users,user_friends" 
  1. Import to another directory:
go run ariga.io/entimport/cmd/entimport -dialect postgres -dsn "..." -schema-path "/some/path/here"

Future Work

  • Index support (currently Unique index is supported).
  • Support for all data types (for example uuid in Postgres).
  • Support for Default value in columns.
  • Support for editing schema both manually and automatically (real upsert and not only overwrite)
  • Postgres special types: postgres.NetworkType, postgres.BitType, *schema.SpatialType, postgres.CurrencyType, postgres.XMLType, postgres.ArrayType, postgres.UserDefinedType.

Known Caveats:

  • Schema files are overwritten by new calls to entimport.
  • There is no difference in DB schema between O2O Bidirectional and O2O Same Type - both will result in the same ent schema.
  • There is no difference in DB schema between M2M Bidirectional and M2M Same Type - both will result in the same
  • ent schema.
  • In recursive relations the edge names will be prefixed with child_ & parent_.
  • For example: users with M2M relation to itself will result in:
func (User) Edges() []ent.Edge {
return []ent.Edge{edge.To("child_users", User.Type), edge.From("parent_users", User.Type)}
}

Feedback & Support

For discussion and support, open an issue or join our channel in the gophers Slack.

Issues
Owner
ariga
ariga
Dbmate is a database migration tool, to keep your database schema in sync across multiple developers and your production servers.

Dbmate is a database migration tool, to keep your database schema in sync across multiple developers and your production servers. It is a stand

Adrian Macneil 2.1k Oct 23, 2021
Apache AVRO for go

avro The purpose of this package is to facilitate use of AVRO with go strong typing. Features github.com/khezen/avro Marshal/Unmarshal AVRO schema git

Guillaume Simonneau 25 Aug 2, 2021
Goose database migration tool - fork of https://bitbucket.org/liamstask/goose

goose Goose is a database migration tool. Manage your database schema by creating incremental SQL changes or Go functions. Goals of this fork github.c

Pressly Inc. 1.9k Oct 16, 2021
Dead simple Go database migration library.

migrator Dead simple Go database migration library. Features Simple code Usage as a library, embeddable and extensible on your behalf Support of any d

David Lobe 116 Jul 26, 2021
Django style fixtures for Golang's excellent built-in database/sql library.

go-fixtures Django style fixtures for Golang's excellent built-in database/sql library. Currently only YAML fixtures are supported. There are two rese

Richard Knop 26 Apr 8, 2021
Simple migration tool for MySQL

prrn Simple migration tool for MySQL This is a CLI that helps you create a DB migration file. There is no need to write up and down files from scratch

kamijin_fanta 6 Jul 27, 2021
A simple database migration tool using an sql.DB connection and fs.FS for the migration source

A simple database migration tool using an sql.DB connection and fs.FS for the migration source. It has no non-test dependencies.

 maragu 30 Sep 28, 2021
Minimalistic database migration helper for Gorm ORM

Gormigrate Gormigrate is a minimalistic migration helper for Gorm. Gorm already has useful migrate functions, just misses proper schema versioning and

null 685 Oct 22, 2021
Database migrations. CLI and Golang library.

migrate Database migrations written in Go. Use as CLI or import as library. Migrate reads migrations from sources and applies them in correct order to

null 7.4k Oct 23, 2021
Database migrations. CLI and Golang library.

Database migrations written in Go. Use as CLI or import as library.

null 27 May 30, 2021
Simple Migration Tool - written in Go

Pravasan Simple Migration tool intend to be used for any languages, for any db. Please feel free to criticize, comment, etc. Currently this is working

null 24 Mar 22, 2019
mini tools handling migrasion database from cli

mini tools handling migrasion database from cli

null 0 Sep 25, 2021
Database schema evolution library for Go

Try browsing the code on Sourcegraph! Darwin Database schema evolution library for Go Example package main import ( "database/sql" "log" "github.

Guiabolso 123 Oct 2, 2021
Schema management CLI for MySQL

Skeema is a tool for managing MySQL tables and schema changes in a declarative fashion using pure SQL. It provides a CLI tool allowing you to: Export

Skeema 940 Oct 15, 2021
A Go package to help write migrations with go-pg/pg.

go-pg-migrations A Go package to help write migrations with go-pg/pg. Usage Installation Because go-pg now has Go modules support, go-pg-migrations al

Robin Joseph 80 Oct 17, 2021