Skip to content

Commit c8d0220

Browse files
authored
Merge pull request #11 from isoppp/feature/seed
add db seed
2 parents e81312e + e2341c3 commit c8d0220

16 files changed

+837
-2410
lines changed

Makefile

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ postgres:
1717
sqlboiler:
1818
go generate
1919

20+
seed:
21+
go run ./cmd/seed -dev
22+
23+
resetdb:
24+
migrate -path $(_MIGRATION_DIR) -database $(_MIGRATION_DB_PATH) -verbose drop -f && make migrateup && make seed
25+
2026
migrateup:
2127
migrate -path $(_MIGRATION_DIR) -database $(_MIGRATION_DB_PATH) -verbose up
2228

@@ -35,4 +41,4 @@ migrateredo:
3541
test:
3642
go test -v -cover ./...
3743

38-
.PHONY: run dev devw postgres sqlboiler migratedown migratedown1 migrateup migrateup1 migrateredo test
44+
.PHONY: run dev devw postgres sqlboiler seed resetdb migratedown migratedown1 migrateup migrateup1 migrateredo test

cmd/seed/main.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"sandbox-go-api-sqlboiler-rest-auth/db"
6+
"sandbox-go-api-sqlboiler-rest-auth/internal/config"
7+
)
8+
9+
func main() {
10+
cfg := config.NewConfig()
11+
err := db.Seed(cfg)
12+
if err != nil {
13+
log.Fatalln("cannot add seed data", err)
14+
}
15+
log.Println("seed data is added")
16+
}
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

+22-9
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

-1
This file was deleted.

db/migrations/000002_add_session_table.up.sql

-11
This file was deleted.

db/seed.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package db
2+
3+
import (
4+
"database/sql"
5+
_ "embed"
6+
"log"
7+
"sandbox-go-api-sqlboiler-rest-auth/internal/config"
8+
9+
_ "github.com/lib/pq"
10+
)
11+
12+
var (
13+
//go:embed seed.sql
14+
seedDoc string
15+
)
16+
17+
func Seed(cfg *config.Config) error {
18+
// db
19+
db, err := sql.Open("postgres", cfg.GetDataSourceName())
20+
if err != nil {
21+
log.Fatalln("cannot open the database", cfg.GetDataSourceName())
22+
}
23+
defer func() {
24+
_ = db.Close()
25+
}()
26+
tx, err := db.Begin()
27+
if err != nil {
28+
return err
29+
}
30+
31+
if _, err := tx.Exec(seedDoc); err != nil {
32+
if err := tx.Rollback(); err != nil {
33+
return err
34+
}
35+
return err
36+
}
37+
38+
return tx.Commit()
39+
}

db/seed.sql

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
INSERT INTO roles (id, name)
2+
VALUES (1, 'Admin'), (2, 'User')
3+
ON CONFLICT DO NOTHING;
4+
5+
INSERT INTO users (id, email, hashed_password)
6+
VALUES (1, '[email protected]', 'asdf1234'), (2, '[email protected]', 'asdf1234'), (3, '[email protected]', 'asdf1234')
7+
ON CONFLICT DO NOTHING;
8+
9+
INSERT INTO user_role (user_id, role_id)
10+
VALUES (1, 1), (2, 2), (3, 2)
11+
ON CONFLICT DO NOTHING;
12+
13+
INSERT INTO posts (id, user_id, content)
14+
VALUES
15+
(1, 2, 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,'),
16+
(2, 2, 'post 2'),
17+
(3, 2, 'post 3'),
18+
(4, 3, 'post 4'),
19+
(5, 3, 'post 5')
20+
ON CONFLICT DO NOTHING;
21+

internal/boilmodels/boil_suites_test.go

+12-28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/boilmodels/psql_suites_test.go

-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)