init
This commit is contained in:
32
template/services/api/db/db.go
Normal file
32
template/services/api/db/db.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.30.0
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
type DBTX interface {
|
||||
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
|
||||
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
|
||||
QueryRow(context.Context, string, ...interface{}) pgx.Row
|
||||
}
|
||||
|
||||
func New(db DBTX) *Queries {
|
||||
return &Queries{db: db}
|
||||
}
|
||||
|
||||
type Queries struct {
|
||||
db DBTX
|
||||
}
|
||||
|
||||
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
|
||||
return &Queries{
|
||||
db: tx,
|
||||
}
|
||||
}
|
||||
6
template/services/api/db/dbfs.go
Normal file
6
template/services/api/db/dbfs.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package db
|
||||
|
||||
import "embed"
|
||||
|
||||
//go:embed migrations/*.sql
|
||||
var MigrationsFs embed.FS
|
||||
60
template/services/api/db/lib.go
Normal file
60
template/services/api/db/lib.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"<@var(context.project.goprefix)>/<@var(context.project.name)>/config"
|
||||
"github.com/amacneil/dbmate/v2/pkg/dbmate"
|
||||
_ "github.com/amacneil/dbmate/v2/pkg/driver/postgres"
|
||||
"log"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var Q *Queries
|
||||
|
||||
func RunDbMigrations() error {
|
||||
|
||||
err := config.Load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
conf := *config.Conf
|
||||
dbUrl, err := url.Parse(strings.Join([]string{
|
||||
"postgresql://",
|
||||
conf.Database.User,
|
||||
":",
|
||||
conf.Database.Password,
|
||||
"@",
|
||||
conf.Database.Host,
|
||||
":",
|
||||
strconv.Itoa(conf.Database.Port),
|
||||
"/",
|
||||
conf.Database.Name,
|
||||
}, ""))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db := dbmate.New(dbUrl)
|
||||
db.FS = MigrationsFs
|
||||
db.MigrationsDir = []string{"migrations"}
|
||||
migrations, err := db.FindMigrations()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, m := range migrations {
|
||||
log.Default().Println(m.Version, m.FilePath)
|
||||
}
|
||||
db.AutoDumpSchema = false
|
||||
log.Default().Println("\nApplying...")
|
||||
err = db.CreateAndMigrate()
|
||||
if err != nil {
|
||||
log.Default().Println(err.Error())
|
||||
}
|
||||
err = db.DumpSchema()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
10
template/services/api/db/migrations/20260404121052_init.sql
Normal file
10
template/services/api/db/migrations/20260404121052_init.sql
Normal file
@@ -0,0 +1,10 @@
|
||||
-- migrate:up
|
||||
CREATE TABLE todo (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
task VARCHAR(255) NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
done bool DEFAULT false
|
||||
);
|
||||
-- migrate:down
|
||||
DROP TABLE todo;
|
||||
17
template/services/api/db/models.go
Normal file
17
template/services/api/db/models.go
Normal file
@@ -0,0 +1,17 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.30.0
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type Todo struct {
|
||||
ID pgtype.UUID
|
||||
Task string
|
||||
CreatedAt pgtype.Timestamp
|
||||
UpdatedAt pgtype.Timestamp
|
||||
Done pgtype.Bool
|
||||
}
|
||||
14
template/services/api/db/query/todo.sql
Normal file
14
template/services/api/db/query/todo.sql
Normal file
@@ -0,0 +1,14 @@
|
||||
-- name: CreateTodo :one
|
||||
insert into todo (id,task) values ($1,$2) returning *;
|
||||
|
||||
-- name: ListTodos :many
|
||||
select * from todo;
|
||||
|
||||
-- name: GetTodo :one
|
||||
select * from todo where id = $1 limit 1;
|
||||
|
||||
-- name: UpdateTodo :one
|
||||
update todo set task = $1, done = $2 where id = $3 returning *;
|
||||
|
||||
-- name: DeleteTodo :exec
|
||||
delete from todo where id = $1;
|
||||
86
template/services/api/db/schema.sql
Normal file
86
template/services/api/db/schema.sql
Normal file
@@ -0,0 +1,86 @@
|
||||
\restrict dbmate
|
||||
|
||||
-- Dumped from database version 17.9
|
||||
-- Dumped by pg_dump version 17.9
|
||||
|
||||
SET statement_timeout = 0;
|
||||
SET lock_timeout = 0;
|
||||
SET idle_in_transaction_session_timeout = 0;
|
||||
SET transaction_timeout = 0;
|
||||
SET client_encoding = 'UTF8';
|
||||
SET standard_conforming_strings = on;
|
||||
SELECT pg_catalog.set_config('search_path', '', false);
|
||||
SET check_function_bodies = false;
|
||||
SET xmloption = content;
|
||||
SET client_min_messages = warning;
|
||||
SET row_security = off;
|
||||
|
||||
--
|
||||
-- Name: pgcrypto; Type: EXTENSION; Schema: -; Owner: -
|
||||
--
|
||||
|
||||
CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA public;
|
||||
|
||||
|
||||
--
|
||||
-- Name: EXTENSION pgcrypto; Type: COMMENT; Schema: -; Owner: -
|
||||
--
|
||||
|
||||
COMMENT ON EXTENSION pgcrypto IS 'cryptographic functions';
|
||||
|
||||
|
||||
SET default_tablespace = '';
|
||||
|
||||
SET default_table_access_method = heap;
|
||||
|
||||
--
|
||||
-- Name: schema_migrations; Type: TABLE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE TABLE public.schema_migrations (
|
||||
version character varying NOT NULL
|
||||
);
|
||||
|
||||
|
||||
--
|
||||
-- Name: todo; Type: TABLE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE TABLE public.todo (
|
||||
id uuid DEFAULT gen_random_uuid() NOT NULL,
|
||||
task character varying(255) NOT NULL,
|
||||
created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
|
||||
done boolean DEFAULT false
|
||||
);
|
||||
|
||||
|
||||
--
|
||||
-- Name: schema_migrations schema_migrations_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY public.schema_migrations
|
||||
ADD CONSTRAINT schema_migrations_pkey PRIMARY KEY (version);
|
||||
|
||||
|
||||
--
|
||||
-- Name: todo todo_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY public.todo
|
||||
ADD CONSTRAINT todo_pkey PRIMARY KEY (id);
|
||||
|
||||
|
||||
--
|
||||
-- PostgreSQL database dump complete
|
||||
--
|
||||
|
||||
\unrestrict dbmate
|
||||
|
||||
|
||||
--
|
||||
-- Dbmate schema migrations
|
||||
--
|
||||
|
||||
INSERT INTO public.schema_migrations (version) VALUES
|
||||
('20260404121052');
|
||||
113
template/services/api/db/todo.sql.go
Normal file
113
template/services/api/db/todo.sql.go
Normal file
@@ -0,0 +1,113 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.30.0
|
||||
// source: todo.sql
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const createTodo = `-- name: CreateTodo :one
|
||||
insert into todo (id,task) values ($1,$2) returning id, task, created_at, updated_at, done
|
||||
`
|
||||
|
||||
type CreateTodoParams struct {
|
||||
ID pgtype.UUID
|
||||
Task string
|
||||
}
|
||||
|
||||
func (q *Queries) CreateTodo(ctx context.Context, arg CreateTodoParams) (Todo, error) {
|
||||
row := q.db.QueryRow(ctx, createTodo, arg.ID, arg.Task)
|
||||
var i Todo
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Task,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Done,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const deleteTodo = `-- name: DeleteTodo :exec
|
||||
delete from todo where id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteTodo(ctx context.Context, id pgtype.UUID) error {
|
||||
_, err := q.db.Exec(ctx, deleteTodo, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const getTodo = `-- name: GetTodo :one
|
||||
select id, task, created_at, updated_at, done from todo where id = $1 limit 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetTodo(ctx context.Context, id pgtype.UUID) (Todo, error) {
|
||||
row := q.db.QueryRow(ctx, getTodo, id)
|
||||
var i Todo
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Task,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Done,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listTodos = `-- name: ListTodos :many
|
||||
select id, task, created_at, updated_at, done from todo
|
||||
`
|
||||
|
||||
func (q *Queries) ListTodos(ctx context.Context) ([]Todo, error) {
|
||||
rows, err := q.db.Query(ctx, listTodos)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Todo
|
||||
for rows.Next() {
|
||||
var i Todo
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Task,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Done,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const updateTodo = `-- name: UpdateTodo :one
|
||||
update todo set task = $1, done = $2 where id = $3 returning id, task, created_at, updated_at, done
|
||||
`
|
||||
|
||||
type UpdateTodoParams struct {
|
||||
Task string
|
||||
Done pgtype.Bool
|
||||
ID pgtype.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateTodo(ctx context.Context, arg UpdateTodoParams) (Todo, error) {
|
||||
row := q.db.QueryRow(ctx, updateTodo, arg.Task, arg.Done, arg.ID)
|
||||
var i Todo
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Task,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Done,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
Reference in New Issue
Block a user