DropIndex

DropIndex drops named index “in” on database table “tn”. The method performs a check for the existence of the specified index prior to initiating the drop request to the database. In the event that the specified index does not exist, no error is returned as the database is deemed to be in the correct state.

When using DropIndex it is important to consider whether the table’s source model (go struct annotated with ‘sqac:’ tags) contains the index declaration. If the dropped index remains in the source go struct’s ‘sqac:’ tags it will be recreated if PublicDB.AlterTables is called with said go struct in the future.

Errors encountered during DropIndex execution are returned to the caller immediately.

DropIndex Example

The following structure ‘Depot’ has been used to create table “depot” in the target database. We will subsequently use the PublicDB.CreateIndex() method to create a new non-unique index on table columns “region” and “county”:

package main

import (
  "log"

  "github.com/1414C/sqac"
  _ "github.com/SAP/go-hdb/driver"
  _ "github.com/denisenkom/go-mssqldb"
  _ "github.com/go-sql-driver/mysql"
  _ "github.com/lib/pq"
  _ "github.com/mattn/go-sqlite3"
)

func main() {

  var (
    Handle sqac.PublicDB
  )

  // table depot declaration with index idx_depot_region_county declaration
  type Depot struct {
    DepotNum   int       `db:"depot_num" sqac:"primary_key:inc"`
    CreateDate time.Time `db:"create_date" sqac:"nullable:false;default:now();"`
    Country    string    `db:"country" sqac:"nullable:false;default:CA"`
    Province   string    `db:"province" sqac:"nullable:false;default:AB"`
    Region     string    `db:"region" sqac:"nullable:false;default:YYC;index:idx_depot_region_county"`
    County     string    `db:"county" sqac:"nullable:false;index:idx_depot_region_county"`
    Population int       `db:"population" sqac:"nullable:false;default:0;index:non-unique"`
  }

  // Create a PublicDB instance to connect to the test sqlite db
  Handle = sqac.Create("sqlite", false, false, "testdb.sqlite")
  defer Handle.Close()

  // Ensure table "depot" exists in the database
  err := Handle.DestructiveResetTables(Depot{})
  if err != nil {
    log.Fatalf("%s", err.Error())
  }

  // Determine the table name in the database
  tn := sqac.GetTableName(Depot{})    // "depot"
  if tn == "" {
    log.Fatalf("Unable to determine table name for struct %v", Depot{})
  }

  // Drop index "idx_depot_region_county" on table "depot"
  err = Handle.DropIndex(tn, "idx_depot_region_county")
  if err != nil {
    log.Errorf("%s", err.Error())
  }
  Handle.Close()
}