Skip to content

Commit

Permalink
SQL support ALTER TABLE RENAME TO (#952)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriehSchneier authored Jan 20, 2025
1 parent faed16f commit bca8a9c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 5 deletions.
Binary file modified internal/bundles/assets/import_sql_cli.arraiz
Binary file not shown.
30 changes: 30 additions & 0 deletions pkg/importer/sql/sql.arrai
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ table_alteration -> ADD_COLUMN = ("ADD" "COLUMN"? column_name attr_type
| ADD_CONSTRAINT = ("ADD" table_constraint)
| DROP_CONSTRAINT = ("DROP" "CONSTRAINT" constraint_name)
| OWNER_TO = ("OWNER" "TO" name)
| RENAME_TO = ("RENAME" "TO" name)
| ADD_ROW_DELETION_POLICY = ("ADD" row_deletion_policy)
| DROP_ROW_DELETION_POLICY = ("DROP" "ROW" "DELETION" "POLICY")
| REPLACE_ROW_DELETION_POLICY = ("REPLACE" row_deletion_policy)
Expand Down Expand Up @@ -429,6 +430,13 @@ let evalTableAlteration = \parsed cond parsed {
alteration: data,
),

(table_alteration: (RENAME_TO: data, ...), ...): (
type: "rename_to",
alteration: (
name: data.name.'' rank (:.@),
),
),

(table_alteration: (drop_index: data, ...), ...): (
type: "drop_index",
alteration: data,
Expand Down Expand Up @@ -665,13 +673,35 @@ let renameCol = \m \t \from \to changeCols(m, t, \cs
cs without old with old +> (@item: old.@item +> (name: to))
);

# renameTo updates the name of table t in m to newName.
let renameTo = \m \t \newName
m +> (
entities: m.entities => \e e +> (
# replace table name
name: cond {e.name = t: newName, _: e.name},
# replace references in interleaved_in
cluster: e.cluster >> \c c +> (
interleaved_in: cond {c.interleaved_in = t: newName, _: c.interleaved_in},
),
# replace references in foreign_keys
foreign_keys: e.foreign_keys => \f f +> (foreign_keys: f.foreign_keys => \ff ff +> (
reference_table: cond {ff.reference_table = t: newName, _: ff.reference_table},
)),
),
# replace indexes
indexes: m.indexes => \i i +> (
table_name: cond {i.table_name = t: newName, _: i.table_name},
),
);

# Applies a single table alteration given model, table name and alteration data.
let stepAlt = \m \t \(:type, alteration:alt)
cond type {
'add_column': addCol(m, t, alt),
'drop_column': dropCol(m, t, alt.name),
'alter_column': alterCol(m, t, alt),
'rename_column': renameCol(m, t, alt.from, alt.to),
'rename_to': renameTo(m, t, alt.name),
# 'on_delete': TODO,
# 'add_constraint': TODO,
# 'drop_constraint': TODO,
Expand Down
2 changes: 2 additions & 0 deletions pkg/importer/sql/tests/spanner/spanner.sql
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,5 @@ CREATE CHANGE STREAM foo FOR bar OPTIONS( retention_period = '14d' );
CREATE VIEW v AS SELECT * from t;
CREATE VIEW vWithSecurityInvoker SQL SECURITY INVOKER AS SELECT * from t;
CREATE VIEW vWithSecurityDefiner SQL SECURITY DEFINER AS SELECT * from t;

ALTER TABLE Account RENAME TO Account1Renamed;
10 changes: 5 additions & 5 deletions pkg/importer/sql/tests/spanner/spanner.sysl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Code generated by Sysl. DO NOT EDIT.

customeraccounts:
!table Account [indexes=[["name:AccountsByNum","key_parts:AccountNum(desc)"],["name:Complex","key_parts:AccountNum,BSB(desc),Balance(asc)"]]]:
!table Account1Renamed [indexes=[["name:AccountsByNum","key_parts:AccountNum(desc)"],["name:Complex","key_parts:AccountNum,BSB(desc),Balance(asc)"]]]:
@primary_key = ["AccountNum"]
AccountNum <: string(23) [~pk]
BSB <: string(6)
Expand All @@ -18,9 +18,9 @@ customeraccounts:
Tag <: int64?
Refresh <: datetime? [allow_commit_timestamp="TRUE"]

!table AccountAddress [interleave_in_parent="Account", interleave_on_delete="cascade"]:
!table AccountAddress [interleave_in_parent="Account1Renamed", interleave_on_delete="cascade"]:
@primary_key = ["AccountNum", "AddressPostCode", "LastUpdated"]
AccountNum <: Account.AccountNum [~pk, ~asc]
AccountNum <: Account1Renamed.AccountNum [~pk, ~asc]
AddressPostCode <: string(10) [~pk, ~desc]
LastUpdated <: datetime? [allow_commit_timestamp="true", ~pk]
AddressLine1 <: bytes? [~max]
Expand All @@ -41,9 +41,9 @@ customeraccounts:
@primary_key = ["AccountNum", "CustomerID"]
CustomerID <: Customer.CustomerID [~pk, ~fk]
Customer <: string(36)
AccountNum <: Account.AccountNum [~pk, ~asc, ~fk]
AccountNum <: Account1Renamed.AccountNum [~pk, ~asc, ~fk]
LegalRole <: string(10)
BranchID <: Account.BSB [~fk]
BranchID <: Account1Renamed.BSB [~fk]
Permissions <: sequence of string(10)?

!table PayID:
Expand Down
26 changes: 26 additions & 0 deletions pkg/importer/sql/tests/spanner_test.arrai
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,32 @@ let foo_ix = applyDropIndex(dropIndex.data, foo_i);

actual = expected,

renamed:
let actual = applyStmt(parseDdl('
CREATE DATABASE test;
CREATE TABLE foo (a INT64) PRIMARY KEY (a);
CREATE TABLE bar (a INT64, t TIMESTAMP OPTIONS (allow_commit_timestamp = true)) PRIMARY KEY (a, t), INTERLEAVE IN PARENT foo;
CREATE TABLE fkey (a INT64, CONSTRAINT FK_A FOREIGN KEY (a) REFERENCES foo (a)) PRIMARY KEY (a ASC);
CREATE INDEX fooIndex ON foo (a DESC);
ALTER TABLE foo RENAME TO baz;
'), empty);

let a = (name: 'a', type: 'int64', length: 0, nullable: true, options: {}, scale: 0);
let t = (name: 't', type: 'datetime', length: 0, nullable: true, options: {'allow_commit_timestamp': 'true'}, scale: 0);
let f = (constraint_name: 'FK_A', foreign_keys: {(attribute: 'a', reference_attribute: 'a', reference_table: 'baz')}, on_delete: {}, on_update: {});
let expected = (
entities: {
|name , attributes, primary_key, foreign_keys, unique_keys, name_prefix, cluster |
('baz', [a] , ['a'] , {} , {} , '' , {} ),
('fkey', [a] , ['a(asc)'] , {f} , {} , '' , {} ),
('bar', [a, t] , ['a', 't'] , {} , {} , '' , [(interleaved_in: 'baz', on_delete: {})]),
},
indexes: {|interleaved_table, key_parts, name, nullfiltered, storing_col, table_name, unique| ({}, ['a(desc)'], 'fooIndex', {}, {}, 'baz', {})},
schema: {|name| ('test')},
);

actual = expected,

defaultNumeric:
let actual = applyStmt(parseDdl(`
CREATE TABLE foo (
Expand Down

0 comments on commit bca8a9c

Please sign in to comment.