Written by

Product Specialist - Cloud Operations at InterSystems Corporation
Question Steven LeBlanc · Dec 17, 2015

API equivalent to 'Recreate a database' option in ^DATABASE routine?

Hi,

Is there any API equivalent (within Config.Databases class, or elsewhere) that has the same functionality as the 'Recreate a database' option in the ^DATABASE routine?

This option was added to ^DATABASE (according to internal Devlog CFL1263):

to recreate a database which is equivalent to deleting the .DAT file and recreating it.

I tested this, and the recreate option also appears to also preserve the original database parameters (e.g. max size, resource name).  But when I separately try Delete, and then Create, it does not preserve those settings, so the 'Recreate' option provides additional functionality. 

Is there a way to accomplish this programmatically through a supported API, other than scripting the ^DATABASE routine?

Thanks,

Steve

Comments

Timothy Leavitt · Dec 17, 2015

I think SYS.Database (in %SYS), rather than Config.Databases, can accomplish what you want. Particularly, you can open an object representing a database, call its Delete() method, and then call %Save() on it. That seems to have the same effect you're looking for.

Here's a sample class (Demo.Recreate):

Include %occInclude

Class Demo.Recreate

{

ClassMethod Run(pDBDirectory As %String)

{

    new $Namespace

    zn "%SYS"

    try {    

        //Get the database

        set tDB = ##class(SYS.Database).%OpenId(pDBDirectory)

        If '$IsObject(tDB) {

            $$$ThrowStatus($$$ERROR($$$GeneralError,"Database "_pDBDirectory_" not found."))

        }

        

        write !,"Properties of database:",!

        zw tDB

        

        write !

        

        //For demonstration purposes: show contents of a global in that DB

        for i=1:1:10 {

            set ^["^^"_pDBDirectory]demo(i) = i

        }

        write "Contents of ^[""^^"_pDBDirectory_"""]demo: ",!

        zw ^["^^"_pDBDirectory]demo

        

        write !

        

        write "Deleting database..."

        $$$THROWONERROR(tSC,tDB.Delete())

        write " done.",!

        write "Recreating database..."

        $$$THROWONERROR(tSC,tDB.%Save())

        write " done.",!

        

        write !

        

        write !,"Properties of database:",!

        zw tDB

        

        write !

        

        //For demonstration purposes: show that contents of global in that DB are gone

        write "Contents of ^[""^^"_pDBDirectory_"""]demo: ",!

        zw ^["^^"_pDBDirectory]demo

        

        zw tDB

    } catch anyException {

        write anyException.DisplayString(),!

    }

}

}

0
Steven LeBlanc · Dec 17, 2015

Looks like that does the trick. Thanks Tim!

0