-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from xinbo/master
sqlalchemy: Support references to tables in other schemas
- Loading branch information
Showing
3 changed files
with
43 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from sqlalchemy.dialects.postgresql.psycopg2 import PGCompiler_psycopg2, expression | ||
|
||
|
||
class CockroachCompiler(PGCompiler_psycopg2): | ||
|
||
# TODO(bdarnell): remove this when cockroachdb/cockroach#17008 is fixed. | ||
def returning_clause(self, stmt, returning_cols): | ||
columns = [ | ||
'.'.join(self._label_select_column(None, c, True, False, {}).split('.')[-2:]) | ||
for c in expression._select_iterables(returning_cols) | ||
] | ||
|
||
return 'RETURNING ' + ', '.join(columns) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from sqlalchemy import Table, MetaData, testing | ||
from sqlalchemy.testing import fixtures | ||
|
||
|
||
class AcrossSchemaTest(fixtures.TestBase): | ||
TEST_DATABASE = 'test_sqlalchemy_across_schema' | ||
|
||
def teardown_method(self, method): | ||
testing.db.execute("DROP TABLE IF EXISTS {}.users".format(self.TEST_DATABASE)) | ||
testing.db.execute("DROP DATABASE IF EXISTS {}".format(self.TEST_DATABASE)) | ||
|
||
def setup_method(self): | ||
testing.db.execute("CREATE DATABASE IF NOT EXISTS {}".format(self.TEST_DATABASE)) | ||
testing.db.execute("CREATE TABLE IF NOT EXISTS {}.users (name STRING PRIMARY KEY)".format(self.TEST_DATABASE)) | ||
self.meta = MetaData(testing.db, schema=self.TEST_DATABASE) | ||
|
||
def test_get_columns_indexes_across_schema(self): | ||
# get_columns and get_indexes use default db uri schema. | ||
# across schema table must use schema.table | ||
Table('users', self.meta, autoload=True, schema=self.TEST_DATABASE) | ||
|
||
def test_returning_clause(self): | ||
# TODO(bdarnell): remove this when cockroachdb/cockroach#17008 is fixed. | ||
# across schema returning is schema.table.id but cockroachdb not support. | ||
table = Table('users', self.meta, autoload=True, schema=self.TEST_DATABASE) | ||
table.insert().values(dict(name='John')).returning(table.c.name).execute() |