-
Notifications
You must be signed in to change notification settings - Fork 28
Add postgres Store #975
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ChrisSchinnerl
wants to merge
1
commit into
master
Choose a base branch
from
chris/postgres-init
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add postgres Store #975
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,77 @@ | ||
| package postgres | ||
|
|
||
| import ( | ||
| "context" | ||
| _ "embed" // for init.sql | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "go.sia.tech/core/types" | ||
| "go.uber.org/zap" | ||
| "lukechampine.com/frand" | ||
| ) | ||
|
|
||
| // init queries are run when the database is first created. | ||
| // | ||
| //go:embed init.sql | ||
| var initDatabase string | ||
|
|
||
| func (s *Store) initNewDatabase(target int64) error { | ||
| return s.transaction(func(ctx context.Context, tx *txn) error { | ||
| if _, err := tx.Exec(ctx, initDatabase); err != nil { | ||
| return err | ||
| } else if err := setDBVersion(ctx, tx, target); err != nil { | ||
| return fmt.Errorf("failed to set initial database version: %w", err) | ||
| } else if err := generateHostKey(ctx, tx); err != nil { | ||
| return fmt.Errorf("failed to generate host key: %w", err) | ||
| } | ||
| return nil | ||
| }) | ||
| } | ||
|
ChrisSchinnerl marked this conversation as resolved.
|
||
|
|
||
| func (s *Store) upgradeDatabase(current, target int64) error { | ||
| log := s.log.Named("migrations").With(zap.Int64("target", target)) | ||
| for ; current < target; current++ { | ||
| version := current + 1 // initial schema is version 1, migration 0 is version 2, etc. | ||
| log := log.With(zap.Int64("version", version)) | ||
| start := time.Now() | ||
| fn := migrations[current-1] | ||
| err := s.transaction(func(ctx context.Context, tx *txn) error { | ||
| if err := fn(ctx, tx, log); err != nil { | ||
| return err | ||
| } | ||
| return setDBVersion(ctx, tx, version) | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("migration %d failed: %w", version, err) | ||
| } | ||
| log.Info("migration complete", zap.Duration("elapsed", time.Since(start))) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (s *Store) init(target int64) error { | ||
| version := getDBVersion(context.Background(), s.pool) | ||
| switch { | ||
| case version == 0: | ||
| if err := s.initNewDatabase(target); err != nil { | ||
| return fmt.Errorf("failed to initialize database: %w", err) | ||
| } | ||
| case version < target: | ||
| s.log.Info("database version is out of date;", zap.Int64("version", version), zap.Int64("target", target)) | ||
| if err := s.upgradeDatabase(version, target); err != nil { | ||
| return fmt.Errorf("failed to upgrade database: %w", err) | ||
| } | ||
| case version > target: | ||
| return fmt.Errorf("database version %v is newer than expected %v. database downgrades are not supported", version, target) | ||
| } | ||
| // nothing to do | ||
| return nil | ||
| } | ||
|
|
||
| func generateHostKey(ctx context.Context, tx *txn) (err error) { | ||
| key := types.NewPrivateKeyFromSeed(frand.Bytes(32)) | ||
| var dbID int64 | ||
| err = tx.QueryRow(ctx, `UPDATE global_settings SET host_key=$1 RETURNING id`, []byte(key)).Scan(&dbID) | ||
| return | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.