Skip to content

Commit

Permalink
feat: date to timestamp
Browse files Browse the repository at this point in the history
use timestamp instead of date because that includes the time too.
  • Loading branch information
yamcodes committed Oct 8, 2023
1 parent 8cf526c commit e0a1dcb
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 4 deletions.
3 changes: 3 additions & 0 deletions db/migrations/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { drizzle } from 'drizzle-orm/postgres-js';
import { migrate } from 'drizzle-orm/postgres-js/migrator';
import { migrationsClient } from '@/database.providers';
import { exit } from 'process';

await migrate(drizzle(migrationsClient), {
migrationsFolder: `${import.meta.dir}`,
});

exit();
4 changes: 4 additions & 0 deletions src/db/migrations/0001_lonely_albert_cleary.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE "users" ALTER COLUMN "created_at" SET DATA TYPE timestamp;
ALTER TABLE "users" ALTER COLUMN "created_at" SET DEFAULT now();
ALTER TABLE "users" ALTER COLUMN "updated_at" SET DATA TYPE timestamp;
ALTER TABLE "users" ALTER COLUMN "updated_at" SET DEFAULT now();
75 changes: 75 additions & 0 deletions src/db/migrations/meta/0001_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"version": "5",
"dialect": "pg",
"id": "88c3b35e-75d5-4ec6-af4f-eb0c19bb1506",
"prevId": "fd31b50f-294e-45c4-b277-09076a56eb4c",
"tables": {
"users": {
"name": "users",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"email": {
"name": "email",
"type": "text",
"primaryKey": false,
"notNull": true
},
"bio": {
"name": "bio",
"type": "text",
"primaryKey": false,
"notNull": true
},
"image": {
"name": "image",
"type": "text",
"primaryKey": false,
"notNull": true
},
"password": {
"name": "password",
"type": "text",
"primaryKey": false,
"notNull": true
},
"username": {
"name": "username",
"type": "text",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": false,
"default": "now()"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp",
"primaryKey": false,
"notNull": false,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"schemas": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
}
}
7 changes: 7 additions & 0 deletions src/db/migrations/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
"when": 1696355375578,
"tag": "0000_faithful_logan",
"breakpoints": false
},
{
"idx": 1,
"version": "5",
"when": 1696801661624,
"tag": "0001_lonely_albert_cleary",
"breakpoints": false
}
]
}
7 changes: 3 additions & 4 deletions src/users/users.schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Type } from '@sinclair/typebox';
import { sql } from 'drizzle-orm';
import { date, pgTable, serial, text } from 'drizzle-orm/pg-core';
import { pgTable, serial, text, timestamp } from 'drizzle-orm/pg-core';
import { createInsertSchema, createSelectSchema } from 'drizzle-typebox';

export const users = pgTable('users', {
Expand All @@ -10,8 +9,8 @@ export const users = pgTable('users', {
image: text('image').notNull(),
password: text('password').notNull(),
username: text('username').notNull(),
created_at: date('created_at').default(sql`CURRENT_DATE`),
updated_at: date('updated_at').default(sql`CURRENT_DATE`),
created_at: timestamp('created_at').defaultNow(),
updated_at: timestamp('updated_at').defaultNow(),
});

// Schema for inserting a user - can be used to validate API requests
Expand Down

0 comments on commit e0a1dcb

Please sign in to comment.