mirror of
https://github.com/sbrow/envr.git
synced 2025-12-29 15:47:38 -05:00
refactor!: Dir is no longer stored in the database.
BREAKING CHANGE: Dir is now derived from Path rather than stored in the DB. Your DB will need to be updated.
This commit is contained in:
21
app/db.go
21
app/db.go
@@ -72,7 +72,6 @@ func newDb() (*sql.DB, error) {
|
|||||||
} else {
|
} else {
|
||||||
_, err := db.Exec(`create table envr_env_files (
|
_, err := db.Exec(`create table envr_env_files (
|
||||||
path text primary key not null
|
path text primary key not null
|
||||||
, dir text not null
|
|
||||||
, remotes text -- JSON
|
, remotes text -- JSON
|
||||||
, sha256 text not null
|
, sha256 text not null
|
||||||
, contents text not null
|
, contents text not null
|
||||||
@@ -150,7 +149,7 @@ func restoreDB(path string, destDB *sql.DB) error {
|
|||||||
|
|
||||||
// Returns all the EnvFiles present in the database.
|
// Returns all the EnvFiles present in the database.
|
||||||
func (db *Db) List() (results []EnvFile, err error) {
|
func (db *Db) List() (results []EnvFile, err error) {
|
||||||
rows, err := db.db.Query("select * from envr_env_files")
|
rows, err := db.db.Query("select path, remotes, sha256, contents from envr_env_files")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -160,11 +159,14 @@ func (db *Db) List() (results []EnvFile, err error) {
|
|||||||
var envFile EnvFile
|
var envFile EnvFile
|
||||||
var remotesJson []byte
|
var remotesJson []byte
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
err := rows.Scan(&envFile.Path, &envFile.Dir, &remotesJson, &envFile.Sha256, &envFile.contents)
|
err := rows.Scan(&envFile.Path, &remotesJson, &envFile.Sha256, &envFile.contents)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Populate Dir from Path
|
||||||
|
envFile.Dir = filepath.Dir(envFile.Path)
|
||||||
|
|
||||||
if err := json.Unmarshal(remotesJson, &envFile.Remotes); err != nil {
|
if err := json.Unmarshal(remotesJson, &envFile.Remotes); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -279,9 +281,9 @@ func (db *Db) Insert(file EnvFile) error {
|
|||||||
|
|
||||||
// Insert into database
|
// Insert into database
|
||||||
_, err = db.db.Exec(`
|
_, err = db.db.Exec(`
|
||||||
INSERT OR REPLACE INTO envr_env_files (path, dir, remotes, sha256, contents)
|
INSERT OR REPLACE INTO envr_env_files (path, remotes, sha256, contents)
|
||||||
VALUES (?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?)
|
||||||
`, file.Path, file.Dir, string(remotesJSON), file.Sha256, file.contents)
|
`, file.Path, string(remotesJSON), file.Sha256, file.contents)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to insert env file: %w", err)
|
return fmt.Errorf("failed to insert env file: %w", err)
|
||||||
@@ -294,12 +296,15 @@ func (db *Db) Insert(file EnvFile) error {
|
|||||||
func (db *Db) Fetch(path string) (envFile EnvFile, err error) {
|
func (db *Db) Fetch(path string) (envFile EnvFile, err error) {
|
||||||
var remotesJSON string
|
var remotesJSON string
|
||||||
|
|
||||||
row := db.db.QueryRow("SELECT path, dir, remotes, sha256, contents FROM envr_env_files WHERE path = ?", path)
|
row := db.db.QueryRow("SELECT path, remotes, sha256, contents FROM envr_env_files WHERE path = ?", path)
|
||||||
err = row.Scan(&envFile.Path, &envFile.Dir, &remotesJSON, &envFile.Sha256, &envFile.contents)
|
err = row.Scan(&envFile.Path, &remotesJSON, &envFile.Sha256, &envFile.contents)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return EnvFile{}, fmt.Errorf("failed to fetch env file: %w", err)
|
return EnvFile{}, fmt.Errorf("failed to fetch env file: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Populate Dir from Path
|
||||||
|
envFile.Dir = filepath.Dir(envFile.Path)
|
||||||
|
|
||||||
if err = json.Unmarshal([]byte(remotesJSON), &envFile.Remotes); err != nil {
|
if err = json.Unmarshal([]byte(remotesJSON), &envFile.Remotes); err != nil {
|
||||||
return EnvFile{}, fmt.Errorf("failed to unmarshal remotes: %w", err)
|
return EnvFile{}, fmt.Errorf("failed to unmarshal remotes: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type EnvFile struct {
|
type EnvFile struct {
|
||||||
Path string
|
Path string
|
||||||
|
// Dir is derived from Path, and is not stored in the database.
|
||||||
Dir string
|
Dir string
|
||||||
Remotes []string // []string
|
Remotes []string // []string
|
||||||
Sha256 string
|
Sha256 string
|
||||||
|
|||||||
Reference in New Issue
Block a user