Skip to content

Commit

Permalink
Merge pull request #52 from tzzzoz/add-datetime-with-tz
Browse files Browse the repository at this point in the history
Add datetime with timezone
  • Loading branch information
dcupif authored Nov 5, 2024
2 parents 6c5125e + 603a132 commit 49cd327
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 46 deletions.
9 changes: 5 additions & 4 deletions internal/infra/postgresql/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ var ErrUnsupportedPartitionKeyType = errors.New("unsupported partition key colum
type ColumnType string

const (
Date ColumnType = "date"
DateTime ColumnType = "timestamp"
UUID ColumnType = "uuid"
Date ColumnType = "date"
DateTime ColumnType = "timestamp"
DateTimeWithTZ ColumnType = "timestamp with time zone"
UUID ColumnType = "uuid"
)

func (p Postgres) GetColumnDataType(schema, table, column string) (ColumnType, error) {
Expand All @@ -40,7 +41,7 @@ func (p Postgres) GetColumnDataType(schema, table, column string) (ColumnType, e
case "timestamp without time zone":
return DateTime, nil
case "timestamp with time zone":
return DateTime, nil
return DateTimeWithTZ, nil
case "uuid":
return UUID, nil
default:
Expand Down
2 changes: 1 addition & 1 deletion internal/infra/postgresql/column_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestGetColumn(t *testing.T) {
{
"Date time with time zone",
"timestamp with time zone",
DateTime,
DateTimeWithTZ,
},
{
"UUID",
Expand Down
30 changes: 30 additions & 0 deletions pkg/ppm/bounds.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ func parseBounds(partition postgresql.PartitionResult) (lowerBound time.Time, up
return lowerBound, upperBound, nil
}

lowerBound, upperBound, err = parseBoundAsDateTimeWithTimezone(partition)
if err == nil {
return lowerBound, upperBound, nil
}

lowerBound, upperBound, err = parseBoundAsUUIDv7(partition)
if err == nil {
return lowerBound, upperBound, nil
Expand Down Expand Up @@ -70,6 +75,23 @@ func parseBoundAsDateTime(partition postgresql.PartitionResult) (lowerBound, upp
return lowerBound, upperBound, nil
}

func parseBoundAsDateTimeWithTimezone(partition postgresql.PartitionResult) (lowerBound, upperBound time.Time, err error) {
lowerBound, err = time.Parse("2006-01-02 15:04:05Z07", partition.LowerBound)
if err != nil {
return time.Time{}, time.Time{}, fmt.Errorf("can't parse lowerbound as datetime with timezone: %w", err)
}

upperBound, err = time.Parse("2006-01-02 15:04:05Z07", partition.UpperBound)
if err != nil {
return time.Time{}, time.Time{}, fmt.Errorf("can't parse upperbound as datetime with timezone: %w", err)
}

lowerBound = convertToDateTimeWithoutTimezone(lowerBound)
upperBound = convertToDateTimeWithoutTimezone(upperBound)

return lowerBound, upperBound, nil
}

func parseBoundAsUUIDv7(partition postgresql.PartitionResult) (lowerBound, upperBound time.Time, err error) {
lowerBoundUUID, err := uuid.Parse(partition.LowerBound)
if err != nil {
Expand All @@ -90,3 +112,11 @@ func parseBoundAsUUIDv7(partition postgresql.PartitionResult) (lowerBound, upper

return lowerBound, upperBound, nil
}

func convertToDateTimeWithoutTimezone(bound time.Time) time.Time {
parsedTime, err := time.Parse("2006-01-02 15:04:05", bound.UTC().Format("2006-01-02 15:04:05"))
if err != nil {
return time.Time{}
}
return parsedTime

Check failure on line 121 in pkg/ppm/bounds.go

View workflow job for this annotation

GitHub Actions / golangci

return statements should not be cuddled if block has more than two lines (wsl)
}
11 changes: 11 additions & 0 deletions pkg/ppm/bounds_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ func TestParseBounds(t *testing.T) {
"2024-01-01T10:00:00Z",
"2025-02-03T12:53:00Z",
},
{
"Datetime with timezone bounds",
postgresql.PartitionResult{
Schema: "public",
Name: "my_table",
LowerBound: "2024-01-01 23:30:00-01",
UpperBound: "2025-02-03 00:30:00+01",
},
"2024-01-02T00:30:00Z",
"2025-02-02T23:30:00Z",
},
{
"UUIDv7 bounds",
postgresql.PartitionResult{
Expand Down
1 change: 1 addition & 0 deletions pkg/ppm/checkpartition.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (
var SupportedPartitionKeyDataType = []postgresql.ColumnType{
postgresql.Date,
postgresql.DateTime,
postgresql.DateTimeWithTZ,
postgresql.UUID,
}

Expand Down
81 changes: 41 additions & 40 deletions pkg/ppm/checkpartition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,48 +52,49 @@ func TestCheckPartitions(t *testing.T) {
// Build mock for each partitions
for _, p := range partitions {
var tables []partition.Partition

var table partition.Partition

for i := 0; i <= p.Retention; i++ {
switch p.Interval {
case partition.Daily:
table, _ = p.GeneratePartition(time.Now().AddDate(0, 0, -i))
case partition.Weekly:
table, _ = p.GeneratePartition(time.Now().AddDate(0, 0, -i*7))
case partition.Quarterly:
table, _ = p.GeneratePartition(time.Now().AddDate(0, i*-3, 0))
case partition.Monthly:
table, _ = p.GeneratePartition(time.Now().AddDate(0, -i, 0))
case partition.Yearly:
table, _ = p.GeneratePartition(time.Now().AddDate(-i, 0, 0))
default:
t.Errorf("unuspported partition interval in retention table mock")
}

postgreSQLMock.On("GetColumnDataType", table.Schema, table.ParentTable, p.PartitionKey).Return(postgresql.Date, nil).Once()
tables = append(tables, table)
var retentionTables []partition.Partition

Check failure on line 55 in pkg/ppm/checkpartition_test.go

View workflow job for this annotation

GitHub Actions / golangci

declarations should never be cuddled (wsl)
var preprovisionedTables []partition.Partition

Check failure on line 56 in pkg/ppm/checkpartition_test.go

View workflow job for this annotation

GitHub Actions / golangci

declarations should never be cuddled (wsl)

// Create retention partitions
forDate := time.Now()
switch p.Interval {

Check failure on line 60 in pkg/ppm/checkpartition_test.go

View workflow job for this annotation

GitHub Actions / golangci

switch statements should only be cuddled with variables switched (wsl)
case partition.Daily:
retentionTables, _ = p.GetRetentionPartitions(forDate)
case partition.Weekly:
retentionTables, _ = p.GetRetentionPartitions(forDate)
case partition.Quarterly:
retentionTables, _ = p.GetRetentionPartitions(forDate)
case partition.Monthly:
retentionTables, _ = p.GetRetentionPartitions(forDate)
case partition.Yearly:
retentionTables, _ = p.GetRetentionPartitions(forDate)
default:
t.Errorf("unuspported partition interval in retention table mock")
}

for i := 0; i <= p.PreProvisioned; i++ {
switch p.Interval {
case partition.Daily:
table, _ = p.GeneratePartition(time.Now().AddDate(0, 0, i))
case partition.Weekly:
table, _ = p.GeneratePartition(time.Now().AddDate(0, 0, i*7))
case partition.Monthly:
table, _ = p.GeneratePartition(time.Now().AddDate(0, i, 0))
case partition.Quarterly:
table, _ = p.GeneratePartition(time.Now().AddDate(0, i*3, 0))
case partition.Yearly:
table, _ = p.GeneratePartition(time.Now().AddDate(i, 0, 0))
default:
t.Errorf("unuspported partition interval in preprovisonned table mock")
}

postgreSQLMock.On("GetColumnDataType", table.Schema, table.ParentTable, p.PartitionKey).Return(postgresql.Date, nil).Once()
tables = append(tables, table)
tables = append(tables, retentionTables...)

Check failure on line 74 in pkg/ppm/checkpartition_test.go

View workflow job for this annotation

GitHub Actions / golangci

append only allowed to cuddle with appended value (wsl)

// Create current partition
currentPartition, _ := p.GeneratePartition(forDate)
tables = append(tables, currentPartition)

// Create preprovisioned partitions
switch p.Interval {
case partition.Daily:
preprovisionedTables, _ = p.GetPreProvisionedPartitions(forDate)
case partition.Weekly:
preprovisionedTables, _ = p.GetPreProvisionedPartitions(forDate)
case partition.Monthly:
preprovisionedTables, _ = p.GetPreProvisionedPartitions(forDate)
case partition.Quarterly:
preprovisionedTables, _ = p.GetPreProvisionedPartitions(forDate)
case partition.Yearly:
preprovisionedTables, _ = p.GetPreProvisionedPartitions(forDate)
default:
t.Errorf("unuspported partition interval in preprovisonned table mock")
}
tables = append(tables, preprovisionedTables...)

Check failure on line 95 in pkg/ppm/checkpartition_test.go

View workflow job for this annotation

GitHub Actions / golangci

append only allowed to cuddle with appended value (wsl)

postgreSQLMock.On("GetColumnDataType", p.Schema, p.Table, p.PartitionKey).Return(postgresql.Date, nil).Once()

postgreSQLMock.On("GetPartitionSettings", p.Schema, p.Table).Return(string(partition.Range), p.PartitionKey, nil).Once()

Expand Down
2 changes: 1 addition & 1 deletion pkg/ppm/provisioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (p PPM) CreatePartition(partitionConfiguration partition.Configuration, par
case postgresql.Date:
lowerBound = partition.LowerBound.Format("2006-01-02")
upperBound = partition.UpperBound.Format("2006-01-02")
case postgresql.DateTime:
case postgresql.DateTime, postgresql.DateTimeWithTZ:
lowerBound = partition.LowerBound.Format("2006-01-02 00:00:00")
upperBound = partition.UpperBound.Format("2006-01-02 00:00:00")
case postgresql.UUID:
Expand Down

0 comments on commit 49cd327

Please sign in to comment.