@@ -48,7 +48,7 @@ protected enum Action {
48
48
SET_LOGGED_UNLOGGED , //
49
49
NOT_OF , //
50
50
OWNER_TO , //
51
- REPLICA_IDENTITY
51
+ REPLICA_IDENTITY , ALTER_VIEW_RENAME_COLUMN // RENAME COLUMN old_name TO new_name (for views)
52
52
}
53
53
54
54
public PostgresAlterTableGenerator (PostgresTable randomTable , PostgresGlobalState globalState ,
@@ -98,6 +98,20 @@ public List<Action> getActions(ExpectedErrors errors) {
98
98
// make it more likely that the ALTER TABLE succeeds
99
99
action = Randomly .subset (Randomly .smallNumber (), Action .values ());
100
100
}
101
+
102
+ // If this is a view, only allow view-compatible operations
103
+ if (randomTable .isView ()) {
104
+ // Remove all table-specific operations for views
105
+ action .removeIf (a -> a != Action .ALTER_VIEW_RENAME_COLUMN );
106
+ // If no view operations remain, add the rename column action
107
+ if (action .isEmpty ()) {
108
+ action .add (Action .ALTER_VIEW_RENAME_COLUMN );
109
+ }
110
+ } else {
111
+ // Remove view-specific actions if this is a table
112
+ action .remove (Action .ALTER_VIEW_RENAME_COLUMN );
113
+ }
114
+
101
115
if (randomTable .getColumns ().size () == 1 ) {
102
116
action .remove (Action .ALTER_TABLE_DROP_COLUMN );
103
117
}
@@ -120,14 +134,24 @@ public SQLQueryAdapter generate() {
120
134
int i = 0 ;
121
135
List <Action > action = getActions (errors );
122
136
StringBuilder sb = new StringBuilder ();
123
- sb .append ("ALTER TABLE " );
124
- if (Randomly .getBoolean ()) {
125
- sb .append (" ONLY" );
126
- errors .add ("cannot use ONLY for foreign key on partitioned table" );
137
+
138
+ // Check if we're dealing with a view operation
139
+ boolean isViewOperation = action .contains (Action .ALTER_VIEW_RENAME_COLUMN );
140
+
141
+ if (isViewOperation ) {
142
+ sb .append ("ALTER VIEW " );
143
+ } else {
144
+ sb .append ("ALTER TABLE " );
145
+ if (Randomly .getBoolean ()) {
146
+ sb .append (" ONLY" );
147
+ errors .add ("cannot use ONLY for foreign key on partitioned table" );
148
+ }
127
149
}
150
+
128
151
sb .append (" " );
129
152
sb .append (randomTable .getName ());
130
153
sb .append (" " );
154
+
131
155
for (Action a : action ) {
132
156
if (i ++ != 0 ) {
133
157
sb .append (", " );
@@ -364,6 +388,17 @@ public SQLQueryAdapter generate() {
364
388
errors .add ("cannot use invalid index" );
365
389
}
366
390
break ;
391
+ case ALTER_VIEW_RENAME_COLUMN :
392
+ sb .append ("RENAME COLUMN " );
393
+ PostgresColumn columnToRename = randomTable .getRandomColumn ();
394
+ sb .append (columnToRename .getName ());
395
+ sb .append (" TO " );
396
+ sb .append ("new_" + columnToRename .getName () + "_" + r .getInteger (1 , 1000 ));
397
+ errors .add ("column does not exist" );
398
+ errors .add ("column name already exists" );
399
+ errors .add ("cannot rename column of view" );
400
+ errors .add ("permission denied" );
401
+ break ;
367
402
default :
368
403
throw new AssertionError (a );
369
404
}
0 commit comments