comparison rhodecode/lib/dbmigrate/migrate/changeset/databases/visitor.py @ 833:9753e0907827 beta

added dbmigrate package, added model changes moved out upgrade db command to that package
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 11 Dec 2010 01:54:12 +0100
parents
children 08d2dcd71666
comparison
equal deleted inserted replaced
832:634596f81cfd 833:9753e0907827
1 """
2 Module for visitor class mapping.
3 """
4 import sqlalchemy as sa
5
6 from migrate.changeset import ansisql
7 from migrate.changeset.databases import (sqlite,
8 postgres,
9 mysql,
10 oracle,
11 firebird)
12
13
14 # Map SA dialects to the corresponding Migrate extensions
15 DIALECTS = {
16 "default": ansisql.ANSIDialect,
17 "sqlite": sqlite.SQLiteDialect,
18 "postgres": postgres.PGDialect,
19 "postgresql": postgres.PGDialect,
20 "mysql": mysql.MySQLDialect,
21 "oracle": oracle.OracleDialect,
22 "firebird": firebird.FBDialect,
23 }
24
25
26 def get_engine_visitor(engine, name):
27 """
28 Get the visitor implementation for the given database engine.
29
30 :param engine: SQLAlchemy Engine
31 :param name: Name of the visitor
32 :type name: string
33 :type engine: Engine
34 :returns: visitor
35 """
36 # TODO: link to supported visitors
37 return get_dialect_visitor(engine.dialect, name)
38
39
40 def get_dialect_visitor(sa_dialect, name):
41 """
42 Get the visitor implementation for the given dialect.
43
44 Finds the visitor implementation based on the dialect class and
45 returns and instance initialized with the given name.
46
47 Binds dialect specific preparer to visitor.
48 """
49
50 # map sa dialect to migrate dialect and return visitor
51 sa_dialect_name = getattr(sa_dialect, 'name', 'default')
52 migrate_dialect_cls = DIALECTS[sa_dialect_name]
53 visitor = getattr(migrate_dialect_cls, name)
54
55 # bind preparer
56 visitor.preparer = sa_dialect.preparer(sa_dialect)
57
58 return visitor
59
60 def run_single_visitor(engine, visitorcallable, element,
61 connection=None, **kwargs):
62 """Taken from :meth:`sqlalchemy.engine.base.Engine._run_single_visitor`
63 with support for migrate visitors.
64 """
65 if connection is None:
66 conn = engine.contextual_connect(close_with_result=False)
67 else:
68 conn = connection
69 visitor = visitorcallable(engine.dialect, conn)
70 try:
71 if hasattr(element, '__migrate_visit_name__'):
72 fn = getattr(visitor, 'visit_' + element.__migrate_visit_name__)
73 else:
74 fn = getattr(visitor, 'visit_' + element.__visit_name__)
75 fn(element, **kwargs)
76 finally:
77 if connection is None:
78 conn.close()