Example #1
0
        internal static string EnsureStorageForTests(string connectionString, PartitionMap partitionMap)
        {
            var schema = "test" + Guid.NewGuid().ToString("N").Substring(0, 8);
            var source = partitionMap.Hosts.Select(x =>
            {
                var cb = new NpgsqlConnectionStringBuilder(connectionString)
                {
                    Host   = x.Host,
                    Port   = x.Port,
                    Enlist = false,
                };
                return(Builder: cb, x.Partitions);
            });

            Parallel.ForEach(source, src =>
            {
                using (var conn = new NpgsqlConnection(src.Builder.ToString()))
                {
                    conn.Open();
                    using (var cmd = SqlCommands.EnsureSchema(schema, src.Builder.Username))
                    {
                        cmd.Connection = conn;
                        cmd.ExecuteNonQuery();
                    }

                    using (var cmd = SqlCommands.EnsureStorage(src.Partitions, schema))
                    {
                        cmd.Connection = conn;
                        cmd.ExecuteNonQuery();
                    }
                }
            });

            return(schema);
        }
Example #2
0
        /// <summary>
        /// Creates partition (table) in the host if it does not exists.
        /// It also creates database schema if it is missing (before it creates the table).
        /// </summary>
        /// <param name="connection">database connection string</param>
        /// <param name="schema">database schema name</param>
        /// <param name="partition">partition number</param>
        public async ValueTask <bool> EnsureStorageIsReadyAsync(NpgsqlConnection connection, string schema, int partition = 0)
        {
            var key = $"{connection.Host}:{connection.Port}/{schema}/{partition}";

            if (FinishedInits.Contains(key))
            {
                return(false);
            }

            try
            {
                await InitLock.WaitAsync().ConfigureAwait(false);

                if (FinishedInits.Contains(key))
                {
                    return(false);
                }

                try
                {
                    // Initialization of internal database tables needs to be created in new dedicated connection
                    // which doesn't enlist to any ambient transactions.
                    var connBuilder = new NpgsqlConnectionStringBuilder(connection.ConnectionString)
                    {
                        Enlist = false,
                    };

                    var owner = connection.GetUserId();
                    using (var conn = new NpgsqlConnection(connBuilder.ToString()))
                        using (var cmd = SqlCommands.EnsureStorage(partition, owner, schema))
                        {
                            await conn.OpenAsync().ConfigureAwait(false);

                            cmd.Connection = conn;
                            await cmd.ExecuteNonQueryAsync().ConfigureAwait(false);
                        }
                }
                catch (PostgresException ex)
                {
                    if (ex.SqlState != "42P07") // 42P07 = duplicate table
                    {
                        throw;
                    }
                }

                FinishedInits.Add(key);
                return(true);
            }
            finally
            {
                InitLock.Release();
            }
        }
Example #3
0
        internal static string EnsureStorageForTests(string connectionString, string schema = null)
        {
            schema ??= "test" + Guid.NewGuid().ToString("N").Substring(0, 8);
            var cb = new NpgsqlConnectionStringBuilder(connectionString)
            {
                Enlist = false,
            };

            try
            {
                using (var conn = new NpgsqlConnection(cb.ToString()))
                {
                    conn.Open();
                    using (var cmd = SqlCommands.EnsureSchema(schema, cb.Username))
                    {
                        cmd.Connection = conn;
                        cmd.ExecuteNonQuery();
                    }

                    using (var cmd = SqlCommands.EnsureStorage(schema))
                    {
                        cmd.Connection = conn;
                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (PostgresException ex)
            {
                if (!IsThrownBecauseItAlreadyExists(ex))
                {
                    throw;
                }
            }

            return(schema);
        }