Merge pull request #12019 from GOodCoffeeLover/feat/rw-psql

sql driver with rw-roles
pull/12390/head
Matt Farina 1 year ago committed by GitHub
commit 4feafb528b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -94,8 +94,42 @@ func (s *SQL) Name() string {
return SQLDriverName
}
// Check if all migrations al
func (s *SQL) checkAlreadyApplied(migrations []*migrate.Migration) bool {
// make map (set) of ids for fast search
migrationsIds := make(map[string]struct{})
for _, migration := range migrations {
migrationsIds[migration.Id] = struct{}{}
}
// get list of applied migrations
migrate.SetDisableCreateTable(true)
records, err := migrate.GetMigrationRecords(s.db.DB, postgreSQLDialect)
migrate.SetDisableCreateTable(false)
if err != nil {
s.Log("checkAlreadyApplied: failed to get migration records: %v", err)
return false
}
for _, record := range records {
if _, ok := migrationsIds[record.Id]; ok {
s.Log("checkAlreadyApplied: found previous migration (Id: %v) applied at %v", record.Id, record.AppliedAt)
delete(migrationsIds, record.Id)
}
}
// check if all migrations appliyed
if len(migrationsIds) != 0 {
for id := range migrationsIds {
s.Log("checkAlreadyApplied: find unapplied migration (id: %v)", id)
}
return false
}
return true
}
func (s *SQL) ensureDBSetup() error {
// Populate the database with the relations we need if they don't exist yet
migrations := &migrate.MemoryMigrationSource{
Migrations: []*migrate.Migration{
{
@ -121,9 +155,9 @@ func (s *SQL) ensureDBSetup() error {
CREATE INDEX ON %s (%s);
CREATE INDEX ON %s (%s);
CREATE INDEX ON %s (%s);
GRANT ALL ON %s TO PUBLIC;
ALTER TABLE %s ENABLE ROW LEVEL SECURITY;
`,
sqlReleaseTableName,
@ -200,6 +234,12 @@ func (s *SQL) ensureDBSetup() error {
},
}
// Check that init migration already applied
if s.checkAlreadyApplied(migrations.Migrations) {
return nil
}
// Populate the database with the relations we need if they don't exist yet
_, err := migrate.Exec(s.db.DB, postgreSQLDialect, migrations, migrate.Up)
return err
}

@ -21,6 +21,7 @@ import (
"time"
sqlmock "github.com/DATA-DOG/go-sqlmock"
migrate "github.com/rubenv/sql-migrate"
rspb "helm.sh/helm/v3/pkg/release"
)
@ -530,3 +531,51 @@ func mockGetReleaseCustomLabels(mock sqlmock.Sqlmock, key string, namespace stri
}
eq.WillReturnRows(returnRows).RowsWillBeClosed()
}
func TestSqlChechkAppliedMigrations(t *testing.T) {
cases := []struct {
migrationsToApply []*migrate.Migration
appliedMigrationsIds []string
expectedResult bool
errorExplanation string
}{
{
migrationsToApply: []*migrate.Migration{{Id: "init1"}, {Id: "init2"}, {Id: "init3"}},
appliedMigrationsIds: []string{"1", "2", "init1", "3", "init2", "4", "5"},
expectedResult: false,
errorExplanation: "Has found one migration id \"init3\" as applied, that was not applied",
},
{
migrationsToApply: []*migrate.Migration{{Id: "init1"}, {Id: "init2"}, {Id: "init3"}},
appliedMigrationsIds: []string{"1", "2", "init1", "3", "init2", "4", "init3", "5"},
expectedResult: true,
errorExplanation: "Has not found one or more migration ids, that was applied",
},
{
migrationsToApply: []*migrate.Migration{{Id: "init"}},
appliedMigrationsIds: []string{"1", "2", "3", "inits", "4", "tinit", "5"},
expectedResult: false,
errorExplanation: "Has found single \"init\", that was not applied",
},
{
migrationsToApply: []*migrate.Migration{{Id: "init"}},
appliedMigrationsIds: []string{"1", "2", "init", "3", "init2", "4", "init3", "5"},
expectedResult: true,
errorExplanation: "Has not found single migration id \"init\", that was applied",
},
}
for i, c := range cases {
sqlDriver, mock := newTestFixtureSQL(t)
rows := sqlmock.NewRows([]string{"id", "applied_at"})
for _, id := range c.appliedMigrationsIds {
rows.AddRow(id, time.Time{})
}
mock.
ExpectQuery("").
WillReturnRows(rows)
mock.ExpectCommit()
if sqlDriver.checkAlreadyApplied(c.migrationsToApply) != c.expectedResult {
t.Errorf("Test case: %v, Expected: %v, Have: %v, Explanation: %v", i, c.expectedResult, !c.expectedResult, c.errorExplanation)
}
}
}

Loading…
Cancel
Save