Skip to content

Commit 49cd327

Browse files
authored
Merge pull request #52 from tzzzoz/add-datetime-with-tz
Add datetime with timezone
2 parents 6c5125e + 603a132 commit 49cd327

File tree

7 files changed

+90
-46
lines changed

7 files changed

+90
-46
lines changed

internal/infra/postgresql/column.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ var ErrUnsupportedPartitionKeyType = errors.New("unsupported partition key colum
1111
type ColumnType string
1212

1313
const (
14-
Date ColumnType = "date"
15-
DateTime ColumnType = "timestamp"
16-
UUID ColumnType = "uuid"
14+
Date ColumnType = "date"
15+
DateTime ColumnType = "timestamp"
16+
DateTimeWithTZ ColumnType = "timestamp with time zone"
17+
UUID ColumnType = "uuid"
1718
)
1819

1920
func (p Postgres) GetColumnDataType(schema, table, column string) (ColumnType, error) {
@@ -40,7 +41,7 @@ func (p Postgres) GetColumnDataType(schema, table, column string) (ColumnType, e
4041
case "timestamp without time zone":
4142
return DateTime, nil
4243
case "timestamp with time zone":
43-
return DateTime, nil
44+
return DateTimeWithTZ, nil
4445
case "uuid":
4546
return UUID, nil
4647
default:

internal/infra/postgresql/column_internal_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestGetColumn(t *testing.T) {
5050
{
5151
"Date time with time zone",
5252
"timestamp with time zone",
53-
DateTime,
53+
DateTimeWithTZ,
5454
},
5555
{
5656
"UUID",

pkg/ppm/bounds.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ func parseBounds(partition postgresql.PartitionResult) (lowerBound time.Time, up
3030
return lowerBound, upperBound, nil
3131
}
3232

33+
lowerBound, upperBound, err = parseBoundAsDateTimeWithTimezone(partition)
34+
if err == nil {
35+
return lowerBound, upperBound, nil
36+
}
37+
3338
lowerBound, upperBound, err = parseBoundAsUUIDv7(partition)
3439
if err == nil {
3540
return lowerBound, upperBound, nil
@@ -70,6 +75,23 @@ func parseBoundAsDateTime(partition postgresql.PartitionResult) (lowerBound, upp
7075
return lowerBound, upperBound, nil
7176
}
7277

78+
func parseBoundAsDateTimeWithTimezone(partition postgresql.PartitionResult) (lowerBound, upperBound time.Time, err error) {
79+
lowerBound, err = time.Parse("2006-01-02 15:04:05Z07", partition.LowerBound)
80+
if err != nil {
81+
return time.Time{}, time.Time{}, fmt.Errorf("can't parse lowerbound as datetime with timezone: %w", err)
82+
}
83+
84+
upperBound, err = time.Parse("2006-01-02 15:04:05Z07", partition.UpperBound)
85+
if err != nil {
86+
return time.Time{}, time.Time{}, fmt.Errorf("can't parse upperbound as datetime with timezone: %w", err)
87+
}
88+
89+
lowerBound = convertToDateTimeWithoutTimezone(lowerBound)
90+
upperBound = convertToDateTimeWithoutTimezone(upperBound)
91+
92+
return lowerBound, upperBound, nil
93+
}
94+
7395
func parseBoundAsUUIDv7(partition postgresql.PartitionResult) (lowerBound, upperBound time.Time, err error) {
7496
lowerBoundUUID, err := uuid.Parse(partition.LowerBound)
7597
if err != nil {
@@ -90,3 +112,11 @@ func parseBoundAsUUIDv7(partition postgresql.PartitionResult) (lowerBound, upper
90112

91113
return lowerBound, upperBound, nil
92114
}
115+
116+
func convertToDateTimeWithoutTimezone(bound time.Time) time.Time {
117+
parsedTime, err := time.Parse("2006-01-02 15:04:05", bound.UTC().Format("2006-01-02 15:04:05"))
118+
if err != nil {
119+
return time.Time{}
120+
}
121+
return parsedTime
122+
}

pkg/ppm/bounds_internal_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ func TestParseBounds(t *testing.T) {
3737
"2024-01-01T10:00:00Z",
3838
"2025-02-03T12:53:00Z",
3939
},
40+
{
41+
"Datetime with timezone bounds",
42+
postgresql.PartitionResult{
43+
Schema: "public",
44+
Name: "my_table",
45+
LowerBound: "2024-01-01 23:30:00-01",
46+
UpperBound: "2025-02-03 00:30:00+01",
47+
},
48+
"2024-01-02T00:30:00Z",
49+
"2025-02-02T23:30:00Z",
50+
},
4051
{
4152
"UUIDv7 bounds",
4253
postgresql.PartitionResult{

pkg/ppm/checkpartition.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var (
2121
var SupportedPartitionKeyDataType = []postgresql.ColumnType{
2222
postgresql.Date,
2323
postgresql.DateTime,
24+
postgresql.DateTimeWithTZ,
2425
postgresql.UUID,
2526
}
2627

pkg/ppm/checkpartition_test.go

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -52,48 +52,49 @@ func TestCheckPartitions(t *testing.T) {
5252
// Build mock for each partitions
5353
for _, p := range partitions {
5454
var tables []partition.Partition
55-
56-
var table partition.Partition
57-
58-
for i := 0; i <= p.Retention; i++ {
59-
switch p.Interval {
60-
case partition.Daily:
61-
table, _ = p.GeneratePartition(time.Now().AddDate(0, 0, -i))
62-
case partition.Weekly:
63-
table, _ = p.GeneratePartition(time.Now().AddDate(0, 0, -i*7))
64-
case partition.Quarterly:
65-
table, _ = p.GeneratePartition(time.Now().AddDate(0, i*-3, 0))
66-
case partition.Monthly:
67-
table, _ = p.GeneratePartition(time.Now().AddDate(0, -i, 0))
68-
case partition.Yearly:
69-
table, _ = p.GeneratePartition(time.Now().AddDate(-i, 0, 0))
70-
default:
71-
t.Errorf("unuspported partition interval in retention table mock")
72-
}
73-
74-
postgreSQLMock.On("GetColumnDataType", table.Schema, table.ParentTable, p.PartitionKey).Return(postgresql.Date, nil).Once()
75-
tables = append(tables, table)
55+
var retentionTables []partition.Partition
56+
var preprovisionedTables []partition.Partition
57+
58+
// Create retention partitions
59+
forDate := time.Now()
60+
switch p.Interval {
61+
case partition.Daily:
62+
retentionTables, _ = p.GetRetentionPartitions(forDate)
63+
case partition.Weekly:
64+
retentionTables, _ = p.GetRetentionPartitions(forDate)
65+
case partition.Quarterly:
66+
retentionTables, _ = p.GetRetentionPartitions(forDate)
67+
case partition.Monthly:
68+
retentionTables, _ = p.GetRetentionPartitions(forDate)
69+
case partition.Yearly:
70+
retentionTables, _ = p.GetRetentionPartitions(forDate)
71+
default:
72+
t.Errorf("unuspported partition interval in retention table mock")
7673
}
77-
78-
for i := 0; i <= p.PreProvisioned; i++ {
79-
switch p.Interval {
80-
case partition.Daily:
81-
table, _ = p.GeneratePartition(time.Now().AddDate(0, 0, i))
82-
case partition.Weekly:
83-
table, _ = p.GeneratePartition(time.Now().AddDate(0, 0, i*7))
84-
case partition.Monthly:
85-
table, _ = p.GeneratePartition(time.Now().AddDate(0, i, 0))
86-
case partition.Quarterly:
87-
table, _ = p.GeneratePartition(time.Now().AddDate(0, i*3, 0))
88-
case partition.Yearly:
89-
table, _ = p.GeneratePartition(time.Now().AddDate(i, 0, 0))
90-
default:
91-
t.Errorf("unuspported partition interval in preprovisonned table mock")
92-
}
93-
94-
postgreSQLMock.On("GetColumnDataType", table.Schema, table.ParentTable, p.PartitionKey).Return(postgresql.Date, nil).Once()
95-
tables = append(tables, table)
74+
tables = append(tables, retentionTables...)
75+
76+
// Create current partition
77+
currentPartition, _ := p.GeneratePartition(forDate)
78+
tables = append(tables, currentPartition)
79+
80+
// Create preprovisioned partitions
81+
switch p.Interval {
82+
case partition.Daily:
83+
preprovisionedTables, _ = p.GetPreProvisionedPartitions(forDate)
84+
case partition.Weekly:
85+
preprovisionedTables, _ = p.GetPreProvisionedPartitions(forDate)
86+
case partition.Monthly:
87+
preprovisionedTables, _ = p.GetPreProvisionedPartitions(forDate)
88+
case partition.Quarterly:
89+
preprovisionedTables, _ = p.GetPreProvisionedPartitions(forDate)
90+
case partition.Yearly:
91+
preprovisionedTables, _ = p.GetPreProvisionedPartitions(forDate)
92+
default:
93+
t.Errorf("unuspported partition interval in preprovisonned table mock")
9694
}
95+
tables = append(tables, preprovisionedTables...)
96+
97+
postgreSQLMock.On("GetColumnDataType", p.Schema, p.Table, p.PartitionKey).Return(postgresql.Date, nil).Once()
9798

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

pkg/ppm/provisioning.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (p PPM) CreatePartition(partitionConfiguration partition.Configuration, par
103103
case postgresql.Date:
104104
lowerBound = partition.LowerBound.Format("2006-01-02")
105105
upperBound = partition.UpperBound.Format("2006-01-02")
106-
case postgresql.DateTime:
106+
case postgresql.DateTime, postgresql.DateTimeWithTZ:
107107
lowerBound = partition.LowerBound.Format("2006-01-02 00:00:00")
108108
upperBound = partition.UpperBound.Format("2006-01-02 00:00:00")
109109
case postgresql.UUID:

0 commit comments

Comments
 (0)