Skip to content

Commit

Permalink
the end date is set to 1st of next month as the upperbound is excluded
Browse files Browse the repository at this point in the history
  • Loading branch information
acn-dgopa committed Dec 8, 2024
1 parent 958ece8 commit aabe5d8
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task InsertAuthenticationEvent(AuthenticationEvent authenticationEv
{
const string INSERTAUTHNEVENT = /*strpsql*/
"""
INSERT INTO authentication.eventlogv1 (
INSERT INTO authentication.eventlogv2 (
sessionid,
externalsessionid,
subscriptionkey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task InsertAuthorizationEvent(AuthorizationEvent authorizationEvent
{
const string INSERTAUTHZEVENT = /*strpsql*/
"""
INSERT INTO authz.eventlogv1(
INSERT INTO authz.eventlogv2(
sessionid,
created,
subjectuserid,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

-- Table: authentication.eventlogv2
CREATE TABLE IF NOT EXISTS authentication.eventlogv2
(
sessionid text,
externalsessionid text,
subscriptionkey text,
externaltokenissuer text,
created timestamp with time zone NOT NULL,
userid integer,
supplierid text,
orgnumber integer,
eventtypeid integer,
authenticationmethodid integer,
authenticationlevelid integer,
ipaddress text,
isauthenticated boolean NOT NULL,
CONSTRAINT authenticationeventtype_fkey FOREIGN KEY (eventtypeid)
REFERENCES authentication.authenticationeventtype (authenticationeventtypeid) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
NOT VALID,
CONSTRAINT authenticationlevel_fkey FOREIGN KEY (authenticationlevelid)
REFERENCES authentication.authenticationlevel (authenticationlevelid) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
NOT VALID,
CONSTRAINT authenticationmethod_fkey FOREIGN KEY (authenticationmethodid)
REFERENCES authentication.authenticationmethod (authenticationmethodid) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
NOT VALID
) PARTITION BY RANGE (created);

CREATE INDEX ON authentication.eventlogv2 (created);

-- Table: authz.eventlogv2
CREATE TABLE IF NOT EXISTS authz.eventlogv2
(
sessionid text,
created timestamp with time zone NOT NULL,
subjectuserid INTEGER,
subjectorgcode text,
subjectorgnumber INTEGER,
subjectparty INTEGER,
resourcepartyid INTEGER,
resource text,
instanceid text,
operation text,
ipaddress text,
contextrequestjson jsonb,
decision integer,
CONSTRAINT authzdecisionid_fkey FOREIGN KEY (decision)
REFERENCES authz.decision (decisionid) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
NOT VALID
) PARTITION BY RANGE (created);

CREATE INDEX ON authz.eventlogv2 (created);
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public async Task CreatePartitions(IReadOnlyList<Partition> partitions, Cancella
var cmd = batch.CreateBatchCommand();
cmd.CommandText = /*strpsql*/$"""
CREATE TABLE IF NOT EXISTS {partition.SchemaName}.{partition.Name}
PARTITION OF {partition.SchemaName}.eventlogv1
PARTITION OF {partition.SchemaName}.eventlogv2
FOR VALUES FROM ('{partition.StartDate:yyyy-MM-dd}') TO ('{partition.EndDate:yyyy-MM-dd}')
""";
batch.BatchCommands.Add(cmd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ internal IReadOnlyList<Partition> GetPartitionsForCurrentAndAdjacentMonths()
var (nextMonthStartDate, nextMonthEndDate) = GetMonthStartAndEndDate(now.AddMonths(1));

// Create partition names
var pastMonthPartitionName = $"eventlogv1_y{pastMonthStartDate.Year}m{pastMonthStartDate.Month:D2}";
var currentMonthPartitionName = $"eventlogv1_y{currentMonthStartDate.Year}m{currentMonthStartDate.Month:D2}";
var nextMonthPartitionName = $"eventlogv1_y{nextMonthStartDate.Year}m{nextMonthStartDate.Month:D2}";
var pastMonthPartitionName = $"eventlogv2_y{pastMonthStartDate.Year}m{pastMonthStartDate.Month:D2}";
var currentMonthPartitionName = $"eventlogv2_y{currentMonthStartDate.Year}m{currentMonthStartDate.Month:D2}";
var nextMonthPartitionName = $"eventlogv2_y{nextMonthStartDate.Year}m{nextMonthStartDate.Month:D2}";

// List of partitions for both schemas
return new List<Partition>
Expand All @@ -162,7 +162,7 @@ internal IReadOnlyList<Partition> GetPartitionsForCurrentAndAdjacentMonths()
internal (DateOnly startDate, DateOnly endDate) GetMonthStartAndEndDate(DateOnly date)
{
DateOnly startDate = new DateOnly(date.Year, date.Month, 1);
DateOnly endDate = startDate.AddMonths(1).AddDays(-1);
DateOnly endDate = startDate.AddMonths(1);
return (startDate, endDate);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task ExecuteAsync_CreatesCurrentMonthPartition_OnlyOnce()
var currentDate = DateOnly.FromDateTime(TimeProvider.GetUtcNow().UtcDateTime);
var currentMonth = currentDate.Month;
var currentYear = currentDate.Year;
var partitionName = $"eventlogv1_y{currentYear}m{currentMonth:D2}";
var partitionName = $"eventlogv2_y{currentYear}m{currentMonth:D2}";

var checkAuthenticationPartitionCommand = $"SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema='authentication' and table_name='{partitionName}');";
await using NpgsqlCommand pgcom = DataSource.CreateCommand(checkAuthenticationPartitionCommand);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public void GetPartitionsForCurrentAndAdjacentMonths_ReturnsCorrectPartitions()

// Assert
Assert.Equal(6, partitions.Count);
Assert.Contains(partitions, p => p.Name == "eventlogv1_y2023m09" && p.StartDate == new DateOnly(2023, 9, 1) && p.EndDate == new DateOnly(2023, 9, 30));
Assert.Contains(partitions, p => p.Name == "eventlogv1_y2023m10" && p.StartDate == new DateOnly(2023, 10, 1) && p.EndDate == new DateOnly(2023, 10, 31));
Assert.Contains(partitions, p => p.Name == "eventlogv1_y2023m11" && p.StartDate == new DateOnly(2023, 11, 1) && p.EndDate == new DateOnly(2023, 11, 30));
Assert.Contains(partitions, p => p.Name == "eventlogv2_y2023m09" && p.StartDate == new DateOnly(2023, 9, 1) && p.EndDate == new DateOnly(2023, 10, 1));
Assert.Contains(partitions, p => p.Name == "eventlogv2_y2023m10" && p.StartDate == new DateOnly(2023, 10, 1) && p.EndDate == new DateOnly(2023, 11, 1));
Assert.Contains(partitions, p => p.Name == "eventlogv2_y2023m11" && p.StartDate == new DateOnly(2023, 11, 1) && p.EndDate == new DateOnly(2023, 12, 1));
}

[Theory]
[InlineData(2023, 10, 15, 2023, 10, 1, 2023, 10, 31)]
[InlineData(2023, 2, 1, 2023, 2, 1, 2023, 2, 28)]
[InlineData(2024, 2, 1, 2024, 2, 1, 2024, 2, 29)] // Leap year
[InlineData(2023, 10, 15, 2023, 10, 1, 2023, 11, 1)]
[InlineData(2023, 2, 1, 2023, 2, 1, 2023, 3, 1)]
[InlineData(2024, 2, 1, 2024, 2, 1, 2024, 3, 1)]
public void GetMonthStartAndEndDate_ReturnsCorrectDates(int year, int month, int day, int expectedStartYear, int expectedStartMonth, int expectedStartDay, int expectedEndYear, int expectedEndMonth, int expectedEndDay)
{
// Arrange
Expand All @@ -55,8 +55,8 @@ public void GetPartitionsForCurrentAndAdjacentMonths_CrossYearBoundary_ReturnsCo

// Assert
Assert.Equal(6, partitions.Count);
Assert.Contains(partitions, p => p.Name == "eventlogv1_y2023m11" && p.StartDate == new DateOnly(2023, 11, 1) && p.EndDate == new DateOnly(2023, 11, 30));
Assert.Contains(partitions, p => p.Name == "eventlogv1_y2023m12" && p.StartDate == new DateOnly(2023, 12, 1) && p.EndDate == new DateOnly(2023, 12, 31));
Assert.Contains(partitions, p => p.Name == "eventlogv1_y2024m01" && p.StartDate == new DateOnly(2024, 1, 1) && p.EndDate == new DateOnly(2024, 1, 31));
Assert.Contains(partitions, p => p.Name == "eventlogv2_y2023m11" && p.StartDate == new DateOnly(2023, 11, 1) && p.EndDate == new DateOnly(2023, 12, 1));
Assert.Contains(partitions, p => p.Name == "eventlogv2_y2023m12" && p.StartDate == new DateOnly(2023, 12, 1) && p.EndDate == new DateOnly(2024, 1, 1));
Assert.Contains(partitions, p => p.Name == "eventlogv2_y2024m01" && p.StartDate == new DateOnly(2024, 1, 1) && p.EndDate == new DateOnly(2024, 2, 1));
}
}

0 comments on commit aabe5d8

Please sign in to comment.