Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue115 #142

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions yuniql-cli/CommandLineService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,11 @@ public int RunEraseOption(EraseOption opts)
.Select(t => new KeyValuePair<string, string>(t.Split("=")[0], t.Split("=")[1]))
.ToList();

//run all erase scripts
//run all drop scripts
var migrationService = _migrationServiceFactory.Create(opts.Platform);
migrationService.Initialize(opts.ConnectionString, opts.CommandTimeout);
migrationService.Erase(opts.Path, tokens, opts.CommandTimeout, opts.Environment);

_traceService.Success($"Schema erase completed successfuly on {opts.Path}.");
return 0;
} catch(Exception ex)
Expand Down Expand Up @@ -380,6 +380,51 @@ public int RunPlatformsOption(PlatformsOption opts)
}
}

public int RunDropOption(DropOption opts)
{
try
{
//if no path provided, we default into current directory
if(string.IsNullOrEmpty(opts.Path))
{
var workingPath = _environmentService.GetCurrentDirectory();
opts.Path = workingPath;
}

//if no connection string provided, we default into environment variable or throw exception
if(string.IsNullOrEmpty(opts.ConnectionString))
{
opts.ConnectionString = _environmentService.GetEnvironmentVariable(ENVIRONMENT_VARIABLE.YUNIQL_CONNECTION_STRING);
}

//if no target platform provided, we default into sqlserver
if(string.IsNullOrEmpty(opts.Platform))
{
opts.Platform = _environmentService.GetEnvironmentVariable(ENVIRONMENT_VARIABLE.YUNIQL_TARGET_PLATFORM);
if(string.IsNullOrEmpty(opts.Platform))
{
opts.Platform = SUPPORTED_DATABASES.SQLSERVER;
}
}

//parse tokens
var tokens = opts.Tokens
.Select(t => new KeyValuePair<string, string>(t.Split("=")[0], t.Split("=")[1]))
.ToList();

//run all drop scripts
var migrationService = _migrationServiceFactory.Create(opts.Platform);
migrationService.Initialize(opts.ConnectionString, opts.CommandTimeout);
migrationService.Drop(opts.Path, tokens, opts.CommandTimeout, opts.Environment);

_traceService.Success($"Database drop completed successfuly on {opts.Path}.");
return 0;
} catch(Exception ex)
{
return OnException(ex, "Failed to execute drop function", opts.Debug, _traceService);
}
}

private int OnException(Exception exception, string headerMessage, bool debug, ITraceService traceService)
{
var userMessage = debug ? exception.ToString() : $"{exception.Message} {exception.InnerException?.Message}";
Expand Down
24 changes: 24 additions & 0 deletions yuniql-cli/DropOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using CommandLine;
using System;
using System.Collections.Generic;

namespace Yuniql.CLI
{
//yuniql info
[Verb("drop", HelpText = "Drop database.")]
public class DropOption : BasePlatformOption
{
//yuniql erase -k "Token1=TokenValue1" -k "Token2=TokenValue2" -k "Token3=TokenValue3" | --token "..." --token "..." --token "..."
//yuniql erase -k "Token1=TokenValue1,Token2=TokenValue2,Token3=TokenValue3" | --token "...,...,..."
[Option('k', "token", Required = false, HelpText = "Replace tokens using the passed key-value pairs.", Separator = ',')]
public IEnumerable<string> Tokens { get; set; } = new List<string>();

//yuniql erase --force
[Option('f', "force", Required = true, HelpText = "Force execution of drop commands.")]
public bool Force { get; set; }

//yuniql <command> --environment "DEV" | --environment "PROD"
[Option("environment", Required = false, HelpText = "Environment code for environment-aware scripts.")]
public string Environment { get; set; }
}
}
4 changes: 2 additions & 2 deletions yuniql-cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static int Main(string[] args)
traceService);

var resultCode = Parser.Default
.ParseArguments<InitOption, RunOption, ListOption, NextVersionOption, VerifyOption, EraseOption, BaselineOption, RebaseOption, PlatformsOption>(args)
.ParseArguments<InitOption, RunOption, ListOption, NextVersionOption, VerifyOption, EraseOption, BaselineOption, RebaseOption, PlatformsOption, DropOption>(args)
.MapResult((InitOption opts) => Dispatch(commandLineService.RunInitOption, opts, traceService),
(RunOption opts) => Dispatch(commandLineService.RunMigration, opts, traceService),
(NextVersionOption opts) => Dispatch(commandLineService.IncrementVersion, opts, traceService),
Expand All @@ -34,8 +34,8 @@ public static int Main(string[] args)
(BaselineOption opts) => Dispatch(commandLineService.RunBaselineOption, opts, traceService),
(RebaseOption opts) => Dispatch(commandLineService.RunRebaseOption, opts, traceService),
(ArchiveOption opts) => Dispatch(commandLineService.RunArchiveOption, opts, traceService),
(DropOption opts) => Dispatch(commandLineService.RunDropOption, opts, traceService),
(PlatformsOption opts) => Dispatch(commandLineService.RunPlatformsOption, opts, traceService),

errs => 1);

return resultCode;
Expand Down
2 changes: 1 addition & 1 deletion yuniql-cli/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"Yuniql.CLI": {
"commandName": "Project",
"commandLineArgs": "platforms",
"commandLineArgs": "drop --force",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its better we keep the old values. Its dangeours to default this to drop :)

"environmentVariables": {
"Key": "Value"
},
Expand Down
196 changes: 96 additions & 100 deletions yuniql-core/IMigrationService.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
using Yuniql.Extensibility;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Data;
using Yuniql.Extensibility;

namespace Yuniql.Core
namespace Yuniql.Core
{
/// <summary>
/// Runs migrations by executing alls scripts in the workspace directory.
/// <summary>
/// Runs migrations by executing alls scripts in the workspace directory.
/// </summary>
public interface IMigrationService
public interface IMigrationService
{
/// <summary>
/// Initializes the current instance of <see cref="MigrationService"./>
/// <summary>
/// Initializes the current instance of <see cref="MigrationService"./>
/// </summary>
/// <param name="connectionString">Connection string to target database server or instance.</param>
/// <param name="commandTimeout">Command timeout in seconds.</param>
void Initialize(string connectionString, int? commandTimeout = null);

/// <summary>
/// Returns the current migration version applied in target database.
/// <summary>
/// Returns the current migration version applied in target database.
/// </summary>
string GetCurrentVersion(string schemaName = null, string tableName = null);

/// <summary>
/// Returns all migration versions applied in the target database
/// <summary>
/// Returns all migration versions applied in the target database
/// </summary>
List<DbVersion> GetAllVersions(string schemaName = null, string tableName = null);

/// <summary>
/// Runs migrations by executing alls scripts in the workspace directory.
/// When CSV files are present also run bulk import operations to target database table having same file name.
/// <summary>
/// Runs migrations by executing alls scripts in the workspace directory. When CSV files are present also run bulk
/// import operations to target database table having same file name.
/// </summary>
/// <param name="workingPath">The directory path to migration project.</param>
/// <param name="targetVersion">The maximum version to run to. When NULL, runs migration to the latest version found in the workspace path.</param>
/// <param name="autoCreateDatabase">When TRUE, creates the database in the target host.</param>
/// <param name="tokens">Token kev/value pairs to replace tokens in script files.</param>
/// <param name="verifyOnly">When TRUE, runs the migration in uncommitted mode. No changes are committed to target database. When NULL, runs migration in atomic mode.</param>
/// <param name="verifyOnly">
/// When TRUE, runs the migration in uncommitted mode. No changes are committed to target database. When NULL, runs
/// migration in atomic mode.
/// </param>
/// <param name="bulkSeparator">Bulk file values separator character in the CSV bulk import files. When NULL, uses comma.</param>
/// <param name="metaSchemaName">Schema name for schema versions table. When empty, uses the default schema in the target data platform. </param>
/// <param name="metaSchemaName">Schema name for schema versions table. When empty, uses the default schema in the target data platform.</param>
/// <param name="metaTableName">Table name for schema versions table. When empty, uses __yuniqldbversion.</param>
/// <param name="commandTimeout">Command timeout in seconds. When NULL, it uses default provider command timeout.</param>
/// <param name="bulkBatchSize">Batch rows to processed when performing bulk import. When NULL, it uses default provider batch size.</param>
Expand All @@ -45,94 +48,87 @@ public interface IMigrationService
/// <param name="environmentCode">Environment code for environment-aware scripts.</param>
/// <param name="resumeFromFailure">The resume from failure.</param>
/// <param name="noTransaction">When TRUE, migration will run without using transactions</param>
void Run(
string workingPath,
string targetVersion = null,
bool? autoCreateDatabase = null,
List<KeyValuePair<string, string>> tokens = null,
bool? verifyOnly = null,
string bulkSeparator = null,
string metaSchemaName = null,
string metaTableName = null,
int? commandTimeout = null,
int? bulkBatchSize = null,
string appliedByTool = null,
string appliedByToolVersion = null,
string environmentCode = null,
NonTransactionalResolvingOption? resumeFromFailure = null,
bool noTransaction = false
);
void Run(string workingPath,
string targetVersion = null,
bool? autoCreateDatabase = null,
List<KeyValuePair<string, string>> tokens = null,
bool? verifyOnly = null,
string bulkSeparator = null,
string metaSchemaName = null,
string metaTableName = null,
int? commandTimeout = null,
int? bulkBatchSize = null,
string appliedByTool = null,
string appliedByToolVersion = null,
string environmentCode = null,
NonTransactionalResolvingOption? resumeFromFailure = null,
bool noTransaction = false);

/// <summary>
/// Executes erase scripts presentin _erase directory and subdirectories.
/// <summary>
/// Executes erase scripts presentin _erase directory and subdirectories.
/// </summary>
/// <param name="workingPath">The directory path to migration project.</param>
/// <param name="tokens">Token kev/value pairs to replace tokens in script files.</param>
/// <param name="commandTimeout">Command timeout in seconds.</param>
/// <param name="environmentCode">Environment code for environment-aware scripts.</param>

bool IsTargetDatabaseLatest(string targetVersion, string schemaName = null, string tableName = null);

void RunNonVersionScripts(
IDbConnection connection,
IDbTransaction transaction,
string workingPath,
List<KeyValuePair<string, string>> tokenKeyPairs = null,
string bulkSeparator = null,
int? commandTimeout = null,
string environmentCode = null
);

void RunVersionScripts(
IDbConnection connection,
IDbTransaction transaction,
List<string> dbVersions,
string workingPath,
string targetVersion,
NonTransactionalContext nonTransactionalContext,
List<KeyValuePair<string, string>> tokenKeyPairs = null,
string bulkSeparator = null,
string metaSchemaName = null,
string metaTableName = null,
int? commandTimeout = null,
int? bulkBatchSize = null,
string appliedByTool = null,
string appliedByToolVersion = null,
string environmentCode = null
);

void RunBulkImport(
IDbConnection connection,
IDbTransaction transaction,
string workingPath,
string scriptDirectory,
string bulkSeparator = null,
int? bulkBatchSize = null,
int? commandTimeout = null,
string environmentCode = null
);

void RunSqlScripts(
IDbConnection connection,
IDbTransaction transaction,
NonTransactionalContext nonTransactionalContext,
string version,
string workingPath,
string scriptDirectory,
string metaSchemaName,
string metaTableName,
List<KeyValuePair<string, string>> tokenKeyPairs = null,
int? commandTimeout = null,
string environmentCode = null,
string appliedByTool = null,
string appliedByToolVersion = null
);

void Erase(
string workingPath,
List<KeyValuePair<string, string>> tokens = null,
int? commandTimeout = null,
string environmentCode = null
);
}
bool IsTargetDatabaseLatest(string targetVersion, string schemaName = null, string tableName = null);

void RunNonVersionScripts(IDbConnection connection,
IDbTransaction transaction,
string workingPath,
List<KeyValuePair<string, string>> tokenKeyPairs = null,
string bulkSeparator = null,
int? commandTimeout = null,
string environmentCode = null);

void RunVersionScripts(IDbConnection connection,
IDbTransaction transaction,
List<string> dbVersions,
string workingPath,
string targetVersion,
NonTransactionalContext nonTransactionalContext,
List<KeyValuePair<string, string>> tokenKeyPairs = null,
string bulkSeparator = null,
string metaSchemaName = null,
string metaTableName = null,
int? commandTimeout = null,
int? bulkBatchSize = null,
string appliedByTool = null,
string appliedByToolVersion = null,
string environmentCode = null);

void RunBulkImport(IDbConnection connection,
IDbTransaction transaction,
string workingPath,
string scriptDirectory,
string bulkSeparator = null,
int? bulkBatchSize = null,
int? commandTimeout = null,
string environmentCode = null);

void RunSqlScripts(IDbConnection connection,
IDbTransaction transaction,
NonTransactionalContext nonTransactionalContext,
string version,
string workingPath,
string scriptDirectory,
string metaSchemaName,
string metaTableName,
List<KeyValuePair<string, string>> tokenKeyPairs = null,
int? commandTimeout = null,
string environmentCode = null,
string appliedByTool = null,
string appliedByToolVersion = null);

void Erase(string workingPath,
List<KeyValuePair<string, string>> tokens = null,
int? commandTimeout = null,
string environmentCode = null);

void Drop(string workingPath,
List<KeyValuePair<string, string>> tokens = null,
int? commandTimeout = null,
string environmentCode = null);
}
}
Loading