create spatial indices
parent
631470bbd9
commit
ab9480e1ee
|
@ -22,6 +22,10 @@ type Deployer interface {
|
|||
RemoveBackup() error
|
||||
}
|
||||
|
||||
type Finisher interface {
|
||||
Finish() error
|
||||
}
|
||||
|
||||
var databases map[string]func(Config, *mapping.Mapping) (DB, error)
|
||||
|
||||
func init() {
|
||||
|
|
|
@ -236,13 +236,7 @@ func (pg *PostGIS) InsertBatch(table string, rows [][]interface{}) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
if tx != nil {
|
||||
if err := tx.Rollback(); err != nil {
|
||||
log.Println("rollback failed", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
defer rollbackIfTx(&tx)
|
||||
|
||||
sql := spec.InsertSQL()
|
||||
stmt, err := tx.Prepare(sql)
|
||||
|
@ -262,7 +256,7 @@ func (pg *PostGIS) InsertBatch(table string, rows [][]interface{}) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tx = nil
|
||||
tx = nil // set nil to prevent rollback
|
||||
return nil
|
||||
|
||||
}
|
||||
|
@ -286,7 +280,6 @@ func tableExists(tx *sql.Tx, schema, table string) (bool, error) {
|
|||
table, schema)
|
||||
row := tx.QueryRow(sql)
|
||||
err := row.Scan(&exists)
|
||||
// fmt.Println(exists, err, sql)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
@ -308,13 +301,7 @@ func (pg *PostGIS) rotate(source, dest, backup string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
if tx != nil {
|
||||
if err := tx.Rollback(); err != nil {
|
||||
log.Println("rollback failed", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
defer rollbackIfTx(&tx)
|
||||
|
||||
for tableName, _ := range pg.Tables {
|
||||
tableName = pg.Prefix + tableName
|
||||
|
@ -365,7 +352,7 @@ func (pg *PostGIS) rotate(source, dest, backup string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tx = nil
|
||||
tx = nil // set nil to prevent rollback
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -377,18 +364,20 @@ func (pg *PostGIS) RevertDeploy() error {
|
|||
return pg.rotate(pg.BackupSchema, "public", pg.Schema)
|
||||
}
|
||||
|
||||
func rollbackIfTx(tx **sql.Tx) {
|
||||
if *tx != nil {
|
||||
if err := tx.Rollback(); err != nil {
|
||||
log.Println("rollback failed", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}()
|
||||
defer rollbackIfTx(&tx)
|
||||
|
||||
backup := pg.BackupSchema
|
||||
|
||||
|
@ -413,7 +402,36 @@ func (pg *PostGIS) RemoveBackup() error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tx = nil
|
||||
tx = nil // set nil to prevent rollback
|
||||
return nil
|
||||
}
|
||||
|
||||
// Finish creates spatial indices on all tables.
|
||||
func (pg *PostGIS) Finish() error {
|
||||
tx, err := pg.Db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rollbackIfTx(&tx)
|
||||
|
||||
for tableName, table := range pg.Tables {
|
||||
tableName := pg.Prefix + tableName
|
||||
for _, col := range table.Columns {
|
||||
if col.Type.Name() == "GEOMETRY" {
|
||||
sql := fmt.Sprintf(`CREATE INDEX "%s_geom" ON "%s"."%s" USING GIST ("%s")`,
|
||||
tableName, pg.Schema, tableName, col.Name)
|
||||
_, err := tx.Exec(sql)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tx = nil // set nil to prevent rollback
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
28
goposm.go
28
goposm.go
|
@ -76,6 +76,14 @@ func main() {
|
|||
go stats.MemProfiler(parts[0], interval)
|
||||
}
|
||||
|
||||
if (*write || *read != "") && (*revertDeploy || *removeBackup) {
|
||||
log.Fatal("-revertdeploy and -removebackup not compatible with -read/-write")
|
||||
}
|
||||
|
||||
if *revertDeploy && (*removeBackup || *deployProduction) {
|
||||
log.Fatal("-revertdeploy not compatible with -deployproduction/-removebackup")
|
||||
}
|
||||
|
||||
osmCache := cache.NewOSMCache(*cachedir)
|
||||
|
||||
if *read != "" && osmCache.Exists() {
|
||||
|
@ -168,11 +176,21 @@ func main() {
|
|||
nodeWriter.Close()
|
||||
insertBuffer.Close()
|
||||
dbWriter.Close()
|
||||
|
||||
if db, ok := db.(database.Finisher); ok {
|
||||
if err := db.Finish(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
log.Fatal("database not finishable")
|
||||
}
|
||||
}
|
||||
|
||||
if *deployProduction {
|
||||
if db, ok := db.(database.Deployer); ok {
|
||||
db.Deploy()
|
||||
if err := db.Deploy(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
log.Fatal("database not deployable")
|
||||
}
|
||||
|
@ -180,7 +198,9 @@ func main() {
|
|||
|
||||
if *revertDeploy {
|
||||
if db, ok := db.(database.Deployer); ok {
|
||||
db.RevertDeploy()
|
||||
if err := db.RevertDeploy(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
log.Fatal("database not deployable")
|
||||
}
|
||||
|
@ -188,7 +208,9 @@ func main() {
|
|||
|
||||
if *removeBackup {
|
||||
if db, ok := db.(database.Deployer); ok {
|
||||
db.RemoveBackup()
|
||||
if err := db.RemoveBackup(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
log.Fatal("database not deployable")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue