Database script refactoring