123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- //zlint:file-ignore U1000 this is a variable source demo
- package main
- import (
- "context"
- "errors"
- "fmt"
- "log"
- "os"
- "time"
- "entgo.io/ent/dialect/sql"
- _ "github.com/go-sql-driver/mysql"
- // _ "github.com/mattn/go-sqlite3"
- "code.osinet.fr/fgm/go__ent_demo/ent"
- )
- const (
- name = "a8m"
- dbPath = "ent.db"
- )
- func userDemo(ctx context.Context, client *ent.Client) {
- u, err := CreateUser(ctx, client)
- if err != nil {
- log.Fatalf("failed creating user: %v", err)
- }
- fmt.Println("created: ", u)
- u, err = QueryUser(ctx, client)
- if err != nil {
- log.Fatalf("failed querying user: %v", err)
- }
- fmt.Println("found: ", u)
- }
- func edgeDemo(ctx context.Context, client *ent.Client) {
- u, err := CreateCars(ctx, client)
- if err != nil {
- log.Fatal(err)
- }
- fmt.Println("owner: ", u)
- }
- func setupSQLite() (*ent.Client, error) {
- if err := os.Remove(dbPath); err != nil && !errors.Is(err, os.ErrNotExist) {
- return nil, fmt.Errorf("failed cleaning up old DB: %w", err)
- }
- // client, err := ent.Open("sqlite3", "file:ent.db?mode=memory&cache=shared&_fk=1")
- client, err := ent.Open("sqlite3", "file:"+dbPath+"?cache=shared&_fk=1")
- if err != nil {
- return nil, fmt.Errorf("failed opening connection to SQLite: %w", err)
- }
- return client, err
- }
- func setupMariaDB() (*ent.Client, error) {
- user := os.Getenv("ENTDEMO_USER")
- pass := os.Getenv("ENTDEMO_PASS")
- drv, err := sql.Open("mysql", fmt.Sprintf("%s:%s@/go_entdemo?parseTime=true", user, pass))
- if err != nil {
- return nil, fmt.Errorf("failed opening connection to DB server: %w", err)
- }
- db := drv.DB()
- db.SetMaxIdleConns(10)
- db.SetMaxOpenConns(100)
- db.SetConnMaxLifetime(time.Hour)
- db.Exec("SET foreign_key_checks = 0")
- for _, tbl := range []string{
- "cars",
- "group_users",
- "groups",
- "user_following",
- "user_friends",
- "users",
- } {
- _, err := db.Exec("DROP TABLE IF EXISTS " + tbl)
- if err != nil {
- return nil, fmt.Errorf("dropping table %s: %w", tbl, err)
- }
- }
- db.Exec("SET foreign_key_checks = 1")
- return ent.NewClient(ent.Driver(drv)), nil
- }
- func setup(ctx context.Context) (client *ent.Client, err error) {
- // if client, err = setupSQLite(); err != nil {
- // return nil, err
- // }
- if client, err = setupMariaDB(); err != nil {
- return nil, err
- }
- // Run the auto migration tool.
- if err := client.Debug().Schema.Create(ctx); // schema.WithGlobalUniqueID(true),
- err != nil {
- return nil, fmt.Errorf("failed creating schema resources: %w", err)
- }
- return client, nil
- }
- func main() {
- ctx := context.Background()
- client, err := setup(ctx)
- if err != nil {
- log.Fatalln(err)
- }
- defer client.Close()
- // userDemo(ctx, client)
- // edgeDemo(ctx, client)
- if err := CreateGraph(ctx, client); err != nil {
- log.Fatalf("failed creating graph: %c", err)
- }
- // if err := QueryGithub(ctx, client); err != nil {
- // log.Fatalf("failed querying github cars: %v", err)
- // }
- if err := QueryArielCars(ctx, client); err != nil {
- log.Fatalf("failed querying Ariel's cars: %v", err)
- }
- // if err := QueryGroupWithUsers(ctx, client); err != nil {
- // log.Fatalf("failed querying groups with users: %v", err)
- // }
- }
|