Skip to content

Commit

Permalink
marklogic-community#109 added ability to add and update triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
dmcassel committed Mar 4, 2016
1 parent 4f0f675 commit 65eac6e
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 0 deletions.
25 changes: 25 additions & 0 deletions deploy/lib/server_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,8 @@ def deploy
deploy_schemas
when 'cpf'
deploy_cpf
when 'triggers'
deploy_triggers
else
raise HelpException.new("deploy", "Invalid WHAT")
end
Expand Down Expand Up @@ -1868,6 +1870,29 @@ def clean_cpf
r = execute_query %Q{#{cpf_code} cpf:clean-cpf()}, :db_name => @properties["ml.content-db"]
end

def deploy_triggers
logger.info "Deploying Triggers"
if !@properties["ml.triggers-db"]
raise ExitException.new("Deploy triggers requires a triggers database")
end

if !File.exist?(ServerConfig.expand_path("#{@@path}/triggers-config.xml"))
logger.error <<-ERR.strip_heredoc
Before you can deploy triggers, you must define a configuration. Steps:
1. Copy deploy/sample/triggers-config.sample.xml to deploy/triggers-config.xml
2. Edit deploy/triggers-config.xml to specify your trigger(s)
3. Run 'ml <env> deploy triggers')
ERR
else
triggers_config = File.read ServerConfig.expand_path("#{@@path}/triggers-config.xml")
replace_properties(triggers_config, "triggers-config.xml")
triggers_code = File.read ServerConfig.expand_path("#{@@path}/lib/xquery/triggers.xqy")
query = %Q{#{triggers_code} triggers:load-from-config(#{triggers_config})}
logger.debug(query)
r = execute_query(query, :db_name => @properties["ml.content-db"])
end
end

def xcc
@xcc ||=
begin
Expand Down
121 changes: 121 additions & 0 deletions deploy/lib/xquery/triggers.xqy
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
(:
Copyright 2016 MarkLogic Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
:)
xquery version "1.0-ml";

declare namespace trgr="http://marklogic.com/xdmp/triggers";

declare namespace triggers = "http://marklogic.com/roxy/triggers";

declare option xdmp:mapping "false";

(:
: Loads a pipeline from a configuration xml
:
:@param $config - the configuration xml
:
:)
declare function triggers:load-from-config($config as element(trgr:triggers))
{
let $triggers-db := xdmp:triggers-database()
let $test :=
if ($triggers-db = 0) then
fn:error(xs:QName("TRIGGERS-DB"), "You must have a triggers database configured to deploy triggers.")
else ()
for $trgr in $config/trgr:trigger
let $name := $trgr/trgr:name
let $desc as xs:string := $trgr/trgr:description
let $data-event := $trgr/trgr:data-event
let $module :=
<trgr:module>
<trgr:database>{xdmp:database($trgr/trgr:module/trgr:database/fn:string())}</trgr:database>
{
$trgr/trgr:module/* except $trgr/trgr:module/trgr:database
}
</trgr:module>
let $enabled as xs:boolean := $trgr/trgr:enabled
let $permissions as element()* :=
(
triggers:resolve-permissions($trgr/trgr:permissions/sec:permission),
xdmp:default-permissions()
)[1]
let $recursive as xs:boolean := ($trgr/trgr:recursive, fn:true())[1]
let $priority as xs:string? := $trgr/trgr:task-priority
let $_ :=
if (fn:empty($priority) or $priority = ("normal", "higher")) then ()
else
fn:error(xs:QName("PRIORITY-VALUE"), 'Task priority must be "normal" or "higher".')
return (
xdmp:eval(
'xquery version "1.0-ml";
import module namespace trgr="http://marklogic.com/xdmp/triggers" at "/MarkLogic/triggers.xqy";
declare variable $name external;
declare variable $desc external;
declare variable $data-event external;
declare variable $module external;
declare variable $enabled external;
declare variable $permissions external;
declare variable $recursive external;
declare variable $priority external;
if (fn:exists(/trgr:trigger/trgr:trigger-name[. = $name])) then
(: trigger already exists. update it :)
(
trgr:trigger-set-description($name, $desc),
trgr:trigger-set-event($name, $data-event),
trgr:trigger-set-module($name, $module),
if ($enabled) then trgr:trigger-enable($name)
else trgr:trigger-disable($name),
trgr:trigger-set-permissions($name, $permissions),
trgr:trigger-set-recursive($name, $recursive),
trgr:trigger-set-task-priority($name, $priority)
)
else
(: new trigger. create it. :)
trgr:create-trigger(
$name, $desc,
$data-event,
$module,
$enabled,
$permissions,
$recursive,
($priority, "normal")[1]
)',
map:new((
map:entry("name", $name),
map:entry("desc", $desc),
map:entry("data-event", $data-event),
map:entry("module", $module),
map:entry("enabled", $enabled),
map:entry("permissions", $permissions),
map:entry("recursive", $recursive),
map:entry("priority", ($priority, "normal")[1])
)),
<options xmlns="xdmp:eval">
<database>{xdmp:triggers-database()}</database>
</options>
)
)
};

declare function triggers:resolve-permissions($perms as element(sec:permission)*)
{
for $perm in $perms
return
<sec:permission>
{$perm/sec:capability}
<sec:role-id>{xdmp:role($perm/sec:role-name)}</sec:role-id>
</sec:permission>
};
34 changes: 34 additions & 0 deletions deploy/sample/triggers-config.sample.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<triggers xmlns:trgr="http://marklogic.com/xdmp/triggers">
<trgr:trigger>
<trgr:name>CreateTrigger</trgr:name>
<trgr:description>Trigger to enrich XML ingested via MLCP</trgr:description>
<trgr:data-event>
<trgr:directory-scope>
<trgr:uri>/myDir/</trgr:uri>
<trgr:depth>1</trgr:depth>
</trgr:directory-scope>
<trgr:document-content>
<trgr:update-kind>modify</trgr:update-kind>
</trgr:document-content>
<trgr:when>pre-commit</trgr:when>
</trgr:data-event>
<trgr:module>
<trgr:database name="roxy-modules"/>
<trgr:root>/modules/</trgr:root>
<trgr:path>log-create.xqy</trgr:path>
</trgr:module>
<trgr:enabled>true</trgr:enabled>
<trgr:permissions>
<sec:permission>
<sec:capability>read</sec:capability>
<sec:role-name>reader</sec:role-name>
</sec:permission>,
<sec:permission>
<sec:capability>update</sec:capability>
<sec:role-name>writer</sec:role-name>
</sec:permission>
</trgr:permissions>
<trgr:recursive>false</trgr:recursive>
<trgr:task-priority>normal</trgr:task-priority>
</trgr:trigger>
</triggers>

0 comments on commit 65eac6e

Please sign in to comment.