Skip to content

Commit

Permalink
refactor: change the database setup
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquemarlon committed Nov 16, 2024
1 parent f1694d9 commit c8e845d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 69 deletions.
1 change: 0 additions & 1 deletion cmd/dapp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"os"

"github.com/tribeshq/tribes/cmd/dapp/root"
)

Expand Down
4 changes: 2 additions & 2 deletions cmd/dapp/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ func run(cmd *cobra.Command, args []string) {
var db *gorm.DB
var err error
if useMemoryDB {
db, err = configs.SetupSQliteMemory(":memory:")
db, err = configs.SetupSQlite(":memory:")
if err != nil {
slog.Error("Failed to setup in-memory SQLite database", "error", err)
os.Exit(1)
}
slog.Info("In-memory database initialized")
} else {
db, err = configs.SetupSQlite("./tribes.db")
db, err = configs.SetupSQlite("tribes.db")
if err != nil {
slog.Error("Failed to setup SQLite database", "error", err)
os.Exit(1)
Expand Down
106 changes: 40 additions & 66 deletions configs/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,88 +15,62 @@ import (

func SetupSQlite(path string) (*gorm.DB, error) {
logger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
log.New(os.Stdout, "\r\n", log.LstdFlags),
logger.Config{
SlowThreshold: time.Second, // Slow SQL threshold
LogLevel: logger.Info, // Log level
Colorful: true, // Enable color
SlowThreshold: time.Second,
LogLevel: logger.Info,
Colorful: true,
},
)

db, err := gorm.Open(sqlite.Open(path), &gorm.Config{
Logger: logger,
})
if err != nil {
return nil, fmt.Errorf("failed to open database: %v", err)
return nil, fmt.Errorf("failed to open database: %w", err)
}
err = db.AutoMigrate(
entity.Order{},

if err := db.AutoMigrate(
entity.User{},
entity.Order{},
entity.Contract{},
entity.Crowdfunding{},
entity.Crowdfunding{},
)
if err != nil {
return nil, fmt.Errorf("failed to run migrations: %v", err)
); err != nil {
return nil, fmt.Errorf("failed to run migrations: %w", err)
}

err = db.Table("users").Create([]map[string]interface{}{
{
"role": "admin",
"address": common.HexToAddress("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266").String(),
"created_at": 0,
},
{
"role": "",
"address": common.HexToAddress("0xf49Fc2E6478982F125c0F38d38f67B32772604B4").String(),
"created_at": 0,
},
}).Error
if err != nil {
return nil, fmt.Errorf("failed to create users: %w", err)
}
return db, nil
}

func SetupSQliteMemory(path string) (*gorm.DB, error) {
logger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
logger.Config{
SlowThreshold: time.Second, // Slow SQL threshold
LogLevel: logger.Info, // Log level
Colorful: true, // Enable color
},
)

db, err := gorm.Open(sqlite.Open(path), &gorm.Config{
Logger: logger,
})
if err != nil {
return nil, fmt.Errorf("failed to open database: %v", err)
}
err = db.AutoMigrate(
entity.Order{},
entity.User{},
entity.Crowdfunding{},
entity.Crowdfunding{},
)
if err != nil {
return nil, fmt.Errorf("failed to run migrations: %v", err)
var users []map[string]interface{}
if path == ":memory:" {
users = []map[string]interface{}{
{
"role": "admin",
"address": common.HexToAddress("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266").String(),
"created_at": 0,
},
{
"role": "creator",
"address": common.HexToAddress("0xf49Fc2E6478982F125c0F38d38f67B32772604B4").String(),
"created_at": 0,
},
}
} else {
users = []map[string]interface{}{
{
"role": "admin",
"address": common.HexToAddress("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266").String(),
"created_at": 0,
},
{
"role": "user",
"address": common.HexToAddress("0xf49Fc2E6478982F125c0F38d38f67B32772604B4").String(),
"created_at": 0,
},
}
}

err = db.Table("users").Create([]map[string]interface{}{
{
"role": "admin",
"address": common.HexToAddress("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266").String(),
"created_at": 0,
},
{
"role": "",
"address": common.HexToAddress("0xf49Fc2E6478982F125c0F38d38f67B32772604B4").String(),
"created_at": 0,
},
}).Error
if err != nil {
if err := db.Table("users").Create(users).Error; err != nil {
return nil, fmt.Errorf("failed to create users: %w", err)
}

return db, nil
}

0 comments on commit c8e845d

Please sign in to comment.