예제 #1
0
		public int Update(DataBossConfiguration config) {
			Open();
			var pending = GetPendingMigrations(config);
			log.Info("{0} pending migrations found.", pending.Count);

			using(var targetScope = GetTargetScope(config)) {
				var migrator = new DataBossMigrator(info => targetScope);
				return migrator.ApplyRange(pending) ? 0 : -1;
			}
		}
예제 #2
0
		public int Initialize(DataBossConfiguration config) {
			EnsureDataBase(config.GetConnectionString());
			using(var cmd = new SqlCommand(scripter.CreateMissing(typeof(DataBossHistory)), db))
			{
				Open();
				using(var r = cmd.ExecuteReader())
					while(r.Read())
						log.Info("{0}", r.GetValue(0));
			}
			return 0;
		}
예제 #3
0
		public int Status(DataBossConfiguration config) {
			Open();
			var pending = GetPendingMigrations(config);
			if(pending.Count != 0) {
				var message = new StringBuilder();
				message.AppendLine("Pending migrations:");
				foreach(var item in pending)
					message.AppendFormat("  {0} - {1}", item.Info.FullId, item.Info.Name);
				log.Info(message.ToString());
			}
			return pending.Count;
		}
예제 #4
0
		public List<DataBossMigrationInfo> GetAppliedMigrations(DataBossConfiguration config) {
			using(var cmd = new SqlCommand("select object_id('__DataBossHistory', 'U')", db)) {
				if(cmd.ExecuteScalar() is DBNull)
					throw new InvalidOperationException($"DataBoss has not been initialized, run: {ProgramName} init <target>");
				cmd.CommandText = scripter.Select(typeof(DataBossMigrationInfo), typeof(DataBossHistory));
				using(var reader = cmd.ExecuteReader()) {
					return objectReader.Read<DataBossMigrationInfo>(reader).ToList();
				}
			}
		}
예제 #5
0
		List<IDataBossMigration> GetPendingMigrations(DataBossConfiguration config) {
			var applied = new HashSet<string>(GetAppliedMigrations(config).Select(x => x.FullId));
			Func<IDataBossMigration, bool> notApplied = x => !applied.Contains(x.Info.FullId);

			return GetTargetMigration(config.Migrations).Flatten()
				.Where(item => item.HasQueryBatches)
				.Where(notApplied)
				.ToList();
		}
예제 #6
0
		IDataBossMigrationScope GetTargetScope(DataBossConfiguration config) {
			if(string.IsNullOrEmpty(config.Script)) {
				return new DataBossLogMigrationScope(log, new DataBossSqlMigrationScope(db));
			}
			return config.Script == "con:"
				? new DataBossScriptMigrationScope(Console.Out, false) 
				: new DataBossScriptMigrationScope(new StreamWriter(File.Create(config.Script)), true);
		}