Skip to content

Commit

Permalink
Adds IGNORE statement on SQL to avoid errors on existing rows. Refs #3
Browse files Browse the repository at this point in the history
  • Loading branch information
ariel17 committed Mar 7, 2023
1 parent be2cd20 commit b04845c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
10 changes: 5 additions & 5 deletions pkg/repositories/repository_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,31 @@ func (m *mysqlRepository) GetStatus() error {
}

func (m *mysqlRepository) AddPerson(person models.Person) error {
query := "INSERT INTO `persons` (`id`, `name`, `date_of_birth`, `nationality`) VALUES (?, ?, ?, ?)"
query := "INSERT IGNORE INTO `persons` (`id`, `name`, `date_of_birth`, `nationality`) VALUES (?, ?, ?, ?)"
_, err := m.db.Exec(query, person.ID, person.Name, person.DateOfBirth, person.Nationality)
return err
}

func (m *mysqlRepository) AddTeam(team models.Team) error {
query := "INSERT INTO `teams` (`tla`, `name`, `short_name`, `area_name`, `address`) VALUES (?, ?, ?, ?, ?)"
query := "INSERT IGNORE INTO `teams` (`tla`, `name`, `short_name`, `area_name`, `address`) VALUES (?, ?, ?, ?, ?)"
_, err := m.db.Exec(query, team.TLA, team.Name, team.ShortName, team.AreaName, team.Address)
return err
}

func (m *mysqlRepository) AddCompetition(competition models.Competition) error {
query := "INSERT INTO `competitions` (`code`, `name`, `area_name`) VALUES (?, ?, ?)"
query := "INSERT IGNORE INTO `competitions` (`code`, `name`, `area_name`) VALUES (?, ?, ?)"
_, err := m.db.Exec(query, competition.Code, competition.Name, competition.AreaName)
return err
}

func (m *mysqlRepository) AddTeamToCompetition(team models.Team, competition models.Competition) error {
query := "INSERT INTO `competitions_teams` (`competition_code`, `team_tla`) VALUES (?, ?)"
query := "INSERT IGNORE INTO `competitions_teams` (`competition_code`, `team_tla`) VALUES (?, ?)"
_, err := m.db.Exec(query, competition.Code, team.TLA)
return err
}

func (m *mysqlRepository) AddPersonToTeam(person models.Person, team models.Team) error {
query := "INSERT INTO `teams_persons` (`team_tla`, `person_id`, `position`) VALUES (?, ?, ?)"
query := "INSERT IGNORE INTO `teams_persons` (`team_tla`, `person_id`, `position`) VALUES (?, ?, ?)"
_, err := m.db.Exec(query, team.TLA, person.ID, person.Position)
return err
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/repositories/repository_mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestMysqlRepository_AddPerson(t *testing.T) {

r := &mysqlRepository{db: db}

expectedExec := mock.ExpectExec("INSERT INTO `persons`").
expectedExec := mock.ExpectExec("INSERT IGNORE INTO `persons`").
WithArgs(person.ID, person.Name, person.DateOfBirth, person.Nationality)

if tc.isSuccess {
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestMysqlRepository_AddTeam(t *testing.T) {

r := &mysqlRepository{db: db}

expectedExec := mock.ExpectExec("INSERT INTO `teams`").
expectedExec := mock.ExpectExec("INSERT IGNORE INTO `teams`").
WithArgs(team.TLA, team.Name, team.ShortName, team.AreaName, team.Address)

if tc.isSuccess {
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestMysqlRepository_AddCompetition(t *testing.T) {

r := &mysqlRepository{db: db}

expectedExec := mock.ExpectExec("INSERT INTO `competitions`").
expectedExec := mock.ExpectExec("INSERT IGNORE INTO `competitions`").
WithArgs(competition.Code, competition.Name, competition.AreaName)

if tc.isSuccess {
Expand Down Expand Up @@ -157,7 +157,7 @@ func TestMysqlRepository_AddTeamToCompetition(t *testing.T) {

r := &mysqlRepository{db: db}

expectedExec := mock.ExpectExec("INSERT INTO `competitions_teams`").
expectedExec := mock.ExpectExec("INSERT IGNORE INTO `competitions_teams`").
WithArgs(competition.Code, team.TLA)

if tc.isSuccess {
Expand Down Expand Up @@ -205,7 +205,7 @@ func TestMysqlRepository_AddPersonToTeam(t *testing.T) {

r := &mysqlRepository{db: db}

expectedExec := mock.ExpectExec("INSERT INTO `teams_persons`").
expectedExec := mock.ExpectExec("INSERT IGNORE INTO `teams_persons`").
WithArgs(team.TLA, tc.person.ID, tc.person.Position)

if tc.isSuccess {
Expand Down

0 comments on commit b04845c

Please sign in to comment.