class Physique::FluentMigratorTasksBuilder

Constants

GLOBAL_TASKS

Public Instance Methods

build_tasks() click to toggle source
# File lib/physique/task_builders/fluent_migrator.rb, line 50
def build_tasks
  dbs = solution.fluent_migrator_dbs
  return if dbs.blank?

  dbs.each do |db|
    expand_project_config db
    task_namespace = db_task_name(db)

    namespace :db do
      namespace task_namespace do
        # First look at the scripts_dir and add a task for every sql file that you find
        defaults = default_tasks(db.name)
        add_script_tasks db, defaults

        # Then add the default minimum required tasks in case the scripts_dir didn't contain them
        add_default_db_tasks db, defaults

        # Add the migrate and rollback tasks
        add_migrator_tasks db

        # Add the tasks to create the db from scratch
        add_create_tasks

        # Add a task to create a new migration in the db project
        add_new_migration_task db
      end
    end

    # Rebuild the databases when running tests
    task :test => "db:#{task_namespace}:rebuild"
  end

  alias_default_tasks
end

Private Instance Methods

add_create_tasks() click to toggle source
# File lib/physique/task_builders/fluent_migrator.rb, line 174
def add_create_tasks
  # Setup the database from nothing
  desc 'Create the database and run all migrations'
  task :setup => [ :create, :migrate, :seed ]

  # Drop and recreate the database
  desc 'Drop and recreate the database'
  task :rebuild => [ :drop, :setup ]
end
add_default_db_tasks(db, defaults) click to toggle source
# File lib/physique/task_builders/fluent_migrator.rb, line 118
def add_default_db_tasks(db, defaults)
  defaults.each do |task_name,task_details|
    unless Rake::Task.task_defined? "db:#{db_task_name(db)}:#{task_name.to_s}"
      desc task_details[:description]
      sqlcmd task_name do |s|
        s.command = task_details[:command]
        s.server_name = db.instance
        s.set_variable 'DATABASE_NAME', db.name
      end
    end
  end
end
add_migrator_tasks(db) click to toggle source
# File lib/physique/task_builders/fluent_migrator.rb, line 137
def add_migrator_tasks(db)
  require 'physique/tasks/fluent_migrator'

  # Compile just the database project.
  # This task is registered as a dependency of the migration
  # tasks to ensure the latest code is available.
  build :compile_db => [ :restore ] do |b|
    b.target = [ 'Build' ]
    b.file = db.project_file
    b.prop 'Configuration', solution.compile.configuration
    b.logging = solution.compile.logging
  end

  block = lambda &method(:configure_migration)

  # Migrate up
  desc 'Migrate database to the latest version'
  fluent_migrator :migrate => [ :compile_db ], &block.curry.(db, 'migrate:up')

  # Migrate down
  desc 'Rollback the database to the previous version'
  fluent_migrator :rollback => [ :compile_db ], &block.curry.(db, 'rollback')

  # Try the migration
  desc 'Migrate and then immediately rollback'
  task :try => [ :migrate, :rollback ]
end
add_new_migration_task(db) click to toggle source
# File lib/physique/task_builders/fluent_migrator.rb, line 202
def add_new_migration_task(db)
  desc 'Create a new migration file with the specified name'
  task :new_migration, :name, :description do |t, args|
    name, description = args[:name], args[:description]

    unless name
      abort [
        %Q{Usage: rake "#{t.name}[name[,description]]"},
        desc,
      ].join "\n\n"
    end

    # Save the new migration file
    version = migration_version
    migration_file_name = "#{version}_#{name}.cs"
    migration_content = migration_template(version, name, description, db.project_namespace)
    save_file migration_content, "#{db.project_dir}/Migrations/#{migration_file_name}"

    # Add the new migration file to the project
    Albacore::Project.new(db.project_file).tap do |p|
      p.add_compile_node :Migrations, migration_file_name
      p.save
    end
  end
end
add_script_tasks(db, defaults) click to toggle source
# File lib/physique/task_builders/fluent_migrator.rb, line 98
def add_script_tasks(db, defaults)
  # Need to keep the forward slashes for FileList.
  scripts_dir = db.scripts_dir.gsub('\\', '/')
  FileList["#{scripts_dir}/*.sql"].each do |f|
    task_name = File.basename(f, '.*')

    desc get_script_task_description(defaults, task_name, db)
    sqlcmd task_name do |s|
      s.file = f
      s.server_name = db.instance
      s.set_variable 'DATABASE_NAME', db.name
    end
  end
end
alias_default_tasks() click to toggle source
# File lib/physique/task_builders/fluent_migrator.rb, line 261
def alias_default_tasks
  Rake.application.tasks
    .select {|t| should_alias_db_task?(t)}
    .group_by {|t| db_command(t) }
    .each do |command,tasks|
      desc global_task_description(command,tasks)
      task "db:#{command}", tasks[0].arg_names => tasks.map {|t| t.name }
    end
end
configure_migration(db, task, config) click to toggle source
# File lib/physique/task_builders/fluent_migrator.rb, line 165
def configure_migration(db, task, config)
  config.instance = db.instance
  config.database = db.name
  config.task = task
  config.dll = %{"#{db.output_dll}"}
  config.exe = locate_tool(tool_in_output_folder(db) || tool_in_nuget_package)
  config.output_to_file
end
db_command(task) click to toggle source
# File lib/physique/task_builders/fluent_migrator.rb, line 271
def db_command(task)
  task.name.split(':').last.to_sym
end
db_task_name(db) click to toggle source
# File lib/physique/task_builders/fluent_migrator.rb, line 198
def db_task_name(db)
  db.task_alias.downcase
end
default_tasks(database) click to toggle source
# File lib/physique/task_builders/fluent_migrator.rb, line 131
def default_tasks(database)
  { create: { description: 'Create the database', command: "CREATE DATABASE #{database}" },
    drop: { description: 'Drop the database', command: "DROP DATABASE #{database}"},
    seed: { description: 'Seed the database with test data', command: "SELECT 'No seed script found'" } }
end
existing_path(path) click to toggle source
# File lib/physique/task_builders/fluent_migrator.rb, line 192
def existing_path(path)
  path = path.gsub('\\', '/')
  return path if FileList[path].any? { |p| File.exists? p }
  nil
end
expand_project_config(db) click to toggle source
# File lib/physique/task_builders/fluent_migrator.rb, line 87
def expand_project_config(db)
  project = Albacore::Project.new(db.project_file)
  db[:project_namespace] = project.namespace
  db[:project_dir] = project.proj_path_base
  db[:scripts_dir] = "#{db[:project_dir]}\\#{db.scripts_dir}"

  build_conf = solution.compile.configuration
  db[:output_path] = project.output_path build_conf
  db[:output_dll] = File.expand_path("#{db.project_dir}\\#{project.output_dll(build_conf)}")
end
get_script_task_description(defaults, task_name, db) click to toggle source
# File lib/physique/task_builders/fluent_migrator.rb, line 113
def get_script_task_description(defaults, task_name, db)
  default_task = defaults[task_name.to_sym]
  default_task ? default_task[:description] : "Executes #{task_name}.sql on #{db.name} in the #{db.scripts_dir} folder."
end
global_task_description(command, tasks) click to toggle source
# File lib/physique/task_builders/fluent_migrator.rb, line 281
def global_task_description(command, tasks)
  return GLOBAL_TASKS[command] unless only_one_db_configured?

  # Blank out the comment to hide the task in the list by default
  description = tasks[0].comment
  tasks[0].clear_comments
  description
end
migration_template(version, name, description, project_namespace) click to toggle source
# File lib/physique/task_builders/fluent_migrator.rb, line 232
    def migration_template(version, name, description, project_namespace)
      description = ", \"#{description}\"" unless description.nil?
      return <<TEMPLATE
using FluentMigrator;

namespace #{project_namespace}.Migrations
{
    [Migration(#{version}#{description})]
    public class #{name} : Migration
    {
        public override void Up()
        {
            // Add migration code here
        }

        public override void Down()
        {
            // Add migration rollback code here
        }
    }
}
TEMPLATE
    end
migration_version() click to toggle source
# File lib/physique/task_builders/fluent_migrator.rb, line 228
def migration_version
  Time.now.utc.strftime('%Y%m%d%H%M%S')
end
only_one_db_configured?() click to toggle source
# File lib/physique/task_builders/fluent_migrator.rb, line 290
def only_one_db_configured?
  solution.fluent_migrator_dbs.length == 1
end
save_file(content, file_path) click to toggle source
# File lib/physique/task_builders/fluent_migrator.rb, line 256
def save_file(content, file_path)
  raise "#{file_path} already exists, cancelling" if File.exists? file_path
  File.open(file_path, 'w') { |f| f.write(content) }
end
should_alias_db_task?(task) click to toggle source
# File lib/physique/task_builders/fluent_migrator.rb, line 275
def should_alias_db_task?(task)
  task.name.starts_with?('db') &&
    (only_one_db_configured? ||
      GLOBAL_TASKS.has_key?(db_command(task)))
end
tool_in_nuget_package() click to toggle source
# File lib/physique/task_builders/fluent_migrator.rb, line 188
def tool_in_nuget_package
  existing_path "#{solution.nuget.restore_location}/FluentMigrator*/tools/Migrate.exe"
end
tool_in_output_folder(db) click to toggle source
# File lib/physique/task_builders/fluent_migrator.rb, line 184
def tool_in_output_folder(db)
  existing_path "#{db.output_path}/Migrate.exe"
end