Something went wrong. Try again.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970import Foundationimport Testing@testable import EffemAppView
@Suite("SQL statement splitting")struct SQLSplitterTests { @Test("Statements split on semicolons outside strings, identifiers and comments") func split() { let sql = """ -- leading comment; with a semicolon CREATE TABLE "a;b" (x TEXT DEFAULT 'one;two'); INSERT INTO t VALUES ('it''s;fine'); -- trailing; comment CREATE FUNCTION f() RETURNS void AS $$ BEGIN PERFORM 1; END; $$ LANGUAGE plpgsql;
SELECT 1 """ let statements = SQLStatementSplitter.split(sql) #expect(statements.count == 4) #expect(statements[0] == "CREATE TABLE \"a;b\" (x TEXT DEFAULT 'one;two')") #expect(statements[1] == "INSERT INTO t VALUES ('it''s;fine')") #expect(statements[2].hasPrefix("CREATE FUNCTION f() RETURNS void AS $$ BEGIN PERFORM 1; END; $$")) #expect(statements[3] == "SELECT 1") }
@Test("Every bundled migration splits into non-empty statements") func bundled() throws { let migrations = try Migrator.bundledMigrations() #expect(migrations.count == 13) #expect(migrations.map(\.version) == ["0001", "0002", "0003", "0004", "0005", "0006", "0007", "0008", "0009", "0010", "0011", "0012", "0013"]) for migration in migrations { let statements = SQLStatementSplitter.split(migration.sql) #expect(!statements.isEmpty, "\(migration.name)") #expect(statements.allSatisfy { !$0.isEmpty }, "\(migration.name)") } #expect(migrations[1].runsOutsideTransaction) #expect(!migrations[0].runsOutsideTransaction) }}
@Suite("Migration files")struct MigrationFileTests { @Test("Version, checksum and the no-transaction directive are read from the file") func parsing() throws { let contents = Data("-- migrate:no-transaction\n\nCREATE INDEX CONCURRENTLY idx ON t (a);\n".utf8) let file = try MigrationFile(name: "0042_add_index.sql", contents: contents) #expect(file.version == "0042") #expect(file.runsOutsideTransaction) #expect(file.checksum.count == 64) let later = try MigrationFile(name: "0042_add_index.sql", contents: contents + Data("\n".utf8)) #expect(later.checksum != file.checksum) #expect(!MigrationFile.hasNoTransactionDirective("CREATE TABLE t (a int);\n-- migrate:no-transaction")) #expect(throws: MigrationError.self) { try MigrationFile(name: "_nameless.sql", contents: contents) } }
@Test("The checksums match what the Go runner recorded for the shared files") func checksums() throws { // sha256 of the unmodified Go migration files, which the Swift runner must // reproduce so a database migrated by the Go service is accepted as is. let migrations = try Migrator.bundledMigrations() let first = try #require(migrations.first { $0.version == "0001" }) #expect(first.name == "0001_initial.sql") #expect(first.checksum == GoChecksums.initial) }}
/// Values computed with `shasum -a 256` over the Go repository's files.enum GoChecksums { static let initial = "3adce806655b8491a17994e02078f748c2775518d570d3ef9af7e7e88cecaa33"}