COPY DB vs. CREATE DB
Wednesday, 16 February 2022 · 4 messages · last reply 17 Feb 2022
EK
I’m not sure I have the best answer, but I’ve been working on this myself. Not sure this answer is actually helpful…
The technique I’m using is:
1. See if target database ($to) exists.
2. If target database exists, rename it to a generated “backup” name: db:alter($to, $backupName)
3. Copy source database to target db:copy($from, $to)I have a separate function to clean up the backup databases periodically (I making the backups just in case right now).
I’m doing this with jobs from within XQuery (jobs module, as recommended by Christian).
I’m building a little “orchestration” infrastructure that simplifies the construction of the individual jobs, which are just little XQueries as shown in the docs for jobs:eval():
let $database := 'aair'
let $cleanBackups := dbadmin:makeJob('dbadmin:cleanupBackupDatabases')
let $makeBackupDatabase := dbadmin:makeJob('dbadmin:makeBackupDatabase', $database)
let $copyProdToTemp := dbadmin:makeJob('dbadmin:copyProdToTempValidationReport', $database)
let $jobs as xs:string* := (
$cleanBackups,
$makeBackupDatabase,
$copyProdToTemp,
()
)return dbadmin:runJobs($jobs)
Where dbadmin:makeJob() looks like:
(:~
: Constructs an XQuery that will run the specified function with the specified
: string parameters.
: @param parms Parameter values. Each value will be passed to the function as a quoted string.
: @return An XQuery string suitable for running with job:eval() or xquery:eval()
:)
declare function dbadmin:makeJob(
$funcName as xs:string,
$parms as xs:string*
) as xs:string {
orch:makeJob(
'http://servicenow.com/xquery/module/database-admin',
$funcName,
$parms)
};And orch:makeJob() just does the string concatenation to build the job string.
So basically at the level of orchestrating actions as separate transactions I can treat them syntactically as function calls through the makeJob() functions and each module provides its own makeJob() function to associate its namespace. Probably a way to abstract that out too but this is sufficient for now.
Cheers,
E
_____________________________________________
Eliot Kimber
Sr Staff Content Engineer
O: 512 554 9368
M: 512 554 9368
servicenow.com<https://www.servicenow.com>
LinkedIn<https://www.linkedin.com/company/servicenow> | Twitter<https://twitter.com/servicenow> | YouTube<https://www.youtube.com/user/servicenowinc> | Facebook<https://www.facebook.com/servicenow>
Quoted text · BaseX-Talk · 9 lines
From: BaseX-Talk <basex-talk-bounces at mailman.uni-konstanz.de> on behalf of Jonathan Robie <jonathan.robie at gmail.com> Date: Wednesday, February 16, 2022 at 2:38 PM To: BaseX <basex-talk at mailman.uni-konstanz.de> Subject: [basex-talk] COPY DB vs. CREATE DB [External Email] What's the easiest way to copy a database, whether or not there is an existing copy with a given name? If I load a database with CREATE DB, it replaces any existing database with the same name. I would like to do the same with COPY DB, but it raises an error if there is a database with the same name. And DELETE DB raises an error if there isn't a database with that name. So what's the right way to do this? Jonathan
JR
Jonathan Robie16 Feb 2022 22:38 #
What's the easiest way to copy a database, whether or not there is an existing copy with a given name?
If I load a database with CREATE DB, it replaces any existing database with the same name.
I would like to do the same with COPY DB, but it raises an error if there is a database with the same name. And DELETE DB raises an error if there isn't a database with that name. So what's the right way to do this?
Jonathan
CG
Christian Grün16 Feb 2022 23:25 #
Hi Jonathan,
If you use the XQuery function
db:copy, an existing database will simply be overwritten. The much older COPY command should by all means behave identically, so I’ve just updated to BaseX to make this happen [1,2]. You can use the command XQUERY db:copy('old', 'new') if you don’t want to wait for BaseX 9.7 (but it should only be a few days from now).
Best,
Christian
[1] https://files.basex.org/releases/latest/
[2] https://docs.basex.org/wiki/Commands#COPYQuoted text · Jonathan Robie · 9 lines
On Wed, Feb 16, 2022 at 9:38 PM Jonathan Robie <jonathan.robie at gmail.com> wrote: > > What's the easiest way to copy a database, whether or not there is an existing copy with a given name? > > If I load a database with CREATE DB, it replaces any existing database with the same name. > > I would like to do the same with COPY DB, but it raises an error if there is a database with the same name. And DELETE DB raises an error if there isn't a database with that name. So what's the right way to do this? > > Jonathan
JR
Jonathan Robie17 Feb 2022 00:55 #
Perfect.
In this particular case, doing it from within the query is better regardless, since the query makes no sense if it doesn't start by doing this.
Thanks!
Jonathan
Quoted text · Christian Grün · 33 lines
On Wed, Feb 16, 2022 at 5:25 PM Christian Grün <christian.gruen at gmail.com> wrote: > Hi Jonathan, > > If you use the XQuery function db:copy, an existing database will > simply be overwritten. The much older COPY command should by all means > behave identically, so I’ve just updated to BaseX to make this happen > [1,2]. You can use the command XQUERY db:copy('old', 'new') if you > don’t want to wait for BaseX 9.7 (but it should only be a few days > from now). > > Best, > Christian > > [1] https://files.basex.org/releases/latest/ > [2] https://docs.basex.org/wiki/Commands#COPY > > > On Wed, Feb 16, 2022 at 9:38 PM Jonathan Robie <jonathan.robie at gmail.com> > wrote: > > > > What's the easiest way to copy a database, whether or not there is an > existing copy with a given name? > > > > If I load a database with CREATE DB, it replaces any existing database > with the same name. > > > > I would like to do the same with COPY DB, but it raises an error if > there is a database with the same name. And DELETE DB raises an error if > there isn't a database with that name. So what's the right way to do this? > > > > Jonathan >
Written for BaseX 9.6. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery