diff --git a/cmd/dapp/main.go b/cmd/dapp/main.go index 199ec2a..6893cc5 100644 --- a/cmd/dapp/main.go +++ b/cmd/dapp/main.go @@ -2,7 +2,6 @@ package main import ( "os" - "github.com/tribeshq/tribes/cmd/dapp/root" ) diff --git a/cmd/dapp/root/root.go b/cmd/dapp/root/root.go index ef259c6..0524abd 100644 --- a/cmd/dapp/root/root.go +++ b/cmd/dapp/root/root.go @@ -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) diff --git a/configs/sqlite.go b/configs/sqlite.go index 3b6a1cb..2d7b1d2 100644 --- a/configs/sqlite.go +++ b/configs/sqlite.go @@ -15,11 +15,11 @@ 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, }, ) @@ -27,76 +27,50 @@ func SetupSQlite(path string) (*gorm.DB, error) { 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 }