views:

15

answers:

0

This is a simple way to do Rails-style migrations in a VBA application. Just add additional migrations like migration(name, sql_string, database) to run_migratons and call run_migrations somewhere in the beginning of your execution.

Function migrate(signature As String, sql As String, dbs As DAO.database)
    Dim rs As DAO.Recordset
    Set rs = dbs.OpenRecordset("select * from versions where migration = '" & signature & "'")
    If rs.EOF Then
       dbs.Execute (sql)
       rs.AddNew
       rs("migration") = signature
       rs.Update
    End If
End Function

Function setup_versions(dbs As DAO.database)
   Dim t As DAO.TableDef

   On Error Resume Next
   Set t = dbs.TableDefs("versions")
   If Err.Number <> 0 Then
       dbs.Execute ("CREATE TABLE versions (migration text)")
   End If
   Err.Clear
End Function

Function run_migrations(dbs As DAO.database)
    setup_versions(dbs)
    migrate("20100315142400_create_table", "CREATE TABLE table_name (field1 type, field 2 type)", dbs)
    'add migrations here'
End Function