Beispiel #1
0
        /// <inheritdoc />
        public void WaitTillDbStarting(PostgresConnectionSettings settings, int waitMs = 5000)
        {
            var       cs          = settings.GetConnectionStringToServer().ConnectionString;
            const int sleepTimeMs = 50;

            for (var i = 1; ; i++)
            {
                using (var conn = GetConnection(cs))
                {
                    try
                    {
                        conn.Open();
                        return;
                    }
                    catch (Exception ee)
                    {
                        Trace.WriteLine(ee.Message);
                    }
                }
                if (i * sleepTimeMs > waitMs)
                {
                    throw new InvalidOperationException("Не удалось дождаться запуска базы данных.");
                }
                Thread.Sleep(sleepTimeMs);
            }
        }
Beispiel #2
0
 /// <inheritdoc />
 public void CreateDatabaseIfNotExists(PostgresConnectionSettings settings)
 {
     if (IsDatabaseExists(settings) == false)
     {
         Trace.WriteLine($"Creating the database with name {settings.Database}");
         CreateDatabase(settings);
     }
 }
Beispiel #3
0
 /// <summary>
 /// Create simple postgres connection factory
 /// </summary>
 /// <param name="dbSettings">Postgres connection settings</param>
 public PostgresConnectionFactory(PostgresConnectionSettings dbSettings)
 {
     this.connectionString  = dbSettings.GetConnectionStringBuilder();
     this.connectionString  = connectionString ?? throw new ArgumentNullException(nameof(connectionString));
     this.connectionFactory = () =>
     {
         var conn = new Npgsql.NpgsqlConnection(connectionString.ConnectionString);
         conn.Open();
         return(conn);
     };
 }
Beispiel #4
0
        /// <inheritdoc />
        public bool IsDatabaseExists(PostgresConnectionSettings settings)
        {
            using var conn = GetConnection(settings.GetConnectionStringToServer().ConnectionString);
            conn.Open();

            using var cmd  = new NpgsqlCommand();
            cmd.Connection = (NpgsqlConnection)conn;

            cmd.CommandText = $"SELECT datname FROM pg_catalog.pg_database WHERE lower(datname) = lower('{settings.Database}');";
            var res = cmd.ExecuteScalar()?.ToString();

            return(settings.Database.ToLowerInvariant() == res?.ToLowerInvariant());
        }
Beispiel #5
0
        /// <inheritdoc />
        public void CreateDatabase(PostgresConnectionSettings settings)
        {
            using var conn = GetConnection(settings.GetConnectionStringToServer().ConnectionString);
            try
            {
                conn.Open();
                using var cmd  = new NpgsqlCommand();
                cmd.Connection = (NpgsqlConnection)conn;

                cmd.CommandText = $"CREATE DATABASE {settings.Database} ENCODING 'UTF8'";
                cmd.ExecuteNonQuery();
            }
            catch (PostgresException e)
            {
                // "42P04: database already exists"
                // Error codes: http://www.postgresql.org/docs/current/static/errcodes-appendix.html
                if (e.SqlState != "42P04")
                {
                    throw new InvalidOperationException(e.Message);
                }

                throw new InvalidOperationException($"Database with name {settings.Database} already exists", e);
            }
        }