Skip to content
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

Materialized View Tables Incompatible with Table Command Regex #6

Open
erikreedstrom opened this issue Nov 17, 2015 · 1 comment
Open

Comments

@erikreedstrom
Copy link

The regex used to parse table commands identified below does not currently allow for creating a table based on a query. This prevents the use of tables as materialized views.

Example

create_table :long_query,
             as: "SELECT * FROM orders INNER JOIN line_items ON order_id=orders.id")

generates:

CREATE TABLE "long_query" AS
  SELECT * FROM orders INNER JOIN line_items ON order_id=orders.id

https://github.com/SchemaPlus/schema_plus_core/blob/master/lib/schema_plus/core/sql_struct.rb#L12-L14

@ronen
Copy link
Member

ronen commented Nov 18, 2015

Oh dear yes good point. I don't think it'd be too bad to fix this, but I'm a bit swamped now... any chance you'd be willing to make a PR?

Thinking out loud I think the regex could be enhanced like this:

%r{
          ^
          (?<command>.*\bTABLE\b) \s*
            (?<quote>['"`])(?<name>\S+)\k<quote> \s*
            ( 
                 (
                    \( \s*
                      (?<body>.*) \s*
                    \) \s*
                   (?<options> \S.*)?
                 )
                 |
                 (
                  (?<as> \s+ AS \s+)
                  (?<body>.*) \s*
                 )
            )
            $
}mxi

and parse! could set

self.create_as? = !!m[:as]

and in the assemble method example create_as? to choose the proper reconstruction. The create_as? boolean would need to be added to the doc as part of the API.

Plus would need to set up a test for this case in spec/sql_struct_spec.rb (Of course do that first and see that it fails!)

...any interest in doing this? If not I'll get to it when I get a chance but might not be several weeks.

@ronen ronen mentioned this issue Jun 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants