-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(repository): move migrations to folder and refactor repository
- Loading branch information
Showing
10 changed files
with
134 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../000002_create_postgraphile_view.down.sql → .../000002_create_postgraphile_view.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
-- (c) Cartesi and individual authors (see AUTHORS) | ||
-- SPDX-License-Identifier: Apache-2.0 (see LICENSE) | ||
|
||
DROP SCHEMA graphql CASCADE; | ||
DROP SCHEMA graphql CASCADE; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// (c) Cartesi and individual authors (see AUTHORS) | ||
// SPDX-License-Identifier: Apache-2.0 (see LICENSE) | ||
|
||
package schema | ||
|
||
import ( | ||
"embed" | ||
"errors" | ||
"fmt" | ||
"log/slog" | ||
|
||
"github.com/golang-migrate/migrate/v4" | ||
mig "github.com/golang-migrate/migrate/v4" | ||
_ "github.com/golang-migrate/migrate/v4/database/postgres" | ||
_ "github.com/golang-migrate/migrate/v4/source/file" | ||
"github.com/golang-migrate/migrate/v4/source/iofs" | ||
) | ||
|
||
//go:embed migrations/* | ||
var content embed.FS | ||
|
||
const ExpectedVersion uint = 2 | ||
|
||
type Schema struct { | ||
migrate *mig.Migrate | ||
} | ||
|
||
func New(postgresEndpoint string) (*Schema, error) { | ||
driver, err := iofs.New(content, "migrations") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
migrate, err := mig.NewWithSourceInstance("iofs", driver, postgresEndpoint) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Schema{migrate: migrate}, nil | ||
} | ||
|
||
func (s *Schema) Version() (uint, error) { | ||
version, _, err := s.migrate.Version() | ||
if err != nil && errors.Is(err, migrate.ErrNilVersion) { | ||
return version, fmt.Errorf("No valid database schema found") | ||
} | ||
return version, err | ||
} | ||
|
||
func (s *Schema) Upgrade() error { | ||
if err := s.migrate.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (s *Schema) Downgrade() error { | ||
if err := s.migrate.Down(); err != nil && !errors.Is(err, migrate.ErrNoChange) { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (s *Schema) Close() { | ||
source, db := s.migrate.Close() | ||
if source != nil { | ||
slog.Error("Error releasing migration sources", "error", source) | ||
} | ||
if db != nil { | ||
slog.Error("Error closing db connection", "error", db) | ||
} | ||
} | ||
|
||
func (s *Schema) ValidateVersion() (uint, error) { | ||
version, err := s.Version() | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
if version != ExpectedVersion { | ||
format := "Database schema version mismatch. Expected %d but it is %d" | ||
return 0, fmt.Errorf(format, ExpectedVersion, version) | ||
} | ||
return version, nil | ||
} |
Oops, something went wrong.