From bffe472e9e515d28c61ec7a785ce9bc65d9795a8 Mon Sep 17 00:00:00 2001 From: x7airworker Date: Tue, 12 Mar 2024 18:52:35 +0100 Subject: [PATCH 1/3] Made dates fields not nullable --- .../org/javawebstack/orm/migration/AutoMigrator.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/javawebstack/orm/migration/AutoMigrator.java b/src/main/java/org/javawebstack/orm/migration/AutoMigrator.java index e97ab25..a0228b1 100644 --- a/src/main/java/org/javawebstack/orm/migration/AutoMigrator.java +++ b/src/main/java/org/javawebstack/orm/migration/AutoMigrator.java @@ -63,7 +63,7 @@ private static void migrateTable(SQLPool pool, TableInfo info, boolean tableExis sb.append('(') .append(parameterTypes) .append(')'); - sb.append(info.isNotNull(fieldName) ? " NOT NULL" : " NULL"); + sb.append(info.isNotNull(fieldName) || isDatesField(info, fieldName) ? " NOT NULL" : " NULL"); if (info.isAutoIncrement() && info.getIdField().equals(fieldName)) sb.append(" AUTO_INCREMENT"); if (columnKeys.containsKey(columnName)) { @@ -194,4 +194,11 @@ private static List getTables(SQLPool pool) { } } + private static boolean isDatesField(TableInfo info, String fieldName) { + if (!info.hasDates()) + return false; + + return fieldName.equals(info.getCreatedField()) || fieldName.equals(info.getUpdatedField()); + } + } From b3146d6c94a0236e3ba1a663396adc43e282375b Mon Sep 17 00:00:00 2001 From: x7airworker Date: Wed, 13 Mar 2024 18:27:52 +0100 Subject: [PATCH 2/3] Changed implementation to isNotNull method --- src/main/java/org/javawebstack/orm/TableInfo.java | 9 +++++++++ .../org/javawebstack/orm/migration/AutoMigrator.java | 9 +-------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/javawebstack/orm/TableInfo.java b/src/main/java/org/javawebstack/orm/TableInfo.java index b9c1c59..79471ee 100644 --- a/src/main/java/org/javawebstack/orm/TableInfo.java +++ b/src/main/java/org/javawebstack/orm/TableInfo.java @@ -289,6 +289,15 @@ public Object getDefault(String fieldName) { public boolean isNotNull(String fieldName) { if (idField.equals(fieldName)) return true; + + if (hasDates()) { + if (fieldName.equals(getCreatedField())) + return true; + if (fieldName.equals(getUpdatedField())) + return true; + } + + return false; } diff --git a/src/main/java/org/javawebstack/orm/migration/AutoMigrator.java b/src/main/java/org/javawebstack/orm/migration/AutoMigrator.java index a0228b1..4f2eb0b 100644 --- a/src/main/java/org/javawebstack/orm/migration/AutoMigrator.java +++ b/src/main/java/org/javawebstack/orm/migration/AutoMigrator.java @@ -63,7 +63,7 @@ private static void migrateTable(SQLPool pool, TableInfo info, boolean tableExis sb.append('(') .append(parameterTypes) .append(')'); - sb.append(info.isNotNull(fieldName) || isDatesField(info, fieldName) ? " NOT NULL" : " NULL"); + sb.append(info.isNotNull(fieldName) ? " NOT NULL" : " NULL"); if (info.isAutoIncrement() && info.getIdField().equals(fieldName)) sb.append(" AUTO_INCREMENT"); if (columnKeys.containsKey(columnName)) { @@ -194,11 +194,4 @@ private static List getTables(SQLPool pool) { } } - private static boolean isDatesField(TableInfo info, String fieldName) { - if (!info.hasDates()) - return false; - - return fieldName.equals(info.getCreatedField()) || fieldName.equals(info.getUpdatedField()); - } - } From d361a2fe1400422f8b30575eea7b43a2b754ceed Mon Sep 17 00:00:00 2001 From: x7airworker Date: Wed, 13 Mar 2024 18:28:25 +0100 Subject: [PATCH 3/3] Removed space --- src/main/java/org/javawebstack/orm/migration/AutoMigrator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/javawebstack/orm/migration/AutoMigrator.java b/src/main/java/org/javawebstack/orm/migration/AutoMigrator.java index 4f2eb0b..e97ab25 100644 --- a/src/main/java/org/javawebstack/orm/migration/AutoMigrator.java +++ b/src/main/java/org/javawebstack/orm/migration/AutoMigrator.java @@ -63,7 +63,7 @@ private static void migrateTable(SQLPool pool, TableInfo info, boolean tableExis sb.append('(') .append(parameterTypes) .append(')'); - sb.append(info.isNotNull(fieldName) ? " NOT NULL" : " NULL"); + sb.append(info.isNotNull(fieldName) ? " NOT NULL" : " NULL"); if (info.isAutoIncrement() && info.getIdField().equals(fieldName)) sb.append(" AUTO_INCREMENT"); if (columnKeys.containsKey(columnName)) {