Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

lib.Migrations.asp 8.5KB

il y a 8 mois
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <%
  2. 'Represents a single migration (either up or down)
  3. Class Migration_Class
  4. Private m_name
  5. Private m_migration_instance
  6. Private m_sql_array
  7. Private m_sql_array_size
  8. Private m_connection
  9. Private m_has_errors
  10. Public Tracing 'bool
  11. Public Property Get Name
  12. Name = m_name
  13. End Property
  14. Public Property Get Migration
  15. set Migration = m_migration_instance
  16. End Property
  17. Public Property Set Migration(obj)
  18. set m_migration_instance = obj
  19. End Property
  20. Public Property Get HasErrors
  21. HasErrors = m_has_errors
  22. End Property
  23. Private Sub Class_Initialize
  24. m_sql_array = array()
  25. redim m_sql_array(-1)
  26. m_has_errors = false
  27. End Sub
  28. Public Sub Initialize(name, migration_instance, connection)
  29. m_name = name
  30. set m_migration_instance = migration_instance
  31. set m_migration_instance.Migration = Me 'how circular can we get? ...
  32. set m_connection = connection
  33. End Sub
  34. Private Sub Class_Terminate
  35. 'm_connection.Close
  36. 'set m_connection = Nothing
  37. End Sub
  38. Public Sub [Do](sql)
  39. dim new_size : new_size = ubound(m_sql_array) + 1
  40. redim preserve m_sql_array(new_size)
  41. m_sql_array(new_size) = sql
  42. 'put "Added command: " & sql
  43. End Sub
  44. Public Sub Irreversible
  45. Err.Raise 1, "Migration_Class:Irreversible", "Migration cannot proceed because this migration is irreversible."
  46. End Sub
  47. Public Sub DownDataWarning
  48. put "Migration can be downversioned but data changes cannot take place due to the nature of the Up migration in this set."
  49. End Sub
  50. Public Function Query(sql)
  51. put "Query: " & sql
  52. set Query = m_connection.Execute(sql)
  53. End Function
  54. Public Sub DoUp
  55. Migration.Up
  56. ShowCommands
  57. ExecuteCommands
  58. End Sub
  59. Public Sub DoDown
  60. Migration.Down
  61. ShowCommands
  62. ExecuteCommands
  63. End Sub
  64. Private Sub ShowCommands
  65. put ""
  66. put "Commands:"
  67. dim i : i = 0
  68. For i = 0 to ubound(m_sql_array)
  69. put "&nbsp;&nbsp;&nbsp;&nbsp;Command: " & m_sql_array(i)
  70. Next
  71. put ""
  72. End Sub
  73. Private Sub ExecuteCommands
  74. dim i : i = 0
  75. dim sql
  76. m_connection.BeginTrans 'wrap entire process in transaction, rollback If error encountered on any statement
  77. For i = 0 to ubound(m_sql_array)
  78. sql = m_sql_array(i)
  79. If not m_has_errors then 'avoid further processing If errors exist
  80. On Error Resume Next
  81. put "Executing: " & sql
  82. m_connection.Execute sql
  83. If Err.Number <> 0 then 'something went wrong, rollback the transaction and display an error
  84. m_has_errors = true
  85. m_connection.RollbackTrans
  86. put_error "Error during migration: " & Err.Description
  87. put_error "SQL: " & sql
  88. exit sub
  89. End If
  90. On Error Goto 0
  91. End If
  92. Next
  93. m_connection.CommitTrans 'surprisingly no errors were encountered, so commit the entire transaction
  94. End Sub
  95. 'force font color dIfference
  96. Private Sub put(s)
  97. If Me.Tracing then response.write "<div style='color: #999'>" & s & "</div>"
  98. End Sub
  99. End Class
  100. '---------------------------------------------------------------------------------------------------------------------
  101. 'Represents the collection of migrations to be performed
  102. Class Migrations_Class
  103. Private m_migrations_array ' 1-based to match migration naming scheme, ignore the first element
  104. Private m_migrations_array_size
  105. Private m_version
  106. Private m_connection
  107. Private m_connection_string
  108. Private m_has_errors
  109. Public Tracing 'bool
  110. Private Sub Class_Initialize
  111. m_migrations_array = array()
  112. m_migrations_array_size = 0 ' 1-based, ignore the first element
  113. redim m_migrations_array(m_migrations_array_size)
  114. m_has_errors = false
  115. End Sub
  116. Private Sub Class_Terminate
  117. On Error Resume Next
  118. m_connection.Close
  119. set m_connection = Nothing
  120. On Error Goto 0
  121. End Sub
  122. 'force font color dIfference
  123. Private Sub put(s)
  124. If Me.Tracing then response.write "<div style='color: #999'>" & s & "</div>"
  125. End Sub
  126. Public Sub Initialize(connection_string)
  127. m_connection_string = connection_string
  128. set m_connection = Server.CreateObject("ADODB.Connection")
  129. m_connection.Open m_connection_string
  130. put "Initialized: " & typename(m_connection)
  131. End Sub
  132. Public Sub Add(name)
  133. m_migrations_array_size = m_migrations_array_size + 1
  134. redim preserve m_migrations_array(m_migrations_array_size)
  135. dim M : set M = new Migration_Class
  136. dim migration_instance : set migration_instance = eval("new " & name)
  137. M.Initialize name, migration_instance, m_connection
  138. M.Tracing = Me.Tracing
  139. set m_migrations_array(m_migrations_array_size) = M
  140. End Sub
  141. Public Sub MigrateUp
  142. MigrateUpTo m_migrations_array_size
  143. End Sub
  144. Public Sub MigrateUpBy(num)
  145. MigrateUpTo Version + num
  146. End Sub
  147. Public Sub MigrateUpTo(requested_version)
  148. requested_version = CInt(requested_version)
  149. put "Migrating Up To Version " & requested_version
  150. dim M, class_name
  151. If Version >= requested_version then
  152. put_error "DB already at higher version than requested up migration."
  153. ElseIf requested_version > m_migrations_array_size then
  154. put_error "Requested version exceeds available migrations. Only " & m_migrations_array_size & " migrations are available."
  155. Else
  156. While (NextVersion <= requested_version) and (not m_has_errors)
  157. set M = m_migrations_array(NextVersion)
  158. put ""
  159. put "<b>Up: " & M.name & "</b>"
  160. M.DoUp
  161. m_has_errors = M.HasErrors
  162. If not m_has_errors then IncrementVersion
  163. Wend
  164. End If
  165. End Sub
  166. Public Sub MigrateDown
  167. MigrateDownTo 0
  168. End Sub
  169. Public Sub MigrateDownBy(num)
  170. MigrateDownTo Version - num
  171. End Sub
  172. Public Sub MigrateDownTo(requested_version)
  173. requested_version = CInt(requested_version)
  174. put "Migrating Down To Version: " & requested_version
  175. dim M, class_name
  176. If requested_version < 0 then
  177. put_error "Cannot migrate down to a version less than 0."
  178. ElseIf requested_version > Version then
  179. put_error "Cannot migrate down to a version higher than the current version."
  180. ElseIf requested_version = Version then
  181. put_error "Cannot migrate down to the current version, already there."
  182. Else
  183. While (Version > requested_version) and (not m_has_errors)
  184. set M = m_migrations_array(Version)
  185. put ""
  186. put "<b>Down: " & M.Name & "</b>"
  187. M.DoDown
  188. m_has_errors = M.HasErrors
  189. If not m_has_errors then DecrementVersion
  190. Wend
  191. End If
  192. End Sub
  193. Public Property Get Version
  194. If IsEmpty(m_version) then
  195. m_version = GetDBVersion()
  196. End If
  197. Version = m_version
  198. End Property
  199. Private Property Let Version(val)
  200. m_version = val
  201. m_connection.Execute "update meta_migrations set version = " & m_version
  202. End Property
  203. Public Property Get NextVersion
  204. NextVersion = Version + 1
  205. End Property
  206. Private Function GetDBVersion()
  207. dim rs : set rs = m_connection.Execute("select version from meta_migrations")
  208. If rs.BOF or rs.EOF then
  209. GetDBVersion = NULL
  210. Else
  211. GetDBVersion = rs("version")
  212. End If
  213. rs.Close
  214. set rs = Nothing
  215. End Function
  216. Private Sub IncrementVersion
  217. If not m_has_errors then Version = Version + 1
  218. End Sub
  219. Private Sub DecrementVersion
  220. If not m_has_errors then Version = Version - 1
  221. End Sub
  222. End Class
  223. dim Migrations_Class__Singleton
  224. Function Migrations()
  225. If IsEmpty(Migrations_Class__Singleton) then set Migrations_Class__Singleton = new Migrations_Class
  226. set Migrations = Migrations_Class__Singleton
  227. End Function
  228. %>

Powered by TurnKey Linux.