add -removebackup option
parent
599a1d39f5
commit
631470bbd9
|
@ -17,8 +17,9 @@ type DB interface {
|
|||
}
|
||||
|
||||
type Deployer interface {
|
||||
DeployProduction() error
|
||||
Deploy() error
|
||||
RevertDeploy() error
|
||||
RemoveBackup() error
|
||||
}
|
||||
|
||||
var databases map[string]func(Config, *mapping.Mapping) (DB, error)
|
||||
|
|
|
@ -369,7 +369,7 @@ func (pg *PostGIS) rotate(source, dest, backup string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (pg *PostGIS) DeployProduction() error {
|
||||
func (pg *PostGIS) Deploy() error {
|
||||
return pg.rotate(pg.Schema, "public", pg.BackupSchema)
|
||||
}
|
||||
|
||||
|
@ -377,6 +377,46 @@ func (pg *PostGIS) RevertDeploy() error {
|
|||
return pg.rotate(pg.BackupSchema, "public", pg.Schema)
|
||||
}
|
||||
|
||||
func (pg *PostGIS) RemoveBackup() error {
|
||||
tx, err := pg.Db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
if tx != nil {
|
||||
if err := tx.Rollback(); err != nil {
|
||||
log.Println("rollback failed", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
backup := pg.BackupSchema
|
||||
|
||||
for tableName, _ := range pg.Tables {
|
||||
tableName = pg.Prefix + tableName
|
||||
|
||||
backupExists, err := tableExists(tx, backup, tableName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if backupExists {
|
||||
log.Printf("removing backup of %s from %s", tableName, backup)
|
||||
err = dropTableIfExists(tx, backup, tableName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tx = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func New(conf database.Config, m *mapping.Mapping) (database.DB, error) {
|
||||
db := &PostGIS{}
|
||||
db.Tables = make(map[string]*TableSpec)
|
||||
|
|
13
goposm.go
13
goposm.go
|
@ -42,6 +42,7 @@ var (
|
|||
mappingFile = flag.String("mapping", "", "mapping file")
|
||||
deployProduction = flag.Bool("deployproduction", false, "deploy production")
|
||||
revertDeploy = flag.Bool("revertdeploy", false, "revert deploy to production")
|
||||
removeBackup = flag.Bool("removebackup", false, "remove backups from deploy")
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -104,7 +105,7 @@ func main() {
|
|||
|
||||
var db database.DB
|
||||
|
||||
if *write || *deployProduction || *revertDeploy {
|
||||
if *write || *deployProduction || *revertDeploy || *removeBackup {
|
||||
connType := database.ConnectionType(*connection)
|
||||
conf := database.Config{
|
||||
Type: connType,
|
||||
|
@ -171,7 +172,7 @@ func main() {
|
|||
|
||||
if *deployProduction {
|
||||
if db, ok := db.(database.Deployer); ok {
|
||||
db.DeployProduction()
|
||||
db.Deploy()
|
||||
} else {
|
||||
log.Fatal("database not deployable")
|
||||
}
|
||||
|
@ -184,6 +185,14 @@ func main() {
|
|||
log.Fatal("database not deployable")
|
||||
}
|
||||
}
|
||||
|
||||
if *removeBackup {
|
||||
if db, ok := db.(database.Deployer); ok {
|
||||
db.RemoveBackup()
|
||||
} else {
|
||||
log.Fatal("database not deployable")
|
||||
}
|
||||
}
|
||||
progress.Stop()
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue