add migration system
This commit is contained in:
@@ -1,96 +0,0 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: files.sql
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
const createFile = `-- name: CreateFile :one
|
||||
INSERT INTO files (
|
||||
name,storage_key,checksum,created_at,updated_at,deleted_at
|
||||
) VALUES (
|
||||
?, ?, ?,date(),date(),date()
|
||||
)
|
||||
RETURNING id, name, storage_key, checksum, created_at, updated_at, deleted_at
|
||||
`
|
||||
|
||||
type CreateFileParams struct {
|
||||
Name string `json:"name"`
|
||||
StorageKey string `json:"storage_key"`
|
||||
Checksum string `json:"checksum"`
|
||||
}
|
||||
|
||||
func (q *Queries) CreateFile(ctx context.Context, arg CreateFileParams) (File, error) {
|
||||
row := q.db.QueryRowContext(ctx, createFile, arg.Name, arg.StorageKey, arg.Checksum)
|
||||
var i File
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.StorageKey,
|
||||
&i.Checksum,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.DeletedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getFile = `-- name: GetFile :one
|
||||
SELECT id, name, storage_key, checksum, created_at, updated_at, deleted_at FROM files
|
||||
WHERE id = ? LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetFile(ctx context.Context, id string) (File, error) {
|
||||
row := q.db.QueryRowContext(ctx, getFile, id)
|
||||
var i File
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.StorageKey,
|
||||
&i.Checksum,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.DeletedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listFiles = `-- name: ListFiles :many
|
||||
SELECT id, name, storage_key, checksum, created_at, updated_at, deleted_at FROM files
|
||||
ORDER BY name
|
||||
`
|
||||
|
||||
func (q *Queries) ListFiles(ctx context.Context) ([]File, error) {
|
||||
rows, err := q.db.QueryContext(ctx, listFiles)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []File
|
||||
for rows.Next() {
|
||||
var i File
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.StorageKey,
|
||||
&i.Checksum,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.DeletedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: placeholder.sql
|
||||
// source: health.sql
|
||||
|
||||
package db
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/golang-migrate/migrate/v4"
|
||||
"github.com/golang-migrate/migrate/v4/database/sqlite"
|
||||
"github.com/golang-migrate/migrate/v4/source/file"
|
||||
)
|
||||
|
||||
func RunMigrations(database *sql.DB, migrationsURL string) error {
|
||||
sourceDriver, err := (&file.File{}).Open(migrationsURL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("open migrations source: %w", err)
|
||||
}
|
||||
|
||||
dbDriver, err := sqlite.WithInstance(database, &sqlite.Config{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("create sqlite driver: %w", err)
|
||||
}
|
||||
|
||||
m, err := migrate.NewWithInstance("file", sourceDriver, "sqlite", dbDriver)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create migrate instance: %w", err)
|
||||
}
|
||||
defer m.Close()
|
||||
|
||||
if err := m.Up(); err != nil && err != migrate.ErrNoChange {
|
||||
return fmt.Errorf("run migrations: %w", err)
|
||||
}
|
||||
|
||||
log.Println("Database migrations applied successfully")
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
-- Rollback 001_init
|
||||
@@ -0,0 +1,3 @@
|
||||
-- VaultDrop 001: Initial schema
|
||||
-- This is a placeholder. Add your first CREATE TABLE here.
|
||||
SELECT 1;
|
||||
@@ -1,20 +0,0 @@
|
||||
CREATE TABLE files (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
storage_key TEXT NOT NULL,
|
||||
checksum TEXT NOT NULL,
|
||||
created_at INTEGER NOT NULL,
|
||||
updated_at INTEGER NOT NULL,
|
||||
deleted_at INTEGER
|
||||
);
|
||||
|
||||
CREATE TABLE tags (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE file_tags (
|
||||
file_id TEXT NOT NULL,
|
||||
tag_id TEXT NOT NULL,
|
||||
PRIMARY KEY (file_id, tag_id)
|
||||
);
|
||||
@@ -3,27 +3,3 @@
|
||||
// sqlc v1.31.1
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type File struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
StorageKey string `json:"storage_key"`
|
||||
Checksum string `json:"checksum"`
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
UpdatedAt int64 `json:"updated_at"`
|
||||
DeletedAt sql.NullInt64 `json:"deleted_at"`
|
||||
}
|
||||
|
||||
type FileTag struct {
|
||||
FileID string `json:"file_id"`
|
||||
TagID string `json:"tag_id"`
|
||||
}
|
||||
|
||||
type Tag struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
-- name: GetFile :one
|
||||
SELECT * FROM files
|
||||
WHERE id = ? LIMIT 1;
|
||||
|
||||
-- name: ListFiles :many
|
||||
SELECT * FROM files
|
||||
ORDER BY name;
|
||||
|
||||
-- name: CreateFile :one
|
||||
INSERT INTO files (
|
||||
name,storage_key,checksum,created_at,updated_at,deleted_at
|
||||
) VALUES (
|
||||
?, ?, ?,date(),date(),date()
|
||||
)
|
||||
RETURNING *;
|
||||
@@ -0,0 +1,2 @@
|
||||
-- name: GetHealth :one
|
||||
SELECT 1 AS ok;
|
||||
@@ -1,8 +1,6 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
@@ -10,7 +8,6 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/vaultdrop/backend/internal/db"
|
||||
"github.com/vaultdrop/backend/internal/service"
|
||||
)
|
||||
|
||||
@@ -172,8 +169,6 @@ func (h *Handlers) GetFile(c *gin.Context) {
|
||||
|
||||
func (h *Handlers) UploadFile(c *gin.Context) {
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
file, err := c.FormFile("file")
|
||||
|
||||
if err != nil {
|
||||
@@ -220,20 +215,14 @@ func (h *Handlers) UploadFile(c *gin.Context) {
|
||||
}
|
||||
|
||||
checksum := service.CreateSHA256Hash(fileByte)
|
||||
// save generate meta data
|
||||
createFileParams := db.CreateFileParams{
|
||||
Name: file.Filename,
|
||||
StorageKey: dst,
|
||||
Checksum: string(checksum[:]),
|
||||
}
|
||||
|
||||
dbFile, err := h.queries.CreateFile(ctx, createFileParams)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
fmt.Println(dbFile.Checksum)
|
||||
// TODO: re-enable once files table + query are added
|
||||
// createFileParams := db.CreateFileParams{
|
||||
// Name: file.Filename,
|
||||
// StorageKey: dst,
|
||||
// Checksum: string(checksum[:]),
|
||||
// }
|
||||
// dbFile, err := h.queries.CreateFile(ctx, createFileParams)
|
||||
_ = checksum
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"error": gin.H{
|
||||
|
||||
Reference in New Issue
Block a user