Skip to content

Commit

Permalink
Create indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
harshsinghvi committed Nov 17, 2023
1 parent 6b27af3 commit c9a08d7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion controllers/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func EditTodo(c *gin.Context) {
var todo models.Todo
c.BindJSON(&todo)

querry := database.Connection.Model(&models.Todo{}).Set("completed = ?", todo.Completed)
querry := database.Connection.Model(&models.Todo{}).Set("completed = ?", todo.Completed).Set("updated_at = ?", time.Now())

if todo.Text != "" {
querry.Set("text = ?", todo.Text)
Expand Down
10 changes: 8 additions & 2 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ var Connection *pg.DB
func IsDtabaseReady() bool {
ctx := Connection.Context()
var version string

_, err := Connection.QueryOneContext(ctx, pg.Scan(&version), "SELECT version()")
if err != nil {
log.Printf("Failed to connect to database")
Expand Down Expand Up @@ -71,6 +70,13 @@ func CreateTodoTable() error {
log.Printf("Error while creating todo table, Reason: %v\n", createError)
return createError
}
log.Printf("Todo table created")

_, err := Connection.Exec(`CREATE UNIQUE INDEX IF NOT EXISTS index_todo ON todos(id, completed, created_at, updated_at);`)

if err != nil {
log.Printf(err.Error())
}

log.Printf("Todo table and indexes created")
return nil
}
1 change: 0 additions & 1 deletion models/todo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ type Todo struct {
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}

0 comments on commit c9a08d7

Please sign in to comment.