refactor: Reorder methods to place public methods before private methods
This commit is contained in:
parent
1902c149d2
commit
08f4328580
|
@ -109,54 +109,6 @@ func (p *Postgres) Updated(ks []item.Kind, t time.Time) ([]item.Item, error) {
|
|||
return result, nil
|
||||
}
|
||||
|
||||
func (p *Postgres) migrate(wanted []string) error {
|
||||
// Create migration table if not exists
|
||||
_, err := p.db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS migration
|
||||
(id SERIAL PRIMARY KEY, query TEXT)
|
||||
`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
||||
}
|
||||
|
||||
// Find existing migrations
|
||||
rows, err := p.db.Query(`SELECT query FROM migration ORDER BY id`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var existing []string
|
||||
for rows.Next() {
|
||||
var query string
|
||||
if err := rows.Scan(&query); err != nil {
|
||||
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
||||
}
|
||||
existing = append(existing, query)
|
||||
}
|
||||
|
||||
// Compare and execute missing migrations
|
||||
missing, err := compareMigrations(wanted, existing)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
||||
}
|
||||
|
||||
for _, query := range missing {
|
||||
if _, err := p.db.Exec(query); err != nil {
|
||||
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
||||
}
|
||||
|
||||
// Register migration
|
||||
if _, err := p.db.Exec(`
|
||||
INSERT INTO migration (query) VALUES ($1)
|
||||
`, query); err != nil {
|
||||
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Postgres) RecursBefore(date time.Time) ([]item.Item, error) {
|
||||
query := `
|
||||
SELECT id, kind, updated, deleted, body, recurrer, recur_next
|
||||
|
@ -209,6 +161,54 @@ func (p *Postgres) RecursNext(id string, date time.Time) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *Postgres) migrate(wanted []string) error {
|
||||
// Create migration table if not exists
|
||||
_, err := p.db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS migration
|
||||
(id SERIAL PRIMARY KEY, query TEXT)
|
||||
`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
||||
}
|
||||
|
||||
// Find existing migrations
|
||||
rows, err := p.db.Query(`SELECT query FROM migration ORDER BY id`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var existing []string
|
||||
for rows.Next() {
|
||||
var query string
|
||||
if err := rows.Scan(&query); err != nil {
|
||||
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
||||
}
|
||||
existing = append(existing, query)
|
||||
}
|
||||
|
||||
// Compare and execute missing migrations
|
||||
missing, err := compareMigrations(wanted, existing)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
||||
}
|
||||
|
||||
for _, query := range missing {
|
||||
if _, err := p.db.Exec(query); err != nil {
|
||||
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
||||
}
|
||||
|
||||
// Register migration
|
||||
if _, err := p.db.Exec(`
|
||||
INSERT INTO migration (query) VALUES ($1)
|
||||
`, query); err != nil {
|
||||
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func compareMigrations(wanted, existing []string) ([]string, error) {
|
||||
var needed []string
|
||||
if len(wanted) < len(existing) {
|
||||
|
|
Loading…
Reference in New Issue