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

Mechanism to Import Events #1435

Open
Leh2 opened this issue Mar 5, 2020 · 6 comments · May be fixed by #3358
Open

Mechanism to Import Events #1435

Leh2 opened this issue Mar 5, 2020 · 6 comments · May be fixed by #3358

Comments

@Leh2
Copy link
Contributor

Leh2 commented Mar 5, 2020

When migrating old data into the event store it would be beneficial to be able to control the timestamp on events.

This would enable us to use the functionality with aggregate stream and timestamp: AggregateStreamAsync<T>(Guid streamId, ... DateTime? timestamp = null)

image

@jeremydmiller
Copy link
Member

@Leh2 If it's okay, I'm going to rename this to "Mechanism to Import Events". Like some kind of bulk writer for events? You can always go to the SQL level of course.

@jeremydmiller jeremydmiller changed the title Make it possible to control timestamp on events Mechanism to Import Events Nov 17, 2020
@jeremydmiller jeremydmiller added this to the 4.0 milestone Nov 17, 2020
@jedidja
Copy link
Contributor

jedidja commented Mar 26, 2021

+1 to this idea. We have a few years of data we're migrating from SQL Server to Marten / Postgres and it would be great to have the ability to create the full stream of events from scratch via Marten's API. But, as you mention, we could just go the SQL level too. The latter would probably require extra debugging time though :)

@jeremydmiller
Copy link
Member

This is dependent on #780.

@jeremydmiller jeremydmiller modified the milestones: 4.0, 4.1.0 May 11, 2021
@oskardudycz oskardudycz modified the milestones: 4.1.0, 4.2.0 Nov 12, 2021
@oskardudycz oskardudycz removed this from the 4.2.0 milestone Nov 21, 2021
@jannikbuschke
Copy link

jannikbuschke commented Apr 25, 2022

This would be great. I am thinking of migrating from EF core /MS SQL Server to marten and some guidance or some helper api to create events with a specific timestamp (in the past) would be nice.

@jannikbuschke
Copy link

Im currently experimenting with the following approach:
Using an IDocumentSessionListener to edit event timestamps just before saving. The events are saved with the given timestamp. The streams table will have the current timestamps, I didnt yet find a way to adjust them, not sure if is a problem.

@oskardudycz @jeremydmiller do you think this is an ok approach for importing existing events from some other data source to marten or do you see obvious problems with this approach?

    public class MigrationEventListener: IDocumentSessionListener
    {
        public Task BeforeSaveChangesAsync(IDocumentSession session, CancellationToken token)
        {
            var streams = session.PendingChanges.Streams();
            foreach (var stream in streams)
            {
                foreach (var e in stream.Events)
                {
                    if (e.Headers.TryGetValue("MigrationTimestamp", out object timestamp))
                    {
                        if (timestamp is DateTimeOffset ts)
                        {
                            e.Timestamp = ts;
                        }
                    }
                }
            }

            return Task.CompletedTask;
        }
}

// usage
var x = session.Events.StartStream(id, new SomeEvent { Id = id });
// set header value to some datetimeoffset in the past, depending on our data
var someExistingTimestampFromThePast = DateTimeOffset.UtcNow.AddDays(-150);
x.Events.First().SetHeader("MigrationTimestamp", someExistingTimestampFromThePast );

@Hona
Copy link

Hona commented Aug 6, 2024

This looks like exactly what I need for my project to be able to use Marten.

Take one data format in, transform it somehow, then load it into Marten.

@Hona Hona linked a pull request Aug 7, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants