コード例 #1
0
        /// <summary>
        /// Remove any functions and tables that are present in the database. Note that this should only be called when
        /// connecting to the temporary database
        /// </summary>
        public void CleanDatabase()
        {
            if (!_tempDatabaseUsed)
            {
                throw new Exception("CleanDatabase() was called on something other than the temporary database. This method will wipe out the entire database schema and data.");
            }

            var schema = GetDatabaseSchema();

            foreach (var function in schema.Functions)
            {
                string command = CslCommandGenerator.GenerateFunctionDropCommand(function.Value.Name, true);
                _adminClient.ExecuteControlCommand(command);
            }

            foreach (var table in schema.Tables)
            {
                string command = CslCommandGenerator.GenerateTableDropCommand(table.Value.Name, true);
                _adminClient.ExecuteControlCommand(command);
            }
        }
コード例 #2
0
ファイル: SettingsForm.cs プロジェクト: microsoft/synckusto
        /// <summary>
        /// Test out the settings before saving them.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            Cursor lastCursor = Cursor.Current;

            Cursor.Current = Cursors.WaitCursor;

            SettingsWrapper.TableFieldsOnNewLine   = cbTableFieldsOnNewLine.Checked;
            SettingsWrapper.CreateMergeEnabled     = cbCreateMerge.Checked;
            SettingsWrapper.KustoObjectDropWarning = chkTableDropWarning.Checked;
            SettingsWrapper.AADAuthority           = txtAuthority.Text;

            // Only check the Kusto settings if they changed
            if (SettingsWrapper.KustoClusterForTempDatabases != txtKustoCluster.Text || SettingsWrapper.TemporaryKustoDatabase != txtKustoDatabase.Text)
            {
                // Allow for multiple ways of specifying a cluster name
                if (string.IsNullOrEmpty(txtKustoCluster.Text))
                {
                    MessageBox.Show($"No Kusto cluster was specified.", "Missing Info", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                string clusterName = QueryEngine.NormalizeClusterName(txtKustoCluster.Text);

                string databaseName = txtKustoDatabase.Text;
                if (string.IsNullOrEmpty(databaseName))
                {
                    MessageBox.Show($"No Kusto database was specified.", "Missing Info", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                // If the required info is present, update the cluster textbox with the modified cluster url
                txtKustoCluster.Text = clusterName;

                // Verify connection and permissions by creating and removing a function
                var connString = new KustoConnectionStringBuilder(clusterName)
                {
                    FederatedSecurity = true,
                    InitialCatalog    = databaseName,
                    Authority         = txtAuthority.Text
                };
                var adminClient = KustoClientFactory.CreateCslAdminProvider(connString);

                try
                {
                    string functionName = "SyncKustoPermissionsTest" + Guid.NewGuid();
                    adminClient.ExecuteControlCommand(
                        CslCommandGenerator.GenerateCreateOrAlterFunctionCommand(
                            functionName,
                            "",
                            "",
                            new Dictionary <string, string>(),
                            "{print now()}"));
                    adminClient.ExecuteControlCommand(CslCommandGenerator.GenerateFunctionDropCommand(functionName));
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains("403-Forbidden"))
                    {
                        MessageBox.Show($"The current user does not have permission to create a function on cluster('{clusterName}').database('{databaseName}')", "Error Validating Permissions", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else if (ex.Message.Contains("failed to resolve the service name"))
                    {
                        MessageBox.Show($"Cluster {clusterName} could not be found.", "Error Validating Permissions", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else if (ex.Message.Contains("Kusto client failed to perform authentication"))
                    {
                        MessageBox.Show($"Could not authenticate with AAD. Please verify that the AAD Authority is specified correctly.", "Error Authenticating", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        MessageBox.Show($"Unknown error: {ex.Message}", "Error Validating Permissions", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    return;
                }

                // Verify that the scratch database is empty
                try
                {
                    long functionCount = 0;
                    long tableCount    = 0;

                    using (var functionReader = adminClient.ExecuteControlCommand(".show functions | count"))
                    {
                        functionReader.Read();
                        functionCount = functionReader.GetInt64(0);
                    }

                    using (var tableReader = adminClient.ExecuteControlCommand(".show tables | count"))
                    {
                        tableReader.Read();
                        tableCount = tableReader.GetInt64(0);
                    }

                    if (functionCount != 0 || tableCount != 0)
                    {
                        MessageBox.Show($"Drop all functions and tables in the {txtKustoDatabase.Text} database before specifying this as the temporary database. " +
                                        $"This check is performed to reinforce the point that this databse will be wiped every time a comparison is run.",
                                        "Error Validating Empty Database",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Unknown error: {ex.Message}", "Error Validating Empty Database", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                // Store the settings now that we know they work
                SettingsWrapper.KustoClusterForTempDatabases = clusterName;
                SettingsWrapper.TemporaryKustoDatabase       = databaseName;
            }

            this.Close();

            Cursor.Current = lastCursor;
        }
        /// <summary>
        /// Test out the settings before saving them.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            Cursor lastCursor = Cursor.Current;

            Cursor.Current = Cursors.WaitCursor;

            // Allow for multiple ways of specifying a cluster name
            string clusterName = txtKustoCluster.Text;

            if (string.IsNullOrEmpty(clusterName))
            {
                MessageBox.Show($"No Kusto cluster was specified.", "Missing Info", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if (!clusterName.EndsWith(".kusto.windows.net"))
            {
                clusterName = $"https://{clusterName}.kusto.windows.net";
            }

            string databaseName = txtKustoDatabase.Text;

            if (string.IsNullOrEmpty(databaseName))
            {
                MessageBox.Show($"No Kusto database was specified.", "Missing Info", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // If the required info is present, update the cluster textbox with the modified cluster url
            txtKustoCluster.Text = clusterName;

            // Verify connection and permissions by creating and removing a function
            try
            {
                var connString = new KustoConnectionStringBuilder(clusterName)
                {
                    FederatedSecurity = true,
                    InitialCatalog    = databaseName,
                    Authority         = txtAuthority.Text
                };
                var    adminClient  = KustoClientFactory.CreateCslAdminProvider(connString);
                string functionName = "SyncKustoPermissionsTest";
                adminClient.ExecuteControlCommand(
                    CslCommandGenerator.GenerateCreateOrAlterFunctionCommand(
                        functionName,
                        "",
                        "",
                        new Dictionary <string, string>(),
                        "{print now()}"));
                adminClient.ExecuteControlCommand(CslCommandGenerator.GenerateFunctionDropCommand(functionName));

                // Store the settings now that we know they work
                SettingsWrapper.KustoClusterForTempDatabases = clusterName;
                SettingsWrapper.TemporaryKustoDatabase       = databaseName;
                SettingsWrapper.AADAuthority = txtAuthority.Text;

                this.Close();
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("403-Forbidden"))
                {
                    MessageBox.Show($"The current user does not have permission to create a function on cluster('{clusterName}').database('{databaseName}')", "Error Validating Permissions", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if (ex.Message.Contains("failed to resolve the service name"))
                {
                    MessageBox.Show($"Cluster {clusterName} could not be found.", "Error Validating Permissions", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if (ex.Message.Contains("Kusto client failed to perform authentication"))
                {
                    MessageBox.Show($"Could not authenticate with AAD. Please verify that the AAD Authority is specified correctly.", "Error Authenticating", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show($"Unknown error: {ex.Message}", "Error Validating Permissions", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            Cursor.Current = lastCursor;
        }