Skip to content

Commit

Permalink
Add Copy-CouchDBDatabase cmdlet
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoGuadrini committed Jul 9, 2019
1 parent 88759a7 commit 62e2779
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 18 deletions.
4 changes: 3 additions & 1 deletion PSCouchDB/PSCouchDB.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'PSCouchDB.psm1'

# Version number of this module.
ModuleVersion = '1.10.0'
ModuleVersion = '1.11.0'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down Expand Up @@ -71,6 +71,7 @@
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @("Get-CouchDBDatabase",
"Test-CouchDBDatabase",
"Copy-CouchDBDatabase",
"Get-CouchDBServer",
"Get-CouchDBDatabaseInfo",
"Get-CouchDBDatabaseChanges",
Expand Down Expand Up @@ -158,6 +159,7 @@

# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = @("gcdb",
"cpdb",
"tcdb",
"gcsi",
"gcdbc",
Expand Down
66 changes: 60 additions & 6 deletions PSCouchDB/PSCouchDB.psm1
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Alias for all export cmdlets
New-Alias -Name "tcdb" -Value Test-CouchDBDatabase -Option ReadOnly
New-Alias -Name "cpdb" -Value Copy-CouchDBDatabase -Option ReadOnly
New-Alias -Name "gcdb" -Value Get-CouchDBDatabase -Option ReadOnly
New-Alias -Name "gcbpl" -Value Get-CouchDBDatabasePurgedLimit -Option ReadOnly
New-Alias -Name "gcsi" -Value Get-CouchDBServer -Option ReadOnly
Expand Down Expand Up @@ -948,6 +949,65 @@ function Test-CouchDBDatabase () {
Send-CouchDBRequest -Server $Server -Port $Port -Method "HEAD" -Database $Database -Authorization $Authorization -Ssl:$Ssl
}

function Copy-CouchDBDatabase () {
<#
.SYNOPSIS
Copy database.
.DESCRIPTION
Create a new database by copying it from an existing one.
.PARAMETER Server
The CouchDB server name. Default is localhost.
.PARAMETER Port
The CouchDB server port. Default is 5984.
.PARAMETER Database
The source CouchDB database.
.PARAMETER Destination
The destination CouchDB database. A new name must be specified.
.PARAMETER ExcludeIds
Array of Docids to exclude to copy.
.PARAMETER Authorization
The CouchDB authorization form; user and password.
Authorization format like this: user:password
.PARAMETER Ssl
Set ssl connection on CouchDB server.
This modify protocol to https and port to 6984.
.EXAMPLE
Test-CouchDBDatabase -Database test -Destination test_new -Authorization admin:password
Copy a test database in a new test_new database.
.LINK
https://pscouchdb.readthedocs.io/en/latest/databases.html#copy-a-database
#>
[CmdletBinding()]
param(
[string] $Server,
[int] $Port,
[Parameter(mandatory = $true, ValueFromPipeline = $true)]
[string] $Database,
[Parameter(mandatory = $true)]
[string] $Destination,
[array] $ExcludeIds,
[string] $Authorization,
[switch] $Ssl
)
$count = 0
$all_docs = Get-CouchDBDocument -Server $Server -Port $Port -Database $Database -Authorization $Authorization -Ssl:$Ssl
if (-not(Test-CouchDBDatabase -Server $Server -Port $Port -Database $Destination -Authorization $Authorization -Ssl:$Ssl -ErrorAction SilentlyContinue)) {
New-CouchDBDatabase -Server $Server -Port $Port -Database $Destination -Authorization $Authorization -Ssl:$Ssl | Out-Null
} else {
throw "Database $Destination exists! Choose another name."
}
foreach ($doc in $all_docs.rows) {
if ($ExcludeIds -notcontains $doc.id) {
$count++
$Data = Get-CouchDBDocument -Server $Server -Port $Port -Database $Database -Document $doc.id -Authorization $Authorization -Ssl:$Ssl | ConvertTo-Json -Depth 99
New-CouchDBDocument -Server $Server -Port $Port -Database $Destination -Document $doc.id -Data $($Data -replace '"_rev":.*,',"") -Authorization $Authorization -Ssl:$Ssl
Write-Progress -Activity "Copy document $($doc.id) in a new database $Destination in progress" -Status "Progress $count/$($all_docs.total_rows)" -PercentComplete ($count/$all_docs.total_rows*100)
} else {
Write-Host "Skip $($doc.id) because exists in exclude list."
}
}
}

function Get-CouchDBDatabase () {
<#
.SYNOPSIS
Expand Down Expand Up @@ -5386,10 +5446,6 @@ function Read-CouchDBLog () {
Read or tail log.
.DESCRIPTION
Read, tail and follow CouchDB log (couch.log).
.PARAMETER Server
The CouchDB server name. Default is localhost.
.PARAMETER Port
The CouchDB server port. Default is 5984.
.PARAMETER Path
The path of log file. Default, is C:\CouchDB\couch.log on Windows and /var/log/couchdb/couch.log on Unix.
.PARAMETER Level
Expand Down Expand Up @@ -5418,8 +5474,6 @@ function Read-CouchDBLog () {
#>
[CmdletBinding()]
param(
[string] $Server,
[int] $Port,
[string] $Path,
[ValidateSet("debug", "info", "notice", "warning", "error", "critical", "alert", "emergency")]
[Parameter(ValueFromPipeline = $true)]
Expand Down
Binary file modified docs/build/doctrees/cmdlets.doctree
Binary file not shown.
Binary file modified docs/build/doctrees/databases.doctree
Binary file not shown.
Binary file modified docs/build/doctrees/environment.pickle
Binary file not shown.
16 changes: 12 additions & 4 deletions docs/build/html/_sources/cmdlets.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,13 @@ Server

.. code-block:: powershell
Read-CouchDBLog [[-Server] <String>] [[-Port] <Int32>] [[-Path] <String>] [[-Level] <String>] [-Follow] [[-Tail] <Int32>] [<CommonParameters>]
Read-CouchDBLog [[-Path] <String>] [[-Level] <String>] [-Follow] [[-Tail] <Int32>] [<CommonParameters>]
**Clear-CouchDBLog**

.. code-block:: powershell
Clear-CouchDBLog [[-Server] <String>] [[-Port] <Int32>] [[-Path] <String>] [-Rotate] [<CommonParameters>]
Clear-CouchDBLog [[-Path] <String>] [-Rotate] [<CommonParameters>]
Replication
***********
Expand Down Expand Up @@ -264,6 +264,12 @@ Databases
Test-CouchDBDatabase [[-Server] <String>] [[-Port] <Int32>] [-Database] <String> [[-Authorization] <String>] [-Ssl] [<CommonParameters>]
**Copy-CouchDBDatabase**

.. code-block:: powershell
Copy-CouchDBDatabase [[-Server] <String>] [[-Port] <Int32>] [-Database] <String> [-Destination] <String> [[-ExcludeIds] <Array>] [[-Authorization] <String>] [-Ssl] [<CommonParameters>]
**Get-CouchDBDatabase**

.. code-block:: powershell
Expand Down Expand Up @@ -534,7 +540,8 @@ _______
ccdd -> Compress-CouchDBDesignDocument
ccdoc -> Clear-CouchDBDocuments
ccview -> Clear-CouchDBView
cpdoc -> Copy-CouchDBDocument
cpdoc -> Copy-CouchDBDocument
gcdb -> Copy-CouchDBDatabase
eccl -> Enable-CouchDBCluster
fcdoc -> Find-CouchDBDocuments
finddoc -> Find-CouchDBDocuments
Expand Down Expand Up @@ -608,4 +615,5 @@ _______
exportdb -> Export-CouchDBDatabase
icdb -> Export-CouchDBDatabase
importdb -> Export-CouchDBDatabase
rdblog -> Read-CouchDBLog
rdblog -> Read-CouchDBLog
cdblog -> Clear-CouchDBLog
9 changes: 9 additions & 0 deletions docs/build/html/_sources/databases.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ Deletes the specified database, and all the documents and attachments contained
Remove-CouchDBDatabase -Database test -Authorization "admin:password"
Copy a database
_______________

Create a new database *test_copy* by copying it from *test* database.

.. code-block:: powershell
Copy-CouchDBDatabase -Database test -Destination test_copy -Authorization "admin:password"
Index
_____

Expand Down
10 changes: 8 additions & 2 deletions docs/build/html/cmdlets.html
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,11 @@ <h3>Server<a class="headerlink" href="#server" title="Permalink to this headline
</pre></div>
</div>
<p><strong>Read-CouchDBLog</strong></p>
<div class="highlight-powershell notranslate"><div class="highlight"><pre><span></span><span class="nb">Read-CouchDBLog</span> <span class="p">[[</span><span class="n">-Server</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;]</span> <span class="p">[[</span><span class="n">-Port</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">Int32</span><span class="p">&gt;]</span> <span class="p">[[</span><span class="n">-Path</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;]</span> <span class="p">[[</span><span class="n">-Level</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;]</span> <span class="p">[</span><span class="n">-Follow</span><span class="p">]</span> <span class="p">[[</span><span class="n">-Tail</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">Int32</span><span class="p">&gt;]</span> <span class="p">[&lt;</span><span class="n">CommonParameters</span><span class="p">&gt;]</span>
<div class="highlight-powershell notranslate"><div class="highlight"><pre><span></span><span class="nb">Read-CouchDBLog</span> <span class="p">[[</span><span class="n">-Path</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;]</span> <span class="p">[[</span><span class="n">-Level</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;]</span> <span class="p">[</span><span class="n">-Follow</span><span class="p">]</span> <span class="p">[[</span><span class="n">-Tail</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">Int32</span><span class="p">&gt;]</span> <span class="p">[&lt;</span><span class="n">CommonParameters</span><span class="p">&gt;]</span>
</pre></div>
</div>
<p><strong>Clear-CouchDBLog</strong></p>
<div class="highlight-powershell notranslate"><div class="highlight"><pre><span></span><span class="nb">Clear-CouchDBLog</span> <span class="p">[[</span><span class="n">-Server</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;]</span> <span class="p">[[</span><span class="n">-Port</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">Int32</span><span class="p">&gt;]</span> <span class="p">[[</span><span class="n">-Path</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;]</span> <span class="p">[</span><span class="n">-Rotate</span><span class="p">]</span> <span class="p">[&lt;</span><span class="n">CommonParameters</span><span class="p">&gt;]</span>
<div class="highlight-powershell notranslate"><div class="highlight"><pre><span></span><span class="nb">Clear-CouchDBLog</span> <span class="p">[[</span><span class="n">-Path</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;]</span> <span class="p">[</span><span class="n">-Rotate</span><span class="p">]</span> <span class="p">[&lt;</span><span class="n">CommonParameters</span><span class="p">&gt;]</span>
</pre></div>
</div>
</div>
Expand Down Expand Up @@ -348,6 +348,10 @@ <h3>Databases<a class="headerlink" href="#databases" title="Permalink to this he
<div class="highlight-powershell notranslate"><div class="highlight"><pre><span></span><span class="nb">Test-CouchDBDatabase</span> <span class="p">[[</span><span class="n">-Server</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;]</span> <span class="p">[[</span><span class="n">-Port</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">Int32</span><span class="p">&gt;]</span> <span class="p">[</span><span class="n">-Database</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;</span> <span class="p">[[</span><span class="n">-Authorization</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;]</span> <span class="p">[</span><span class="n">-Ssl</span><span class="p">]</span> <span class="p">[&lt;</span><span class="n">CommonParameters</span><span class="p">&gt;]</span>
</pre></div>
</div>
<p><strong>Copy-CouchDBDatabase</strong></p>
<div class="highlight-powershell notranslate"><div class="highlight"><pre><span></span><span class="nb">Copy-CouchDBDatabase</span> <span class="p">[[</span><span class="n">-Server</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;]</span> <span class="p">[[</span><span class="n">-Port</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">Int32</span><span class="p">&gt;]</span> <span class="p">[</span><span class="n">-Database</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;</span> <span class="p">[</span><span class="n">-Destination</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;</span> <span class="p">[[</span><span class="n">-ExcludeIds</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">Array</span><span class="p">&gt;]</span> <span class="p">[[</span><span class="n">-Authorization</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;]</span> <span class="p">[</span><span class="n">-Ssl</span><span class="p">]</span> <span class="p">[&lt;</span><span class="n">CommonParameters</span><span class="p">&gt;]</span>
</pre></div>
</div>
<p><strong>Get-CouchDBDatabase</strong></p>
<div class="highlight-powershell notranslate"><div class="highlight"><pre><span></span><span class="nb">Get-CouchDBDatabase</span> <span class="p">[[</span><span class="n">-Server</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;]</span> <span class="p">[[</span><span class="n">-Port</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">Int32</span><span class="p">&gt;]</span> <span class="p">[[</span><span class="n">-Database</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;]</span> <span class="p">[[</span><span class="n">-Authorization</span><span class="p">]</span> <span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;]</span> <span class="p">[</span><span class="n">-Ssl</span><span class="p">]</span> <span class="p">[&lt;</span><span class="n">CommonParameters</span><span class="p">&gt;]</span>
</pre></div>
Expand Down Expand Up @@ -540,6 +544,7 @@ <h2>Aliases<a class="headerlink" href="#aliases" title="Permalink to this headli
<span class="n">ccdoc</span> <span class="p">-&gt;</span> <span class="nb">Clear-CouchDBDocuments</span>
<span class="n">ccview</span> <span class="p">-&gt;</span> <span class="nb">Clear-CouchDBView</span>
<span class="n">cpdoc</span> <span class="p">-&gt;</span> <span class="nb">Copy-CouchDBDocument</span>
<span class="n">gcdb</span> <span class="p">-&gt;</span> <span class="nb">Copy-CouchDBDatabase</span>
<span class="n">eccl</span> <span class="p">-&gt;</span> <span class="nb">Enable-CouchDBCluster</span>
<span class="n">fcdoc</span> <span class="p">-&gt;</span> <span class="n">Find-CouchDBDocuments</span>
<span class="n">finddoc</span> <span class="p">-&gt;</span> <span class="n">Find-CouchDBDocuments</span>
Expand Down Expand Up @@ -614,6 +619,7 @@ <h2>Aliases<a class="headerlink" href="#aliases" title="Permalink to this headli
<span class="n">icdb</span> <span class="p">-&gt;</span> <span class="nb">Export-CouchDBDatabase</span>
<span class="n">importdb</span> <span class="p">-&gt;</span> <span class="nb">Export-CouchDBDatabase</span>
<span class="n">rdblog</span> <span class="p">-&gt;</span> <span class="nb">Read-CouchDBLog</span>
<span class="n">cdblog</span> <span class="p">-&gt;</span> <span class="nb">Clear-CouchDBLog</span>
</pre></div>
</div>
</div>
Expand Down
8 changes: 8 additions & 0 deletions docs/build/html/databases.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<li class="toctree-l2"><a class="reference internal" href="#read-a-database">Read a database</a></li>
<li class="toctree-l2"><a class="reference internal" href="#create-a-database">Create a database</a></li>
<li class="toctree-l2"><a class="reference internal" href="#remove-a-database">Remove a database</a></li>
<li class="toctree-l2"><a class="reference internal" href="#copy-a-database">Copy a database</a></li>
<li class="toctree-l2"><a class="reference internal" href="#index">Index</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#get-a-index">Get a index</a></li>
<li class="toctree-l3"><a class="reference internal" href="#create-a-new-index">Create a new index</a></li>
Expand Down Expand Up @@ -235,6 +236,13 @@ <h2>Remove a database<a class="headerlink" href="#remove-a-database" title="Perm
</pre></div>
</div>
</div>
<div class="section" id="copy-a-database">
<h2>Copy a database<a class="headerlink" href="#copy-a-database" title="Permalink to this headline"></a></h2>
<p>Create a new database <em>test_copy</em> by copying it from <em>test</em> database.</p>
<div class="highlight-powershell notranslate"><div class="highlight"><pre><span></span><span class="nb">Copy-CouchDBDatabase</span> <span class="n">-Database</span> <span class="n">test</span> <span class="n">-Destination</span> <span class="n">test_copy</span> <span class="n">-Authorization</span> <span class="s2">&quot;admin:password&quot;</span>
</pre></div>
</div>
</div>
<div class="section" id="index">
<h2>Index<a class="headerlink" href="#index" title="Permalink to this headline"></a></h2>
<p>Mango is a declarative JSON querying language for CouchDB databases. Mango wraps several index types, starting with the Primary Index out-of-the-box.</p>
Expand Down
1 change: 1 addition & 0 deletions docs/build/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ <h1>Welcome to PSCouchDB’s documentation!<a class="headerlink" href="#welcome-
<li class="toctree-l2"><a class="reference internal" href="databases.html#read-a-database">Read a database</a></li>
<li class="toctree-l2"><a class="reference internal" href="databases.html#create-a-database">Create a database</a></li>
<li class="toctree-l2"><a class="reference internal" href="databases.html#remove-a-database">Remove a database</a></li>
<li class="toctree-l2"><a class="reference internal" href="databases.html#copy-a-database">Copy a database</a></li>
<li class="toctree-l2"><a class="reference internal" href="databases.html#index">Index</a></li>
<li class="toctree-l2"><a class="reference internal" href="databases.html#shards">Shards</a></li>
<li class="toctree-l2"><a class="reference internal" href="databases.html#changes">Changes</a></li>
Expand Down
2 changes: 1 addition & 1 deletion docs/build/html/searchindex.js

Large diffs are not rendered by default.

16 changes: 12 additions & 4 deletions docs/source/cmdlets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,13 @@ Server

.. code-block:: powershell
Read-CouchDBLog [[-Server] <String>] [[-Port] <Int32>] [[-Path] <String>] [[-Level] <String>] [-Follow] [[-Tail] <Int32>] [<CommonParameters>]
Read-CouchDBLog [[-Path] <String>] [[-Level] <String>] [-Follow] [[-Tail] <Int32>] [<CommonParameters>]
**Clear-CouchDBLog**

.. code-block:: powershell
Clear-CouchDBLog [[-Server] <String>] [[-Port] <Int32>] [[-Path] <String>] [-Rotate] [<CommonParameters>]
Clear-CouchDBLog [[-Path] <String>] [-Rotate] [<CommonParameters>]
Replication
***********
Expand Down Expand Up @@ -264,6 +264,12 @@ Databases
Test-CouchDBDatabase [[-Server] <String>] [[-Port] <Int32>] [-Database] <String> [[-Authorization] <String>] [-Ssl] [<CommonParameters>]
**Copy-CouchDBDatabase**

.. code-block:: powershell
Copy-CouchDBDatabase [[-Server] <String>] [[-Port] <Int32>] [-Database] <String> [-Destination] <String> [[-ExcludeIds] <Array>] [[-Authorization] <String>] [-Ssl] [<CommonParameters>]
**Get-CouchDBDatabase**

.. code-block:: powershell
Expand Down Expand Up @@ -534,7 +540,8 @@ _______
ccdd -> Compress-CouchDBDesignDocument
ccdoc -> Clear-CouchDBDocuments
ccview -> Clear-CouchDBView
cpdoc -> Copy-CouchDBDocument
cpdoc -> Copy-CouchDBDocument
gcdb -> Copy-CouchDBDatabase
eccl -> Enable-CouchDBCluster
fcdoc -> Find-CouchDBDocuments
finddoc -> Find-CouchDBDocuments
Expand Down Expand Up @@ -608,4 +615,5 @@ _______
exportdb -> Export-CouchDBDatabase
icdb -> Export-CouchDBDatabase
importdb -> Export-CouchDBDatabase
rdblog -> Read-CouchDBLog
rdblog -> Read-CouchDBLog
cdblog -> Clear-CouchDBLog
Loading

0 comments on commit 62e2779

Please sign in to comment.