-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add process_identifier to SpanRecord (#248)
* Creates 'AddProcessIdentifierToSpanRecordMigration', adds to current Updates SpanRecord to have processIdentifier property Adds Migration+Helpers to help run migrations 'upTo' a certain one. * Update migration to inject list of migrations Needed to make testing specific migrations possible
- Loading branch information
Showing
12 changed files
with
366 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
73 changes: 73 additions & 0 deletions
73
...eStorage/Migration/Migrations/20240523_00_AddProcessIdentifierToSpanRecordMigration.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// | ||
// Copyright © 2023 Embrace Mobile, Inc. All rights reserved. | ||
// | ||
|
||
import GRDB | ||
|
||
struct AddProcessIdentifierToSpanRecordMigration: Migration { | ||
static var identifier = "AddProcessIdentifierToSpanRecord" // DEV: Must not change | ||
|
||
private static var tempSpansTableName = "spans_temp" | ||
|
||
func perform(_ db: GRDB.Database) throws { | ||
|
||
// create copy of `spans` table in `spans_temp` | ||
// include new column 'process_identifier' | ||
try db.create(table: Self.tempSpansTableName) { t in | ||
|
||
t.column(SpanRecord.Schema.id.name, .text).notNull() | ||
t.column(SpanRecord.Schema.traceId.name, .text).notNull() | ||
t.primaryKey([SpanRecord.Schema.traceId.name, SpanRecord.Schema.id.name]) | ||
|
||
t.column(SpanRecord.Schema.name.name, .text).notNull() | ||
t.column(SpanRecord.Schema.type.name, .text).notNull() | ||
t.column(SpanRecord.Schema.startTime.name, .datetime).notNull() | ||
t.column(SpanRecord.Schema.endTime.name, .datetime) | ||
t.column(SpanRecord.Schema.data.name, .blob).notNull() | ||
|
||
// include new column into `spans_temp` table | ||
t.column(SpanRecord.Schema.processIdentifier.name, .text).notNull() | ||
} | ||
|
||
// copy all existing data into temp table | ||
// include default value for `process_identifier` | ||
try db.execute(literal: """ | ||
INSERT INTO 'spans_temp' ( | ||
'id', | ||
'trace_id', | ||
'name', | ||
'type', | ||
'start_time', | ||
'end_time', | ||
'data', | ||
'process_identifier' | ||
) SELECT | ||
id, | ||
trace_id, | ||
name, | ||
type, | ||
start_time, | ||
end_time, | ||
data, | ||
'c0ffee' | ||
FROM 'spans' | ||
""") | ||
|
||
// drop original table | ||
try db.drop(table: SpanRecord.databaseTableName) | ||
|
||
// rename temp table to be original table | ||
try db.rename(table: Self.tempSpansTableName, to: SpanRecord.databaseTableName) | ||
|
||
// Create Trigger on new spans table to prevent endTime from being modified on SpanRecord | ||
try db.execute(sql: | ||
""" | ||
CREATE TRIGGER IF NOT EXISTS prevent_closed_span_modification | ||
BEFORE UPDATE ON \(SpanRecord.databaseTableName) | ||
WHEN OLD.end_time IS NOT NULL | ||
BEGIN | ||
SELECT RAISE(ABORT,'Attempted to modify an already closed span.'); | ||
END; | ||
""" ) | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.