2024-01-17 09:44:47 +00:00
|
|
|
// Copyright (c) 2024 mStar
|
|
|
|
//
|
|
|
|
// Licensed under the EUPL, Version 1.2
|
|
|
|
//
|
|
|
|
// You may not use this work except in compliance with the Licence.
|
|
|
|
// You should have received a copy of the Licence along with this work. If not, see:
|
|
|
|
// <https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12>.
|
|
|
|
// See the Licence for the specific language governing permissions and limitations under the Licence.
|
|
|
|
//
|
|
|
|
|
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2024-01-19 10:21:31 +00:00
|
|
|
"gitlab.com/mstarongitlab/linstrom/config"
|
2024-01-17 09:44:47 +00:00
|
|
|
"gorm.io/driver/postgres"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
2024-01-19 10:21:31 +00:00
|
|
|
type Storage struct {
|
|
|
|
Db *gorm.DB
|
|
|
|
}
|
|
|
|
|
2024-01-17 09:44:47 +00:00
|
|
|
func NewDb(cfg *config.Config) (*gorm.DB, error) {
|
|
|
|
db, err := gorm.Open(postgres.Open(cfg.General.DbPath))
|
|
|
|
if err != nil {
|
2024-01-19 10:21:31 +00:00
|
|
|
return nil, fmt.Errorf("failed to connect to db %s: %w", cfg.General.DbPath, err)
|
|
|
|
}
|
|
|
|
err = db.AutoMigrate(
|
|
|
|
&Person{},
|
|
|
|
&Note{
|
|
|
|
Tags: make([]string, 0),
|
|
|
|
DeliverTo: make([]string, 0),
|
|
|
|
},
|
|
|
|
&Follow{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to apply migrations: %w", err)
|
2024-01-17 09:44:47 +00:00
|
|
|
}
|
|
|
|
return db, nil
|
|
|
|
}
|
2024-01-19 10:21:31 +00:00
|
|
|
|
|
|
|
func NewStorage(cfg *config.Config) (*Storage, error) {
|
|
|
|
db, err := NewDb(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create database connection while creating storage: %w", err)
|
|
|
|
}
|
|
|
|
return &Storage{
|
|
|
|
Db: db,
|
|
|
|
}, nil
|
|
|
|
}
|