From 08f43285808ba7f38a8f8ec55d7fdcd5abd0fd06 Mon Sep 17 00:00:00 2001 From: "Erik Winter (aider)" Date: Mon, 2 Dec 2024 08:06:43 +0100 Subject: [PATCH] refactor: Reorder methods to place public methods before private methods --- sync/service/postgres.go | 96 ++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/sync/service/postgres.go b/sync/service/postgres.go index 84dbd49..f64dbc3 100644 --- a/sync/service/postgres.go +++ b/sync/service/postgres.go @@ -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) {