"""Provide the 'autogenerate' feature which can produce migration operations
automatically."""
-try:
- import builtins
-except ImportError:
- import __builtin__ as builtins
import logging
import re
-from . import util
from sqlalchemy.engine.reflection import Inspector
from sqlalchemy.util import OrderedSet
from sqlalchemy import schema as sa_schema, types as sqltypes
+from . import util
+from .compat import string_types
+
log = logging.getLogger(__name__)
###################################################
}
def _render_server_default(default, autogen_context):
- string_type = getattr(builtins, 'basestring', str)
rendered = _user_defined_render("server_default", default, autogen_context)
if rendered is not False:
return rendered
if isinstance(default, sa_schema.DefaultClause):
- if isinstance(default.arg, string_type):
+ if isinstance(default.arg, string_types):
default = default.arg
else:
default = str(default.arg.compile(
dialect=autogen_context['dialect']))
- if isinstance(default, string_type):
+ if isinstance(default, string_types):
# TODO: this is just a hack to get
# tests to pass until we figure out
# WTF sqlite is doing
--- /dev/null
+import sys
+
+
+py3k = sys.version_info >= (3, 0)
+py3kwarning = getattr(sys, 'py3kwarning', False) or py3k
+py26 = sys.version_info >= (2, 6)
+jython = sys.platform.startswith('java')
+win32 = sys.platform.startswith('win')
+pypy = hasattr(sys, 'pypy_version_info')
+
+if py3k:
+ import builtins as compat_builtins
+ string_types = str,
+ binary_type = bytes
+ text_type = str
+else:
+ import __builtin__ as compat_builtins
+ string_types = basestring,
+ binary_type = str
+ text_type = unicode
+
+
+################################################
+# cross-compatible metaclass implementation
+# Copyright (c) 2010-2012 Benjamin Peterson
+def with_metaclass(meta, base=object):
+ """Create a base class with a metaclass."""
+ return meta("%sBase" % meta.__name__, (base,), {})
+################################################
from sqlalchemy import schema, text
from sqlalchemy import types as sqltypes
+from ..compat import string_types, text_type, with_metaclass
from .. import util
from . import base
_impls = {}
-class DefaultImpl(ImplMeta('_ImplBase', (object,), {})):
+class DefaultImpl(with_metaclass(ImplMeta)):
"""Provide the entrypoint for major migration operations,
including database-specific behavioral variances.
return _impls[dialect.name]
def static_output(self, text):
- text_ = getattr(builtins, 'unicode', str)(text + '\n\n')
+ text_ = text_type(text + '\n\n')
self.output_buffer.write(text_)
if callable(getattr(self.output_buffer, 'flush', None)):
self.output_buffer.flush()
def _exec(self, construct, execution_options=None,
multiparams=(),
params=util.immutabledict()):
- if isinstance(construct, getattr(builtins, 'basestring', str)):
+ if isinstance(construct, string_types):
construct = text(construct)
if self.as_sql:
if multiparams or params:
# TODO: coverage
raise Exception("Execution arguments not allowed with as_sql")
- unicode = getattr(builtins, 'unicode', str)
- self.static_output(unicode(
+ self.static_output(text_type(
construct.compile(dialect=self.dialect)
).replace("\t", " ").strip() + self.command_terminator)
else:
-try:
- import builtins
-except ImportError:
- import __builtin__ as builtins
-
from sqlalchemy.ext.compiler import compiles
from sqlalchemy import types as sqltypes
from sqlalchemy import schema
+from ..compat import string_types
+from .. import util
from .impl import DefaultImpl
from .base import ColumnNullable, ColumnName, ColumnDefault, \
ColumnType, AlterColumn
from .base import alter_table
-from .. import util
class MySQLImpl(DefaultImpl):
__dialect__ = 'mysql'
)
def _render_value(compiler, expr):
- if isinstance(expr, getattr(builtins, 'basestring', str)):
+ if isinstance(expr, string_types):
return "'%s'" % expr
else:
return compiler.sql_compiler.process(expr)
-try:
- import builtins
-except ImportError:
- import __builtin__ as builtins
from contextlib import contextmanager
from sqlalchemy.types import NULLTYPE, Integer
from sqlalchemy import schema as sa_schema
from . import util
+from .compat import string_types
from .ddl import impl
__all__ = ('Operations',)
ForeignKey.
"""
- if isinstance(fk._colspec, getattr(builtins, 'basestring', str)):
+ if isinstance(fk._colspec, string_types):
table_key, cname = fk._colspec.rsplit('.', 1)
sname, tname = self._parse_table_key(table_key)
if table_key not in metadata.tables:
import alembic
from alembic import util
+from alembic.compat import string_types, text_type
from alembic.migration import MigrationContext
from alembic.environment import EnvironmentContext
from alembic.operations import Operations
def assert_compiled(element, assert_string, dialect=None):
dialect = _get_dialect(dialect)
- unicode = getattr(builtins, 'unicode', str)
eq_(
- unicode(element.compile(dialect=dialect)).\
+ text_type(element.compile(dialect=dialect)).\
replace("\n", "").replace("\t", ""),
assert_string.replace("\n", "").replace("\t", "")
)
assert False, "Callable did not raise an exception"
except except_cls as e:
assert re.search(msg, str(e)), "%r !~ %s" % (msg, e)
- print(str(e))
+ print(text_type(e))
def op_fixture(dialect='default', as_sql=False):
impl = _impls[dialect]
# as tests get more involved
self.connection = None
def _exec(self, construct, *args, **kw):
- basestring = getattr(builtins, 'basestring', str)
- unicode = getattr(builtins, 'unicode', str)
- if isinstance(construct, basestring):
+ if isinstance(construct, string_types):
construct = text(construct)
- sql = unicode(construct.compile(dialect=self.dialect))
+ sql = text_type(construct.compile(dialect=self.dialect))
sql = re.sub(r'[\n\t]', '', sql)
self.assertion.append(
sql
-
import re
import sys
from unittest import TestCase