changing user, shortener, project id from serial to text

main
TZGyn 1 year ago
parent 45b5f655ce
commit ec7ab7a6da
Signed by: TZGyn
GPG Key ID: 122EAF77AE81FD4A

@ -0,0 +1,8 @@
ALTER TABLE "project" ALTER COLUMN "id" SET DATA TYPE text;--> statement-breakpoint
ALTER TABLE "project" ALTER COLUMN "user_id" SET DATA TYPE text;--> statement-breakpoint
ALTER TABLE "session" ALTER COLUMN "user_id" SET DATA TYPE text;--> statement-breakpoint
ALTER TABLE "shortener" ALTER COLUMN "id" SET DATA TYPE text;--> statement-breakpoint
ALTER TABLE "shortener" ALTER COLUMN "user_id" SET DATA TYPE text;--> statement-breakpoint
ALTER TABLE "shortener" ALTER COLUMN "project_id" SET DATA TYPE text;--> statement-breakpoint
ALTER TABLE "user" ALTER COLUMN "id" SET DATA TYPE text;--> statement-breakpoint
ALTER TABLE "visitor" ALTER COLUMN "shortener_id" SET DATA TYPE text;

@ -0,0 +1,465 @@
{
"id": "10c8e2dc-3707-4281-913f-2c992280fdd0",
"prevId": "22a24e2f-3a4f-4867-bb4a-a391f6cde519",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.email_verification_token": {
"name": "email_verification_token",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "varchar(255)",
"primaryKey": true,
"notNull": true
},
"user_id": {
"name": "user_id",
"type": "integer",
"primaryKey": false,
"notNull": true
},
"email": {
"name": "email",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"expires_at": {
"name": "expires_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"public.project": {
"name": "project",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true
},
"uuid": {
"name": "uuid",
"type": "uuid",
"primaryKey": false,
"notNull": false,
"default": "gen_random_uuid()"
},
"name": {
"name": "name",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"user_id": {
"name": "user_id",
"type": "text",
"primaryKey": false,
"notNull": true
},
"qr_background": {
"name": "qr_background",
"type": "varchar(7)",
"primaryKey": false,
"notNull": true,
"default": "'#ffffff'"
},
"qr_foreground": {
"name": "qr_foreground",
"type": "varchar(7)",
"primaryKey": false,
"notNull": true,
"default": "'#000000'"
},
"domain_status": {
"name": "domain_status",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"default": "'verified'"
},
"enable_custom_domain": {
"name": "enable_custom_domain",
"type": "boolean",
"primaryKey": false,
"notNull": true,
"default": false
},
"custom_ip": {
"name": "custom_ip",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"custom_domain_id": {
"name": "custom_domain_id",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"custom_domain": {
"name": "custom_domain",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"qr_corner_square_style": {
"name": "qr_corner_square_style",
"type": "varchar",
"primaryKey": false,
"notNull": true,
"default": "'square'"
},
"qr_dot_style": {
"name": "qr_dot_style",
"type": "varchar",
"primaryKey": false,
"notNull": true,
"default": "'square'"
},
"qr_image_base64": {
"name": "qr_image_base64",
"type": "text",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"public.session": {
"name": "session",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "varchar(255)",
"primaryKey": true,
"notNull": true
},
"user_id": {
"name": "user_id",
"type": "text",
"primaryKey": false,
"notNull": true
},
"expires_at": {
"name": "expires_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"public.shortener": {
"name": "shortener",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true
},
"link": {
"name": "link",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"ios": {
"name": "ios",
"type": "boolean",
"primaryKey": false,
"notNull": true,
"default": false
},
"ios_link": {
"name": "ios_link",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"android": {
"name": "android",
"type": "boolean",
"primaryKey": false,
"notNull": true,
"default": false
},
"android_link": {
"name": "android_link",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"code": {
"name": "code",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"user_id": {
"name": "user_id",
"type": "text",
"primaryKey": false,
"notNull": true
},
"active": {
"name": "active",
"type": "boolean",
"primaryKey": false,
"notNull": true,
"default": true
},
"project_id": {
"name": "project_id",
"type": "text",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"shortener_code_unique": {
"name": "shortener_code_unique",
"nullsNotDistinct": false,
"columns": [
"code"
]
}
}
},
"public.user": {
"name": "user",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true
},
"uuid": {
"name": "uuid",
"type": "uuid",
"primaryKey": false,
"notNull": false,
"default": "gen_random_uuid()"
},
"email_verified": {
"name": "email_verified",
"type": "boolean",
"primaryKey": false,
"notNull": true,
"default": false
},
"email": {
"name": "email",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"google_id": {
"name": "google_id",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"username": {
"name": "username",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"password": {
"name": "password",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"plan": {
"name": "plan",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"default": "'free'"
},
"stripe_customer_id": {
"name": "stripe_customer_id",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"qr_background": {
"name": "qr_background",
"type": "varchar(7)",
"primaryKey": false,
"notNull": true,
"default": "'#fff'"
},
"qr_foreground": {
"name": "qr_foreground",
"type": "varchar(7)",
"primaryKey": false,
"notNull": true,
"default": "'#000'"
},
"qr_corner_square_style": {
"name": "qr_corner_square_style",
"type": "varchar",
"primaryKey": false,
"notNull": true,
"default": "'square'"
},
"qr_dot_style": {
"name": "qr_dot_style",
"type": "varchar",
"primaryKey": false,
"notNull": true,
"default": "'square'"
},
"qr_image_base64": {
"name": "qr_image_base64",
"type": "text",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"user_email_unique": {
"name": "user_email_unique",
"nullsNotDistinct": false,
"columns": [
"email"
]
}
}
},
"public.visitor": {
"name": "visitor",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"shortener_id": {
"name": "shortener_id",
"type": "text",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"country_code": {
"name": "country_code",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"country": {
"name": "country",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"city": {
"name": "city",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"device_type": {
"name": "device_type",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"default": "''"
},
"device_vendor": {
"name": "device_vendor",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"default": "''"
},
"os": {
"name": "os",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"default": "''"
},
"browser": {
"name": "browser",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"default": "''"
},
"referer": {
"name": "referer",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"default": "''"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"schemas": {},
"sequences": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}

@ -176,6 +176,13 @@
"when": 1725806107053, "when": 1725806107053,
"tag": "0024_fancy_dust", "tag": "0024_fancy_dust",
"breakpoints": true "breakpoints": true
},
{
"idx": 25,
"version": "7",
"when": 1725888846230,
"tag": "0025_clammy_dark_beast",
"breakpoints": true
} }
] ]
} }

@ -35,7 +35,7 @@
let deleteDialogOpen = false let deleteDialogOpen = false
let deleteShortenerCode = '' let deleteShortenerCode = ''
const openDeleteDialog = (code: string | number) => { const openDeleteDialog = (code: string) => {
deleteShortenerCode = code deleteShortenerCode = code
deleteDialogOpen = true deleteDialogOpen = true
} }

@ -3,7 +3,6 @@ import {
serial, serial,
varchar, varchar,
timestamp, timestamp,
integer,
uuid, uuid,
boolean, boolean,
text, text,
@ -11,7 +10,7 @@ import {
import { relations } from 'drizzle-orm' import { relations } from 'drizzle-orm'
export const user = pgTable('user', { export const user = pgTable('user', {
id: serial('id').primaryKey().notNull(), id: text('id').primaryKey(),
uuid: uuid('uuid').defaultRandom(), uuid: uuid('uuid').defaultRandom(),
email_verified: boolean('email_verified').notNull().default(false), email_verified: boolean('email_verified').notNull().default(false),
email: varchar('email', { length: 255 }).notNull().unique(), email: varchar('email', { length: 255 }).notNull().unique(),
@ -44,7 +43,7 @@ export const user = pgTable('user', {
}) })
export const shortener = pgTable('shortener', { export const shortener = pgTable('shortener', {
id: serial('id').primaryKey().notNull(), id: text('id').primaryKey(),
link: varchar('link', { length: 255 }).notNull(), link: varchar('link', { length: 255 }).notNull(),
ios: boolean('ios').notNull().default(false), ios: boolean('ios').notNull().default(false),
ios_link: varchar('ios_link', { length: 255 }), ios_link: varchar('ios_link', { length: 255 }),
@ -54,16 +53,16 @@ export const shortener = pgTable('shortener', {
createdAt: timestamp('created_at', { mode: 'string' }) createdAt: timestamp('created_at', { mode: 'string' })
.defaultNow() .defaultNow()
.notNull(), .notNull(),
userId: integer('user_id').notNull(), userId: text('user_id').notNull(),
active: boolean('active').notNull().default(true), active: boolean('active').notNull().default(true),
projectId: integer('project_id'), projectId: text('project_id'),
}) })
export const project = pgTable('project', { export const project = pgTable('project', {
id: serial('id').primaryKey().notNull(), id: text('id').primaryKey(),
uuid: uuid('uuid').defaultRandom(), uuid: uuid('uuid').defaultRandom(),
name: varchar('name', { length: 255 }).notNull(), name: varchar('name', { length: 255 }).notNull(),
userId: integer('user_id').notNull(), userId: text('user_id').notNull(),
qr_background: varchar('qr_background', { length: 7 }) qr_background: varchar('qr_background', { length: 7 })
.default('#ffffff') .default('#ffffff')
.notNull(), .notNull(),
@ -95,7 +94,7 @@ export const project = pgTable('project', {
export const visitor = pgTable('visitor', { export const visitor = pgTable('visitor', {
id: serial('id').primaryKey().notNull(), id: serial('id').primaryKey().notNull(),
shortenerId: integer('shortener_id').notNull(), shortenerId: text('shortener_id').notNull(),
createdAt: timestamp('created_at', { mode: 'date' }) createdAt: timestamp('created_at', { mode: 'date' })
.defaultNow() .defaultNow()
.notNull(), .notNull(),
@ -117,7 +116,7 @@ export const visitor = pgTable('visitor', {
export const session = pgTable('session', { export const session = pgTable('session', {
id: varchar('id', { length: 255 }).primaryKey(), id: varchar('id', { length: 255 }).primaryKey(),
userId: integer('user_id').notNull(), userId: text('user_id').notNull(),
expiresAt: timestamp('expires_at', { expiresAt: timestamp('expires_at', {
withTimezone: true, withTimezone: true,
mode: 'date', mode: 'date',
@ -128,7 +127,7 @@ export const emailVerificationToken = pgTable(
'email_verification_token', 'email_verification_token',
{ {
id: varchar('id', { length: 255 }).primaryKey().notNull(), id: varchar('id', { length: 255 }).primaryKey().notNull(),
userId: integer('user_id').notNull(), userId: text('user_id').notNull(),
email: varchar('email', { length: 255 }).notNull(), email: varchar('email', { length: 255 }).notNull(),
expiresAt: timestamp('expires_at', { expiresAt: timestamp('expires_at', {
withTimezone: true, withTimezone: true,

@ -9,7 +9,7 @@ import { env } from '$env/dynamic/private'
declare module 'lucia' { declare module 'lucia' {
interface Register { interface Register {
Lucia: typeof lucia Lucia: typeof lucia
UserId: number UserId: string
DatabaseUserAttributes: DatabaseUserAttributes DatabaseUserAttributes: DatabaseUserAttributes
} }
} }

@ -36,7 +36,7 @@ export const sendEmailVerification = async ({
userId, userId,
email, email,
}: { }: {
userId: number userId: string
email: string email: string
}) => { }) => {
await db await db

@ -16,6 +16,7 @@ import { formSchema } from './schema'
import type { Actions } from './$types' import type { Actions } from './$types'
import { nanoid } from 'nanoid' import { nanoid } from 'nanoid'
import { isAlphanumeric } from '$lib/utils' import { isAlphanumeric } from '$lib/utils'
import { generateId } from 'lucia'
export const load = (async (event) => { export const load = (async (event) => {
const user = event.locals.user const user = event.locals.user
@ -44,7 +45,7 @@ export const load = (async (event) => {
sortBy = 'latest' sortBy = 'latest'
} }
let project_id: number | undefined let project_id: string | undefined
let selected_project: { value: null | string; label: string } = { let selected_project: { value: null | string; label: string } = {
value: null, value: null,
label: 'All', label: 'All',
@ -185,6 +186,7 @@ export const actions: Actions = {
? form.data.custom_code ? form.data.custom_code
: nanoid(8) : nanoid(8)
await db.insert(shortener).values({ await db.insert(shortener).values({
id: generateId(8),
link: form.data.link, link: form.data.link,
projectId: project?.id, projectId: project?.id,
userId: user.id, userId: user.id,

@ -5,6 +5,7 @@ import { zod } from 'sveltekit-superforms/adapters'
import { formSchema } from './schema' import { formSchema } from './schema'
import { fail, type Actions } from '@sveltejs/kit' import { fail, type Actions } from '@sveltejs/kit'
import { project } from '$lib/db/schema' import { project } from '$lib/db/schema'
import { generateId } from 'lucia'
export const load = (async (event) => { export const load = (async (event) => {
const user = event.locals.user const user = event.locals.user
@ -31,6 +32,7 @@ export const actions: Actions = {
const user = event.locals.user const user = event.locals.user
await db.insert(project).values({ await db.insert(project).values({
id: generateId(8),
name: form.data.name, name: form.data.name,
userId: user.id, userId: user.id,
}) })

@ -31,7 +31,7 @@
let deleteDialogOpen = false let deleteDialogOpen = false
let deleteShortenerCode = '' let deleteShortenerCode = ''
const openDeleteDialog = (code: string | number) => { const openDeleteDialog = (code: string) => {
deleteShortenerCode = code deleteShortenerCode = code
deleteDialogOpen = true deleteDialogOpen = true
} }

@ -16,6 +16,7 @@ import { formSchema } from './schema'
import type { Actions } from './$types' import type { Actions } from './$types'
import { nanoid } from 'nanoid' import { nanoid } from 'nanoid'
import { isAlphanumeric } from '$lib/utils' import { isAlphanumeric } from '$lib/utils'
import { generateId } from 'lucia'
export const load = (async (event) => { export const load = (async (event) => {
const { project: selectedProject } = await event.parent() const { project: selectedProject } = await event.parent()
@ -155,6 +156,7 @@ export const actions: Actions = {
? form.data.custom_code ? form.data.custom_code
: nanoid(8) : nanoid(8)
await db.insert(shortener).values({ await db.insert(shortener).values({
id: generateId(8),
link: form.data.link, link: form.data.link,
projectId: project.id, projectId: project.id,
userId: user.id, userId: user.id,

@ -3,6 +3,7 @@ import { google, lucia } from '$lib/server/auth'
import { db } from '$lib/db' import { db } from '$lib/db'
import { user } from '$lib/db/schema' import { user } from '$lib/db/schema'
import { eq } from 'drizzle-orm' import { eq } from 'drizzle-orm'
import { generateId } from 'lucia'
interface GoogleUser { interface GoogleUser {
sub: string // Unique identifier for the user sub: string // Unique identifier for the user
@ -93,6 +94,7 @@ export async function GET(event) {
const insertUser = await db const insertUser = await db
.insert(user) .insert(user)
.values({ .values({
id: generateId(8),
email: googleUser.email, // Using email as username email: googleUser.email, // Using email as username
email_verified: true, email_verified: true,
googleId: googleUser.sub, googleId: googleUser.sub,

@ -10,6 +10,7 @@ import { lucia } from '$lib/server/auth'
import { env } from '$env/dynamic/private' import { env } from '$env/dynamic/private'
import { sendEmailVerification } from '$lib/server/email' import { sendEmailVerification } from '$lib/server/email'
import * as argon2 from 'argon2' import * as argon2 from 'argon2'
import { generateId } from 'lucia'
export const load = (async (event) => { export const load = (async (event) => {
return { return {
@ -52,6 +53,7 @@ export const actions: Actions = {
const returnUsers = await db const returnUsers = await db
.insert(userSchema) .insert(userSchema)
.values({ .values({
id: generateId(8),
email: form.data.email, email: form.data.email,
password: hashedPassword, password: hashedPassword,
}) })

@ -3,6 +3,7 @@ import type { RequestHandler } from './$types'
import { db } from '$lib/db' import { db } from '$lib/db'
import { shortener } from '$lib/db/schema' import { shortener } from '$lib/db/schema'
import { nanoid } from 'nanoid' import { nanoid } from 'nanoid'
import { generateId } from 'lucia'
export const GET: RequestHandler = async () => { export const GET: RequestHandler = async () => {
return new Response() return new Response()
@ -10,7 +11,7 @@ export const GET: RequestHandler = async () => {
const shortenerInsertSchema = z.object({ const shortenerInsertSchema = z.object({
link: z.string().url('Link must be in url format'), link: z.string().url('Link must be in url format'),
projectId: z.number().nullish(), projectId: z.string().nullish(),
}) })
export const POST: RequestHandler = async (event) => { export const POST: RequestHandler = async (event) => {
@ -32,6 +33,7 @@ export const POST: RequestHandler = async (event) => {
const code = nanoid(8) const code = nanoid(8)
await db.insert(shortener).values({ await db.insert(shortener).values({
id: generateId(8),
link: shortenerInsert.data.link, link: shortenerInsert.data.link,
projectId: shortenerInsert.data.projectId, projectId: shortenerInsert.data.projectId,
userId: user.id, userId: user.id,

@ -31,7 +31,7 @@ export const GET: RequestHandler = async (event) => {
const updateShortenerSchema = z.object({ const updateShortenerSchema = z.object({
link: z.string().url(), link: z.string().url(),
projectId: z.number().nullish(), projectId: z.string().nullish(),
active: z.boolean(), active: z.boolean(),
}) })
@ -71,7 +71,7 @@ export const PUT: RequestHandler = async (event) => {
export const DELETE: RequestHandler = async (event) => { export const DELETE: RequestHandler = async (event) => {
const shortenerId = event.params.id const shortenerId = event.params.id
const id = z.coerce.number().positive().safeParse(shortenerId) const id = z.string().safeParse(shortenerId)
if (!id.success) { if (!id.success) {
return new Response( return new Response(

@ -16,10 +16,10 @@ type EmailVerificationToken struct {
} }
type Project struct { type Project struct {
ID int32 ID string
Uuid pgtype.UUID Uuid pgtype.UUID
Name string Name string
UserID int32 UserID string
QrBackground string QrBackground string
QrForeground string QrForeground string
CustomDomain pgtype.Text CustomDomain pgtype.Text
@ -34,17 +34,17 @@ type Project struct {
type Session struct { type Session struct {
ID string ID string
UserID int32 UserID string
ExpiresAt pgtype.Timestamptz ExpiresAt pgtype.Timestamptz
} }
type Shortener struct { type Shortener struct {
ID int32 ID string
Link string Link string
Code string Code string
CreatedAt pgtype.Timestamp CreatedAt pgtype.Timestamp
UserID int32 UserID string
ProjectID pgtype.Int4 ProjectID pgtype.Text
Active bool Active bool
Ios bool Ios bool
IosLink pgtype.Text IosLink pgtype.Text
@ -53,7 +53,7 @@ type Shortener struct {
} }
type User struct { type User struct {
ID int32 ID string
Uuid pgtype.UUID Uuid pgtype.UUID
Email string Email string
Username pgtype.Text Username pgtype.Text
@ -72,7 +72,7 @@ type User struct {
type Visitor struct { type Visitor struct {
ID int32 ID int32
ShortenerID int32 ShortenerID string
CreatedAt pgtype.Timestamp CreatedAt pgtype.Timestamp
CountryCode string CountryCode string
Country string Country string

@ -27,7 +27,7 @@ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
` `
type CreateVisitorParams struct { type CreateVisitorParams struct {
ShortenerID int32 ShortenerID string
DeviceType string DeviceType string
DeviceVendor string DeviceVendor string
Browser string Browser string
@ -96,12 +96,12 @@ type GetShortenerWithDomainParams struct {
} }
type GetShortenerWithDomainRow struct { type GetShortenerWithDomainRow struct {
ID int32 ID string
Link string Link string
Code string Code string
CreatedAt pgtype.Timestamp CreatedAt pgtype.Timestamp
UserID int32 UserID string
ProjectID pgtype.Int4 ProjectID pgtype.Text
Active bool Active bool
Ios bool Ios bool
IosLink pgtype.Text IosLink pgtype.Text

@ -117,7 +117,7 @@ func main() {
uastrings := c.GetReqHeaders()["User-Agent"] uastrings := c.GetReqHeaders()["User-Agent"]
redirecturl := "" redirecturl := ""
var shortenerId int32 var shortenerId string
iosEnabled := false iosEnabled := false
iosRedirectUrl := "" iosRedirectUrl := ""
androidEnabled := false androidEnabled := false

Loading…
Cancel
Save