feat: setup init migration

This commit is contained in:
Henry
2023-11-13 23:37:40 +00:00
parent 3742b68ede
commit 7f2ca58679
8 changed files with 612 additions and 2 deletions

BIN
bun.lockb

Binary file not shown.

View File

@@ -4,6 +4,7 @@ import { env } from "~/env.mjs";
export default {
schema: "./src/server/db/schema.ts",
out: "./src/server/db/migrations",
driver: "mysql2",
dbCredentials: {
connectionString: env.DATABASE_URL,

View File

@@ -5,6 +5,8 @@
"scripts": {
"build": "next build",
"db:push": "dotenv drizzle-kit push:mysql",
"db:generate": "dotenv drizzle-kit generate:mysql",
"db:migrate": "dotenv npx tsx ./src/server/db/migrate.ts",
"db:studio": "dotenv drizzle-kit studio",
"dev": "next dev",
"lint": "next lint",

5
src/server/db/migrate.ts Normal file
View File

@@ -0,0 +1,5 @@
import 'dotenv/config';
import { migrate } from 'drizzle-orm/planetscale-serverless/migrator';
import { db } from './index';
migrate(db, { migrationsFolder: './src/server/db/migrations' }).catch((e) => console.log(e))

View File

@@ -0,0 +1,81 @@
CREATE TABLE `account` (
`userId` varchar(255) NOT NULL,
`type` varchar(255) NOT NULL,
`provider` varchar(255) NOT NULL,
`providerAccountId` varchar(255) NOT NULL,
`refresh_token` text,
`access_token` text,
`expires_at` int,
`token_type` varchar(255),
`scope` varchar(255),
`id_token` text,
`session_state` varchar(255),
CONSTRAINT `account_provider_providerAccountId` PRIMARY KEY(`provider`,`providerAccountId`)
);
--> statement-breakpoint
CREATE TABLE `board` (
`id` bigint AUTO_INCREMENT NOT NULL,
`publicId` varchar(12) NOT NULL,
`name` varchar(255),
`createdBy` varchar(255) NOT NULL,
`createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updatedAt` timestamp ON UPDATE CURRENT_TIMESTAMP,
CONSTRAINT `board_id` PRIMARY KEY(`id`),
CONSTRAINT `board_publicId_unique` UNIQUE(`publicId`)
);
--> statement-breakpoint
CREATE TABLE `card` (
`id` bigint AUTO_INCREMENT NOT NULL,
`publicId` varchar(12) NOT NULL,
`title` varchar(256) NOT NULL,
`description` varchar(256),
`createdBy` varchar(256) NOT NULL,
`createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updatedAt` timestamp ON UPDATE CURRENT_TIMESTAMP,
`listId` bigint NOT NULL,
`index` int NOT NULL,
CONSTRAINT `card_id` PRIMARY KEY(`id`),
CONSTRAINT `card_publicId_unique` UNIQUE(`publicId`),
CONSTRAINT `card_listId_index_unique` UNIQUE(`listId`,`index`)
);
--> statement-breakpoint
CREATE TABLE `list` (
`id` bigint AUTO_INCREMENT NOT NULL,
`publicId` varchar(12) NOT NULL,
`name` varchar(256) NOT NULL,
`createdBy` varchar(256) NOT NULL,
`createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updatedAt` timestamp ON UPDATE CURRENT_TIMESTAMP,
`boardId` bigint NOT NULL,
`index` int NOT NULL,
CONSTRAINT `list_id` PRIMARY KEY(`id`),
CONSTRAINT `list_publicId_unique` UNIQUE(`publicId`),
CONSTRAINT `list_boardId_index_unique` UNIQUE(`boardId`,`index`)
);
--> statement-breakpoint
CREATE TABLE `session` (
`sessionToken` varchar(255) NOT NULL,
`userId` varchar(255) NOT NULL,
`expires` timestamp NOT NULL,
CONSTRAINT `session_sessionToken` PRIMARY KEY(`sessionToken`)
);
--> statement-breakpoint
CREATE TABLE `user` (
`id` varchar(255) NOT NULL,
`name` varchar(255),
`email` varchar(255) NOT NULL,
`emailVerified` timestamp(3) DEFAULT CURRENT_TIMESTAMP(3),
`image` varchar(255),
CONSTRAINT `user_id` PRIMARY KEY(`id`),
CONSTRAINT `user_email_unique` UNIQUE(`email`)
);
--> statement-breakpoint
CREATE TABLE `verificationToken` (
`identifier` varchar(255) NOT NULL,
`token` varchar(255) NOT NULL,
`expires` timestamp NOT NULL,
CONSTRAINT `verificationToken_identifier_token` PRIMARY KEY(`identifier`,`token`)
);
--> statement-breakpoint
CREATE INDEX `userId_idx` ON `account` (`userId`);--> statement-breakpoint
CREATE INDEX `userId_idx` ON `session` (`userId`);

View File

@@ -0,0 +1,508 @@
{
"version": "5",
"dialect": "mysql",
"id": "f4bbbfaf-8b0d-40fd-9414-9e783f1a8197",
"prevId": "00000000-0000-0000-0000-000000000000",
"tables": {
"account": {
"name": "account",
"columns": {
"userId": {
"name": "userId",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"type": {
"name": "type",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"provider": {
"name": "provider",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"providerAccountId": {
"name": "providerAccountId",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"refresh_token": {
"name": "refresh_token",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"access_token": {
"name": "access_token",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"expires_at": {
"name": "expires_at",
"type": "int",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"token_type": {
"name": "token_type",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"scope": {
"name": "scope",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"id_token": {
"name": "id_token",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"session_state": {
"name": "session_state",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {
"userId_idx": {
"name": "userId_idx",
"columns": [
"userId"
],
"isUnique": false
}
},
"foreignKeys": {},
"compositePrimaryKeys": {
"account_provider_providerAccountId": {
"name": "account_provider_providerAccountId",
"columns": [
"provider",
"providerAccountId"
]
}
},
"uniqueConstraints": {}
},
"board": {
"name": "board",
"columns": {
"id": {
"name": "id",
"type": "bigint",
"primaryKey": false,
"notNull": true,
"autoincrement": true
},
"publicId": {
"name": "publicId",
"type": "varchar(12)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"name": {
"name": "name",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"createdBy": {
"name": "createdBy",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"createdAt": {
"name": "createdAt",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "CURRENT_TIMESTAMP"
},
"updatedAt": {
"name": "updatedAt",
"type": "timestamp",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"onUpdate": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {
"board_id": {
"name": "board_id",
"columns": [
"id"
]
}
},
"uniqueConstraints": {
"board_publicId_unique": {
"name": "board_publicId_unique",
"columns": [
"publicId"
]
}
}
},
"card": {
"name": "card",
"columns": {
"id": {
"name": "id",
"type": "bigint",
"primaryKey": false,
"notNull": true,
"autoincrement": true
},
"publicId": {
"name": "publicId",
"type": "varchar(12)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"title": {
"name": "title",
"type": "varchar(256)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"description": {
"name": "description",
"type": "varchar(256)",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"createdBy": {
"name": "createdBy",
"type": "varchar(256)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"createdAt": {
"name": "createdAt",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "CURRENT_TIMESTAMP"
},
"updatedAt": {
"name": "updatedAt",
"type": "timestamp",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"onUpdate": true
},
"listId": {
"name": "listId",
"type": "bigint",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"index": {
"name": "index",
"type": "int",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {
"card_id": {
"name": "card_id",
"columns": [
"id"
]
}
},
"uniqueConstraints": {
"card_publicId_unique": {
"name": "card_publicId_unique",
"columns": [
"publicId"
]
},
"card_listId_index_unique": {
"name": "card_listId_index_unique",
"columns": [
"listId",
"index"
]
}
}
},
"list": {
"name": "list",
"columns": {
"id": {
"name": "id",
"type": "bigint",
"primaryKey": false,
"notNull": true,
"autoincrement": true
},
"publicId": {
"name": "publicId",
"type": "varchar(12)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"name": {
"name": "name",
"type": "varchar(256)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"createdBy": {
"name": "createdBy",
"type": "varchar(256)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"createdAt": {
"name": "createdAt",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "CURRENT_TIMESTAMP"
},
"updatedAt": {
"name": "updatedAt",
"type": "timestamp",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"onUpdate": true
},
"boardId": {
"name": "boardId",
"type": "bigint",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"index": {
"name": "index",
"type": "int",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {
"list_id": {
"name": "list_id",
"columns": [
"id"
]
}
},
"uniqueConstraints": {
"list_publicId_unique": {
"name": "list_publicId_unique",
"columns": [
"publicId"
]
},
"list_boardId_index_unique": {
"name": "list_boardId_index_unique",
"columns": [
"boardId",
"index"
]
}
}
},
"session": {
"name": "session",
"columns": {
"sessionToken": {
"name": "sessionToken",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"userId": {
"name": "userId",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"expires": {
"name": "expires",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {
"userId_idx": {
"name": "userId_idx",
"columns": [
"userId"
],
"isUnique": false
}
},
"foreignKeys": {},
"compositePrimaryKeys": {
"session_sessionToken": {
"name": "session_sessionToken",
"columns": [
"sessionToken"
]
}
},
"uniqueConstraints": {}
},
"user": {
"name": "user",
"columns": {
"id": {
"name": "id",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"name": {
"name": "name",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"email": {
"name": "email",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"emailVerified": {
"name": "emailVerified",
"type": "timestamp(3)",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"default": "CURRENT_TIMESTAMP(3)"
},
"image": {
"name": "image",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {
"user_id": {
"name": "user_id",
"columns": [
"id"
]
}
},
"uniqueConstraints": {
"user_email_unique": {
"name": "user_email_unique",
"columns": [
"email"
]
}
}
},
"verificationToken": {
"name": "verificationToken",
"columns": {
"identifier": {
"name": "identifier",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"token": {
"name": "token",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"expires": {
"name": "expires",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {
"verificationToken_identifier_token": {
"name": "verificationToken_identifier_token",
"columns": [
"identifier",
"token"
]
}
},
"uniqueConstraints": {}
}
},
"schemas": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
}
}

View File

@@ -0,0 +1,13 @@
{
"version": "5",
"dialect": "mysql",
"entries": [
{
"idx": 0,
"version": "5",
"when": 1699896771081,
"tag": "0000_hesitant_nocturne",
"breakpoints": true
}
]
}

View File

@@ -53,7 +53,7 @@ export const lists = mySqlTable(
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: timestamp("updatedAt").onUpdateNow(),
boardId: varchar("boardId", { length: 256 }).notNull(),
boardId: bigint("boardId", { mode: "number" }).notNull(),
index: int("index").notNull()
}, (t) => ({
unq: unique().on(t.boardId, t.index),
@@ -84,7 +84,7 @@ export const cards = mySqlTable(
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: timestamp("updatedAt").onUpdateNow(),
listId: varchar("listId", { length: 256 }).notNull(),
listId: bigint("listId", { mode: "number" }).notNull(),
index: int("index").notNull()
}, (t) => ({
unq: unique().on(t.listId, t.index),