Skip to content

Commit

Permalink
Set unique chart ID column (#1429)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andres Martinez Gotor authored Jan 13, 2020
1 parent 88ab129 commit 672a7b2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 29 deletions.
32 changes: 7 additions & 25 deletions cmd/asset-syncer/postgresql_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ import (
"github.com/kubeapps/common/datastore"
"github.com/kubeapps/kubeapps/pkg/chart/models"
"github.com/kubeapps/kubeapps/pkg/dbutils"
"github.com/lib/pq"
_ "github.com/lib/pq"
log "github.com/sirupsen/logrus"
)

type postgresAssetManager struct {
Expand Down Expand Up @@ -65,7 +63,7 @@ func (m *postgresAssetManager) Sync(charts []models.Chart) error {

func (m *postgresAssetManager) initTables() error {
_, err := m.DB.Exec(fmt.Sprintf(
"CREATE TABLE IF NOT EXISTS %s (ID serial NOT NULL PRIMARY KEY, info jsonb NOT NULL)",
"CREATE TABLE IF NOT EXISTS %s (ID serial NOT NULL PRIMARY KEY, chart_id varchar unique, info jsonb NOT NULL)",
dbutils.ChartTable,
))
if err != nil {
Expand Down Expand Up @@ -112,38 +110,22 @@ func (m *postgresAssetManager) UpdateLastCheck(repoName, checksum string, now ti
}

func (m *postgresAssetManager) importCharts(charts []models.Chart) error {
txn, err := m.DB.Begin()
if err != nil {
log.Fatal(err)
}

stmt, err := txn.Prepare(pq.CopyIn(dbutils.ChartTable, "info"))
if err != nil {
return err
}

for _, chart := range charts {
d, err := json.Marshal(chart)
if err != nil {
return err
}
_, err = stmt.Exec(string(d))
_, err = m.DB.Exec(fmt.Sprintf(`INSERT INTO %s (chart_id, info)
VALUES ($1, $2)
ON CONFLICT (chart_id)
DO UPDATE SET info = $2
`, dbutils.ChartTable), chart.ID, string(d))
if err != nil {
return err
}
}

_, err = stmt.Exec()
if err != nil {
return err
}

err = stmt.Close()
if err != nil {
return err
}

return txn.Commit()
return nil
}

func (m *postgresAssetManager) removeMissingCharts(charts []models.Chart) error {
Expand Down
4 changes: 2 additions & 2 deletions cmd/assetsvc/postgresql_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (m *postgresAssetManager) getPaginatedChartList(repo string, pageNumber, pa

func (m *postgresAssetManager) getChart(chartID string) (models.Chart, error) {
var chart models.ChartIconString
err := m.QueryOne(&chart, fmt.Sprintf("SELECT info FROM %s WHERE info ->> 'ID' = $1", dbutils.ChartTable), chartID)
err := m.QueryOne(&chart, fmt.Sprintf("SELECT info FROM %s WHERE chart_id = $1", dbutils.ChartTable), chartID)
if err != nil {
return models.Chart{}, err
}
Expand Down Expand Up @@ -103,7 +103,7 @@ func (m *postgresAssetManager) getChart(chartID string) (models.Chart, error) {

func (m *postgresAssetManager) getChartVersion(chartID, version string) (models.Chart, error) {
var chart models.Chart
err := m.QueryOne(&chart, fmt.Sprintf("SELECT info FROM %s WHERE info ->> 'ID' = $1", dbutils.ChartTable), chartID)
err := m.QueryOne(&chart, fmt.Sprintf("SELECT info FROM %s WHERE chart_id = $1", dbutils.ChartTable), chartID)
if err != nil {
return models.Chart{}, err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/assetsvc/postgresql_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func Test_PGgetChart(t *testing.T) {
Chart: models.Chart{ID: "foo"},
RawIcon: iconB64,
}
m.On("QueryOne", &models.ChartIconString{}, "SELECT info FROM charts WHERE info ->> 'ID' = $1", []interface{}{"foo"}).Run(func(args mock.Arguments) {
m.On("QueryOne", &models.ChartIconString{}, "SELECT info FROM charts WHERE chart_id = $1", []interface{}{"foo"}).Run(func(args mock.Arguments) {
*args.Get(0).(*models.ChartIconString) = dbChart
})

Expand Down Expand Up @@ -100,7 +100,7 @@ func Test_PGgetChartVersion(t *testing.T) {
{Version: "2.0.0"},
},
}
m.On("QueryOne", &models.Chart{}, "SELECT info FROM charts WHERE info ->> 'ID' = $1", []interface{}{"foo"}).Run(func(args mock.Arguments) {
m.On("QueryOne", &models.Chart{}, "SELECT info FROM charts WHERE chart_id = $1", []interface{}{"foo"}).Run(func(args mock.Arguments) {
*args.Get(0).(*models.Chart) = dbChart
})

Expand Down

0 comments on commit 672a7b2

Please sign in to comment.