I am writing a database migration script in Python in which I am creating a dictionary that can transfer a database between versions. I am currently doing this as follows:
def from1To2():
pass
def from2To1():
pass
migrations = {
1: {'up': from1To2},
2: {'down': from2To1, 'up': from2To3},
3: {'down': from3To2, 'up': from3To4},
4: {'down': from4To3},
}
But every time I create a new migration, I need to write two migration scenarios (up and down) and put them in the migration dictionary. Since the migration functions are really small (usually two lines), I thought about writing them directly in the migration migration dict. In Javascript, it looks something like this:
migrations = {
1: {
'up': function(){ addSomeColumn(); recordChange(); },
'down': function(){ dropSomeColumn(); recordChange(); }
},
2: etc
}
Since migration functions are often two lines, I don't think I can use lambda functions. Does anyone know of any other way to directly write functions in a dict in Python? All tips are welcome!