Default.aspx.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Configuration;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Reflection;
  9. using System.Web;
  10. using System.Web.SessionState;
  11. using System.Web.UI;
  12. using System.Web.UI.WebControls;
  13. using System.Web.UI.HtmlControls;
  14. using Migrator.Framework;
  15. namespace Migrator.Web
  16. {
  17. /// <summary>
  18. /// Web form that can be used to run migrations in a web project.
  19. /// It's recommended that you have some security in place.
  20. /// </summary>
  21. public class Default : Page
  22. {
  23. protected Label _LatestVersion;
  24. protected DropDownList _availableVersions;
  25. protected Button _runMigration;
  26. protected void PageInit(object sender, EventArgs e)
  27. {
  28. }
  29. protected void PageExit(object sender, EventArgs e)
  30. {
  31. }
  32. private void Page_Load(object sender, EventArgs e)
  33. {
  34. if(!IsPostBack)
  35. {
  36. this.BindForm();
  37. }
  38. }
  39. private void RunMigration(object sender, EventArgs e)
  40. {
  41. Migrator mig = GetMigrator();
  42. mig.MigrateTo(int.Parse(this._availableVersions.SelectedValue));
  43. this.BindForm();
  44. }
  45. protected override void OnInit(EventArgs e)
  46. {
  47. InitializeComponent();
  48. base.OnInit(e);
  49. }
  50. private void InitializeComponent()
  51. {
  52. this.Load += new System.EventHandler(Page_Load);
  53. this.Init += new System.EventHandler(PageInit);
  54. this.Unload += new System.EventHandler(PageExit);
  55. this._runMigration.Click += new EventHandler(RunMigration);
  56. }
  57. private void BindForm(){
  58. Migrator mig = GetMigrator();
  59. List<long> appliedMigrations = mig.AppliedMigrations;
  60. long latestMigration = 0;
  61. if(appliedMigrations.Count > 0) {
  62. latestMigration = appliedMigrations[appliedMigrations.Count - 1];
  63. }
  64. this._LatestVersion.Text = latestMigration.ToString();
  65. List<MigrationInfo> availableMigrations = GetMigrationsList(mig);
  66. this._availableVersions.DataSource = availableMigrations;
  67. this._availableVersions.DataValueField = "ID";
  68. this._availableVersions.DataTextField = "ClassName";
  69. this._availableVersions.DataBind();
  70. }
  71. private Migrator GetMigrator()
  72. {
  73. Assembly asm = Assembly.LoadFrom(ConfigurationManager.AppSettings["MigrationAsembly"]);
  74. string provider = ConfigurationManager.AppSettings["MigrationProvider"];
  75. string connectString = ConfigurationManager.AppSettings["ConnectionString"];
  76. Migrator migrator = new Migrator(provider, connectString, asm, false);
  77. return migrator;
  78. }
  79. private List<MigrationInfo> GetMigrationsList(Migrator mig)
  80. {
  81. List<System.Type> migrations = mig.MigrationsTypes;
  82. migrations.Reverse();
  83. List<MigrationInfo> list = new List<MigrationInfo>();
  84. List<System.Type>.Enumerator en = migrations.GetEnumerator();
  85. while(en.MoveNext()){
  86. MigrationInfo info = new MigrationInfo(en.Current);
  87. list.Add(info);
  88. }
  89. return list;
  90. }
  91. public class MigrationInfo
  92. {
  93. private Type _type;
  94. public MigrationInfo(Type type)
  95. {
  96. this._type = type;
  97. }
  98. public Type MigrationType
  99. {
  100. get{ return _type; }
  101. }
  102. public long ID
  103. {
  104. get{ return MigrationLoader.GetMigrationVersion(_type); }
  105. }
  106. public string ClassName
  107. {
  108. get{ return _type.ToString() + " (" + ID + ")"; }
  109. }
  110. }
  111. }
  112. }