-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Support for Create Table As #12860
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
base: main
Are you sure you want to change the base?
Support for Create Table As #12860
Conversation
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.
OK, this is sqla-tester setting up my work on behalf of zzzeek to try to get revision a18f053 of this pull request into gerrit so we can run tests and reviews and stuff
New Gerrit review created for change a18f053: https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/6115 |
I'm very swamped in PRs, sorry this is moving slowly |
are you able to run |
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.
looks good so far. lots of tests. I want to use fixtures as much as we can for these tests to limit the verbosity. We have a decorator @testing.fixture()
that is basically a synonym for pytest fixtures: https://docs.pytest.org/en/6.2.x/fixture.html
a lot of the code in this sqlite suite might be pretty old but let's try to use the more recent patterns for these new tests. (at some point I have to break test_sqlite into a package also).
temporary: bool = False, | ||
if_not_exists: bool = False, | ||
) -> "CreateTableAs": | ||
from .ddl import CreateTableAs |
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.
if this ddl import can't be at the top., we have a system used for in-method imports that's threadsafe
@util.preload_module("sqlalchemy.ddl")
def my_method(self, ...):
ddl = util.preloaded.ddl
return ddl.CreateTableAs(...)
test/dialect/test_sqlite.py
Outdated
self.src_def = "CREATE TABLE src (id INTEGER PRIMARY KEY, name TEXT)" | ||
self.seed_def = "INSERT INTO src (name) VALUES ('a'), ('b')" | ||
|
||
def teardown_test(self): |
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.
note in the exec_sql()
function at the top of the file it has a comment "convert all tests to not use this" :) so for these raw SQL fixtures, I'd do it like this:
def teardown_test(self):
with testing.db.begin() as conn:
conn.exec_driver_sql(...)
conn.exec_driver_sql(...)
conn.exec_driver_sql(...)
...
but also I think these drops can all be turned into fixtures, see my other comments
test/dialect/test_sqlite.py
Outdated
__only_on__ = "sqlite" | ||
__backend__ = True | ||
|
||
@staticmethod |
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.
use a fixture for this:
@testing.fixture
def source_table(self):
return table("src", column("id"), column("name"))
then in a test
def test_thing(self, source_table, connection):
stmt = select(source_table).where(....)
test/dialect/test_sqlite.py
Outdated
return table("src", column("id"), column("name")) | ||
|
||
@staticmethod | ||
def _two_sources(): |
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.
use a fixture here also...
test/dialect/test_sqlite.py
Outdated
|
||
def setup_test(self): | ||
# define a physical src table and seed a couple rows | ||
self.src_def = "CREATE TABLE src (id INTEGER PRIMARY KEY, name TEXT)" |
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.
these can be fixtures
@testing.fixture
def src_def(self, connection):
connection.exec_driver_sql("CREATE TABLE ....")
yield
connection.exec_driver_sql("DROP TABLE ....")
@testing.fixture
def seeded_src_def(self, connection, src_def):
connection.exec_driver_sql("INSERT INTO src (name) VALUES ('a'), ('b')")
then in a test
def test_a_thing(self, seeded_src_def):
# the table is there, and aill also drop when finished
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.
Thank you for the feedback. I updated the SQLite and default dialect tests to use fixtures. A yield fixture also works nicely for cleaning up of tables created via CreateTableAs within the SQLite backend tests.
add CreateTableAs for default dialect and SQLite. add Select.into constructor for CreateTableAs.
a18f053
to
aa14126
Compare
This PR adds support for the Create Table AS construct, along with a new .into() constructor on Select.
Description
Introduces
CreateTableAs
element which can then be used to renderCREATE TABLE ... AS ...
statements.A CreateTableAs can either be constructed directly or from a Select / Selectable by using the new .into constructor method added to the
Select
class.Compilation with the default dialect does not render any flags like TEMPORARY or IF NOT EXISTS. Support for these can be added in the dialect specific compilers. Compilation of CreateTableAs for SQLite supports TEMPORARY and IF NOT EXISTS.
Fixes issue : #4950
Checklist
This pull request is:
must include a complete example of the issue. one line code fixes without an
issue and demonstration will not be accepted.
Fixes: #<issue number>
in the commit messageinclude a complete example of how the feature would look.
Fixes: #<issue number>
in the commit message