Exemplo n.º 1
0
        static int Main(string[] args)
        {
            if (args == null || args.Length == 0)
            {
                return(ExitCode.InvalidArguments);
            }

            try
            {
                if (args[0] == CommandType.AddMigration)
                {
                    var commandParams = AddMigrationParams.FromArgumentsArray(args);
                    return(new AddMigrationExecutor().Run(commandParams));
                }
                else if (args[0] == CommandType.ScriptMigration)
                {
                    var commandParams = ScriptMigrationParams.FromArgumentsArray(args);
                    return(new ScriptMigrationExecutor().Run(commandParams));
                }
                else if (args[0] == CommandType.FindDbContextSubtypes)
                {
                    var commandParams = FindDbContextSubtypeParams.FromArgumentsArray(args);
                    var types         = TypeFinder.GetDbContextTypeFullNamesFromAssebly(commandParams.AssemblyFileName);
                    return(writeResult(types));
                }
                else if (args[0] == CommandType.FindMigrationSubtypes)
                {
                    var commandParams = FindMigrationSubtypeParams.FromArgumentsArray(args);
                    var types         = TypeFinder.GetMigrationByDbContextFromAssebly(commandParams.AssemblyFileName);
                    return(writeResult(types));
                }
            }
            catch (InvalidOperationException)
            {
                return(ExitCode.CanNotFindDbContext);
            }
            catch (FileNotFoundException e)
            {
                LogException(e);
                return(ExitCode.CanNotFindFile);
            }
            catch (ArgumentException e)
            {
                LogException(e);
                return(ExitCode.InvalidArguments);
            }
            catch (Exception e)
            {
                LogException(e);
                return(ExitCode.Exception);
            }

            return(ExitCode.InvalidCommand);
        }
Exemplo n.º 2
0
        public int Run(ScriptMigrationParams migrationParams)
        {
            if (!File.Exists(migrationParams.AssemblyFileName))
            {
                return(ExitCode.CanNotFindFile);
            }

            var context = DbContextFactory.CreateContextInstance(migrationParams.AssemblyFileName, migrationParams.DbContextFullName);

            var serviceCollection = new ServiceCollection();

            serviceCollection
            .AddEntityFrameworkDesignTimeServices()
            .AddDbContextDesignTimeServices(context);

            var designTimeServices = new SqlServerDesignTimeServices();

            designTimeServices.ConfigureDesignTimeServices(serviceCollection);

            var serviceProvider = serviceCollection.BuildServiceProvider();
            var migrator        = serviceProvider.GetService <IMigrator>();

            if (!Directory.Exists(migrationParams.OutputPath))
            {
                Directory.CreateDirectory(migrationParams.OutputPath);
            }

            foreach (var item in migrationParams.Migrations)
            {
                var migrationNames = item.Split(ScriptMigrationParams.MigrationNamesSplitter);
                var migrationSql   = migrator.GenerateScript(
                    fromMigration: migrationNames[0],
                    toMigration: migrationNames[1],
                    idempotent: true);

                File.WriteAllText(Path.Combine(migrationParams.OutputPath, $"{migrationNames[1]}.sql"), migrationSql);
            }

            return(ExitCode.Success);
        }
Exemplo n.º 3
0
        public int ScriptMigration(ScriptMigrationParams commandParams)
        {
            var processRunner = new ProcessRunner();

            return(processRunner.Execute(commandParams));
        }