Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DbUp not creating schema for Journal #8

Open
Tringy opened this issue Sep 21, 2022 · 0 comments
Open

DbUp not creating schema for Journal #8

Tringy opened this issue Sep 21, 2022 · 0 comments
Labels
bug Something isn't working

Comments

@Tringy
Copy link

Tringy commented Sep 21, 2022

I'm trying to deploy a new PostgresSQL instance into Cloud SQL (GCP) using DbUp and i'm getting this error below:

Beginning database upgrade
Checking whether journal table exists..
Journal table does not exist
Executing Database Server script 'OperationalDatabase.OperationalDbUp.Gateway.Scripts.202209061040-00-gcp_gateway.sql'
Checking whether journal table exists..
Creating the "gateway"."schemaversions" table
DB exception has occured in script: 'OperationalDatabase.OperationalDbUp.Gateway.Scripts.202209061040-00-gcp_gateway.sql'
Script block number: -1; Message: 3F000: schema "gateway" does not exist
Npgsql.PostgresException (0x80004005): 3F000: schema "gateway" does not exist
   at Npgsql.NpgsqlConnector.DoReadMessage(Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isPrependedMessage)
   at Npgsql.NpgsqlConnector.ReadMessage(Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications)
   at Npgsql.NpgsqlConnector.ReadMessage(Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications)
   at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming)
   at Npgsql.NpgsqlDataReader.NextResult()
   at Npgsql.NpgsqlCommand.Execute(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteNonQuery(Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteNonQuery()
   at DbUp.Support.TableJournal.EnsureTableExistsAndIsLatestVersion(Func`1 dbCommandFactory)
   at DbUp.Support.ScriptExecutor.<>c__DisplayClass18_0.<Execute>b__0(Func`1 dbCommandFactory)
   at DbUp.Engine.Transactions.NoTransactionStrategy.Execute(Action`1 action)
   at DbUp.Engine.Transactions.DatabaseConnectionManager.ExecuteCommandsWithManagedConnection(Action`1 action)
   at DbUp.Support.ScriptExecutor.Execute(SqlScript script, IDictionary`2 variables)
Upgrade failed due to an unexpected exception:
Npgsql.PostgresException (0x80004005): 3F000: schema "gateway" does not exist

CODE

GatewayDbUp:

using System;
using System.Reflection;
using DbUp.Builder;

namespace OperationalDatabase.OperationalDbUp.Gateway
{
    public class GatewayDbUp : OperationalDbUpHandler
    {
        private readonly bool _enabled;

        public GatewayDbUp()
        {
            bool.TryParse(Environment.GetEnvironmentVariable("GATEWAY_DB_UP_ENABLED"), out _enabled);
        }

        public override void Handle()
        {
            if (_enabled)
            {
                UpTables(GetUpgradeEngineBuilder());
            }

            base.Handle();
        }

        internal void UpTables(UpgradeEngineBuilder upgradeEngineBuilder)
        {
            var engine = upgradeEngineBuilder
                .WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly(),
                    name => name.StartsWith("OperationalDatabase.OperationalDbUp.Gateway.Scripts."))
                .JournalToPostgresqlTable("gateway", "schemaversions")
                .LogToConsole()
                .Build();

            CheckUpgrade(engine.PerformUpgrade());
        }
    }
}

OperationalDbUpHandler: (I have tried to enforce the journal here but it didn't work either)

using System;
using DbUp;
using DbUp.Builder;
using DbUp.Engine;

namespace OperationalDatabase.OperationalDbUp
{
    public class OperationalDbUpHandler : IHandler
    {
        private readonly string _connectionString;
        private static readonly bool _createDatabaseIfNotExists = false;
        private IHandler _nextHandler;

        protected OperationalDbUpHandler()
        {
            _connectionString =
                $"User ID={Environment.GetEnvironmentVariable("USERNAME")};" +
                $"Password={Environment.GetEnvironmentVariable("PASSWORD")};" +
                $"Host={Environment.GetEnvironmentVariable("HOST")};" +
                $"Port={Environment.GetEnvironmentVariable("PORT")};" +
                $"Database={Environment.GetEnvironmentVariable("DATABASE")};Pooling=true;";
        }

        public IHandler SetNext(IHandler handler)
        {
            _nextHandler = handler;
            return handler;
        }

        public virtual void Handle()
        {
            _nextHandler?.Handle();
        }

        public void CreateDb()
        {
            if (_createDatabaseIfNotExists)
            {
                EnsureDatabase.For.PostgresqlDatabase(_connectionString);
            }
        }

        protected UpgradeEngineBuilder GetUpgradeEngineBuilder()
        {
            return DeployChanges.To.PostgresqlDatabase(_connectionString);
            //return DeployChanges.To.PostgresqlDatabase(_connectionString).JournalToPostgresqlTable("gateway", "schemaversions");
        }

        protected void CheckUpgrade(DatabaseUpgradeResult result)
        {
            if (!result.Successful)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(result.Error);
                Console.ResetColor();
                Console.ReadLine();

                throw result.Error;
            }

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine($"Success {GetType().Name}!");
            Console.ResetColor();
        }
    }
}

Packages

	<ItemGroup>
		<PackageReference Include="DbUp" Version="4.6.0" />
		<PackageReference Include="dbup-postgresql" Version="4.6.3" />
	</ItemGroup>

SCRIPT

Script for file 202209061040-00-gcp_gateway.sql

CREATE SCHEMA IF NOT EXISTS "gateway"
  • We've created the gateway schema manually using the same postgresql crecentials that is being used at dbup Environment Variables. The psql user can execute the scripts directly in the console.

DEPLOY TOOLS

  • docker
  • docker-compose
  • k8s
  • CloudSQL (PostgreSQL v14.0)

TL;DR

@Tringy Tringy added the bug Something isn't working label Sep 21, 2022
@droyad droyad transferred this issue from DbUp/DbUp Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Bugs
Development

No branches or pull requests

1 participant