dbdeployer icon indicating copy to clipboard operation
dbdeployer copied to clipboard

Add custom template and fields

Open datacharmer opened this issue 5 years ago • 1 comments

dbdeployer allows already users to customize existing templates. However, the templates are used with pre-defined script names, and executed at immutable stages. Moreover, the data used to fill the templates is not accessible from outside dbdeployer.

We want to allow users to define their own templates, with data loaded dynamically either from the command line or from a file.

The template should be loaded with the option --custom-template=$template_file_name:$data_file_name

where $template_file_name is a file containing the template texts, same as any template already existingand$data_file_name` is a JSON file with this structure:

{
  "script_name": "my_custom_script",
  "context": "single",
  "execution_time": "after_start",
  "data": [
    { "key": "Name1", "value:" "something"},
    {"key": "Name2", "value": "something else"}
  ]
}
  • script_name is the name of the script to be created in the sandbox.
  • context is where we create the script. It could be either "single" (inside each node sandbox if it's a multiple deployment) or "upper" if it is in a composite sandbox. If omitted, it will be taken as "single".
  • execution time can be one of the following:
    • "before_start" in a single sandbox or in a composite node
    • "after_start"
    • "after_grants"
    • "before_initialization" in a composite sandbox
    • "after_initialization"
  • data is an optional collection of key:value pairs that can be referenced in the template.

The data can also be passed with an option --data="key:value". Data passed on the command line will have precedence over data loaded from a file.

Here's a full example:

file: mytemplate

#!/bin/bash
{{.Copyright}}
# Generated by dbdeployer {{.AppVersion}} using a custom template on {{.DateTime}}
source {{.SandboxDir}}/sb_include

./use_all "reset master"
./use_all "create schema logging"
./use_all "create table logging.{{.TableName}}(id int not null auto_increment primary key, t text)"
./use_all "reset master"

file: data.json

{
  "script_name": "makelog",
  "context": "upper",
  "execution_time": "before_initialization",
  "data": [
    {"key": "TableName", "value": "mylog"}
  ]
}

it will be invoked as

dbdeployer deploy replication 5.7 --custom-template=mytemplate:data.json

or

dbdeployer deploy replication 5.7 --custom-template=mytemplate:data.json \
   --data=TableName:dog_food

cc @morgo

datacharmer avatar Mar 06 '19 07:03 datacharmer

This issue has low priority, given that replication across sandboxes was implemented with a much simpler method. We may still implement this issue, but there is no rush.

datacharmer avatar Mar 29 '19 19:03 datacharmer