add apt gestion
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
274ea454dd
commit
c4776d81dd
22 changed files with 1465 additions and 163 deletions
|
|
@ -27,6 +27,9 @@ var migration005 string
|
|||
//go:embed migrations/000006_blocked_packages_name.up.sql
|
||||
var migration006 string
|
||||
|
||||
//go:embed migrations/000007_repo_config.up.sql
|
||||
var migration007 string
|
||||
|
||||
func Open(cfg config.DBConfig) (*sql.DB, error) {
|
||||
db, err := sql.Open("sqlite", cfg.Path)
|
||||
if err != nil {
|
||||
|
|
@ -66,6 +69,7 @@ func runMigrations(db *sql.DB) error {
|
|||
{4, migration004},
|
||||
{5, migration005},
|
||||
{6, migration006},
|
||||
{7, migration007},
|
||||
}
|
||||
|
||||
for _, m := range migrations {
|
||||
|
|
|
|||
4
internal/store/migrations/000007_repo_config.down.sql
Normal file
4
internal/store/migrations/000007_repo_config.down.sql
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
-- SQLite ne supporte pas DROP COLUMN avant 3.35
|
||||
CREATE TABLE repos_backup AS SELECT id, name, type, source_url, frozen, sync_mode, created_at FROM repos;
|
||||
DROP TABLE repos;
|
||||
ALTER TABLE repos_backup RENAME TO repos;
|
||||
1
internal/store/migrations/000007_repo_config.up.sql
Normal file
1
internal/store/migrations/000007_repo_config.up.sql
Normal file
|
|
@ -0,0 +1 @@
|
|||
ALTER TABLE repos ADD COLUMN config TEXT NOT NULL DEFAULT '';
|
||||
|
|
@ -16,6 +16,7 @@ type Repo struct {
|
|||
SourceURL string `db:"source_url"`
|
||||
Frozen bool `db:"frozen"`
|
||||
SyncMode string `db:"sync_mode"`
|
||||
Config string `db:"config"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
}
|
||||
|
||||
|
|
@ -23,8 +24,10 @@ type RepoStore interface {
|
|||
CreateRepo(ctx context.Context, r *Repo) (int64, error)
|
||||
ListRepos(ctx context.Context) ([]Repo, error)
|
||||
GetRepo(ctx context.Context, id int64) (*Repo, error)
|
||||
GetRepoByName(ctx context.Context, name string) (*Repo, error)
|
||||
DeleteRepo(ctx context.Context, id int64) error
|
||||
UpdateRepoSyncMode(ctx context.Context, id int64, mode string) error
|
||||
UpdateRepo(ctx context.Context, id int64, sourceURL, syncMode, config string) error
|
||||
}
|
||||
|
||||
type SQLiteRepoStore struct {
|
||||
|
|
@ -37,8 +40,8 @@ func NewRepoStore(db *sql.DB) *SQLiteRepoStore {
|
|||
|
||||
func (s *SQLiteRepoStore) CreateRepo(ctx context.Context, r *Repo) (int64, error) {
|
||||
res, err := s.db.ExecContext(ctx,
|
||||
`INSERT INTO repos (name, type, source_url, frozen, sync_mode) VALUES (?, ?, ?, ?, ?)`,
|
||||
r.Name, r.Type, r.SourceURL, r.Frozen, r.SyncMode,
|
||||
`INSERT INTO repos (name, type, source_url, frozen, sync_mode, config) VALUES (?, ?, ?, ?, ?, ?)`,
|
||||
r.Name, r.Type, r.SourceURL, r.Frozen, r.SyncMode, r.Config,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
|
|
@ -48,7 +51,7 @@ func (s *SQLiteRepoStore) CreateRepo(ctx context.Context, r *Repo) (int64, error
|
|||
|
||||
func (s *SQLiteRepoStore) ListRepos(ctx context.Context) ([]Repo, error) {
|
||||
rows, err := s.db.QueryContext(ctx,
|
||||
`SELECT id, name, type, source_url, frozen, sync_mode, created_at FROM repos ORDER BY created_at DESC`)
|
||||
`SELECT id, name, type, source_url, frozen, sync_mode, config, created_at FROM repos ORDER BY created_at DESC`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -57,7 +60,7 @@ func (s *SQLiteRepoStore) ListRepos(ctx context.Context) ([]Repo, error) {
|
|||
var repos []Repo
|
||||
for rows.Next() {
|
||||
var r Repo
|
||||
if err := rows.Scan(&r.ID, &r.Name, &r.Type, &r.SourceURL, &r.Frozen, &r.SyncMode, &r.CreatedAt); err != nil {
|
||||
if err := rows.Scan(&r.ID, &r.Name, &r.Type, &r.SourceURL, &r.Frozen, &r.SyncMode, &r.Config, &r.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
repos = append(repos, r)
|
||||
|
|
@ -68,8 +71,22 @@ func (s *SQLiteRepoStore) ListRepos(ctx context.Context) ([]Repo, error) {
|
|||
func (s *SQLiteRepoStore) GetRepo(ctx context.Context, id int64) (*Repo, error) {
|
||||
var r Repo
|
||||
err := s.db.QueryRowContext(ctx,
|
||||
`SELECT id, name, type, source_url, frozen, sync_mode, created_at FROM repos WHERE id = ?`, id,
|
||||
).Scan(&r.ID, &r.Name, &r.Type, &r.SourceURL, &r.Frozen, &r.SyncMode, &r.CreatedAt)
|
||||
`SELECT id, name, type, source_url, frozen, sync_mode, config, created_at FROM repos WHERE id = ?`, id,
|
||||
).Scan(&r.ID, &r.Name, &r.Type, &r.SourceURL, &r.Frozen, &r.SyncMode, &r.Config, &r.CreatedAt)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
func (s *SQLiteRepoStore) GetRepoByName(ctx context.Context, name string) (*Repo, error) {
|
||||
var r Repo
|
||||
err := s.db.QueryRowContext(ctx,
|
||||
`SELECT id, name, type, source_url, frozen, sync_mode, config, created_at FROM repos WHERE name = ?`, name,
|
||||
).Scan(&r.ID, &r.Name, &r.Type, &r.SourceURL, &r.Frozen, &r.SyncMode, &r.Config, &r.CreatedAt)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
|
@ -94,6 +111,21 @@ func (s *SQLiteRepoStore) DeleteRepo(ctx context.Context, id int64) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s *SQLiteRepoStore) UpdateRepo(ctx context.Context, id int64, sourceURL, syncMode, config string) error {
|
||||
res, err := s.db.ExecContext(ctx,
|
||||
`UPDATE repos SET source_url = ?, sync_mode = ?, config = ? WHERE id = ?`,
|
||||
sourceURL, syncMode, config, id,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, _ := res.RowsAffected()
|
||||
if n == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SQLiteRepoStore) UpdateRepoSyncMode(ctx context.Context, id int64, mode string) error {
|
||||
res, err := s.db.ExecContext(ctx, `UPDATE repos SET sync_mode = ? WHERE id = ?`, mode, id)
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue