49 lines
1.9 KiB
C#
49 lines
1.9 KiB
C#
namespace e_suite.Database.Migrator.Migrations;
|
|
|
|
public class ScriptParser
|
|
{
|
|
public static string GetScriptText(ScriptType scriptType, string databaseName)
|
|
{
|
|
var scriptFileName = Path.Combine(AppContext.BaseDirectory, "Scripts", GetScriptFileName(scriptType));
|
|
|
|
if (!File.Exists(scriptFileName))
|
|
return string.Empty;
|
|
|
|
var fileContents = File.ReadAllText(scriptFileName).Trim();
|
|
|
|
return ParseScript(fileContents, databaseName);
|
|
}
|
|
|
|
private static string ParseScript(string fileContents, string databaseName)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(fileContents))
|
|
return fileContents;
|
|
|
|
var parameterDictionary = new Dictionary<string, string>
|
|
{
|
|
{ "SQL_APPLICATION_USER", Environment.GetEnvironmentVariable("SQL_APPLICATION_USER") ?? $"{databaseName}ApplicationUser" },
|
|
{ "SQL_APPLICATION_PASSWORD", Environment.GetEnvironmentVariable("SQL_APPLICATION_PASSWORD") ?? "Plz change me on live, thank you" },
|
|
{ "SQL_DATABASE", Environment.GetEnvironmentVariable("SQL_DATABASE") ?? databaseName },
|
|
{ "SQL_SERVER", Environment.GetEnvironmentVariable("SQL_SERVER") ?? string.Empty },
|
|
};
|
|
|
|
var parsedContents = fileContents;
|
|
foreach (KeyValuePair<string, string> keyValuePair in parameterDictionary)
|
|
{
|
|
parsedContents = parsedContents.Replace("$(" + keyValuePair.Key + ")", keyValuePair.Value);
|
|
}
|
|
|
|
return parsedContents;
|
|
}
|
|
|
|
private static string GetScriptFileName(ScriptType scriptType)
|
|
{
|
|
return scriptType switch
|
|
{
|
|
ScriptType.resetDatabase => "ResetDatabaseScript.sql",
|
|
ScriptType.preMigration => "PreMigrationScript.sql",
|
|
ScriptType.postMigration => "PostMigrationScript.sql",
|
|
_ => throw new NotImplementedException()
|
|
};
|
|
}
|
|
} |