|
require 'albacore' |
|
include REXML |
|
include Rake::DSL |
|
|
|
# configuration edit as necessary |
|
SCHEMAEDITOR_PATH = "#{Rake::Win32::normalize(ENV['PROGRAMFILES'])}/Dovetail Software/SchemaEditor/SchemaEditor.exe" |
|
DATABASE_HOST="." |
|
DATABASE = "mobilecl125" |
|
DATABASE_USER = "sa" |
|
DATABASE_PASSWORD = "sa" |
|
DATABASE_TYPE = "mssql" |
|
DATABASE_CONNECTION = "Data Source=#{DATABASE_HOST};User Id=#{DATABASE_USER};Password=#{DATABASE_PASSWORD}" |
|
|
|
task :default => [:apply_schemascripts, :apply_sql] |
|
|
|
|
|
desc "Execute all sql scripts in the database directory" |
|
task :apply_sql do |
|
apply_oracle_sql if DATABASE_TYPE == "oracle" |
|
Rake::Task["apply_sqlserver_sql"].execute() if DATABASE_TYPE == "mssql" |
|
end |
|
|
|
sqlcmd :apply_sqlserver_sql do |cmd| |
|
puts "Execute sql scripts in #{File.absolute_path("database")} directory" |
|
cmd.server = DATABASE_HOST |
|
cmd.database = DATABASE |
|
cmd.username = DATABASE_USER |
|
cmd.password = DATABASE_PASSWORD |
|
|
|
scripts = Array.new |
|
scripts = scripts.concat(FileList["database/*.sql"]) |
|
cmd.scripts = scripts |
|
end |
|
|
|
#desc "Execute all sql scripts in the database directory using SqlPlus" |
|
def apply_oracle_sql |
|
puts "Execute sql scripts in #{File.absolute_path("database")} directory" |
|
|
|
Dir.glob(File.join('database', "*.sql")) do |sql_script| |
|
sqlFile = File.absolute_path(sql_script).gsub('/','\\') |
|
puts "\n\nApplying sql from file #{sqlFile}\n\n" |
|
sh "sqlplus #{DATABASE_USER}/#{DATABASE_PASSWORD}@#{DATABASE_HOST} @#{sqlFile}" |
|
end |
|
end |
|
|
|
desc "Apply all schema scripts in the schema directory" |
|
task :apply_schemascripts do |
|
|
|
puts "Applying scripts from #{File.absolute_path('schema')} to database #{DATABASE}" |
|
seConfig = 'Default.SchemaEditor' |
|
seReport = 'SchemaDifferenceReport.txt' |
|
|
|
puts "Generating Schema Editor configuraiton file" |
|
sh "\"#{SCHEMAEDITOR_PATH}\" -g" |
|
|
|
#SchemaEditor has different (more verbose) database type configuration than Dovetail SDK |
|
databaseType = (DATABASE_TYPE == 'mssql') ? 'MsSqlServer2005' : 'Oracle9' |
|
|
|
Dir.glob(File.join('schema', "*schemascript.xml")) do |schema_script| |
|
puts "\n\nConfiguring Schema Editor for #{schema_script}" |
|
|
|
File.open(seConfig) do |schema_editor_config_file| |
|
doc = Document.new(schema_editor_config_file) |
|
doc.root.elements['database/type'].text = databaseType |
|
doc.root.elements['database/connectionString'].text = DATABASE_CONNECTION |
|
doc.root.elements['inputFilePath'].text = schema_script.gsub('/','\\') |
|
formatter = REXML::Formatters::Default.new |
|
File.open(seConfig, 'w') do |result| |
|
formatter.write(doc, result) |
|
end |
|
end |
|
|
|
puts "\n\nApplying schemascript #{schema_script}" |
|
sh "\"#{SCHEMAEDITOR_PATH}\" -a" |
|
|
|
if File.exists? seReport |
|
sh "type #{seReport}\n\n\n" |
|
File.delete seReport |
|
end |
|
end |
|
|
|
File.delete(seConfig) |
|
end |