DestructiveResetTables

The DestructiveResetTables method is used to drop and recreate one or more tables in the target database. DestructiveResetTables accepts one or more go struct definitions that have been annotated with “sqac:” tags and uses the provided information to drop and recreate the tables in the database. Note that the DestructiveResetTables method will not attempt to drop a table that does not exist in the target database. It follows that the data in the existing table will be lost unless backed up prior to the call to the DestructiveResetTables method.

If a table in the parameter list does not exist in the database, the method will simply create it using the information provided in the struct tags. No effort is made to determine deltas between existing database tables and their replacement declaration in the incoming parameter list.

Errors encountered during table drop / creation are returned to the caller immediately. This may result in the incomplete processing of the tables contained in parameter i …interface{}.

DestructiveResetTables Example

The following structure ‘Depot’ has been used to create table “depot” in the target database. We will change the type of the Region field to an integer value, thereby necessitating a drop and recreation of the table in the database.

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

The data type of column “Region” has been updated from ‘string’ to ‘int’ in the Depot struct declaration:

  type Depot struct {
    DepotNum   int       `db:"depot_num" sqac:"primary_key:inc"`
    CreateDate time.Time `db:"create_date" sqac:"nullable:false;default:now();"`
    // --> start of the alterations
    Region     int       `db:"region" sqac:"nullable:false;default:0"`
    // --> end of the alterations
    Province   string    `db:"province" sqac:"nullable:false;default:AB"`
    Country    string    `db:"country" sqac:"nullable:false;default:CA"`
    Population int       `db:"population" sqac:"nullable:false;default:0;index:non-unique"`
  }

A complete sample program to perform the destructive reset of existing table “depot” is shown below:

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
  )

  //   Original Depot declaration - left for illustrative purposes
  //   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"`
  //     Population int       `db:"population" sqac:"nullable:false;default:0;index:non-unique"`
  //   }

  // updated Depot declaration with new data type int assigned to field "Region"
  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"`
    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")

  // Drop and recreate table "depot" in the target database via destructive reset
  err := Handle.DestuctiveResetTables(Depot{})
  if err != nil {
    log.Errorf("%s", err.Error())
  }

  // Close the connection
  Handle.Close()
}