Browse Source

[frontend] Add drop functionality to data catalog entries

Johan Åhlén 4 years ago
parent
commit
e48350136f

+ 1 - 0
desktop/core/src/desktop/js/apps/editor/execution/executable.ts

@@ -261,6 +261,7 @@ export default abstract class Executable {
       this.checkStatus();
       this.logs.fetchLogs();
     } catch (err) {
+      console.warn(err);
       this.setStatus(ExecutionStatus.failed);
     }
   }

+ 30 - 0
desktop/core/src/desktop/js/catalog/DataCatalogEntry.ts

@@ -36,6 +36,7 @@ import { Compute, Connector, Namespace } from 'config/types';
 import { hueWindow } from 'types/types';
 import huePubSub from 'utils/huePubSub';
 import I18n from 'utils/i18n';
+import { executeSingleStatement } from 'apps/editor/execution/api';
 import { SqlAnalyzer } from './analyzer/types';
 import {
   CatalogGetOptions,
@@ -514,6 +515,35 @@ export default class DataCatalogEntry {
     return applyCancellable(this.sourceMetaPromise, options);
   }
 
+  drop(cascade?: boolean): CancellablePromise<void> {
+    if (!this.isDatabase() && !this.isTableOrView()) {
+      return CancellablePromise.reject('Drop is only possible for a database, table or view.');
+    }
+    const statement = `DROP ${
+      this.isDatabase() ? 'DATABASE' : this.isView() ? 'VIEW' : 'TABLE'
+    } IF EXISTS \`${this.path.join('`.`')}\`${this.isDatabase() && cascade ? ' CASCADE;' : ';'}`;
+
+    return new CancellablePromise<void>((resolve, reject, onCancel) => {
+      const executePromise = executeSingleStatement({
+        database: this.path[0],
+        connector: this.getConnector(),
+        namespace: this.namespace,
+        compute: this.compute,
+        statement
+      });
+      onCancel(() => {
+        executePromise.cancel();
+      });
+
+      executePromise
+        .then(() => {
+          this.clearCache({ cascade: true }).catch();
+          resolve();
+        })
+        .catch(reject);
+    });
+  }
+
   /**
    * Save the entry to cache
    */