Beispiel #1
0
        private void EnsureDbAndMigrationTableExists(string projectdllPath, Type repoType, string configFilePath)
        {
            var repoBase         = TypeHandler.CreateRepoBase(projectdllPath, repoType); //this might blow up.. might need "args"
            var connectionString = ConnectionStringHandler.FindConnectionString(repoBase, configFilePath);

            CreateRepoDatabase(projectdllPath, repoType, configFilePath);
            //create migration log table, if it doesn't exist.
            var updateMigration = CreateSchemaUpdater(projectdllPath, typeof(MigrationRepo), configFilePath, connectionString);

            updateMigration.Execute(true, true);
        }
Beispiel #2
0
        /// <summary>
        /// Creates an instance of the NHibernate SchemaUpdate class using the repository found.
        /// </summary>
        /// <param name="projectdllPath"></param>
        /// <param name="repoType"></param>
        /// <param name="args">optional constructor arguments</param>
        /// <returns></returns>
        private SchemaUpdate CreateSchemaUpdater(string projectdllPath, Type repoType, string configFilePath, params object[] args)
        {
            var        repoBase = TypeHandler.CreateRepoBase(projectdllPath, repoType, args);
            MethodInfo createRepoSetupMethod = repoType.GetMethod("CreateRepoSetup", BindingFlags.Instance | BindingFlags.NonPublic);
            var        connectionString      = ConnectionStringHandler.FindConnectionString(repoBase, configFilePath);

            var repoSetup = createRepoSetupMethod.Invoke(repoBase, new object[] { connectionString });

            var createConfigMethod = repoSetup.GetType().GetMethod("CreateConfiguration", BindingFlags.Instance | BindingFlags.NonPublic);
            var config             = createConfigMethod.Invoke(repoSetup, null) as FluentConfiguration;

            var updater = new SchemaUpdate(config.BuildConfiguration());

            return(updater);
        }
Beispiel #3
0
        /// <summary>
        /// Enables the ability for that project to be able to use NHibernate Repo migrations, automatic or manual.
        /// </summary>
        public void EnableMigrations(EnableMigrationsCriteria criteria)
        {
            MessageFilter.Register();

            var repoInfo = TypeHandler.FindSingleRepo(criteria.ProjectPath, criteria.RepoName);

            //if it is null something is wrong so drop out.
            if (repoInfo == null)
            {
                return;
            }

            AssertRepoHasEmptyConstructor(repoInfo.RepoType);
            EnsureDbAndMigrationTableExists(criteria.ProjectPath, repoInfo.RepoType, criteria.ConfigFilePath);

            var repoBase = TypeHandler.CreateRepoBase(repoInfo.Assembly.Location, repoInfo.RepoType);

            //create migration log table, if it doesn't exist.
            var updater = CreateSchemaUpdater(criteria.ProjectPath, typeof(MigrationRepo), criteria.ConfigFilePath, repoBase.ConnectionStringOrName);

            updater.Execute(true, true);

            bool multipleFound = false;
            var  configType    = TypeHandler.FindSingleConfiguration(criteria.ProjectPath, repoInfo.RepoType, out multipleFound);

            //this should not happen. should only ever have one config per repo type.
            if (multipleFound)
            {
                return;
            }

            if (configType == null)
            {
                LoggerBase.Log("Adding migration configuration");
                var filePath = new ConfigurationFileHandler().CreateConfigurationFile(criteria.ProjectPath, repoInfo.RepoType.Name, "Migrations", MigrationToUse.Manual);
                new ProjectDteHelper().AddFile(criteria.ProjectPath, "Migrations", filePath, showFile: true);
            }
            else
            {
                LoggerBase.Log("System is already configured for migrations, see class: " + configType.Name);
            }

            //clean up
            ProjectEvalutionHelper.FinishedWithProject(criteria.ProjectPath);
            MessageFilter.Revoke();
        }
Beispiel #4
0
        /// <summary>
        /// Ensures that the migration database exists.
        /// </summary>
        /// <param name="projectdllPath"></param>
        /// <param name="repoType"></param>
        /// <param name="configFilePath"></param>
        /// <param name="args"></param>
        private void CreateRepoDatabase(string projectdllPath, Type repoType, string configFilePath, params object[] args)
        {
            var repoBase         = TypeHandler.CreateRepoBase(projectdllPath, repoType, args);
            var connectionString = ConnectionStringHandler.FindConnectionString(repoBase, configFilePath);

            var builder = new System.Data.SqlClient.SqlConnectionStringBuilder
            {
                ConnectionString = connectionString
            };

            string databaseName = builder.InitialCatalog;

            var migrationDbExists = DatabaseChecking.CheckDatabaseExists(connectionString, databaseName);

            if (!migrationDbExists)
            {
                DatabaseCreation.CreateDatabase(connectionString, databaseName);
            }
        }