Module: Rutema::Elements::SQLServer

Defined in:
lib/rutema/elements/win32.rb

Overview

Elements to drive Microsoft’s SQLServer

Instance Method Summary collapse

Instance Method Details

#element_sqlcmd(step) ⇒ Object

Calls sqlcmd.

Requires the script attribute pointing to the SQL script to execute. Path can be relative to the specification file.

Configuration

Requires a configuration.tool entry with :name=>“sqlcmd” and a :configuration entry pointing to a hash containing the configuration parameters. Configuration parameters are: :host - the host to run the command against (sqlcmd -H) :server - the SQLServer (named instance) (sqlcmd -S) :username - the SQLServer user (sqlcmd -U) :password - (sqlcmd -P) :script_root - The path relative to which pathnames for scripts are calculated. If it’s missing paths are relative to the specification file. Optional

Example Configuration Entry

configuration.tool=:name=>“sqlcmd”,:configuration=>{:host=>“guineapig”,:server=>“DB”,:user=>“foo”,:password=>“bar”}

Extras

Not defining any options (e.g. not defining the configuration.tool entry) results in the script running locally without options

The configuration options can be overriden by element attributes (host,server,username etc.). Additionally the following attributes can be defined: database - the database to run the script against ( sqlcmd -d ) level - Sets the errorlevel for the script (sqlcmd -V)

Example Elements

<sqlcmd script=“some.sql”/> <sqlcmd script=“some.sql” host=“localhost”/> - overriding host <sqlcmd script=“some.sql” database=“MyDB” level=“18”/> - hypersensitive error checking and explicitly executed on MyDB

Raises:

  • (Rutema::ParserError)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rutema/elements/win32.rb', line 37

def element_sqlcmd step
  raise Rutema::ParserError,"Missing required script attribute in sqlcmd step" unless step.has_script?
  cfg=@configuration.tools.sqlcmd[:configuration].dup if @configuration.tools.sqlcmd && @configuration.tools.sqlcmd[:configuration]
  cfg||=Hash.new
  #see if there is a script root directory
  if cfg[:script_root]
    raise Rutema::ParserError,"Script root drectory '#{cfg[:script_root]}' does not exist. Error in the configuration for sqlcmd steps" unless File.exists?(cfg[:script_root])
    script_path=File.expand_path(File.join(cfg[:script_root],step.script))
  else
     script_path=File.expand_path(step.script)
  end
  #we want a specific DB script to run
  step.script=script_path
  raise Rutema::ParserError,"Cannot find script file '#{step.script}' for sqlcmd step" unless File.exists?(step.script)
  #check for overrides
  cfg[:host] = step.host if step.has_host?
  cfg[:server] = step.host if step.has_server?
  cfg[:username] = step.host if step.has_username?
  cfg[:password] = step.host if step.has_password?
  #add the optional attributes
  cfg[:database] = step.database if step.has_database?
  cfg[:level] = step.level if step.has_level?
  #add the script
  cfg[:script]=step.script
  #get the command object
  step.cmd=sqlcmd_command(cfg)
  return step
end