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

Add options for timeout, schema restriction and dbo-prefixing #193

Open
wants to merge 5 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
10 changes: 10 additions & 0 deletions console/BaseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ protected BaseCommand(string command, string oneLineDescription) {
"f|databaseFilesPath=",
"Path to database data and log files.",
o => DatabaseFilesPath = o);
HasOption(
"timeout=",
"Number of seconds for sql command timeout.",
o => Timeout = o);
HasOption(
"noprefixdbo",
"Do not prefix objects in the dbo schema with dbo in their filename (for backwards compatibility).",
o => NoPrefixDbo = o != null);
}

protected string Server { get; set; }
Expand All @@ -40,5 +48,7 @@ protected BaseCommand(string command, string oneLineDescription) {
protected bool Overwrite { get; set; }
protected bool Verbose { get; set; }
protected string DatabaseFilesPath { get; set; }
protected string Timeout { get; set; }
protected bool NoPrefixDbo { get; set; }
}
}
27 changes: 22 additions & 5 deletions console/Script.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public Script()
"A comma separated list of the types that will only be scripted. Valid types: " +
Database.ValidTypes,
o => OnlyTypes = o);
HasOption(
"schemas=",
"A comma separated list of schemas that only the objects within these will be scripted. ",
o => Schemas = o);
}

private Logger _logger;
Expand All @@ -48,6 +52,7 @@ public Script()
protected string DataTablesPattern { get; set; }
protected string DataTablesExcludePattern { get; set; }
protected string TableHint { get; set; }
protected string Schemas { get; set; }

public override int Run(string[] args) {
_logger = new Logger(Verbose);
Expand All @@ -59,6 +64,10 @@ public override int Run(string[] args) {
Overwrite = true;
}

int commandTimeout;
if (!int.TryParse(Timeout, out commandTimeout))
commandTimeout = 30; // Default for SQLCommand

var scriptCommand = new ScriptCommand {
ConnectionString = ConnectionString,
DbName = DbName,
Expand All @@ -67,16 +76,20 @@ public override int Run(string[] args) {
Server = Server,
User = User,
Logger = _logger,
Overwrite = Overwrite
Overwrite = Overwrite,
Timeout = commandTimeout,
PrefixDbo = !NoPrefixDbo
};

var filteredTypes = HandleFilteredTypes();
var namesAndSchemas = HandleDataTables(DataTables);
var dataTableNames = HandleDataTables(DataTables);
var schemas = HandleSchemas(Schemas);

try {
scriptCommand.Execute(namesAndSchemas, DataTablesPattern, DataTablesExcludePattern,
TableHint, filteredTypes);
} catch (Exception ex) {
scriptCommand.Execute(dataTableNames, DataTablesPattern, DataTablesExcludePattern,
TableHint, filteredTypes, schemas);
}
catch (Exception ex) {
throw new ConsoleHelpAsException(ex.Message);
}

Expand All @@ -98,6 +111,10 @@ private List<string> HandleFilteredTypes() {
return Database.Dirs.Except(keepTypes.Except(removeTypes)).ToList();
}

private List<string> HandleSchemas(string schemasString) {
return schemasString?.Split(',').ToList() ?? new List<string>();
}

private Dictionary<string, string> HandleDataTables(string tableNames) {
var dataTables = new Dictionary<string, string>();

Expand Down
9 changes: 6 additions & 3 deletions model/Command/BaseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ public abstract class BaseCommand {
public string ScriptDir { get; set; }
public ILogger Logger { get; set; }
public bool Overwrite { get; set; }
public int Timeout { get; set; }
public bool PrefixDbo { get; set; }

public Database CreateDatabase(IList<string> filteredTypes = null) {
public Database CreateDatabase(IList<string> filteredTypes = null, IList<string> schemas = null) {
filteredTypes = filteredTypes ?? new List<string>();
schemas = schemas ?? new List<string>();

if (!string.IsNullOrEmpty(ConnectionString)) {
if (!string.IsNullOrEmpty(Server) ||
Expand All @@ -26,7 +29,7 @@ public Database CreateDatabase(IList<string> filteredTypes = null) {
"You must not provide both a connection string and a server/db/user/password");
}

return new Database(filteredTypes) {
return new Database(filteredTypes, schemas, PrefixDbo) {
Connection = ConnectionString,
Dir = ScriptDir
};
Expand All @@ -50,7 +53,7 @@ public Database CreateDatabase(IList<string> filteredTypes = null) {
builder.Password = Pass;
}

return new Database(filteredTypes) {
return new Database(filteredTypes, schemas, PrefixDbo) {
Connection = builder.ToString(),
Dir = ScriptDir
};
Expand Down
4 changes: 2 additions & 2 deletions model/Command/CompareCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public class CompareCommand : BaseCommand {
public string OutDiff { get; set; }

public bool Execute() {
var sourceDb = new Database();
var targetDb = new Database();
var sourceDb = new Database(prefix_dbo: PrefixDbo);
var targetDb = new Database(prefix_dbo: PrefixDbo);
sourceDb.Connection = Source;
targetDb.Connection = Target;
sourceDb.Load();
Expand Down
10 changes: 5 additions & 5 deletions model/Command/ScriptCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@

namespace SchemaZen.Library.Command {
public class ScriptCommand : BaseCommand {
public void Execute(Dictionary<string, string> namesAndSchemas, string dataTablesPattern,
public void Execute(Dictionary<string, string> dataTableNames, string dataTablesPattern,
string dataTablesExcludePattern,
string tableHint, List<string> filteredTypes) {
string tableHint, List<string> filteredTypes, List<string> schemas) {
if (!Overwrite && Directory.Exists(ScriptDir)) {
var message = $"{ScriptDir} already exists - you must set overwrite to true";
throw new InvalidOperationException(message);
}

var db = CreateDatabase(filteredTypes);
var db = CreateDatabase(filteredTypes, schemas);

Logger.Log(TraceLevel.Verbose, "Loading database schema...");
db.Load();
db.Load(Timeout);
Logger.Log(TraceLevel.Verbose, "Database schema loaded.");

foreach (var nameAndSchema in namesAndSchemas) {
foreach (var nameAndSchema in dataTableNames) {
AddDataTable(db, nameAndSchema.Key, nameAndSchema.Value);
}

Expand Down
Loading