-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
have a version working with a database
- Loading branch information
1 parent
6e53485
commit 7c1bc4d
Showing
17 changed files
with
279 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/ardanlabs/liarsdice/business/data/migrate" | ||
"github.com/ardanlabs/liarsdice/business/data/sqldb" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var migrateCmd = &cobra.Command{ | ||
Use: "migrate", | ||
Short: "Migrate the database", | ||
Long: `Migrates the database to its most current schema.`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
dbConfig := sqldb.Config{ | ||
User: "postgres", | ||
Password: "postgres", | ||
HostPort: "database-service.liars-system.svc.cluster.local", | ||
Name: "postgres", | ||
MaxIdleConns: 2, | ||
MaxOpenConns: 0, | ||
DisableTLS: true, | ||
} | ||
|
||
return performMigrate(dbConfig) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(migrateCmd) | ||
} | ||
|
||
func performMigrate(cfg sqldb.Config) error { | ||
db, err := sqldb.Open(cfg) | ||
if err != nil { | ||
return fmt.Errorf("connect database: %w", err) | ||
} | ||
defer db.Close() | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
|
||
if err := migrate.Migrate(ctx, db); err != nil { | ||
return fmt.Errorf("migrate database: %w", err) | ||
} | ||
|
||
fmt.Println("migrations complete") | ||
return nil | ||
} |
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
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
Oops, something went wrong.