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

UseArchivedStreamPartitioning=true results in cannot drop inherited constraint "mt_events_stream_id_is_archived_fkey" of relation "mt_events" #3386

Open
jonfowler1231 opened this issue Sep 2, 2024 · 1 comment
Labels

Comments

@jonfowler1231
Copy link

Marten version 7.26.4
PostgreSQL: 16.4

public static IHostBuilder CreateHostBuilder(string[] args, string? connectionString)
 {
     var host = Host.CreateDefaultBuilder();
     host.ConfigureServices((context, services) =>
     {
         if (string.IsNullOrEmpty(connectionString))
         {
             connectionString = context.Configuration.GetConnectionString("EventStore")!;
         }
         var martinService = services.AddMarten(opts =>
         {
             opts.Connection(connectionString);
             opts.Events.AddEventType<StartTripEvent>();
             opts.Events.UseArchivedStreamPartitioning = true;

         });
         martinService.ApplyAllDatabaseChangesOnStartup();
         martinService.AddAsyncDaemon(DaemonMode.Solo);
     });            
     return host;
 }
}

Run the code once and it creates the database schema successfully.
Restart a second time (no code changes) and it errors with:
Npgsql.PostgresException: 42P16: cannot drop inherited constraint "mt_events_stream_id_is_archived_fkey" of relation "mt_events"

Full details below:

[red]ERROR:[/]Marten.Exceptions.MartenSchemaException: DDL Execution for 'All Configured Changes' Failed!

ALTER TABLE public.mt_events DROP CONSTRAINT IF EXISTS mt_events_stream_id_is_archived_fkey;
ALTER TABLE public.mt_events DROP CONSTRAINT IF EXISTS mt_events_stream_id_is_archived_fkey1;
create table public.mt_events_temp as select * from public.mt_events;
drop table public.mt_events cascade;
DROP TABLE IF EXISTS public.mt_events CASCADE;
CREATE TABLE public.mt_events (
    seq_id            bigint                      NOT NULL,
    id                uuid                        NOT NULL,
    stream_id         uuid                        NULL,
    version           bigint                      NOT NULL,
    data              jsonb                       NOT NULL,
    type              varchar(500)                NOT NULL,
    timestamp         timestamp with time zone    NOT NULL DEFAULT '(now())',
    tenant_id         varchar                     NULL DEFAULT '*DEFAULT*',
    mt_dotnet_type    varchar                     NULL,
    is_archived       bool                        NOT NULL DEFAULT FALSE,
CONSTRAINT pkey_mt_events_seq_id_is_archived PRIMARY KEY (seq_id, is_archived)
) PARTITION BY LIST (is_archived);

ALTER TABLE public.mt_events
ADD CONSTRAINT fkey_mt_events_stream_id_is_archived FOREIGN KEY(stream_id, is_archived)
REFERENCES public.mt_streams(id, is_archived)ON DELETE CASCADE
;


CREATE UNIQUE INDEX pk_mt_events_stream_and_version ON public.mt_events USING btree (stream_id, version, is_archived);

CREATE TABLE public.mt_events_archived partition of public.mt_events for values in (TRUE);

CREATE TABLE public.mt_events_default PARTITION OF public.mt_events DEFAULT;

insert into public.mt_events select * from public.mt_events_temp;
drop table public.mt_events_temp cascade;

     Npgsql.PostgresException: 42P16: cannot drop inherited constraint "mt_events_stream_id_is_archived_fkey" of
     relation "mt_events"
       at async ValueTask<IBackendMessage> Npgsql.Internal.NpgsqlConnector.ReadMessageLong(bool async,
          DataRowLoadingMode dataRowLoadingMode, bool readingNotifications, bool isReadingPrependedMessage)
       at TResult
          System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder`1.StateMachineBox`1.System.Threading.Tasks.
          Sources.IValueTaskSource<TResult>.GetResult(short token)
       at async Task<bool> Npgsql.NpgsqlDataReader.NextResult(bool async, bool isConsuming, CancellationToken
          cancellationToken)
       at async Task<bool> Npgsql.NpgsqlDataReader.NextResult(bool async, bool isConsuming, CancellationToken
          cancellationToken)
       at async ValueTask<NpgsqlDataReader> Npgsql.NpgsqlCommand.ExecuteReader(bool async, CommandBehavior behavior,
          CancellationToken cancellationToken)
       at async ValueTask<NpgsqlDataReader> Npgsql.NpgsqlCommand.ExecuteReader(bool async, CommandBehavior behavior,
          CancellationToken cancellationToken)
       at async Task<int> Npgsql.NpgsqlCommand.ExecuteNonQuery(bool async, CancellationToken cancellationToken)
       at async Task Weasel.Postgresql.PostgresqlMigrator.executeDelta(SchemaMigration migration, DbConnection conn,
          AutoCreate autoCreate, IMigrationLogger logger, CancellationToken ct)
  at void Marten.StoreOptions.Weasel.Core.Migrations.IMigrationLogger.OnFailure(DbCommand command, Exception ex)
  at async Task Weasel.Postgresql.PostgresqlMigrator.executeDelta(SchemaMigration migration, DbConnection conn,
     AutoCreate autoCreate, IMigrationLogger logger, CancellationToken ct)
  at async Task<SchemaPatchDifference> Weasel.Core.Migrations.DatabaseBase`1.ApplyAllConfiguredChangesToDatabaseAsync(
     IGlobalLock<TConnection> globalLock, AutoCreate? override, ReconnectionOptions reconnectionOptions,
     CancellationToken ct)
  at async Task<SchemaPatchDifference> Weasel.Core.Migrations.DatabaseBase`1.ApplyAllConfiguredChangesToDatabaseAsync(
     IGlobalLock<TConnection> globalLock, AutoCreate? override, ReconnectionOptions reconnectionOptions,
     CancellationToken ct)
  at async Task Marten.Services.MartenActivator.StartAsync(CancellationToken cancellationToken)
  at async Task Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
  at async Task<bool> Oakton.Commands.RunCommand.Execute(RunInput input)
  at async Task<int> Oakton.CommandExecutor.execute(CommandRun run)

@jeremydmiller
Copy link
Member

@jonfowler1231 I don't think this is going to make it into the next bug sweep release, but soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants