Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce manually managed partitions #56

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions internal/infra/partition/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ const (
)

type Configuration struct {
Schema string `mapstructure:"schema" validate:"required"`
Table string `mapstructure:"table" validate:"required"`
PartitionKey string `mapstructure:"partitionKey" validate:"required"`
Interval Interval `mapstructure:"interval" validate:"required,oneof=daily weekly monthly quarterly yearly"`
Retention int `mapstructure:"retention" validate:"required,gt=0"`
PreProvisioned int `mapstructure:"preProvisioned" validate:"required,gt=0"`
CleanupPolicy CleanupPolicy `mapstructure:"cleanupPolicy" validate:"required,oneof=drop detach"`
Schema string `mapstructure:"schema" validate:"required"`
Table string `mapstructure:"table" validate:"required"`
PartitionKey string `mapstructure:"partitionKey" validate:"required"`
Interval Interval `mapstructure:"interval" validate:"required,oneof=daily weekly monthly quarterly yearly"`
Retention int `mapstructure:"retention" validate:"required,gt=0"`
PreProvisioned int `mapstructure:"preProvisioned" validate:"required,gt=0"`
CleanupPolicy CleanupPolicy `mapstructure:"cleanupPolicy" validate:"required,oneof=drop detach"`
ManuallyManagedPartitions []string `mapstructure:"manuallyManagedPartitions"`
}

func (p Configuration) GeneratePartition(forDate time.Time) (Partition, error) {
Expand Down
23 changes: 19 additions & 4 deletions pkg/ppm/checkpartition.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ func IsSupportedKeyDataType(dataType postgresql.ColumnType) bool {
return slices.Contains(SupportedPartitionKeyDataType, dataType)
}

func (p *PPM) comparePartitions(existingTables, expectedTables []partition.Partition) (unexpectedTables, missingTables, incorrectBounds []partition.Partition) {
func (p *PPM) comparePartitions(existingTables,
expectedTables []partition.Partition,
manuallyManagedPartitionNames []string,
) (unexpectedTables, missingTables, incorrectBounds []partition.Partition) {
existing := make(map[string]partition.Partition)
expectedAndExists := make(map[string]bool)

Expand Down Expand Up @@ -143,8 +146,18 @@ func (p *PPM) comparePartitions(existingTables, expectedTables []partition.Parti

for _, t := range existingTables {
if _, found := expectedAndExists[t.Name]; !found {
// Only in existingTables and not in both
unexpectedTables = append(unexpectedTables, t)
isPartitionManuallyManaged := false

for _, manuallyManagedPartition := range manuallyManagedPartitionNames {
if manuallyManagedPartition == t.Name {
isPartitionManuallyManaged = true
}
}

if !isPartitionManuallyManaged {
// Only in existingTables and not in both
unexpectedTables = append(unexpectedTables, t)
}
}
}

Expand Down Expand Up @@ -190,7 +203,9 @@ func (p *PPM) checkPartitionsConfiguration(config partition.Configuration) error
return fmt.Errorf("could not list partitions: %w", err)
}

unexpected, missing, incorrectBound := p.comparePartitions(foundPartitions, expectedPartitions)
unexpected, missing, incorrectBound := p.comparePartitions(foundPartitions,
expectedPartitions,
config.ManuallyManagedPartitions)

if len(unexpected) > 0 {
partitionContainAnError = true
Expand Down
80 changes: 80 additions & 0 deletions pkg/ppm/checkpartition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,83 @@ func TestUnsupportedPartitionsStrategy(t *testing.T) {
})
}
}

func TestPPM_comparePartitions(t *testing.T) {
p := partition.Partition{
ParentTable: "ParentTable",
Schema: "Schema",
Name: "Name",
}

type result struct {
unexpectedTables []partition.Partition
missingTables []partition.Partition
incorrectBounds []partition.Partition
}

tests := []struct {
name string
existingTables []partition.Partition
expectedTables []partition.Partition
manuallyManagedPartitionNames []string
result result
}{
{
name: "all existing is expected",
existingTables: []partition.Partition{
p,
},
expectedTables: []partition.Partition{
p,
},
result: result{},
},
{
name: "manually managed partition",
existingTables: []partition.Partition{
p,
},
manuallyManagedPartitionNames: []string{"Name"},
result: result{},
},
{
name: "missing table",
expectedTables: []partition.Partition{
p,
},
result: result{
missingTables: []partition.Partition{
p,
},
},
},
{
name: "unexpected table",
existingTables: []partition.Partition{
p,
},
result: result{
unexpectedTables: []partition.Partition{
p,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := ppm.PPM{}
gotUnexpectedTables, gotMissingTables, gotIncorrectBounds := ppm.ComparePartitions(&p, tt.existingTables,
tt.expectedTables,
tt.manuallyManagedPartitionNames)
assert.DeepEqual(t,
tt.result.unexpectedTables,
gotUnexpectedTables)
assert.DeepEqual(t,
tt.result.missingTables,
gotMissingTables)
assert.DeepEqual(t,
tt.result.incorrectBounds,
gotIncorrectBounds)
})
}
}
2 changes: 1 addition & 1 deletion pkg/ppm/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (p PPM) CleanupPartitions() error {
return fmt.Errorf("could not list partitions: %w", err)
}

unexpected, _, _ := p.comparePartitions(foundPartitions, expectedPartitions)
unexpected, _, _ := p.comparePartitions(foundPartitions, expectedPartitions, config.ManuallyManagedPartitions)

for _, partition := range unexpected {
err := p.DetachPartition(partition)
Expand Down
10 changes: 10 additions & 0 deletions pkg/ppm/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ppm

import "github.com/qonto/postgresql-partition-manager/internal/infra/partition"

func ComparePartitions(p *PPM,
existingTables, expectedTables []partition.Partition,
manuallyManagedPartitionNames []string,
) (unexpectedTables, missingTables, incorrectBounds []partition.Partition) {
return p.comparePartitions(existingTables, expectedTables, manuallyManagedPartitionNames)
}
Loading