-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Issue #1697] add more foreign table models and update setup_foreign_tables.py #1769
Merged
Merged
Changes from 10 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4a23597
[Issue #1697] add more foreign table models and update setup_foreign_…
jamesbursa 43da4b8
Depend on SQLAlchemy to do more of the generating for us
jamesbursa 0c6b2fe
Convert to basic column types: `integer`, `text`, etc.
jamesbursa 76a9a4b
Full set of foreign tables
jamesbursa e194044
Simplify table definitions
jamesbursa 9944d7d
Fix column orders
jamesbursa 5b28b7f
Update schema names
jamesbursa bfd7d7f
Rename Base to ForeignBase
jamesbursa 0465bdc
Use constant instead of hard-code schema name
jamesbursa 8d4a24c
Remove unwanted code from setup_foreign_tables.py
jamesbursa f377748
Make primary keys non-null
jamesbursa 15e92ec
Add type annotations
jamesbursa 8027c22
Exclude dialect.py from type annotations
jamesbursa 3cba153
Update search path for `make login-db`
jamesbursa 7e2efad
Fix test failure in test_via_cli
jamesbursa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ | |
|
||
class Schemas(StrEnum): | ||
API = "api" | ||
LEGACY = "legacy" | ||
STAGING = "staging" |
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
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,9 @@ | ||
# | ||
# SQLAlchemy models for foreign tables. | ||
# | ||
|
||
from . import forecast, foreignbase, opportunity, synopsis | ||
|
||
metadata = foreignbase.metadata | ||
|
||
__all__ = ["metadata", "forecast", "opportunity", "synopsis"] |
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,42 @@ | ||
# | ||
# Support for generating SQL for "CREATE FOREIGN TABLE". | ||
# | ||
|
||
import re | ||
|
||
import sqlalchemy | ||
|
||
|
||
class ForeignTableDDLCompiler(sqlalchemy.sql.compiler.DDLCompiler): | ||
"""SQLAlchemy compiler for creating foreign tables.""" | ||
|
||
def create_table_constraints(self, _table, _include_foreign_key_constraints=None, **kw): | ||
return "" # Don't generate any constraints. | ||
|
||
def visit_create_table(self, create, **kw): | ||
table = create.element | ||
table._prefixes = ("FOREIGN",) # Add "FOREIGN" before "TABLE". | ||
sql = super().visit_create_table(create, **kw) | ||
table._prefixes = () | ||
return sql | ||
|
||
def post_create_table(self, table): | ||
# Add foreign options at the end. | ||
return f" SERVER grants OPTIONS (schema 'EGRANTSADMIN', table '{table.name.upper()}')" | ||
|
||
def visit_create_column(self, create, first_pk=False, **kw): | ||
column = create.element | ||
sql = super().visit_create_column(create, first_pk, **kw) | ||
if sql and column.primary_key: | ||
# Add "OPTIONS ..." to primary key column. | ||
sql = re.sub(r"^(.*?)( NOT NULL)?$", r"\1 OPTIONS (key 'true')\2", sql) | ||
return sql | ||
|
||
|
||
class ForeignTableDialect(sqlalchemy.dialects.postgresql.dialect): | ||
"""SQLAlchemy dialect for creating foreign tables. | ||
|
||
See https://docs.sqlalchemy.org/en/20/dialects/ | ||
""" | ||
|
||
ddl_compiler = ForeignTableDDLCompiler |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing type definitions on a few of these. FIne to just set them to
Any
if they're complex internal types to SQLAlchemy and not just str/bool or similar. A quick check of the base class, it doesn't look to type them inline, so not a lot to go on.