-
Notifications
You must be signed in to change notification settings - Fork 250
/
Copy pathPostgreSqlSeeder.cs
37 lines (29 loc) · 1.45 KB
/
PostgreSqlSeeder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using Npgsql;
using Steeltoe.Connectors;
using Steeltoe.Connectors.PostgreSql;
namespace Steeltoe.Samples.PostgreSql;
internal sealed class PostgreSqlSeeder
{
public static async Task CreateSampleDataAsync(IServiceProvider serviceProvider)
{
var connectorFactory = serviceProvider.GetRequiredService<ConnectorFactory<PostgreSqlOptions, NpgsqlConnection>>();
await using NpgsqlConnection connection = connectorFactory.Get().GetConnection();
await connection.OpenAsync();
await DropCreateTableAsync(connection);
await InsertSampleDataAsync(connection);
}
private static async Task DropCreateTableAsync(NpgsqlConnection connection)
{
var dropCommand = new NpgsqlCommand("DROP TABLE IF EXISTS TestData;", connection);
await dropCommand.ExecuteNonQueryAsync();
var createCommand = new NpgsqlCommand("CREATE TABLE IF NOT EXISTS TestData(Id INT PRIMARY KEY, MyText VARCHAR(255));", connection);
await createCommand.ExecuteNonQueryAsync();
}
private static async Task InsertSampleDataAsync(NpgsqlConnection connection)
{
var insertCommand1 = new NpgsqlCommand("INSERT INTO TestData(Id, MyText) VALUES(1, 'Row1 Text');", connection);
await insertCommand1.ExecuteNonQueryAsync();
var insertCommand2 = new NpgsqlCommand("INSERT INTO TestData(Id, MyText) VALUES(2, 'Row2 Text');", connection);
await insertCommand2.ExecuteNonQueryAsync();
}
}