Exemplo n.º 1
0
    public InitialMigrationCreator(ICmdHelper cmdHelper, DotnetEfToolManager dotnetEfToolManager)
    {
        CmdHelper           = cmdHelper;
        DotnetEfToolManager = dotnetEfToolManager;

        Logger = NullLogger <InitialMigrationCreator> .Instance;
    }
 public CreateMigrationAndRunMigratorCommand(ICmdHelper cmdHelper, InitialMigrationCreator initialMigrationCreator, DotnetEfToolManager dotnetEfToolManager)
 {
     _initialMigrationCreator = initialMigrationCreator;
     CmdHelper           = cmdHelper;
     DotnetEfToolManager = dotnetEfToolManager;
     Logger = NullLogger <CreateMigrationAndRunMigratorCommand> .Instance;
 }
    public virtual async Task ExecuteAsync(CommandLineArgs commandLineArgs)
    {
        if (commandLineArgs.Target.IsNullOrEmpty())
        {
            throw new CliUsageException("DbMigrations folder path is missing!");
        }

        var dbMigrationsFolder = commandLineArgs.Target;

        var nolayers = commandLineArgs.Options.ContainsKey("nolayers");
        var dbMigratorProjectPath = GetDbMigratorProjectPath(dbMigrationsFolder);

        if (!nolayers && dbMigratorProjectPath == null)
        {
            throw new Exception("DbMigrator is not found!");
        }

        await DotnetEfToolManager.BeSureInstalledAsync();

        var migrationsCreatedSuccessfully = await _initialMigrationCreator.CreateAsync(commandLineArgs.Target, !nolayers);

        if (migrationsCreatedSuccessfully)
        {
            if (nolayers)
            {
                CmdHelper.RunCmd("dotnet run --migrate-database", Path.GetDirectoryName(Path.Combine(dbMigrationsFolder, "MyCompanyName.MyProjectName")));
            }
            else
            {
                CmdHelper.RunCmd("dotnet run", Path.GetDirectoryName(dbMigratorProjectPath));
            }
            await Task.CompletedTask;
        }
        else
        {
            var exceptionMsg = "Migrations failed! A migration command didn't run successfully.";

            Logger.LogError(exceptionMsg);
            throw new Exception(exceptionMsg);
        }
    }
Exemplo n.º 4
0
    public async Task <bool> CreateAsync(string targetProjectFolder, bool layeredTemplate = true)
    {
        if (targetProjectFolder == null || !Directory.Exists(targetProjectFolder))
        {
            Logger.LogError($"This path doesn't exist: {targetProjectFolder}");
            return(false);
        }

        Logger.LogInformation("Creating initial migrations...");

        await DotnetEfToolManager.BeSureInstalledAsync();

        var tenantDbContextName = FindTenantDbContextName(targetProjectFolder);
        var dbContextName       = tenantDbContextName != null?
                                  FindDbContextName(targetProjectFolder)
                                      : null;

        var migrationOutput       = AddMigrationAndGetOutput(targetProjectFolder, dbContextName, "Migrations");
        var tenantMigrationOutput = tenantDbContextName != null?
                                    AddMigrationAndGetOutput(targetProjectFolder, tenantDbContextName, "TenantMigrations")
                                        : null;

        var migrationSuccess = CheckMigrationOutput(migrationOutput) && CheckMigrationOutput(tenantMigrationOutput);

        if (migrationSuccess)
        {
            Logger.LogInformation("Initial migrations are created.");
        }
        else
        {
            Logger.LogError("Creating initial migrations process is failed! Details:" + Environment.NewLine
                            + migrationOutput + Environment.NewLine
                            + tenantMigrationOutput + Environment.NewLine);
        }

        return(migrationSuccess);
    }