Пример #1
0
        private static async Task DbMigration()
        {
            AppConfig configObj = DatabaseServer.GetConfigObj();
            string    user      = configObj.User.Login;
            string    pw        = configObj.User.Password;
            string    dbn       = configObj.User.DatabaseName;

            // подключаемся через супер юзера

            string connString1 = DatabaseServer.GetConnectString(configObj.DbHost, configObj.SuperUser.Login, configObj.SuperUser.Password, configObj.SuperUser.DatabaseName);

            await using (var conn1 = new NpgsqlConnection(connString1))
            {
                await conn1.OpenAsync();

                // Define a query
                var isUserExist = await DatabaseServer.IsUserExist(conn1, user);

                var isDatabaseExists = await DatabaseServer.IsDatabaseExist(conn1, dbn);

                if (isUserExist)
                {
                    Console.WriteLine($"{isUserExist}, User exists.");
                }
                else
                {
                    Console.WriteLine($"{isUserExist}, User doesn't exists. Creating new user...");

                    await DatabaseServer.UserCreation(conn1, user, pw);
                }

                if (isDatabaseExists)
                {
                    Console.WriteLine($"{isDatabaseExists}, Database exists.");
                }
                else
                {
                    Console.WriteLine($"{isDatabaseExists}, Database doesn't exist. Creating new database...");
                    await DatabaseServer.DatabaseCreation(conn1, user, dbn);
                }

                // подключаемся через юзера
                string connString2 = DatabaseServer.GetConnectString(configObj.DbHost, configObj.User.Login, configObj.User.Password, configObj.User.DatabaseName);

                await using (var conn2 = new NpgsqlConnection(connString2))
                {
                    await conn2.OpenAsync();

                    string tableName     = "form";
                    var    isTableExists = await DatabaseServer.IsCurrentTableExist(conn2, tableName);

                    if (isTableExists)
                    {
                        Console.WriteLine($"Table {tableName} exists");
                    }
                    else
                    {
                        await DatabaseServer.TableCreation(conn2, tableName);

                        Console.WriteLine($"Table {tableName} is created");
                    }
                }
            }
        }