Пример #1
0
        public string Generate(IConfiguration configuration, string connectionString, string providerName, IEnumerable <string> tablesToIgnore, IEnumerable <string> indexesToIgnore, IEnumerable <KeyValuePair <string, string> > extraPluralizationWords, IAnswerProvider answerProvider)
        {
            // set up the database connection
            var dialectFactory           = new DialectFactory();
            var dialect                  = dialectFactory.Create(providerName, connectionString);
            var dbProviderFactoryFactory = new DbProviderFactoryFactory();
            var dbProviderFactory        = dbProviderFactoryFactory.Create(providerName, connectionString);

            IEnumerable <IMap> fromMaps;

            if (!dbProviderFactory.DatabaseExists(connectionString, providerName, dialect))
            {
                fromMaps = Enumerable.Empty <IMap>();
                return(this.GenerateScript(fromMaps, configuration.Maps, dialect, new NullStatisticsProvider(), answerProvider, tablesToIgnore, indexesToIgnore));
            }
            else
            {
                // get the schema from the existing database
                var schemaReaderFactory         = new SchemaReaderFactory();
                var schemaReader                = schemaReaderFactory.GetSchemaReader(providerName);
                var connectionStringManipulator = new ConnectionStringManipulator(dbProviderFactory, connectionString);
                using (var connection = dbProviderFactory.CreateConnection()) {
                    connection.ConnectionString = connectionString;
                    connection.Open();
                    var schema = schemaReader.Read(connection, connectionStringManipulator.GetDatabaseName());

                    // reverse engineer the maps
                    var engineer = new Engineer(extraPluralizationWords.Union(configuration.Maps.Select(m => new KeyValuePair <string, string>(m.Type.Name, m.Table)))); // we use our configuration to inform us as to the correct naming of tables
                    fromMaps = engineer.ReverseEngineer(schema, dialect, tablesToIgnore, answerProvider, false);
                    return(this.GenerateScript(fromMaps, configuration.Maps, dialect, new StatisticsProvider(connection, dialect), answerProvider, tablesToIgnore, indexesToIgnore));
                }
            }
        }
Пример #2
0
        public void Execute(IConfiguration configuration, string connectionString, string providerName, IEnumerable <string> tablesToIgnore, IEnumerable <string> indexesToIgnore, IEnumerable <KeyValuePair <string, string> > extraPluralizationWords, IAnswerProvider answerProvider)
        {
            var script = this.scriptGenerator.Generate(configuration, connectionString, providerName, tablesToIgnore, indexesToIgnore, extraPluralizationWords, answerProvider);

            if (string.IsNullOrWhiteSpace(script))
            {
                using (new ColorContext(ConsoleColor.Green)) {
                    Log.Logger.Information("No migration script to run");
                    return;
                }
            }

            // set up the database connection
            var dialectFactory           = new DialectFactory();
            var dialect                  = dialectFactory.Create(providerName, connectionString);
            var dbProviderFactoryFactory = new DbProviderFactoryFactory();
            var dbProviderFactory        = dbProviderFactoryFactory.Create(providerName, connectionString);

            dbProviderFactory.CreateDatabaseIfNotExists(connectionString, providerName, dialect);

            // run the script
            using (var connection = dbProviderFactory.CreateConnection()) {
                connection.ConnectionString = connectionString;
                connection.Open();
                connection.Execute(script);
            }
        }
Пример #3
0
        public void Execute(ISeeder seeder, IConfiguration configuration, string connectionString, string providerName)
        {
            // set up the database connection
            var dialectFactory           = new DialectFactory();
            var dialect                  = dialectFactory.Create(providerName, connectionString);
            var dbProviderFactoryFactory = new DbProviderFactoryFactory();
            var dbProviderFactory        = dbProviderFactoryFactory.Create(providerName, connectionString);

            if (!dbProviderFactory.DatabaseExists(connectionString, providerName, dialect))
            {
                Log.Logger.Error("Database doesn't exist");
                return;
            }

            // run the script
            var sqlSessionCreator = new SqlDatabase(configuration, dbProviderFactory, connectionString, dialect);

            using (var session = sqlSessionCreator.BeginSession()) {
                seeder.Seed(session);
                session.Complete();
            }
        }