Skip to content

Commit e6bf2fa

Browse files
committed
Fix migration
1 parent 0dc1cd7 commit e6bf2fa

4 files changed

Lines changed: 11 additions & 8 deletions

File tree

realty-common/src/main/java/io/github/md5sha256/realty/database/RealtyLogicImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ public record RegionInfo(
836836
placeholders.put("price", CurrencyFormatter.format(lease.price()));
837837
placeholders.put("duration", DurationFormatter.format(Duration.ofSeconds(lease.durationSeconds())));
838838
placeholders.put("start_date", lease.startDate().toString());
839-
placeholders.put("end_date", lease.endDate().toString());
839+
placeholders.put("end_date", lease.endDate() != null ? lease.endDate().toString() : "");
840840
if (lease.maxExtensions() != null) {
841841
placeholders.put("extensions", lease.currentMaxExtensions() + "/" + lease.maxExtensions());
842842
} else {

realty-common/src/main/java/io/github/md5sha256/realty/database/entity/LeaseContractEntity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* @param price Rental price (must be > 0)
1616
* @param durationSeconds Lease duration in seconds (must be > 0)
1717
* @param startDate When the lease started
18-
* @param endDate When the current lease period ends
18+
* @param endDate When the current lease period ends, or {@code null} if no tenant
1919
* @param currentMaxExtensions Current extension count (nullable; must be ≤ maxExtensions when present)
2020
* @param maxExtensions Maximum allowed extensions (nullable)
2121
* @see io.github.md5sha256.realty.api.LeaseContract
@@ -27,7 +27,7 @@ public record LeaseContractEntity(
2727
double price,
2828
long durationSeconds,
2929
@NotNull LocalDateTime startDate,
30-
@NotNull LocalDateTime endDate,
30+
@Nullable LocalDateTime endDate,
3131
@Nullable Integer currentMaxExtensions,
3232
@Nullable Integer maxExtensions
3333
) {

realty-common/src/main/java/io/github/md5sha256/realty/database/maria/mapper/MariaLeaseContractMapper.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ INSERT INTO LeaseContract (landlordId, tenantId, price, durationSeconds, startDa
5555
#{price},
5656
#{durationSeconds},
5757
NOW(),
58-
NOW() + INTERVAL #{durationSeconds} SECOND,
58+
CASE WHEN #{tenantId} IS NOT NULL THEN NOW() + INTERVAL #{durationSeconds} SECOND ELSE NULL END,
5959
CASE WHEN #{maxRenewals} >= 0 THEN 0 ELSE NULL END,
6060
CASE WHEN #{maxRenewals} >= 0 THEN #{maxRenewals} ELSE NULL END
6161
)
@@ -166,6 +166,7 @@ AND lc.endDate < NOW()
166166
@Update("""
167167
UPDATE LeaseContract
168168
SET tenantId = NULL,
169+
endDate = NULL,
169170
currentMaxExtensions = CASE WHEN maxExtensions IS NOT NULL THEN 0 ELSE NULL END
170171
WHERE leaseContractId = #{leaseContractId}
171172
""")
@@ -203,6 +204,10 @@ int updateLandlordByRegion(@Param("worldGuardRegionId") @NotNull String worldGua
203204
INNER JOIN Contract c ON c.contractId = lc.leaseContractId AND c.contractType = 'contract'
204205
INNER JOIN RealtyRegion rr ON rr.realtyRegionId = c.realtyRegionId
205206
SET lc.tenantId = #{tenantId},
207+
lc.endDate = CASE
208+
WHEN #{tenantId} IS NULL THEN NULL
209+
ELSE lc.endDate
210+
END,
206211
lc.currentMaxExtensions = CASE
207212
WHEN #{tenantId} IS NULL AND lc.maxExtensions IS NOT NULL THEN 0
208213
ELSE lc.currentMaxExtensions

realty-common/src/main/resources/sql/migrations/V7__lease_end_date.sql

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,5 @@ ALTER TABLE LeaseContract
22
ADD COLUMN endDate DATETIME;
33

44
UPDATE LeaseContract
5-
SET endDate = startDate + INTERVAL durationSeconds SECOND;
6-
7-
ALTER TABLE LeaseContract
8-
MODIFY COLUMN endDate DATETIME NOT NULL;
5+
SET endDate = startDate + INTERVAL durationSeconds SECOND
6+
WHERE tenant IS NOT NULL;

0 commit comments

Comments
 (0)