main.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //zlint:file-ignore U1000 this is a variable source demo
  2. package main
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "log"
  8. "os"
  9. "time"
  10. "entgo.io/ent/dialect/sql"
  11. _ "github.com/go-sql-driver/mysql"
  12. // _ "github.com/mattn/go-sqlite3"
  13. "code.osinet.fr/fgm/go__ent_demo/ent"
  14. )
  15. const (
  16. name = "a8m"
  17. dbPath = "ent.db"
  18. )
  19. func userDemo(ctx context.Context, client *ent.Client) {
  20. u, err := CreateUser(ctx, client)
  21. if err != nil {
  22. log.Fatalf("failed creating user: %v", err)
  23. }
  24. fmt.Println("created: ", u)
  25. u, err = QueryUser(ctx, client)
  26. if err != nil {
  27. log.Fatalf("failed querying user: %v", err)
  28. }
  29. fmt.Println("found: ", u)
  30. }
  31. func edgeDemo(ctx context.Context, client *ent.Client) {
  32. u, err := CreateCars(ctx, client)
  33. if err != nil {
  34. log.Fatal(err)
  35. }
  36. fmt.Println("owner: ", u)
  37. }
  38. func setupSQLite() (*ent.Client, error) {
  39. if err := os.Remove(dbPath); err != nil && !errors.Is(err, os.ErrNotExist) {
  40. return nil, fmt.Errorf("failed cleaning up old DB: %w", err)
  41. }
  42. // client, err := ent.Open("sqlite3", "file:ent.db?mode=memory&cache=shared&_fk=1")
  43. client, err := ent.Open("sqlite3", "file:"+dbPath+"?cache=shared&_fk=1")
  44. if err != nil {
  45. return nil, fmt.Errorf("failed opening connection to SQLite: %w", err)
  46. }
  47. return client, err
  48. }
  49. func setupMariaDB() (*ent.Client, error) {
  50. user := os.Getenv("ENTDEMO_USER")
  51. pass := os.Getenv("ENTDEMO_PASS")
  52. drv, err := sql.Open("mysql", fmt.Sprintf("%s:%s@/go_entdemo?parseTime=true", user, pass))
  53. if err != nil {
  54. return nil, fmt.Errorf("failed opening connection to DB server: %w", err)
  55. }
  56. db := drv.DB()
  57. db.SetMaxIdleConns(10)
  58. db.SetMaxOpenConns(100)
  59. db.SetConnMaxLifetime(time.Hour)
  60. db.Exec("SET foreign_key_checks = 0")
  61. for _, tbl := range []string{
  62. "cars",
  63. "group_users",
  64. "groups",
  65. "user_following",
  66. "user_friends",
  67. "users",
  68. } {
  69. _, err := db.Exec("DROP TABLE IF EXISTS " + tbl)
  70. if err != nil {
  71. return nil, fmt.Errorf("dropping table %s: %w", tbl, err)
  72. }
  73. }
  74. db.Exec("SET foreign_key_checks = 1")
  75. return ent.NewClient(ent.Driver(drv)), nil
  76. }
  77. func setup(ctx context.Context) (client *ent.Client, err error) {
  78. // if client, err = setupSQLite(); err != nil {
  79. // return nil, err
  80. // }
  81. if client, err = setupMariaDB(); err != nil {
  82. return nil, err
  83. }
  84. // Run the auto migration tool.
  85. if err := client.Debug().Schema.Create(ctx); // schema.WithGlobalUniqueID(true),
  86. err != nil {
  87. return nil, fmt.Errorf("failed creating schema resources: %w", err)
  88. }
  89. return client, nil
  90. }
  91. func main() {
  92. ctx := context.Background()
  93. client, err := setup(ctx)
  94. if err != nil {
  95. log.Fatalln(err)
  96. }
  97. defer client.Close()
  98. // userDemo(ctx, client)
  99. // edgeDemo(ctx, client)
  100. if err := CreateGraph(ctx, client); err != nil {
  101. log.Fatalf("failed creating graph: %c", err)
  102. }
  103. // if err := QueryGithub(ctx, client); err != nil {
  104. // log.Fatalf("failed querying github cars: %v", err)
  105. // }
  106. if err := QueryArielCars(ctx, client); err != nil {
  107. log.Fatalf("failed querying Ariel's cars: %v", err)
  108. }
  109. // if err := QueryGroupWithUsers(ctx, client); err != nil {
  110. // log.Fatalf("failed querying groups with users: %v", err)
  111. // }
  112. }