Created
July 28, 2023 11:45
-
-
Save sturlath/10fb2b778f342d727c2a2ad891cf3a4a to your computer and use it in GitHub Desktop.
This is the code that works for this question https://stackoverflow.com/questions/76786142/problem-running-a-ef-migration-from-a-docker-image-to-a-postgressql-server-from
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.EntityFrameworkCore; | |
using Microsoft.Extensions.DependencyInjection; | |
using Npgsql; | |
namespace DockerPSQLGitHubActionDebug | |
{ | |
class Program | |
{ | |
static async Task Main(string[] args) | |
{ | |
var services = new ServiceCollection(); | |
var connStringPostgresDb = "Host=myPostgreSQLServer.postgres.database.azure.com;Username=MyAdminUser;Password=LegalWorkingPassWord;Database=postgres;"; | |
PrintOtVaribles(); | |
TryConnectWithNpgql(connStringPostgresDb); | |
var connStringTestDB = "Host=myPostgreSQLServer.postgres.database.azure.com;Username=MyAdminUser;Password=LegalWorkingPassWord;Database=testdb;"; | |
using MyContext context = await TryToRunMigration(services, connStringTestDB); | |
} | |
private static void PrintOtVaribles() | |
{ | |
var SECRET_DEV_POSTGRESQL_ADMIN_LOGIN = Environment.GetEnvironmentVariable("SECRET_DEV_POSTGRESQL_ADMIN_LOGIN"); | |
var DB_PASENV_DEV_POSTGRESQL_ADMIN_LOGINSWORD = Environment.GetEnvironmentVariable("DB_PASENV_DEV_POSTGRESQL_ADMIN_LOGINSWORD"); | |
var SECRET_DEV_POSTGRESQL_ADMIN_PASSWORD = Environment.GetEnvironmentVariable("SECRET_DEV_POSTGRESQL_ADMIN_PASSWORD"); | |
var ENV_DEV_POSTGRESQL_ADMIN_PASSWORD = Environment.GetEnvironmentVariable("ENV_DEV_POSTGRESQL_ADMIN_PASSWORD"); | |
Console.WriteLine($"SECRET_DEV_POSTGRESQL_ADMIN_LOGIN: {SECRET_DEV_POSTGRESQL_ADMIN_LOGIN}"); | |
Console.WriteLine($"DB_PASENV_DEV_POSTGRESQL_ADMIN_LOGINSWORD: {DB_PASENV_DEV_POSTGRESQL_ADMIN_LOGINSWORD}"); | |
Console.WriteLine($"SECRET_DEV_POSTGRESQL_ADMIN_PASSWORD: {SECRET_DEV_POSTGRESQL_ADMIN_PASSWORD}"); | |
Console.WriteLine($"ENV_DEV_POSTGRESQL_ADMIN_PASSWORD: {ENV_DEV_POSTGRESQL_ADMIN_PASSWORD}"); | |
} | |
private static async Task<MyContext> TryToRunMigration(ServiceCollection services, string connString) | |
{ | |
//Configure DBContext with your connection string. | |
services.AddDbContext<MyContext>(options => options.UseNpgsql(connString)); | |
//Building service provider | |
var serviceProvider = services.BuildServiceProvider(); | |
var context = serviceProvider.GetRequiredService<MyContext>(); | |
try | |
{ | |
//Running migration | |
Console.WriteLine("Applying migrations..."); | |
await context.Database.MigrateAsync(); | |
Console.WriteLine("Migration test was successful!"); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine("Migration test failed!"); | |
Console.WriteLine($"Error: {ex.Message}"); | |
} | |
return context; | |
} | |
private static void TryConnectWithNpgql(string connString) | |
{ | |
try | |
{ | |
using var conn = new NpgsqlConnection(connString); | |
conn.Open(); // This will throw an exception if there's an issue connecting | |
Console.WriteLine("Successfully connected to the database."); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine($"Failed to connect to the database: {ex.Message}"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment