Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 31 additions & 28 deletions internal/cmd/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,22 @@ func (d *databaseListItem) MarshalCSVValue() interface{} {

// Database is the human table for show/create/update, including settings.
type Database struct {
Name string `header:"name" json:"name"`
Kind string `header:"kind" json:"kind"`
DefaultBranch string `header:"default_branch" json:"default_branch"`
RequireApprovalForDeploy bool `header:"require_approval_for_deploy" json:"require_approval_for_deploy"`
RestrictBranchRegion bool `header:"restrict_branch_region" json:"restrict_branch_region"`
AllowDataBranching bool `header:"allow_data_branching" json:"allow_data_branching"`
ForeignKeysEnabled bool `header:"foreign_keys_enabled" json:"foreign_keys_enabled"`
AutomaticMigrations string `header:"automatic_migrations" json:"automatic_migrations"`
MigrationFramework string `header:"migration_framework" json:"migration_framework"`
MigrationTableName string `header:"migration_table_name" json:"migration_table_name"`
InsightsRawQueries bool `header:"insights_raw_queries" json:"insights_raw_queries"`
InsightsEnabled bool `header:"insights_enabled" json:"insights_enabled"`
ProductionBranchWebConsole bool `header:"production_branch_web_console" json:"production_branch_web_console"`
CreatedAt int64 `header:"created_at,timestamp(ms|utc|human)" json:"created_at"`
UpdatedAt int64 `header:"updated_at,timestamp(ms|utc|human)" json:"updated_at"`
Name string `header:"name" json:"name"`
Kind string `header:"kind" json:"kind"`
DefaultBranch string `header:"default_branch" json:"default_branch"`
RequireApprovalForDeploy bool `header:"require_approval_for_deploy" json:"require_approval_for_deploy"`
RestrictBranchRegion bool `header:"restrict_branch_region" json:"restrict_branch_region"`
AllowDataBranching bool `header:"allow_data_branching" json:"allow_data_branching"`
ForeignKeysEnabled bool `header:"foreign_keys_enabled" json:"foreign_keys_enabled"`
AutomaticMigrations string `header:"automatic_migrations" json:"automatic_migrations"`
MigrationFramework string `header:"migration_framework" json:"migration_framework"`
MigrationTableName string `header:"migration_table_name" json:"migration_table_name"`
InsightsRawQueries bool `header:"insights_raw_queries" json:"insights_raw_queries"`
InsightsEnabled bool `header:"insights_enabled" json:"insights_enabled"`
ProductionBranchWebConsole bool `header:"production_branch_web_console" json:"production_branch_web_console"`
MultipleAdminsRequiredForDeletion bool `header:"multiple_admins_required_for_deletion" json:"multiple_admins_required_for_deletion"`
CreatedAt int64 `header:"created_at,timestamp(ms|utc|human)" json:"created_at"`
UpdatedAt int64 `header:"updated_at,timestamp(ms|utc|human)" json:"updated_at"`

orig *ps.Database
}
Expand All @@ -87,19 +88,20 @@ type Database struct {
// database model, including settings.
func toDatabase(db *ps.Database) *Database {
out := &Database{
Name: db.Name,
Kind: string(db.Kind),
DefaultBranch: db.DefaultBranch,
RequireApprovalForDeploy: db.RequireApprovalForDeploy,
RestrictBranchRegion: db.RestrictBranchRegion,
AllowDataBranching: db.AllowDataBranching,
ForeignKeysEnabled: db.ForeignKeysEnabled,
InsightsRawQueries: db.InsightsRawQueries,
InsightsEnabled: db.InsightsEnabled,
ProductionBranchWebConsole: db.ProductionBranchWebConsole,
CreatedAt: db.CreatedAt.UTC().UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond)),
UpdatedAt: db.UpdatedAt.UTC().UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond)),
orig: db,
Name: db.Name,
Kind: string(db.Kind),
DefaultBranch: db.DefaultBranch,
RequireApprovalForDeploy: db.RequireApprovalForDeploy,
RestrictBranchRegion: db.RestrictBranchRegion,
AllowDataBranching: db.AllowDataBranching,
ForeignKeysEnabled: db.ForeignKeysEnabled,
InsightsRawQueries: db.InsightsRawQueries,
InsightsEnabled: db.InsightsEnabled,
ProductionBranchWebConsole: db.ProductionBranchWebConsole,
MultipleAdminsRequiredForDeletion: db.MultipleAdminsRequiredForDeletion,
CreatedAt: db.CreatedAt.UTC().UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond)),
UpdatedAt: db.UpdatedAt.UTC().UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond)),
orig: db,
}

if db.AutomaticMigrations != nil {
Expand Down Expand Up @@ -161,6 +163,7 @@ func printDatabaseHuman(p *printer.Printer, db *Database) {
p.Printf("%-32s %t\n", "Restrict Branch Region", db.RestrictBranchRegion)
p.Printf("%-32s %t\n", "Insights Enabled", db.InsightsEnabled)
p.Printf("%-32s %t\n", "Production Branch Web Console", db.ProductionBranchWebConsole)
p.Printf("%-32s %t\n", "Multi-Admin Deletion", db.MultipleAdminsRequiredForDeletion)

if vitess {
p.Printf("%-32s %t\n", "Insights Raw Queries", db.InsightsRawQueries)
Expand Down
28 changes: 17 additions & 11 deletions internal/cmd/database/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ import (
// UpdateCmd updates configurable database settings.
func UpdateCmd(ch *cmdutil.Helper) *cobra.Command {
var flags struct {
newName string
defaultBranch string
requireApprovalForDeploy bool
restrictBranchRegion bool
allowDataBranching bool
allowForeignKeyConstraints bool
automaticMigrations bool
migrationFramework string
migrationTableName string
insightsRawQueries bool
productionBranchWebConsole bool
newName string
defaultBranch string
requireApprovalForDeploy bool
restrictBranchRegion bool
allowDataBranching bool
allowForeignKeyConstraints bool
automaticMigrations bool
migrationFramework string
migrationTableName string
insightsRawQueries bool
productionBranchWebConsole bool
multipleAdminsRequiredForDeletion bool
}

cmd := &cobra.Command{
Expand Down Expand Up @@ -67,6 +68,10 @@ Flags marked "Vitess only" are rejected for PostgreSQL databases.`,
req.ProductionBranchWebConsole = &flags.productionBranchWebConsole
changed = true
}
if cmd.Flags().Changed("multiple-admins-required-for-deletion") {
req.MultipleAdminsRequiredForDeletion = &flags.multipleAdminsRequiredForDeletion
changed = true
}

if cmd.Flags().Changed("insights-raw-queries") {
req.InsightsRawQueries = &flags.insightsRawQueries
Expand Down Expand Up @@ -163,6 +168,7 @@ Flags marked "Vitess only" are rejected for PostgreSQL databases.`,
cmd.Flags().StringVar(&flags.defaultBranch, "default-branch", "", "The default branch of the database (PostgreSQL and Vitess)")
cmd.Flags().BoolVar(&flags.restrictBranchRegion, "restrict-branch-region", false, "Limit branch creation to the database region (PostgreSQL and Vitess)")
cmd.Flags().BoolVar(&flags.productionBranchWebConsole, "production-branch-web-console", false, "Allow the web console on the production branch (PostgreSQL and Vitess)")
cmd.Flags().BoolVar(&flags.multipleAdminsRequiredForDeletion, "multiple-admins-required-for-deletion", false, "Require approval from two admins to delete the database (PostgreSQL and Vitess)")

cmd.Flags().BoolVar(&flags.insightsRawQueries, "insights-raw-queries", false, "Collect full SQL queries for Insights (Vitess only; PostgreSQL uses pgconf.pginsights.raw_queries)")
cmd.Flags().BoolVar(&flags.requireApprovalForDeploy, "require-approval-for-deploy", false, "Require admin approval for deploy requests (Vitess only)")
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/database/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func TestDatabase_UpdateCmd(t *testing.T) {
c.Assert(*req.InsightsRawQueries, qt.IsFalse)
c.Assert(req.ProductionBranchWebConsole, qt.IsNotNil)
c.Assert(*req.ProductionBranchWebConsole, qt.IsFalse)
c.Assert(req.MultipleAdminsRequiredForDeletion, qt.IsNotNil)
c.Assert(*req.MultipleAdminsRequiredForDeletion, qt.IsTrue)
return res, nil
},
}
Expand Down Expand Up @@ -97,6 +99,7 @@ func TestDatabase_UpdateCmd(t *testing.T) {
"--migration-table-name", tableName,
"--insights-raw-queries=false",
"--production-branch-web-console=false",
"--multiple-admins-required-for-deletion=true",
})
err := cmd.Execute()

Expand Down
68 changes: 35 additions & 33 deletions internal/planetscale/databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,20 @@ type DeleteDatabaseRequest struct {
// UpdateDatabaseSettingsRequest encapsulates the request for updating
// database settings.
type UpdateDatabaseSettingsRequest struct {
Organization string `json:"-"`
Database string `json:"-"`
NewName *string `json:"new_name,omitempty"`
AutomaticMigrations *bool `json:"automatic_migrations,omitempty"`
MigrationFramework *string `json:"migration_framework,omitempty"`
MigrationTableName *string `json:"migration_table_name,omitempty"`
RequireApprovalForDeploy *bool `json:"require_approval_for_deploy,omitempty"`
RestrictBranchRegion *bool `json:"restrict_branch_region,omitempty"`
AllowDataBranching *bool `json:"allow_data_branching,omitempty"`
AllowForeignKeyConstraints *bool `json:"allow_foreign_key_constraints,omitempty"`
InsightsRawQueries *bool `json:"insights_raw_queries,omitempty"`
ProductionBranchWebConsole *bool `json:"production_branch_web_console,omitempty"`
DefaultBranch *string `json:"default_branch,omitempty"`
Organization string `json:"-"`
Database string `json:"-"`
NewName *string `json:"new_name,omitempty"`
AutomaticMigrations *bool `json:"automatic_migrations,omitempty"`
MigrationFramework *string `json:"migration_framework,omitempty"`
MigrationTableName *string `json:"migration_table_name,omitempty"`
RequireApprovalForDeploy *bool `json:"require_approval_for_deploy,omitempty"`
RestrictBranchRegion *bool `json:"restrict_branch_region,omitempty"`
AllowDataBranching *bool `json:"allow_data_branching,omitempty"`
AllowForeignKeyConstraints *bool `json:"allow_foreign_key_constraints,omitempty"`
InsightsRawQueries *bool `json:"insights_raw_queries,omitempty"`
ProductionBranchWebConsole *bool `json:"production_branch_web_console,omitempty"`
MultipleAdminsRequiredForDeletion *bool `json:"multiple_admins_required_for_deletion,omitempty"`
DefaultBranch *string `json:"default_branch,omitempty"`
}

// GetDatabaseThrottlerRequest gets the database-level Vitess migration throttler.
Expand Down Expand Up @@ -156,26 +157,27 @@ const (

// Database represents a PlanetScale database
type Database struct {
Name string `json:"name"`
Notes string `json:"notes"`
Region Region `json:"region"`
State DatabaseState `json:"state"`
Kind DatabaseEngine `json:"kind"`
HtmlURL string `json:"html_url"`
DefaultBranch string `json:"default_branch"`
RequireApprovalForDeploy bool `json:"require_approval_for_deploy"`
RestrictBranchRegion bool `json:"restrict_branch_region"`
AllowDataBranching bool `json:"allow_data_branching"`
ForeignKeysEnabled bool `json:"foreign_keys_enabled"`
AutomaticMigrations *bool `json:"automatic_migrations"`
InsightsRawQueries bool `json:"insights_raw_queries"`
InsightsEnabled bool `json:"insights_enabled"`
ProductionBranchWebConsole bool `json:"production_branch_web_console"`
MigrationTableName *string `json:"migration_table_name"`
MigrationFramework *string `json:"migration_framework"`
EncryptionKeyID string `json:"encryption_key_id,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Name string `json:"name"`
Notes string `json:"notes"`
Region Region `json:"region"`
State DatabaseState `json:"state"`
Kind DatabaseEngine `json:"kind"`
HtmlURL string `json:"html_url"`
DefaultBranch string `json:"default_branch"`
RequireApprovalForDeploy bool `json:"require_approval_for_deploy"`
RestrictBranchRegion bool `json:"restrict_branch_region"`
AllowDataBranching bool `json:"allow_data_branching"`
ForeignKeysEnabled bool `json:"foreign_keys_enabled"`
AutomaticMigrations *bool `json:"automatic_migrations"`
InsightsRawQueries bool `json:"insights_raw_queries"`
InsightsEnabled bool `json:"insights_enabled"`
ProductionBranchWebConsole bool `json:"production_branch_web_console"`
MultipleAdminsRequiredForDeletion bool `json:"multiple_admins_required_for_deletion"`
MigrationTableName *string `json:"migration_table_name"`
MigrationFramework *string `json:"migration_framework"`
EncryptionKeyID string `json:"encryption_key_id,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}

// Database represents a list of PlanetScale databases
Expand Down
28 changes: 15 additions & 13 deletions internal/planetscale/databases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ func TestDatabases_UpdateSettings(t *testing.T) {
c.Assert(body["migration_framework"], qt.Equals, framework)
c.Assert(body["migration_table_name"], qt.Equals, tableName)
c.Assert(body["production_branch_web_console"], qt.Equals, disabled)
c.Assert(body["multiple_admins_required_for_deletion"], qt.Equals, requireApproval)

w.WriteHeader(200)
out := `{
Expand Down Expand Up @@ -559,19 +560,20 @@ func TestDatabases_UpdateSettings(t *testing.T) {

ctx := context.Background()
db, err := client.Databases.UpdateSettings(ctx, &UpdateDatabaseSettingsRequest{
Organization: testOrg,
Database: testDatabase,
NewName: &newName,
DefaultBranch: &defaultBranch,
RequireApprovalForDeploy: &requireApproval,
RestrictBranchRegion: &disabled,
AllowDataBranching: &disabled,
AllowForeignKeyConstraints: &disabled,
AutomaticMigrations: &disabled,
MigrationFramework: &framework,
MigrationTableName: &tableName,
InsightsRawQueries: &insightsRaw,
ProductionBranchWebConsole: &disabled,
Organization: testOrg,
Database: testDatabase,
NewName: &newName,
DefaultBranch: &defaultBranch,
RequireApprovalForDeploy: &requireApproval,
RestrictBranchRegion: &disabled,
AllowDataBranching: &disabled,
AllowForeignKeyConstraints: &disabled,
AutomaticMigrations: &disabled,
MigrationFramework: &framework,
MigrationTableName: &tableName,
InsightsRawQueries: &insightsRaw,
ProductionBranchWebConsole: &disabled,
MultipleAdminsRequiredForDeletion: &requireApproval,
})

c.Assert(err, qt.IsNil)
Expand Down