Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace MyApps.Migrations
- {
- public class _001_Initial : Migration
- {
- public override void Up()
- {
- //Execute your upgrade query here
- }
- public override void Down()
- {
- //Execute your downgrade query here
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using SubSonic;
- using SubSonic.Migrations;
- namespace MyApps.Migrations
- {
- internal static class MigrationHelper
- {
- const string NameSpace = "MyApps.Migrations";
- private const string SCHEMA_INFO = "SubSonicSchemaInfo";
- public static int CurrentVersion { get { return currentVersion; } }
- public static int AppVersion { get { return latestVersion; } }
- public static bool IsUpdateAvailable { get { return (updateVersion.Count > 0); } }
- public static bool IsAppVersionOlder { get; private set; }
- public static bool Checked { get; internal set; }
- private static int currentVersion;
- private static int latestVersion;
- private static List<string> updateVersion;
- private static List<string> availableVersion;
- static MigrationHelper()
- {
- Checked = false;
- }
- /// <summary>
- /// Migrates the specified migration directory.
- /// </summary>
- public static void CheckForMigration()
- {
- currentVersion = Migrator.GetCurrentVersion("YourProviderName");
- Type[] allTypes =
- System.Reflection.Assembly.GetExecutingAssembly().GetTypes();
- availableVersion = new List<string>();
- foreach (Type type in allTypes)
- {
- if (type.Namespace == NameSpace)
- if (type.Name.Substring(0, 1) == "_")
- availableVersion.Add(type.Name);
- }
- availableVersion.Sort();
- updateVersion = new List<string>();
- foreach (string s in availableVersion)
- {
- int version = 0;
- if (int.TryParse(s.Substring(1,3), out version))
- {
- if (version > currentVersion)
- {
- updateVersion.Add(s);
- }
- latestVersion = version;
- }
- }
- IsAppVersionOlder = (latestVersion < currentVersion);
- //log.WriteLine(string.Format(
- ///"CheckForMigration: DbVer = {0}, AppVer = {1}, UpdateAvailable = {2}, IsAppOlder = {3}",
- //currentVersion, latestVersion, updateVersion.Count, IsAppVersionOlder));
- Checked = true;
- }
- internal static void Migrate()
- {
- foreach (string s in updateVersion)
- {
- Migration _migration = (Migration)Activator.CreateInstance(
- System.Reflection.Assembly.GetExecutingAssembly().GetType(
- "MyApps.Migrations." + s));
- _migration.Migrate("YourProviderName", Migration.MigrationDirection.Up);
- IncrementVersion();
- }
- }
- private static void IncrementVersion()
- {
- new Update(SCHEMA_INFO,
- "YourProviderName").SetExpression("version").EqualTo("version+1").Execute();
- }
- }
- }
Add Comment
Please, Sign In to add comment