BLOG-43 feat: created_time defult value & updated_time trigger
All checks were successful
Frontend CI / build (push) Successful in 1m28s
PR Title Check / pr-title-check (pull_request) Successful in 16s

This commit is contained in:
SquidSpirit 2025-05-22 16:34:47 +08:00
parent e49c5b888a
commit 3b374b2d75

View File

@ -8,8 +8,8 @@ CREATE TABLE "post" (
"content" TEXT NOT NULL, "content" TEXT NOT NULL,
"published_time" TIMESTAMP, "published_time" TIMESTAMP,
"deleted_time" TIMESTAMP, "deleted_time" TIMESTAMP,
"created_time" TIMESTAMP NOT NULL, "created_time" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_time" TIMESTAMP NOT NULL "updated_time" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
); );
CREATE TABLE "label" ( CREATE TABLE "label" (
@ -17,8 +17,8 @@ CREATE TABLE "label" (
"name" TEXT NOT NULL, "name" TEXT NOT NULL,
"color" BIGINT NOT NULL CHECK ("color" >= 0 AND "color" <= 4294967295), "color" BIGINT NOT NULL CHECK ("color" >= 0 AND "color" <= 4294967295),
"deleted_time" TIMESTAMP, "deleted_time" TIMESTAMP,
"created_time" TIMESTAMP NOT NULL, "created_time" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_time" TIMESTAMP NOT NULL "updated_time" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
); );
CREATE TABLE "post_label" ( CREATE TABLE "post_label" (
@ -28,3 +28,24 @@ CREATE TABLE "post_label" (
FOREIGN KEY ("post_id") REFERENCES "post" ("id") ON DELETE CASCADE, FOREIGN KEY ("post_id") REFERENCES "post" ("id") ON DELETE CASCADE,
FOREIGN KEY ("label_id") REFERENCES "label" ("id") ON DELETE CASCADE FOREIGN KEY ("label_id") REFERENCES "label" ("id") ON DELETE CASCADE
); );
-- Auto update `updated_time` trigger
CREATE FUNCTION update_updated_time_column() RETURNS TRIGGER AS $$
BEGIN
NEW.updated_time = CURRENT_TIMESTAMP;
return NEW;
END;
$$ LANGUAGE 'plpgsql';
CREATE TRIGGER "update_post_updated_time"
BEFORE UPDATE ON "post"
FOR EACH ROW
EXECUTE FUNCTION update_updated_time_column();
CREATE TRIGGER "update_label_updated_time"
BEFORE UPDATE ON "label"
FOR EACH ROW
EXECUTE FUNCTION update_updated_time_column();