Skip to content

Commit b53e019

Browse files
authored
fix: update to support admin schemas (#1971)
* fix: add phoenix schemas * fix: add other tables and rename tables to fit with the definitions * fix: thumbanil in seed * fix: add a default for the thumbnail column * fix: migrations * fix: schemas
1 parent c80e662 commit b53e019

11 files changed

Lines changed: 8128 additions & 13 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
CREATE EXTENSION IF NOT EXISTS citext;--> statement-breakpoint
2+
3+
-- this is a table to hold the phoenix schema migrations
4+
-- these migrations are currently handled by drizzle.
5+
-- we insert the relevant data inside the table so phoenix is happy
6+
CREATE TABLE "schema_migrations" (
7+
"version" bigint PRIMARY KEY NOT NULL,
8+
"inserted_at" timestamp (0)
9+
);
10+
--> statement-breakpoint
11+
INSERT INTO "schema_migrations" ("version", "inserted_at") VALUES
12+
('20250806110912', '2025-08-27 10:28:02'), -- users and tokens table
13+
('20250807113759', '2025-08-27 10:28:02'), -- published items table
14+
('20250818111736', '2025-08-27 10:28:02'), -- removal notices table
15+
('20250819070000', '2025-08-27 10:28:02'); -- apps and publishers table
16+
--> statement-breakpoint
17+
18+
CREATE TABLE "admins" (
19+
"id" uuid PRIMARY KEY NOT NULL,
20+
"email" "citext" NOT NULL,
21+
"hashed_password" varchar(255),
22+
"confirmed_at" timestamp (0),
23+
"created_at" timestamp (0) NOT NULL,
24+
"updated_at" timestamp (0) NOT NULL,
25+
CONSTRAINT "admins_email_unique" UNIQUE("email")
26+
);
27+
--> statement-breakpoint
28+
CREATE INDEX "admins_email_index" ON "admins" USING btree ("email");--> statement-breakpoint
29+
30+
CREATE TABLE "publication_removal_notices" (
31+
"id" uuid PRIMARY KEY NOT NULL,
32+
"publication_name" varchar(255),
33+
"reason" text,
34+
"item_id" uuid,
35+
"creator_id" uuid,
36+
"created_at" timestamp (0) NOT NULL
37+
);
38+
ALTER TABLE "publication_removal_notices" ADD CONSTRAINT "publication_removal_notices_item_id_item_id_fk" FOREIGN KEY ("item_id") REFERENCES "public"."item"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
39+
ALTER TABLE "publication_removal_notices" ADD CONSTRAINT "publication_removal_notices_creator_id_users_id_fk" FOREIGN KEY ("creator_id") REFERENCES "public"."admins"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
40+
CREATE INDEX "publication_removal_notices_item_id_index" ON "publication_removal_notices" USING btree ("item_id" uuid_ops);--> statement-breakpoint
41+
42+
43+
CREATE TABLE "admins_tokens" (
44+
"id" uuid PRIMARY KEY NOT NULL,
45+
"user_id" uuid NOT NULL,
46+
"token" "bytea" NOT NULL,
47+
"context" varchar(255) NOT NULL,
48+
"sent_to" varchar(255),
49+
"authenticated_at" timestamp (0),
50+
"created_at" timestamp (0) NOT NULL,
51+
CONSTRAINT "admins_tokens_context_token_index" UNIQUE("context","token")
52+
);
53+
--> statement-breakpoint
54+
ALTER TABLE "admins_tokens" ADD CONSTRAINT "admins_tokens_user_id_admins_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."admins"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
55+
CREATE INDEX "admins_tokens_user_id_index" ON "admins_tokens" USING btree ("user_id" uuid_ops);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
ALTER TABLE "publisher" RENAME TO "publishers";--> statement-breakpoint
3+
4+
5+
ALTER TABLE "app" RENAME TO "apps";--> statement-breakpoint
6+
ALTER TABLE "apps" DROP CONSTRAINT "FK_37eb7baab82e11150157ec0b5a6";--> statement-breakpoint
7+
ALTER TABLE "apps" ADD CONSTRAINT "apps_publisher_id_fk" FOREIGN KEY ("publisher_id") REFERENCES "public"."publishers"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
8+
-- Add the new thumbnail column
9+
ALTER TABLE "apps" ADD COLUMN "thumbnail" varchar(255) DEFAULT '' NOT NULL;--> statement-breakpoint
10+
-- Copy the image property from the extra (jsonb) column to the new thumbnail column
11+
UPDATE "apps"
12+
SET "thumbnail" = "extra"->>'image'
13+
WHERE "extra" ? 'image';
14+
--> statement-breakpoint
15+
ALTER TABLE "apps" DROP COLUMN "extra";
16+
--> statement-breakpoint
17+
18+
19+
ALTER TABLE "item_published" RENAME TO "published_items";--> statement-breakpoint
20+
ALTER TABLE "published_items" DROP CONSTRAINT "item_published_creator_id_account_id_fk";
21+
--> statement-breakpoint
22+
ALTER TABLE "published_items" DROP CONSTRAINT "item_published_item_path_item_path_fk";
23+
--> statement-breakpoint
24+
ALTER TABLE "published_items" ADD CONSTRAINT "published_items_creator_id_account_id_fk" FOREIGN KEY ("creator_id") REFERENCES "public"."account"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
25+
ALTER TABLE "published_items" ADD CONSTRAINT "published_items_item_path_item_path_fk" FOREIGN KEY ("item_path") REFERENCES "public"."item"("path") ON DELETE cascade ON UPDATE cascade;
26+
27+
INSERT INTO "schema_migrations" ("version", "inserted_at") VALUES
28+
('20250828053810', '2025-08-28 10:28:02'), -- item and published_items update
29+
('20250901102230', '2025-08-28 10:28:02'), -- add app key
30+
('20250902085812', '2025-08-28 10:28:02'), -- rename users to admins
31+
('20250904060051', '2025-08-28 10:28:02'), -- rename removal_notices to publication_removal_notices
32+
('20250904072624', '2025-08-28 10:28:02'); -- add account table

src/drizzle/customTypes.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,18 @@ export const binaryHash = customType<{ data: Uint8Array; driverData: Buffer }>({
2424
return 'bytea';
2525
},
2626
});
27+
28+
export const citext = customType<{ data: string }>({
29+
dataType() {
30+
return 'citext';
31+
},
32+
});
33+
34+
export const binary = customType<{
35+
data: Buffer;
36+
default: false;
37+
}>({
38+
dataType() {
39+
return 'bytea';
40+
},
41+
});

0 commit comments

Comments
 (0)