Ejemplo n.º 1
0
    /// <summary>
    /// Ensures that the database specified in the connection string exists.
    /// </summary>
    /// <param name="supported">Fluent helper type.</param>
    /// <param name="connectionString">The connection string.</param>
    /// <param name="logger">The <see cref="DbUp.Engine.Output.IUpgradeLog"/> used to record actions.</param>
    /// <param name="timeout">Use this to set the command time out for creating a database in case you're encountering a time out in this operation.</param>
    /// <param name="collation">The collation name to set during database creation</param>
    /// <returns></returns>
    public static void SnowflakeDatabase(
        this SupportedDatabasesForEnsureDatabase supported,
        string connectionString,
        IUpgradeLog logger,
        int timeout      = -1,
        string collation = null)
    {
        using (var connection = new SnowflakeDbConnection())
        {
            connection.ConnectionString = connectionString;
            try
            {
                connection.Open();
            }
            catch (SnowflakeDbException)
            {
                //// Failed to connect to master, lets try direct
                //if (DatabaseExistsIfConnectedToDirectly(logger, connectionString, connection.Database))
                //    return;

                throw;
            }

            var sqlCommandText = $@"create database IF NOT EXISTS ""{connection.Database}"";";

            // Create the database...
            using (var command = new SnowflakeDbCommand()
            {
                CommandText = sqlCommandText,
                Connection = connection,
                CommandType = CommandType.Text
            })
            {
                if (timeout >= 0)
                {
                    command.CommandTimeout = timeout;
                }

                command.ExecuteNonQuery();
            }

            logger.WriteInformation(@"Created database {0}", connection.Database);
        }
    }
Ejemplo n.º 2
0
        public void Run(
            IDbConnection connection,
            IDbTransaction transaction,
            string fileFullPath,
            string delimiter   = null,
            int?batchSize      = null,
            int?commandTimeout = null,
            List <KeyValuePair <string, string> > tokens = null
            )
        {
            //get file name segments from potentially sequenceno.schemaname.tablename filename pattern
            var fileName = Path.GetFileNameWithoutExtension(fileFullPath)
                           .ReplaceTokens(_traceService, tokens);
            var fileNameSegments = fileName.SplitBulkFileName(defaultSchema: "PUBLIC");
            var schemaName       = fileNameSegments.Item2;
            var tableName        = fileNameSegments.Item3;

            var stopwatch = new Stopwatch();

            stopwatch.Start();
            _traceService.Info($"SnowflakeImportService: Started copying data into destination table {schemaName}.{tableName}");

            //read csv file and load into data table
            var sqlStatement           = PrepareMultiRowInsertStatement(schemaName, tableName, fileFullPath, delimiter);
            var statementCorrelationId = Guid.NewGuid().ToString().Fixed();

            _traceService.Debug($"Executing statement {statementCorrelationId}: {Environment.NewLine}{sqlStatement}");

            using (var cmd = new SnowflakeDbCommand())
            {
                cmd.Connection  = connection as SnowflakeDbConnection;
                cmd.Transaction = transaction as SnowflakeDbTransaction;
                cmd.CommandText = sqlStatement;
                cmd.ExecuteNonQuery();
            }

            stopwatch.Stop();
            _traceService?.Debug($"Statement {statementCorrelationId} executed in {stopwatch.ElapsedMilliseconds} ms");
            _traceService.Info($"SnowflakeImportService: Finished copying data into destination table {schemaName}.{tableName} in {stopwatch.ElapsedMilliseconds} ms");
        }