Skip to content

Add PostgreSql alter sequence and drop sequence #4920

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ json_expression ::= {column_name} ( jsona_binary_operator | jsonb_binary_operato
jsona_binary_operator ::= '->' | '->>' | '#>'
jsonb_binary_operator ::= '@>' | '<@' | '?|' | '?&' | '?' | '#-'

extension_stmt ::= create_sequence_stmt | copy_stdin | truncate_stmt | set_stmt {
extension_stmt ::= create_sequence_stmt | copy_stdin | truncate_stmt | set_stmt | drop_sequence_stmt | alter_sequence_stmt {
extends = "com.alecstrong.sql.psi.core.psi.impl.SqlExtensionStmtImpl"
implements = "com.alecstrong.sql.psi.core.psi.SqlExtensionStmt"
override = true
Expand All @@ -391,12 +391,23 @@ create_sequence_stmt ::= CREATE [ (TEMPORARY | TEMP) | 'UNLOGGED' ] 'SEQUENCE' [
mixin = "app.cash.sqldelight.dialects.postgresql.grammar.mixins.CreateSequenceMixin"
}

alter_sequence_stmt ::= ALTER 'SEQUENCE' [ IF EXISTS ] sequence_name
( 'OWNER' TO ( id | 'CURRENT_USER' | 'SESSION_USER' )
| RENAME TO sequence_name
| SET 'SCHEMA' id
| ( [ AS sequence_data_type ] (sequence_parameters) * ) )
{
mixin = "app.cash.sqldelight.dialects.postgresql.grammar.mixins.AlterSequenceMixin"
}

drop_sequence_stmt ::= DROP 'SEQUENCE' [ IF EXISTS ] sequence_name ( COMMA sequence_name ) * [ 'CASCADE' | 'RESTRICT' ]

sequence_parameters ::= 'INCREMENT' [ BY ] {signed_number}
| 'MINVALUE' {signed_number} | NO 'MINVALUE' | 'MAXVALUE' {signed_number} | NO 'MAXVALUE'
| 'START' [ WITH ] {signed_number} | 'CACHE' {signed_number} | [ NO ] 'CYCLE'
| 'OWNED' BY ( {table_name} DOT {column_name} ) | 'NONE' {
pin=2
}
}

copy_option ::= copy_option_format | copy_option_freeze | copy_option_delimiter | copy_option_null | copy_option_header | copy_option_quote | copy_option_escape | copy_option_force_not_null | copy_option_force_null | copy_option_encoding
copy_option_format ::= 'FORMAT' ('TEXT' | 'CSV' | 'BINARY')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package app.cash.sqldelight.dialects.postgresql.grammar.mixins

import app.cash.sqldelight.dialects.postgresql.grammar.psi.PostgreSqlAlterSequenceStmt
import com.alecstrong.sql.psi.core.psi.QueryElement
import com.alecstrong.sql.psi.core.psi.SqlCompositeElementImpl
import com.intellij.lang.ASTNode
import com.intellij.psi.PsiElement

internal abstract class AlterSequenceMixin(node: ASTNode) :
SqlCompositeElementImpl(node),
PostgreSqlAlterSequenceStmt {
// Query any OWNED BY tableName element to allow the columnName to be resolved
override fun queryAvailable(child: PsiElement): Collection<QueryElement.QueryResult> {
return tablesAvailable(child).map { it.query }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
CREATE TABLE abc (
id INTEGER PRIMARY KEY
);

CREATE SEQUENCE integers_01;

ALTER SEQUENCE integers_01 AS INTEGER
INCREMENT 10
MINVALUE 100
MAXVALUE 250000
START 101
CACHE 1
NO CYCLE;

ALTER SEQUENCE IF EXISTS integers_01
INCREMENT BY 2
NO MINVALUE
NO MAXVALUE
START WITH 3
CYCLE;

ALTER SEQUENCE integers_01 START 31
OWNED BY abc.id;

ALTER SEQUENCE IF EXISTS integers_01 RENAME TO integers_02;

ALTER SEQUENCE integers_01 OWNER TO other_role;

ALTER SEQUENCE integers_01 SET SCHEMA other_schema;

ALTER SEQUENCE IF EXISTS integers_01 SET SCHEMA CURRENT_USER;

Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ ALTER TABLE t1
NO CYCLE
);

ALTER TABLE t1 ALTER COLUMN c1_id
ALTER TABLE t1 ALTER COLUMN c1
SET GENERATED BY DEFAULT
SET INCREMENT 1
SET MINVALUE 1
Expand All @@ -71,6 +71,6 @@ SET CACHE 111
SET NO CYCLE
RESTART WITH 1;

ALTER TABLE t1 ALTER COLUMN c1_id
ALTER TABLE t1 ALTER COLUMN c1
SET GENERATED BY DEFAULT
RESTART WITH 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CREATE TABLE abc (
id INTEGER PRIMARY KEY
);

CREATE SEQUENCE integers_01;

CREATE SEQUENCE integers_02;

DROP SEQUENCE integers_01;

DROP SEQUENCE integers_01, integers_02 RESTRICT;

DROP SEQUENCE IF EXISTS integers_01 CASCADE;