Skip to content

Commit

Permalink
adjust the dbClient related logic
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotchenzichang committed Sep 15, 2024
1 parent ec7168c commit 4656c61
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
1 change: 0 additions & 1 deletion database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func (db *Database) AddTable(table *Table) error {
if db.ExistedTable(name) {
return fmt.Errorf("table %s already existed", name)
}
table.dbClient = db.dbClient
db.pendingTables[name] = table
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ func TestNewDatabaseAndAddNewTable(t *testing.T) {
if err != nil {
t.Fatal(err)
}
table, err := NewTableBuilder("test_table").AddFieldInt("test_field_int").AddFieldVarchar("test_field_string").Submit()
table, err := NewTableBuilder("test_table", db.dbClient).AddFieldInt("test_field_int").AddFieldVarchar("test_field_string").Submit()
if err != nil {
t.Fatal(err)
}
Expand Down
11 changes: 6 additions & 5 deletions database/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ type Table struct {
Fields map[string]*Field
}

func NewTable(name string) *Table {
func NewTable(name string, dbClient *gorm.DB) *Table {
return &Table{
name: name,
Fields: map[string]*Field{},
name: name,
Fields: map[string]*Field{},
dbClient: dbClient,
}
}

Expand Down Expand Up @@ -90,9 +91,9 @@ type TableBuilder struct {
table *Table
}

func NewTableBuilder(name string) *TableBuilder {
func NewTableBuilder(name string, dbClient *gorm.DB) *TableBuilder {
return &TableBuilder{
table: NewTable(name),
table: NewTable(name, dbClient),
}
}

Expand Down
2 changes: 1 addition & 1 deletion database/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package database
import "testing"

func TestNewTable(t *testing.T) {
table, err := NewTableBuilder("test_table_name").AddFieldInt("test_field_int").Submit()
table, err := NewTableBuilder("test_table_name", nil).AddFieldInt("test_field_int").Submit()
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 4656c61

Please sign in to comment.