From fc6a9d91fcc0d6874c857421f9179e5f8f33c504 Mon Sep 17 00:00:00 2001 From: Qin Guan Date: Thu, 11 May 2023 23:38:20 +0800 Subject: [PATCH] feat: connecting to db --- .gitignore | 1 + cmd/main.go | 17 +++++++++++++++++ config/config.yaml.example | 6 ++---- go.mod | 1 + go.sum | 1 + internal/config/config.go | 10 ++++------ 6 files changed, 26 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 1b71fe8..a68a3bf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea .vscode config.yaml +*.db diff --git a/cmd/main.go b/cmd/main.go index c9d7ce4..92e8bff 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,10 +1,15 @@ package main import ( + "context" + "fmt" "log" "github.com/np-inprove/server/internal/config" + "github.com/np-inprove/server/internal/ent" "github.com/np-inprove/server/internal/logger" + + _ "github.com/mattn/go-sqlite3" ) func main() { @@ -25,4 +30,16 @@ func main() { } appLogger.Info("Hello, world!") + + client, err := ent.Open(cfg.Database.DriverName, cfg.Database.DataSourceName) + if err != nil { + appLogger.Fatal(fmt.Sprintf("failed opening connection to sqlite: %v", err)) + } + defer func(client *ent.Client) { + _ = client.Close() + }(client) + + if err := client.Schema.Create(context.Background()); err != nil { + appLogger.Fatal(fmt.Sprintf("failed creating schema resources: %v", err)) + } } diff --git a/config/config.yaml.example b/config/config.yaml.example index 93f059e..36892c1 100644 --- a/config/config.yaml.example +++ b/config/config.yaml.example @@ -3,10 +3,8 @@ http: port: 5000 database: - host: - name: - username: - password: + drivername: sqlite3 + datasourcename: file:test.db?_fk=1&cache=shared&mode=memory app: production: false diff --git a/go.mod b/go.mod index 8a4d6cc..631afb8 100644 --- a/go.mod +++ b/go.mod @@ -35,6 +35,7 @@ require ( ) require ( + github.com/mattn/go-sqlite3 v1.14.16 github.com/spf13/viper v1.15.0 go.uber.org/atomic v1.9.0 // indirect go.uber.org/multierr v1.8.0 // indirect diff --git a/go.sum b/go.sum index 3d30d29..eed9718 100644 --- a/go.sum +++ b/go.sum @@ -153,6 +153,7 @@ github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348 h1:MtvEpTB6LX3v github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= +github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM= github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= diff --git a/internal/config/config.go b/internal/config/config.go index 997b419..5ea53aa 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -13,10 +13,8 @@ type Config struct { Port int } Database struct { - Host string - Name string - Username string - Password string + DriverName string + DataSourceName string } App struct { Production bool @@ -62,8 +60,8 @@ func NewTest() (*Config, error) { return nil, err } - if !strings.Contains(config.Database.Name, "test") { - return nil, fmt.Errorf("database name used for testing should contain 'test' substring: %s", config.Database.Name) + if !strings.Contains(config.Database.DataSourceName, "test") { + return nil, fmt.Errorf("database source name used for test cases should contain 'test' substring: %s", config.Database.DataSourceName) } return config, nil