Skip to content

Commit 2e0ff6d

Browse files
committed
fix and merge migration
1 parent e81312e commit 2e0ff6d

File tree

4 files changed

+23
-21
lines changed

4 files changed

+23
-21
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
DROP TABLE IF EXISTS posts;
22
DROP TABLE IF EXISTS user_role;
33
DROP TABLE IF EXISTS roles;
4+
DROP TABLE IF EXISTS sessions;
45
DROP TABLE IF EXISTS users;

db/migrations/000001_init_schema.up.sql

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,45 @@ CREATE TABLE "users"
33
"id" SERIAL PRIMARY KEY,
44
"email" varchar UNIQUE NOT NULL,
55
"hashed_password" varchar NOT NULL,
6-
"created_at" timestamptz NOT NULL DEFAULT (now()),
7-
"updated_at" timestamptz NOT NULL DEFAULT (now())
6+
"created_at" timestamptz NOT NULL DEFAULT (now()),
7+
"updated_at" timestamptz NOT NULL DEFAULT (now())
8+
);
9+
10+
CREATE TABLE "sessions"
11+
(
12+
"id" uuid PRIMARY KEY NOT NULL,
13+
"user_id" int NOT NULL,
14+
"expires_at" timestamp NOT NULL,
15+
"created_at" timestamptz NOT NULL DEFAULT (now())
816
);
917

1018
CREATE TABLE "roles"
1119
(
1220
"id" SERIAL PRIMARY KEY,
1321
"name" varchar UNIQUE NOT NULL,
14-
"created_at" timestamptz NOT NULL DEFAULT (now()),
15-
"updated_at" timestamptz NOT NULL DEFAULT (now())
22+
"created_at" timestamptz NOT NULL DEFAULT (now()),
23+
"updated_at" timestamptz NOT NULL DEFAULT (now())
1624
);
1725

1826
CREATE TABLE "user_role"
1927
(
20-
"id" SERIAL PRIMARY KEY,
2128
"user_id" int NOT NULL,
22-
"role_id" int NOT NULL
29+
"role_id" int NOT NULL,
30+
PRIMARY KEY ("user_id", "role_id")
2331
);
2432

2533
CREATE TABLE "posts"
2634
(
2735
"id" SERIAL PRIMARY KEY,
28-
"content" text NOT NULL,
36+
"content" text NOT NULL,
2937
"created_at" timestamptz NOT NULL DEFAULT (now()),
3038
"updated_at" timestamptz NOT NULL DEFAULT (now()),
31-
"user_id" int NOT NULL
39+
"user_id" int NOT NULL
3240
);
3341

42+
ALTER TABLE "sessions"
43+
ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");
44+
3445
ALTER TABLE "user_role"
3546
ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");
3647

@@ -40,7 +51,9 @@ ALTER TABLE "user_role"
4051
ALTER TABLE "posts"
4152
ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");
4253

43-
CREATE UNIQUE INDEX ON "user_role" ("user_id", "role_id");
54+
CREATE INDEX ON "users" ("created_at");
55+
56+
CREATE INDEX ON "sessions" ("expires_at");
4457

4558
CREATE INDEX ON "posts" ("created_at");
4659

db/migrations/000002_add_session_table.down.sql

Lines changed: 0 additions & 1 deletion
This file was deleted.

db/migrations/000002_add_session_table.up.sql

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)