Exemplo n.º 1
0
        public int Run(DbOptions options)
        {
            var dbInfo = options.DatabaseInfo;
            IDatabaseProvider databaseProvider = _dbProviderFactory.Create(dbInfo.DbProvider);

            string dbName = $@"{options.DatabaseInfo.DataSource}\{options.DatabaseInfo.InitialCatalog}";

            if (options.Command.HasFlag(DbOptionCommands.Drop))
            {
                if (UserConfirmed(options))
                {
                    (databaseProvider as IDatabaseDDLProvider).DropDatabase(dbInfo);
                    LogInfo($"Database {dbName} dropped");
                }
                else
                {
                    return(1);
                }
            }

            if (options.Command.HasFlag(DbOptionCommands.Create))
            {
                (databaseProvider as IDatabaseDDLProvider).CreateDatabase(dbInfo);
                LogInfo($"Database {dbName} created");
            }

            if (options.Command.HasFlag(DbOptionCommands.Upgrade))
            {
                if (!Upgrade(options))
                {
                    return(1);
                }
                LogInfo($"Database {dbName} upgraded from {options.UpgradeScriptsPath}");
            }

            if (options.Command.HasFlag(DbOptionCommands.ExportSchema))
            {
                var persistenceMetaContextFactory = DbToolDependencyResolver.Current.Resolve <IPersistenceMetaContextFactory>();
                var persistenceMetContxt          = persistenceMetaContextFactory.CreateMeta();

                string connString = databaseProvider.BuildConnectionString(dbInfo);
                persistenceMetContxt.ExportSchemaScript(connString, options.ExportScriptFileName);

                LogInfo("Database schema script exported successfully");
                Log.Info($"Database {dbName} schema script exported at {options.ExportScriptFileName}");
            }

            return(0);
        }