code_junkie

O/R mappers that can update the database schema automatically?

Nov 13th, 2011
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. namespace MyApps.Migrations
  2. {
  3. public class _001_Initial : Migration
  4. {
  5. public override void Up()
  6. {
  7. //Execute your upgrade query here
  8. }
  9. public override void Down()
  10. {
  11. //Execute your downgrade query here
  12. }
  13. }
  14. }
  15.  
  16. using System;
  17. using System.Collections.Generic;
  18. using SubSonic;
  19. using SubSonic.Migrations;
  20.  
  21. namespace MyApps.Migrations
  22. {
  23. internal static class MigrationHelper
  24. {
  25. const string NameSpace = "MyApps.Migrations";
  26. private const string SCHEMA_INFO = "SubSonicSchemaInfo";
  27. public static int CurrentVersion { get { return currentVersion; } }
  28. public static int AppVersion { get { return latestVersion; } }
  29. public static bool IsUpdateAvailable { get { return (updateVersion.Count > 0); } }
  30. public static bool IsAppVersionOlder { get; private set; }
  31. public static bool Checked { get; internal set; }
  32. private static int currentVersion;
  33. private static int latestVersion;
  34. private static List<string> updateVersion;
  35. private static List<string> availableVersion;
  36.  
  37. static MigrationHelper()
  38. {
  39. Checked = false;
  40. }
  41.  
  42. /// <summary>
  43. /// Migrates the specified migration directory.
  44. /// </summary>
  45. public static void CheckForMigration()
  46. {
  47. currentVersion = Migrator.GetCurrentVersion("YourProviderName");
  48. Type[] allTypes =
  49. System.Reflection.Assembly.GetExecutingAssembly().GetTypes();
  50. availableVersion = new List<string>();
  51. foreach (Type type in allTypes)
  52. {
  53. if (type.Namespace == NameSpace)
  54. if (type.Name.Substring(0, 1) == "_")
  55. availableVersion.Add(type.Name);
  56. }
  57.  
  58. availableVersion.Sort();
  59. updateVersion = new List<string>();
  60. foreach (string s in availableVersion)
  61. {
  62. int version = 0;
  63. if (int.TryParse(s.Substring(1,3), out version))
  64. {
  65. if (version > currentVersion)
  66. {
  67. updateVersion.Add(s);
  68. }
  69. latestVersion = version;
  70. }
  71. }
  72. IsAppVersionOlder = (latestVersion < currentVersion);
  73. //log.WriteLine(string.Format(
  74. ///"CheckForMigration: DbVer = {0}, AppVer = {1}, UpdateAvailable = {2}, IsAppOlder = {3}",
  75. //currentVersion, latestVersion, updateVersion.Count, IsAppVersionOlder));
  76. Checked = true;
  77. }
  78.  
  79. internal static void Migrate()
  80. {
  81. foreach (string s in updateVersion)
  82. {
  83. Migration _migration = (Migration)Activator.CreateInstance(
  84. System.Reflection.Assembly.GetExecutingAssembly().GetType(
  85. "MyApps.Migrations." + s));
  86. _migration.Migrate("YourProviderName", Migration.MigrationDirection.Up);
  87. IncrementVersion();
  88. }
  89. }
  90.  
  91. private static void IncrementVersion()
  92. {
  93. new Update(SCHEMA_INFO,
  94. "YourProviderName").SetExpression("version").EqualTo("version+1").Execute();
  95. }
  96.  
  97. }
  98. }
Add Comment
Please, Sign In to add comment