Migrate from postgres to sqlite

This commit is contained in:
2024-09-03 19:51:37 +02:00
parent 67aa65386e
commit 8b97e64b59
13 changed files with 474 additions and 461 deletions

View File

@@ -0,0 +1,48 @@
CREATE TABLE `categories` (
`id` integer PRIMARY KEY NOT NULL,
`name` text DEFAULT '',
`name_visible` integer DEFAULT true,
`icon` text DEFAULT 'i-ph:circle-wavy-question-duotone',
`color` text DEFAULT 'gray',
`user_id` integer NOT NULL,
`created_at` integer,
`updated_at` integer,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `tabs` (
`id` integer PRIMARY KEY NOT NULL,
`name` text DEFAULT '',
`primary` integer DEFAULT false,
`icon` text DEFAULT 'i-ph:circle-wavy-question-duotone',
`color` text DEFAULT 'gray',
`link` text DEFAULT '',
`category_id` integer NOT NULL,
`created_at` integer,
`updated_at` integer,
FOREIGN KEY (`category_id`) REFERENCES `categories`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `users` (
`id` integer PRIMARY KEY NOT NULL,
`username` text NOT NULL,
`name` text NOT NULL,
`email` text NOT NULL,
`github_id` text,
`github_token` text,
`google_id` text,
`google_token` text,
`description` text DEFAULT '',
`avatar` text DEFAULT '',
`private` integer DEFAULT false,
`language` text DEFAULT 'en-EN',
`location` text DEFAULT 'unknown',
`subscription` text DEFAULT 'free',
`created_at` integer,
`updated_at` integer
);
--> statement-breakpoint
CREATE UNIQUE INDEX `users_username_unique` ON `users` (`username`);--> statement-breakpoint
CREATE UNIQUE INDEX `users_email_unique` ON `users` (`email`);--> statement-breakpoint
CREATE UNIQUE INDEX `users_github_id_unique` ON `users` (`github_id`);--> statement-breakpoint
CREATE UNIQUE INDEX `users_google_id_unique` ON `users` (`google_id`);

View File

@@ -1,63 +0,0 @@
DO $$ BEGIN
CREATE TYPE "public"."subscription" AS ENUM('free', 'paid');
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "categories" (
"id" serial PRIMARY KEY NOT NULL,
"name" text DEFAULT '',
"name_visible" boolean DEFAULT true,
"icon" text DEFAULT 'i-ph:circle-wavy-question-duotone',
"color" text DEFAULT 'gray',
"user_id" integer NOT NULL,
"created_at" timestamp(0) with time zone DEFAULT now(),
"updated_at" timestamp(0) with time zone DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "tabs" (
"id" serial PRIMARY KEY NOT NULL,
"name" text DEFAULT '',
"primary" boolean DEFAULT false,
"icon" text DEFAULT 'i-ph:circle-wavy-question-duotone',
"color" text DEFAULT 'gray',
"link" text DEFAULT '',
"category_id" integer NOT NULL,
"created_at" timestamp(0) with time zone DEFAULT now(),
"updated_at" timestamp(0) with time zone DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "users" (
"id" serial PRIMARY KEY NOT NULL,
"username" text NOT NULL,
"name" text NOT NULL,
"email" text NOT NULL,
"github_id" text,
"github_token" text,
"google_id" text,
"google_token" text,
"description" text DEFAULT '',
"avatar" text DEFAULT '',
"private" boolean DEFAULT false,
"language" text DEFAULT 'en-EN',
"location" text DEFAULT 'unknown',
"subscription" "subscription" DEFAULT 'free',
"created_at" timestamp(0) with time zone DEFAULT now(),
"updated_at" timestamp(0) with time zone DEFAULT now(),
CONSTRAINT "users_username_unique" UNIQUE("username"),
CONSTRAINT "users_email_unique" UNIQUE("email"),
CONSTRAINT "users_github_id_unique" UNIQUE("github_id"),
CONSTRAINT "users_google_id_unique" UNIQUE("google_id")
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "categories" ADD CONSTRAINT "categories_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "tabs" ADD CONSTRAINT "tabs_category_id_categories_id_fk" FOREIGN KEY ("category_id") REFERENCES "public"."categories"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;

View File

@@ -1,31 +1,33 @@
{
"id": "21470761-4c33-4588-be9b-4927bbcbfe2c",
"version": "6",
"dialect": "sqlite",
"id": "2764f724-b59e-4de8-b7e7-691122ed7e73",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.categories": {
"categories": {
"name": "categories",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"type": "integer",
"primaryKey": true,
"notNull": true
"notNull": true,
"autoincrement": false
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"default": "''"
},
"name_visible": {
"name": "name_visible",
"type": "boolean",
"type": "integer",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"default": true
},
"icon": {
@@ -33,6 +35,7 @@
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"default": "'i-ph:circle-wavy-question-duotone'"
},
"color": {
@@ -40,27 +43,29 @@
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"default": "'gray'"
},
"user_id": {
"name": "user_id",
"type": "integer",
"primaryKey": false,
"notNull": true
"notNull": true,
"autoincrement": false
},
"created_at": {
"name": "created_at",
"type": "timestamp(0) with time zone",
"type": "integer",
"primaryKey": false,
"notNull": false,
"default": "now()"
"autoincrement": false
},
"updated_at": {
"name": "updated_at",
"type": "timestamp(0) with time zone",
"type": "integer",
"primaryKey": false,
"notNull": false,
"default": "now()"
"autoincrement": false
}
},
"indexes": {},
@@ -82,28 +87,30 @@
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"public.tabs": {
"tabs": {
"name": "tabs",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"type": "integer",
"primaryKey": true,
"notNull": true
"notNull": true,
"autoincrement": false
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"default": "''"
},
"primary": {
"name": "primary",
"type": "boolean",
"type": "integer",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"default": false
},
"icon": {
@@ -111,6 +118,7 @@
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"default": "'i-ph:circle-wavy-question-duotone'"
},
"color": {
@@ -118,6 +126,7 @@
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"default": "'gray'"
},
"link": {
@@ -125,27 +134,29 @@
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"default": "''"
},
"category_id": {
"name": "category_id",
"type": "integer",
"primaryKey": false,
"notNull": true
"notNull": true,
"autoincrement": false
},
"created_at": {
"name": "created_at",
"type": "timestamp(0) with time zone",
"type": "integer",
"primaryKey": false,
"notNull": false,
"default": "now()"
"autoincrement": false
},
"updated_at": {
"name": "updated_at",
"type": "timestamp(0) with time zone",
"type": "integer",
"primaryKey": false,
"notNull": false,
"default": "now()"
"autoincrement": false
}
},
"indexes": {},
@@ -167,63 +178,71 @@
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"public.users": {
"users": {
"name": "users",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"type": "integer",
"primaryKey": true,
"notNull": true
"notNull": true,
"autoincrement": false
},
"username": {
"name": "username",
"type": "text",
"primaryKey": false,
"notNull": true
"notNull": true,
"autoincrement": false
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true
"notNull": true,
"autoincrement": false
},
"email": {
"name": "email",
"type": "text",
"primaryKey": false,
"notNull": true
"notNull": true,
"autoincrement": false
},
"github_id": {
"name": "github_id",
"type": "text",
"primaryKey": false,
"notNull": false
"notNull": false,
"autoincrement": false
},
"github_token": {
"name": "github_token",
"type": "text",
"primaryKey": false,
"notNull": false
"notNull": false,
"autoincrement": false
},
"google_id": {
"name": "google_id",
"type": "text",
"primaryKey": false,
"notNull": false
"notNull": false,
"autoincrement": false
},
"google_token": {
"name": "google_token",
"type": "text",
"primaryKey": false,
"notNull": false
"notNull": false,
"autoincrement": false
},
"description": {
"name": "description",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"default": "''"
},
"avatar": {
@@ -231,13 +250,15 @@
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"default": "''"
},
"private": {
"name": "private",
"type": "boolean",
"type": "integer",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"default": false
},
"language": {
@@ -245,6 +266,7 @@
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"default": "'en-EN'"
},
"location": {
@@ -252,81 +274,74 @@
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"default": "'unknown'"
},
"subscription": {
"name": "subscription",
"type": "subscription",
"typeSchema": "public",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"default": "'free'"
},
"created_at": {
"name": "created_at",
"type": "timestamp(0) with time zone",
"type": "integer",
"primaryKey": false,
"notNull": false,
"default": "now()"
"autoincrement": false
},
"updated_at": {
"name": "updated_at",
"type": "timestamp(0) with time zone",
"type": "integer",
"primaryKey": false,
"notNull": false,
"default": "now()"
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"indexes": {
"users_username_unique": {
"name": "users_username_unique",
"nullsNotDistinct": false,
"columns": [
"username"
]
],
"isUnique": true
},
"users_email_unique": {
"name": "users_email_unique",
"nullsNotDistinct": false,
"columns": [
"email"
]
],
"isUnique": true
},
"users_github_id_unique": {
"name": "users_github_id_unique",
"nullsNotDistinct": false,
"columns": [
"github_id"
]
],
"isUnique": true
},
"users_google_id_unique": {
"name": "users_google_id_unique",
"nullsNotDistinct": false,
"columns": [
"google_id"
]
],
"isUnique": true
}
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {
"public.subscription": {
"name": "subscription",
"schema": "public",
"values": [
"free",
"paid"
]
}
},
"schemas": {},
"sequences": {},
"enums": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
"tables": {},
"columns": {}
},
"internal": {
"indexes": {}
}
}

View File

@@ -1,12 +1,12 @@
{
"version": "7",
"dialect": "postgresql",
"dialect": "sqlite",
"entries": [
{
"idx": 0,
"version": "7",
"when": 1725302227098,
"tag": "0000_noisy_randall_flagg",
"version": "6",
"when": 1725385265625,
"tag": "0000_grey_marvel_zombies",
"breakpoints": true
}
]

View File

@@ -1,11 +1,9 @@
import { boolean, integer, pgEnum, pgTable, text } from 'drizzle-orm/pg-core'
import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core'
import { relations } from 'drizzle-orm'
import { id, timestamps } from '../utils/dbFields'
import { Subscription } from '../../types/types'
export const subscriptionEnum = pgEnum('subscription', Subscription)
export const users = pgTable('users', {
export const users = sqliteTable('users', {
id,
username: text('username').notNull().unique(),
name: text('name').notNull(),
@@ -16,17 +14,17 @@ export const users = pgTable('users', {
googleToken: text('google_token'),
description: text('description').default(''),
avatar: text('avatar').default(''),
private: boolean('private').default(false),
private: integer('private', { mode: 'boolean' }).default(false),
language: text('language').default('en-EN'),
location: text('location').default('unknown'),
subscription: subscriptionEnum('subscription').default('free'),
subscription: text('subscription', { enum: Subscription }).default('free'),
...timestamps,
})
export const categories = pgTable('categories', {
export const categories = sqliteTable('categories', {
id,
name: text('name').default(''),
nameVisible: boolean('name_visible').default(true),
nameVisible: integer('name_visible', { mode: 'boolean' }).default(true),
icon: text('icon').default('i-ph:circle-wavy-question-duotone'),
color: text('color').default('gray'),
userId: integer('user_id')
@@ -35,10 +33,10 @@ export const categories = pgTable('categories', {
...timestamps,
})
export const tabs = pgTable('tabs', {
export const tabs = sqliteTable('tabs', {
id,
name: text('name').default(''),
primary: boolean('primary').default(false),
primary: integer('primary', { mode: 'boolean' }).default(false),
icon: text('icon').default('i-ph:circle-wavy-question-duotone'),
color: text('color').default('gray'),
link: text('link').default(''),