@@ -165,6 +165,12 @@ def _view_is_valid(v: View) -> bool:
165165 is_flag = True ,
166166 help = "Don't publish views with labels: {authorized: true} in metadata.yaml" ,
167167)
168+ @click .option (
169+ "--authorized-only" ,
170+ "--authorized_only" ,
171+ is_flag = True ,
172+ help = "Only publish views with labels: {authorized: true} in metadata.yaml" ,
173+ )
168174@click .option (
169175 "--force" ,
170176 is_flag = True ,
@@ -190,6 +196,7 @@ def publish(
190196 dry_run ,
191197 user_facing_only ,
192198 skip_authorized ,
199+ authorized_only ,
193200 force ,
194201 add_managed_label ,
195202 respect_dryrun_skip ,
@@ -200,9 +207,17 @@ def publish(
200207 logging .basicConfig (level = log_level , format = "%(levelname)s %(message)s" )
201208 except ValueError as e :
202209 raise click .ClickException (f"argument --log-level: { e } " )
210+
211+ if skip_authorized and authorized_only :
212+ raise click .ClickException (
213+ "Cannot use both --skip-authorized and --authorized-only"
214+ )
215+
203216 credentials = get_credentials ()
204217
205- views = _collect_views (name , sql_dir , project_id , user_facing_only , skip_authorized )
218+ views = _collect_views (
219+ name , sql_dir , project_id , user_facing_only , skip_authorized , authorized_only
220+ )
206221 if respect_dryrun_skip :
207222 views = [view for view in views if view .path not in DryRun .skipped_files ()]
208223 if add_managed_label :
@@ -247,7 +262,9 @@ def _view_has_changes(target_project, credentials, view):
247262 return view .has_changes (target_project , credentials )
248263
249264
250- def _collect_views (name , sql_dir , project_id , user_facing_only , skip_authorized ):
265+ def _collect_views (
266+ name , sql_dir , project_id , user_facing_only , skip_authorized , authorized_only = False
267+ ):
251268 view_files = paths_matching_name_pattern (
252269 name , sql_dir , project_id , files = ("view.sql" ,)
253270 )
@@ -267,6 +284,17 @@ def _collect_views(name, sql_dir, project_id, user_facing_only, skip_authorized)
267284 and v .metadata .labels .get ("authorized" ) == ""
268285 )
269286 ]
287+ if authorized_only :
288+ views = [
289+ v
290+ for v in views
291+ if (
292+ v .metadata
293+ and v .metadata .labels
294+ # labels with boolean true are translated to ""
295+ and v .metadata .labels .get ("authorized" ) == ""
296+ )
297+ ]
270298 return views
271299
272300
@@ -314,7 +342,13 @@ def _collect_views(name, sql_dir, project_id, user_facing_only, skip_authorized)
314342 "--skip-authorized" ,
315343 "--skip_authorized" ,
316344 is_flag = True ,
317- help = "Don't publish views with labels: {authorized: true} in metadata.yaml" ,
345+ help = "Don't clean views with labels: {authorized: true} in metadata.yaml" ,
346+ )
347+ @click .option (
348+ "--authorized-only" ,
349+ "--authorized_only" ,
350+ is_flag = True ,
351+ help = "Only clean views with labels: {authorized: true} in metadata.yaml" ,
318352)
319353def clean (
320354 name ,
@@ -326,6 +360,7 @@ def clean(
326360 dry_run ,
327361 user_facing_only ,
328362 skip_authorized ,
363+ authorized_only ,
329364):
330365 """Clean managed views."""
331366 # set log level
@@ -334,13 +369,23 @@ def clean(
334369 except ValueError as e :
335370 raise click .ClickException (f"argument --log-level: { e } " )
336371
372+ if skip_authorized and authorized_only :
373+ raise click .ClickException (
374+ "Cannot use both --skip-authorized and --authorized-only"
375+ )
376+
337377 if project_id is None and target_project is None :
338378 raise click .ClickException ("command requires --project-id or --target-project" )
339379
340380 expected_view_ids = {
341381 view .target_view_identifier (target_project )
342382 for view in _collect_views (
343- name , sql_dir , project_id , user_facing_only , skip_authorized
383+ name ,
384+ sql_dir ,
385+ project_id ,
386+ user_facing_only ,
387+ skip_authorized ,
388+ authorized_only ,
344389 )
345390 }
346391
@@ -365,7 +410,13 @@ def clean(
365410 for views in p .starmap (
366411 client_q .with_client ,
367412 (
368- (_list_managed_views , dataset , name , skip_authorized )
413+ (
414+ _list_managed_views ,
415+ dataset ,
416+ name ,
417+ skip_authorized ,
418+ authorized_only ,
419+ )
369420 for dataset in datasets
370421 ),
371422 chunksize = 1 ,
@@ -381,7 +432,9 @@ def clean(
381432 )
382433
383434
384- def _list_managed_views (client , dataset , pattern , skip_authorized ):
435+ def _list_managed_views (
436+ client , dataset , pattern , skip_authorized , authorized_only = False
437+ ):
385438 query = f"""
386439 SELECT
387440 table_catalog || "." || table_schema || "." || table_name AS table_id,
@@ -407,6 +460,7 @@ def _list_managed_views(client, dataset, pattern, skip_authorized):
407460 for row in result
408461 if (pattern is None or fnmatchcase (sql_table_id (row .table_id ), f"*{ pattern } " ))
409462 and (not skip_authorized or not row .is_authorized )
463+ and (not authorized_only or row .is_authorized )
410464 ]
411465
412466
0 commit comments