Skip to content

Commit 419ea13

Browse files
committed
Support special values for postgres defaults (#238)
1 parent 557ce72 commit 419ea13

File tree

1 file changed

+47
-14
lines changed

1 file changed

+47
-14
lines changed

src/data/datatypes.js

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,19 @@ const postgresTypesBase = {
876876
DATE: {
877877
type: "DATE",
878878
checkDefault: (field) => {
879-
return /^\d{4}-\d{2}-\d{2}$/.test(field.default);
879+
const specialValues = [
880+
"epoch",
881+
"infinity",
882+
"-infinity",
883+
"now",
884+
"today",
885+
"tomorrow",
886+
"yesterday",
887+
];
888+
return (
889+
/^\d{4}-\d{2}-\d{2}$/.test(field.default) ||
890+
specialValues.includes(field.default.toLowerCase())
891+
);
880892
},
881893
hasCheck: false,
882894
isSized: false,
@@ -886,7 +898,11 @@ const postgresTypesBase = {
886898
TIME: {
887899
type: "TIME",
888900
checkDefault: (field) => {
889-
return /^(?:[01]?\d|2[0-3]):[0-5]?\d:[0-5]?\d$/.test(field.default);
901+
const specialValues = ["now", "allballs"];
902+
return (
903+
/^(?:[01]?\d|2[0-3]):[0-5]?\d:[0-5]?\d$/.test(field.default) ||
904+
specialValues.includes(field.default.toLowerCase())
905+
);
890906
},
891907
hasCheck: false,
892908
isSized: false,
@@ -896,15 +912,23 @@ const postgresTypesBase = {
896912
TIMESTAMP: {
897913
type: "TIMESTAMP",
898914
checkDefault: (field) => {
899-
if (field.default.toUpperCase() === "CURRENT_TIMESTAMP") {
900-
return true;
901-
}
902-
if (!/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(field.default)) {
903-
return false;
904-
}
905915
const content = field.default.split(" ");
906916
const date = content[0].split("-");
907-
return parseInt(date[0]) >= 1970 && parseInt(date[0]) <= 2038;
917+
const specialValues = [
918+
"epoch",
919+
"infinity",
920+
"-infinity",
921+
"now",
922+
"today",
923+
"tomorrow",
924+
"yesterday",
925+
"current_timestamp",
926+
];
927+
return (
928+
/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(field.default) ||
929+
(parseInt(date[0]) >= 1970 && parseInt(date[0]) <= 2038) ||
930+
specialValues.includes(field.default.toLowerCase())
931+
);
908932
},
909933
hasCheck: false,
910934
isSized: false,
@@ -914,11 +938,20 @@ const postgresTypesBase = {
914938
TIMESTAMPTZ: {
915939
type: "TIMESTAMPTZ",
916940
checkDefault: (field) => {
917-
if (field.default.toUpperCase() === "CURRENT_TIMESTAMP") {
918-
return true;
919-
}
920-
return /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}([+-]\d{2}:\d{2})?$/.test(
921-
field.default,
941+
const specialValues = [
942+
"epoch",
943+
"infinity",
944+
"-infinity",
945+
"now",
946+
"today",
947+
"tomorrow",
948+
"yesterday",
949+
"current_timestamp",
950+
];
951+
return (
952+
/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}([+-]\d{2}:\d{2})?$/.test(
953+
field.default,
954+
) || specialValues.includes(field.default.toLowerCase())
922955
);
923956
},
924957
hasCheck: false,

0 commit comments

Comments
 (0)