Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(drizzle): Migrate to Drizzle ORM #404

Merged
merged 14 commits into from
Jun 8, 2024

Conversation

realmikesolo
Copy link
Contributor

@realmikesolo realmikesolo commented May 8, 2024

This PR was made to migrate from Prisma to Drizzle ORM. It involves rewriting the database interactions using Drizzle ORM.

Instructions

If you directly start the project with Drizzle ORM

  1. Remove unnecessary schema file: Delete lib/legacy-schema.ts file as it is only used when migrating from Prisma.
  2. Initialize Schema: The Drizzle schema located in lib/schema.ts will be used for database queries.
  3. Apply changes to the database: Run the drizzle-kit push command to apply your changes to the database. Learn more about the push command here.
  4. Begin using the template: You can now start using this template with Drizzle ORM.

If you migrating from Prisma

  1. Replace the schema file: Remove the existing lib/schema.ts file and rename lib/legacy-schema.ts to lib/schema.ts.
  2. Update database schema: email column in users table is set to not null to ensure compatibility with drizzle next-auth adapter. Apply this change by running the drizzle-kit push command. Learn more about push command here.
  3. Complete migration: You are now ready to use this template with Drizzle ORM.

Copy link

vercel bot commented May 8, 2024

@realmikesolo is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Member

@leerob leerob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great!

@@ -113,7 +109,7 @@ export default async function SitePostPage({
href={
data.site?.user?.username
? `https://twitter.com/${data.site.user.username}`
: `https://github.com/${data.site?.user?.gh_username}`
: `https://github.com/${data.site?.user?.ghUsername}`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this require a data migration?

lib/actions.ts Outdated
})
.returning();

// unnecessary await
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the await?

lib/actions.ts Outdated
.where(eq(sites.id, site.id))
.returning();

await revalidateTag(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await revalidateTag(
revalidateTag(

lib/actions.ts Outdated
`${site.subdomain}.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}-metadata`,
);
response.customDomain &&
(await revalidateTag(`${site.customDomain}-metadata`));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(await revalidateTag(`${site.customDomain}-metadata`));
revalidateTag(`${site.customDomain}-metadata`);

lib/actions.ts Outdated
})
.returning();

await revalidateTag(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await revalidateTag(
revalidateTag(

@m-torin
Copy link

m-torin commented May 9, 2024

It's an interesting choice to move away from a project that enterprises and governments support to one that's unknown and may require security reviews. What benefits make this worth it?

@leerob
Copy link
Member

leerob commented May 9, 2024

@m-torin if you would like to use Prisma, that's great. Please do! I don't have anything against Prisma. In fact, we just worked with them on this https://db-latency.vercel.app/.

@realmikesolo
Copy link
Contributor Author

realmikesolo commented May 11, 2024

I made some edits based on the review, updated the Drizzle schema, upgraded drizzle-kit to version 0.21.1, and created a new Prisma compatible schema with an initial migration. These can be used if switching from Prisma to Drizzle ORM. The migration was created after pulling the DDL from an existing database, which was initially used with Prisma. The only thing that needs to be done is to set the email field in the User table to 'not null' for type compatibility with the Drizzle adapter for next-auth.

Currently, there are two bugs related to drizzle-kit removing foreign key constraints and recreating them, and drizzle-kit detecting changes in timestamp precision even when there are none. These should be fixed soon. Once they are resolved, the Prisma compatible schema can be used. If it's a new project, it's recommended to use the standard Drizzle schema.

Please let me know if anything needs updating.

import { getPostData, getSiteData } from "@/lib/fetchers";
import BlogCard from "@/components/blog-card";
import BlurImage from "@/components/blur-image";
import MDX from "@/components/mdx";
import { placeholderBlurhash, toDateString } from "@/lib/utils";
import db from "@/lib/db/db";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inline


return posts.length > 0 ? (
const query = db

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make in query syntax

});
const query = db
.select({
...getTableColumns(sites),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant

@realmikesolo
Copy link
Contributor Author

Bugs with Drizzle-kit were fixed

Copy link

vercel bot commented Jun 4, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
platforms ❌ Failed (Inspect) Jun 4, 2024 2:10pm

@realmikesolo realmikesolo requested a review from leerob June 7, 2024 18:24
Copy link

🚨 Potential security issues detected. Learn more about Socket for GitHub ↗︎

To accept the risk, merge this PR and you will not be notified again.

Alert Package NoteSource
Nonpermissive License npm/@vercel/analytics@1.3.1
  • orphan: npm/@vercel/analytics@1.3.1
Copyleft License npm/@vercel/analytics@1.3.1
  • orphan: npm/@vercel/analytics@1.3.1
Nonpermissive License npm/@vercel/analytics@1.1.1
Copyleft License npm/@vercel/analytics@1.1.1

View full report↗︎

Next steps

What do I need to know about license files?

(Experimental) A license not known to be considered permissive was found

Determine whether use of material not offered under a known permissive license works for you

What do I need to know about license files?

(Experimental) Copyleft license information was found

Determine whether use of copyleft material works for you

Take a deeper look at the dependency

Take a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at support [AT] socket [DOT] dev.

Remove the package

If you happen to install a dependency that Socket reports as Known Malware you should immediately remove it and select a different dependency. For other alert types, you may may wish to investigate alternative packages or consider if there are other ways to mitigate the specific risk posed by the dependency.

Mark a package as acceptable risk

To ignore an alert, reply with a comment starting with @SocketSecurity ignore followed by a space separated list of ecosystem/package-name@version specifiers. e.g. @SocketSecurity ignore npm/foo@1.0.0 or ignore all packages with @SocketSecurity ignore-all

  • @SocketSecurity ignore npm/@vercel/analytics@1.3.1
  • @SocketSecurity ignore npm/@vercel/analytics@1.1.1

Copy link
Member

@leerob leerob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@leerob leerob merged commit 7e33556 into vercel:main Jun 8, 2024
1 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants