Skip to content

Commit

Permalink
Support special values for postgres defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
1ilit committed Sep 9, 2024
1 parent 557ce72 commit 2107151
Showing 1 changed file with 47 additions and 14 deletions.
61 changes: 47 additions & 14 deletions src/data/datatypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,19 @@ const postgresTypesBase = {
DATE: {
type: "DATE",
checkDefault: (field) => {
return /^\d{4}-\d{2}-\d{2}$/.test(field.default);
const specialValues = [
"epoch",
"infinity",
"-infinity",
"now",
"today",
"tomorrow",
"yesterday",
];
return (
/^\d{4}-\d{2}-\d{2}$/.test(field.default) ||
specialValues.includes(field.default.toLowerCase())
);
},
hasCheck: false,
isSized: false,
Expand All @@ -886,7 +898,11 @@ const postgresTypesBase = {
TIME: {
type: "TIME",
checkDefault: (field) => {
return /^(?:[01]?\d|2[0-3]):[0-5]?\d:[0-5]?\d$/.test(field.default);
const specialValues = ["now", "allballs"];
return (
/^(?:[01]?\d|2[0-3]):[0-5]?\d:[0-5]?\d$/.test(field.default) ||
specialValues.includes(field.default.toLowerCase())
);
},
hasCheck: false,
isSized: false,
Expand All @@ -896,15 +912,23 @@ const postgresTypesBase = {
TIMESTAMP: {
type: "TIMESTAMP",
checkDefault: (field) => {
if (field.default.toUpperCase() === "CURRENT_TIMESTAMP") {
return true;
}
if (!/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(field.default)) {
return false;
}
const content = field.default.split(" ");
const date = content[0].split("-");
return parseInt(date[0]) >= 1970 && parseInt(date[0]) <= 2038;
const specialValues = [
"epoch",
"infinity",
"-infinity",
"now",
"today",
"tomorrow",
"yesterday",
"current_timestamp",
];
return (
/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(field.default) ||
(parseInt(date[0]) >= 1970 && parseInt(date[0]) <= 2038) ||
specialValues.includes(field.default.toLowerCase())
);
},
hasCheck: false,
isSized: false,
Expand All @@ -914,11 +938,20 @@ const postgresTypesBase = {
TIMESTAMPTZ: {
type: "TIMESTAMPTZ",
checkDefault: (field) => {
if (field.default.toUpperCase() === "CURRENT_TIMESTAMP") {
return true;
}
return /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}([+-]\d{2}:\d{2})?$/.test(
field.default,
const specialValues = [
"epoch",
"infinity",
"-infinity",
"now",
"today",
"tomorrow",
"yesterday",
"current_timestamp",
];
return (
/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}([+-]\d{2}:\d{2})?$/.test(
field.default,
) || specialValues.includes(field.default.toLowerCase())
);
},
hasCheck: false,
Expand Down

0 comments on commit 2107151

Please sign in to comment.