vapor / fluent-kit

Swift ORM (queries, models, and relations) for NoSQL and SQL databases
MIT License
217 stars 116 forks source link

sketch up of hypotetical functionality #562

Closed wibed closed 1 year ago

wibed commented 1 year ago

implement stack like behaviour for migrations.

possible usecase: to micro manage db state inbetween unit tests

this is completely wip and i just wanted to get some feedback before i flesh something out.

example code:


// use case isolated within its own "testworld", implying a baseline of data for "functionality"
  func testContract() async throws {
    var testWorld = testWorld(app)
    let baseURI = RouteMap.contractRoute
    let baseURIString = baseURI.description

    let _app = testWorld.app
    // use case related migrations
    _app.migrations.add(Person.MigrationTestWorldUser())
    try await _app.autoMigrate()

    // tests
    try await contractRegister(&testWorld, uri: baseURIString)
    try await contractSearch(testWorld, uri: baseURIString)
    try await contractFind(testWorld, uri: baseURIString)
    try await contractModify(testWorld, uri: baseURIString)
    try await contractRemove(testWorld, uri: baseURIString)

    // checkout the name of your last migration added to stack
    let lastMigrationName = _app.migrations.top()
    // pop your last migration from migrations stack
    let lastMigrationValue = _app.migrations.pop()
    // revert your last migration before you start next test which is supposed to have a different state or whatever
    try await lastMigrationValue?.revert(on: app.db).get()

  }
0xTim commented 1 year ago

If you know the names of the migrations in the test why not just call them directly rather than implementing a whole stack?

// use case isolated within its own "testworld", implying a baseline of data for "functionality"
  func testContract() async throws {
    var testWorld = testWorld(app)
    let baseURI = RouteMap.contractRoute
    let baseURIString = baseURI.description

    let _app = testWorld.app
    // use case related migrations
    let migration = Person.MigrationTestWorldUser()
    _app.migrations.add(migration)
    try await _app.autoMigrate()

    // tests
    try await contractRegister(&testWorld, uri: baseURIString)
    try await contractSearch(testWorld, uri: baseURIString)
    try await contractFind(testWorld, uri: baseURIString)
    try await contractModify(testWorld, uri: baseURIString)
    try await contractRemove(testWorld, uri: baseURIString)

    try await migration.revert(on: app.db).get()

  }
wibed commented 1 year ago

call me stupid. you are right!