mirror of
https://github.com/Dvorinka/excalidraw-full.git
synced 2026-06-03 22:02:57 +00:00
feat: full project sync - CI fixes, frontend, workspace API, and all changes
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"database/sql"
|
||||
"excalidraw-complete/core"
|
||||
dbpostgres "excalidraw-complete/internal/postgres"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/oklog/ulid/v2"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type postgresStore struct {
|
||||
db *dbpostgres.DB
|
||||
}
|
||||
|
||||
func NewStore(databaseURL string) *postgresStore {
|
||||
db, err := dbpostgres.Open(databaseURL)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to open postgres database: %v", err)
|
||||
}
|
||||
if err := dbpostgres.Migrate(context.Background(), db.DB); err != nil {
|
||||
log.Fatalf("failed to migrate postgres database: %v", err)
|
||||
}
|
||||
return &postgresStore{db: db}
|
||||
}
|
||||
|
||||
func (s *postgresStore) FindID(ctx context.Context, id string) (*core.Document, error) {
|
||||
log := logrus.WithField("document_id", id)
|
||||
var data []byte
|
||||
err := s.db.QueryRowContext(ctx, "SELECT data FROM documents WHERE id = ?", id).Scan(&data)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, fmt.Errorf("document with id %s not found", id)
|
||||
}
|
||||
log.WithError(err).Error("failed to retrieve document")
|
||||
return nil, err
|
||||
}
|
||||
return &core.Document{Data: *bytes.NewBuffer(data)}, nil
|
||||
}
|
||||
|
||||
func (s *postgresStore) Create(ctx context.Context, document *core.Document) (string, error) {
|
||||
id := ulid.Make().String()
|
||||
data := document.Data.Bytes()
|
||||
_, err := s.db.ExecContext(ctx, "INSERT INTO documents (id, data) VALUES (?, ?)", id, data)
|
||||
if err != nil {
|
||||
logrus.WithError(err).WithField("document_id", id).Error("failed to create document")
|
||||
return "", err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (s *postgresStore) List(ctx context.Context, userID string) ([]*core.Canvas, error) {
|
||||
rows, err := s.db.QueryContext(ctx, "SELECT id, name, updated_at, thumbnail FROM canvases WHERE user_id = ? ORDER BY updated_at DESC", userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
canvases := []*core.Canvas{}
|
||||
for rows.Next() {
|
||||
var canvas core.Canvas
|
||||
canvas.UserID = userID
|
||||
if err := rows.Scan(&canvas.ID, &canvas.Name, &canvas.UpdatedAt, &canvas.Thumbnail); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
canvases = append(canvases, &canvas)
|
||||
}
|
||||
return canvases, rows.Err()
|
||||
}
|
||||
|
||||
func (s *postgresStore) Get(ctx context.Context, userID, id string) (*core.Canvas, error) {
|
||||
var canvas core.Canvas
|
||||
canvas.UserID = userID
|
||||
canvas.ID = id
|
||||
err := s.db.QueryRowContext(ctx, "SELECT name, data, created_at, updated_at, thumbnail FROM canvases WHERE user_id = ? AND id = ?", userID, id).Scan(&canvas.Name, &canvas.Data, &canvas.CreatedAt, &canvas.UpdatedAt, &canvas.Thumbnail)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, fmt.Errorf("canvas not found")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &canvas, nil
|
||||
}
|
||||
|
||||
func (s *postgresStore) Save(ctx context.Context, canvas *core.Canvas) error {
|
||||
now := time.Now().UTC()
|
||||
_, err := s.db.ExecContext(ctx, `INSERT INTO canvases (id, user_id, name, data, created_at, updated_at, thumbnail)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT (user_id, id) DO UPDATE
|
||||
SET name = EXCLUDED.name,
|
||||
data = EXCLUDED.data,
|
||||
updated_at = EXCLUDED.updated_at,
|
||||
thumbnail = EXCLUDED.thumbnail`,
|
||||
canvas.ID, canvas.UserID, canvas.Name, canvas.Data, now, now, canvas.Thumbnail,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *postgresStore) Delete(ctx context.Context, userID, id string) error {
|
||||
_, err := s.db.ExecContext(ctx, "DELETE FROM canvases WHERE user_id = ? AND id = ?", userID, id)
|
||||
return err
|
||||
}
|
||||
@@ -1,158 +0,0 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"database/sql"
|
||||
"excalidraw-complete/core"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/oklog/ulid/v2"
|
||||
"github.com/sirupsen/logrus"
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
type sqliteStore struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
// NewStore creates a new SQLite-based store.
|
||||
func NewStore(dataSourceName string) *sqliteStore {
|
||||
db, err := sql.Open("sqlite", dataSourceName)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to open sqlite database: %v", err)
|
||||
}
|
||||
|
||||
// Initialize table for anonymous documents
|
||||
docTableStmt := `CREATE TABLE IF NOT EXISTS documents (id TEXT PRIMARY KEY, data BLOB);`
|
||||
if _, err = db.Exec(docTableStmt); err != nil {
|
||||
log.Fatalf("failed to create documents table: %v", err)
|
||||
}
|
||||
|
||||
// Initialize table for user-owned canvases
|
||||
canvasTableStmt := `
|
||||
CREATE TABLE IF NOT EXISTS canvases (
|
||||
id TEXT NOT NULL,
|
||||
user_id TEXT NOT NULL,
|
||||
name TEXT,
|
||||
thumbnail TEXT,
|
||||
data BLOB,
|
||||
created_at DATETIME,
|
||||
updated_at DATETIME,
|
||||
PRIMARY KEY (user_id, id)
|
||||
);`
|
||||
if _, err = db.Exec(canvasTableStmt); err != nil {
|
||||
log.Fatalf("failed to create canvases table: %v", err)
|
||||
}
|
||||
|
||||
return &sqliteStore{db}
|
||||
}
|
||||
|
||||
// DocumentStore implementation
|
||||
func (s *sqliteStore) FindID(ctx context.Context, id string) (*core.Document, error) {
|
||||
log := logrus.WithField("document_id", id)
|
||||
log.Debug("Retrieving document by ID")
|
||||
var data []byte
|
||||
err := s.db.QueryRowContext(ctx, "SELECT data FROM documents WHERE id = ?", id).Scan(&data)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
log.WithField("error", "document not found").Warn("Document with specified ID not found")
|
||||
return nil, fmt.Errorf("document with id %s not found", id)
|
||||
}
|
||||
log.WithError(err).Error("Failed to retrieve document")
|
||||
return nil, err
|
||||
}
|
||||
document := core.Document{
|
||||
Data: *bytes.NewBuffer(data),
|
||||
}
|
||||
log.Info("Document retrieved successfully")
|
||||
return &document, nil
|
||||
}
|
||||
|
||||
func (s *sqliteStore) Create(ctx context.Context, document *core.Document) (string, error) {
|
||||
id := ulid.Make().String()
|
||||
data := document.Data.Bytes()
|
||||
log := logrus.WithFields(logrus.Fields{
|
||||
"document_id": id,
|
||||
"data_length": len(data),
|
||||
})
|
||||
|
||||
_, err := s.db.ExecContext(ctx, "INSERT INTO documents (id, data) VALUES (?, ?)", id, data)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to create document")
|
||||
return "", err
|
||||
}
|
||||
log.Info("Document created successfully")
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// CanvasStore implementation
|
||||
func (s *sqliteStore) List(ctx context.Context, userID string) ([]*core.Canvas, error) {
|
||||
rows, err := s.db.QueryContext(ctx, "SELECT id, name, updated_at, thumbnail FROM canvases WHERE user_id = ?", userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var canvases []*core.Canvas
|
||||
for rows.Next() {
|
||||
var canvas core.Canvas
|
||||
canvas.UserID = userID
|
||||
if err := rows.Scan(&canvas.ID, &canvas.Name, &canvas.UpdatedAt, &canvas.Thumbnail); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
canvases = append(canvases, &canvas)
|
||||
}
|
||||
return canvases, nil
|
||||
}
|
||||
|
||||
func (s *sqliteStore) Get(ctx context.Context, userID, id string) (*core.Canvas, error) {
|
||||
var canvas core.Canvas
|
||||
canvas.UserID = userID
|
||||
canvas.ID = id
|
||||
err := s.db.QueryRowContext(ctx, "SELECT name, data, created_at, updated_at, thumbnail FROM canvases WHERE user_id = ? AND id = ?", userID, id).Scan(&canvas.Name, &canvas.Data, &canvas.CreatedAt, &canvas.UpdatedAt, &canvas.Thumbnail)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, fmt.Errorf("canvas not found")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &canvas, nil
|
||||
}
|
||||
|
||||
func (s *sqliteStore) Save(ctx context.Context, canvas *core.Canvas) error {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback() // Rollback on any error
|
||||
|
||||
var exists bool
|
||||
err = tx.QueryRowContext(ctx, "SELECT 1 FROM canvases WHERE user_id = ? AND id = ?", canvas.UserID, canvas.ID).Scan(&exists)
|
||||
|
||||
now := time.Now()
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
|
||||
if exists {
|
||||
// Update
|
||||
_, err = tx.ExecContext(ctx, "UPDATE canvases SET name = ?, data = ?, updated_at = ?, thumbnail = ? WHERE user_id = ? AND id = ?", canvas.Name, canvas.Data, now, canvas.Thumbnail, canvas.UserID, canvas.ID)
|
||||
} else {
|
||||
// Insert
|
||||
_, err = tx.ExecContext(ctx, "INSERT INTO canvases (id, user_id, name, data, created_at, updated_at, thumbnail) VALUES (?, ?, ?, ?, ?, ?, ?)", canvas.ID, canvas.UserID, canvas.Name, canvas.Data, now, now, canvas.Thumbnail)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (s *sqliteStore) Delete(ctx context.Context, userID, id string) error {
|
||||
_, err := s.db.ExecContext(ctx, "DELETE FROM canvases WHERE user_id = ? AND id = ?", userID, id)
|
||||
return err
|
||||
}
|
||||
+7
-7
@@ -5,7 +5,7 @@ import (
|
||||
"excalidraw-complete/stores/aws"
|
||||
"excalidraw-complete/stores/filesystem"
|
||||
"excalidraw-complete/stores/memory"
|
||||
"excalidraw-complete/stores/sqlite"
|
||||
"excalidraw-complete/stores/postgres"
|
||||
"os"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
@@ -33,13 +33,13 @@ func GetStore() Store {
|
||||
}
|
||||
storageField["basePath"] = basePath
|
||||
store = filesystem.NewStore(basePath)
|
||||
case "sqlite":
|
||||
dataSourceName := os.Getenv("DATA_SOURCE_NAME")
|
||||
if dataSourceName == "" {
|
||||
dataSourceName = "excalidraw.db" // Default filename
|
||||
case "postgres", "":
|
||||
databaseURL := os.Getenv("DATABASE_URL")
|
||||
if databaseURL == "" {
|
||||
logrus.Fatal("DATABASE_URL environment variable must be set for postgres storage")
|
||||
}
|
||||
storageField["dataSourceName"] = dataSourceName
|
||||
store = sqlite.NewStore(dataSourceName)
|
||||
storageField["databaseURL"] = "configured"
|
||||
store = postgres.NewStore(databaseURL)
|
||||
case "s3":
|
||||
bucketName := os.Getenv("S3_BUCKET_NAME")
|
||||
if bucketName == "" {
|
||||
|
||||
Reference in New Issue
Block a user