1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | |
---|
4 | from migrate.versioning.base import log,operations |
---|
5 | from migrate.versioning import pathed,exceptions |
---|
6 | |
---|
7 | |
---|
8 | class BaseScript(pathed.Pathed): |
---|
9 | """Base class for other types of scripts |
---|
10 | All scripts have the following properties: |
---|
11 | |
---|
12 | source (script.source()) |
---|
13 | The source code of the script |
---|
14 | version (script.version()) |
---|
15 | The version number of the script |
---|
16 | operations (script.operations()) |
---|
17 | The operations defined by the script: upgrade(), downgrade() or both. |
---|
18 | Returns a tuple of operations. |
---|
19 | Can also check for an operation with ex. script.operation(Script.ops.up) |
---|
20 | """ |
---|
21 | |
---|
22 | def __init__(self,path): |
---|
23 | log.info('Loading script %s...' % path) |
---|
24 | self.verify(path) |
---|
25 | super(BaseScript, self).__init__(path) |
---|
26 | log.info('Script %s loaded successfully' % path) |
---|
27 | |
---|
28 | @classmethod |
---|
29 | def verify(cls,path): |
---|
30 | """Ensure this is a valid script, or raise InvalidScriptError |
---|
31 | This version simply ensures the script file's existence |
---|
32 | """ |
---|
33 | try: |
---|
34 | cls.require_found(path) |
---|
35 | except: |
---|
36 | raise exceptions.InvalidScriptError(path) |
---|
37 | |
---|
38 | def source(self): |
---|
39 | fd = open(self.path) |
---|
40 | ret = fd.read() |
---|
41 | fd.close() |
---|
42 | return ret |
---|
43 | |
---|
44 | def run(self, engine): |
---|
45 | raise NotImplementedError() |
---|