-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[YSQL]: #2280 Update catalog manager data while renaming YSQL table, …
…DB and columns. Summary: Update catalog manager maps and data when table, database and columns are renamed. Test Plan: run ``` create database foo; \c test1 create table foo(a int); alter table foo rename to bar; \c yugabyte alter database test1 rename to test2; ``` see the UI updated ``` ctest -R pg_libpq-test ctest -R master-test ``` Reviewers: mihnea, neha Reviewed By: neha Subscribers: yql, bogdan Differential Revision: https://phabricator.dev.yugabyte.com/D7210
- Loading branch information
Zhongwei Zhao
committed
Nov 20, 2019
1 parent
0be6e2a
commit 6a8f17c
Showing
35 changed files
with
649 additions
and
14 deletions.
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
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
88 changes: 88 additions & 0 deletions
88
src/postgres/src/test/regress/expected/yb_feature_alter_rename.out
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,88 @@ | ||
-- | ||
-- ALTER RENAME | ||
-- rename column, table, database | ||
-- | ||
create database test_rename; | ||
create database test_rename1; | ||
\c test_rename | ||
create table foo(a int primary key, b int); | ||
insert into foo (a, b) values (1, 2); | ||
\d | ||
List of relations | ||
Schema | Name | Type | Owner | ||
--------+------+-------+---------- | ||
public | foo | table | yugabyte | ||
(1 row) | ||
|
||
\d foo | ||
Table "public.foo" | ||
Column | Type | Collation | Nullable | Default | ||
--------+---------+-----------+----------+--------- | ||
a | integer | | not null | | ||
b | integer | | | | ||
Indexes: | ||
"foo_pkey" PRIMARY KEY, lsm (a HASH) | ||
|
||
alter table foo rename column b to c; | ||
select a, b from foo; -- fail | ||
ERROR: column "b" does not exist | ||
LINE 1: select a, b from foo; | ||
^ | ||
select a, c from foo; | ||
a | c | ||
---+--- | ||
1 | 2 | ||
(1 row) | ||
|
||
insert into foo (a, b) values (2, 3); -- fail | ||
ERROR: column "b" of relation "foo" does not exist | ||
LINE 1: insert into foo (a, b) values (2, 3); | ||
^ | ||
insert into foo (a, c) values (3, 4); | ||
\d foo | ||
Table "public.foo" | ||
Column | Type | Collation | Nullable | Default | ||
--------+---------+-----------+----------+--------- | ||
a | integer | | not null | | ||
c | integer | | | | ||
Indexes: | ||
"foo_pkey" PRIMARY KEY, lsm (a HASH) | ||
|
||
alter table foo rename to bar; | ||
select * from foo; -- fail | ||
ERROR: relation "foo" does not exist | ||
LINE 1: select * from foo; | ||
^ | ||
select * from bar; | ||
a | c | ||
---+--- | ||
1 | 2 | ||
3 | 4 | ||
(2 rows) | ||
|
||
\d | ||
List of relations | ||
Schema | Name | Type | Owner | ||
--------+------+-------+---------- | ||
public | bar | table | yugabyte | ||
(1 row) | ||
|
||
\c test_rename1; | ||
alter database test_rename rename to test_rename2; | ||
alter database test_rename2 rename to postgres; -- fail | ||
ERROR: database "postgres" already exists | ||
\l | ||
List of databases | ||
Name | Owner | Encoding | Collate | Ctype | Access privileges | ||
-----------------+----------+----------+---------+-------------+----------------------- | ||
postgres | postgres | UTF8 | C | en_US.UTF-8 | | ||
system_platform | postgres | UTF8 | C | en_US.UTF-8 | | ||
template0 | postgres | UTF8 | C | en_US.UTF-8 | =c/postgres + | ||
| | | | | postgres=CTc/postgres | ||
template1 | postgres | UTF8 | C | en_US.UTF-8 | =c/postgres + | ||
| | | | | postgres=CTc/postgres | ||
test_rename1 | yugabyte | UTF8 | C | en_US.UTF-8 | | ||
test_rename2 | yugabyte | UTF8 | C | en_US.UTF-8 | | ||
yugabyte | postgres | UTF8 | C | en_US.UTF-8 | | ||
(7 rows) | ||
|
31 changes: 31 additions & 0 deletions
31
src/postgres/src/test/regress/sql/yb_feature_alter_rename.sql
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,31 @@ | ||
|
||
-- | ||
-- ALTER RENAME | ||
-- rename column, table, database | ||
-- | ||
|
||
create database test_rename; | ||
create database test_rename1; | ||
\c test_rename | ||
|
||
create table foo(a int primary key, b int); | ||
insert into foo (a, b) values (1, 2); | ||
\d | ||
\d foo | ||
|
||
alter table foo rename column b to c; | ||
select a, b from foo; -- fail | ||
select a, c from foo; | ||
insert into foo (a, b) values (2, 3); -- fail | ||
insert into foo (a, c) values (3, 4); | ||
\d foo | ||
|
||
alter table foo rename to bar; | ||
select * from foo; -- fail | ||
select * from bar; | ||
\d | ||
|
||
\c test_rename1; | ||
alter database test_rename rename to test_rename2; | ||
alter database test_rename2 rename to postgres; -- fail | ||
\l |
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
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
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,60 @@ | ||
// Copyright (c) YugaByte, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
// in compliance with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software distributed under the License | ||
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
// or implied. See the License for the specific language governing permissions and limitations | ||
// under the License. | ||
// | ||
|
||
#include "yb/client/namespace_alterer.h" | ||
|
||
#include "yb/client/client-internal.h" | ||
|
||
namespace yb { | ||
namespace client { | ||
|
||
YBNamespaceAlterer::YBNamespaceAlterer( | ||
YBClient* client, const std::string& namespace_name, const std::string& namespace_id) | ||
: client_(client), namespace_name_(namespace_name), namespace_id_(namespace_id) {} | ||
|
||
YBNamespaceAlterer::~YBNamespaceAlterer() {} | ||
|
||
Status YBNamespaceAlterer::Alter() { | ||
master::AlterNamespaceRequestPB req; | ||
RETURN_NOT_OK(ToRequest(&req)); | ||
return client_->data_->AlterNamespace( | ||
client_, req, CoarseMonoClock::Now() + client_->default_admin_operation_timeout()); | ||
} | ||
|
||
YBNamespaceAlterer* YBNamespaceAlterer::RenameTo(const std::string& new_name) { | ||
rename_to_ = new_name; | ||
return this; | ||
} | ||
|
||
YBNamespaceAlterer* YBNamespaceAlterer::SetDatabaseType(YQLDatabase type) { | ||
database_type_ = type; | ||
return this; | ||
} | ||
|
||
Status YBNamespaceAlterer::ToRequest(master::AlterNamespaceRequestPB* req) { | ||
req->mutable_namespace_()->set_name(namespace_name_); | ||
if (!namespace_id_.empty()) { | ||
req->mutable_namespace_()->set_id(namespace_id_); | ||
} | ||
if (database_type_) { | ||
req->mutable_namespace_()->set_database_type(*database_type_); | ||
} | ||
if (!rename_to_) { | ||
return STATUS(InvalidArgument, "Cannot alter namespace without specifying new name"); | ||
} | ||
req->set_new_name(*rename_to_); | ||
return Status::OK(); | ||
} | ||
|
||
} // namespace client | ||
} // namespace yb |
Oops, something went wrong.