var assert = require("assert");
var tmp = require("tmp");
var fs = require("fs");
var async = require("async");
const assertThrows = require("assert-throws-async");

var CoreData = require("../index");

storeTmpName = tmp.tmpNameSync();
var store_url = require("./get_storage_url").replace(":memory:", storeTmpName);

describe("automigrations", function() {
  var db = new CoreData(store_url, { logging: false });

  var model1 = null;
  var model2 = null;

  before(function() {
    model1 = db.createModel("mig1");
    model2 = db.createModel("mig2");

    model1.defineEntity("User", {
      firstname: "string",
      lastname: "string"
    });
    model1.defineEntity("Token", {
      blah: "string"
    });
    model1.defineRelationshipManyToMany("User", "User", "friends", "friends");

    model2.defineEntity("User", {
      username: "string",
      firstname2: "string",
      lastname: "string"
    });
    model2.defineEntity("Company", {
      name: "string"
    });
    model2.defineEntity("Car", {
      model: "string"
    });
    model2.defineRelationshipManyToOne("Company", "User", "users", "company");
    model2.defineRelationshipManyToOne("Company", "Car", "cars", "company");
    model2.defineRelationshipManyToMany(
      "Car",
      "Car",
      "relatedCars",
      "relatedCars"
    );
  });

  it("should autogenerate migration", function() {
    var migrationGenerated = model2.autogenerateMigrationFromModel(model1);

    assert.deepEqual(migrationGenerated.entitiesChanges, [
      { entity: "Company", change: "+" },
      { entity: "Car", change: "+" },
      { entity: "Token", change: "-" }
    ]);
    assert.deepEqual(migrationGenerated.attributesChanges, {
      User: {
        username: "+",
        firstname2: "+",
        firstname: "-"
      }
    });
    console.log(migrationGenerated.relationshipsChanges);
    assert.deepEqual(migrationGenerated.relationshipsChanges, {
      User: {
        friends: "-",
        company: "+"
      }
    });
  });

  it("should not automigrate without automigration sync option", async function() {
    db.setModelVersion("mig1");
    await db.syncSchema({ force: true });

    db.setModelVersion("mig2");
    try {
      await db.syncSchema();
    } catch (e) {
      assert.equal(e.message, "migration mig1=>mig2 not found");
    }
  });

  it("should automigrate with automigration sync option", async function() {
    db.setModelVersion("mig1");
    await db.syncSchema({ force: true });
    db.setModelVersion("mig2");
    await db.syncSchema({ automigration: true });
  });
});

@@ -2048,38 +2048,32 @@
 on sync option%22,
- async
  function() %7B%0A  
@@ -2102,32 +2102,40 @@
 mig1%22);%0A    
-await db
+return db%0A      
 .syncSchema(
@@ -2150,19 +2150,41 @@
  true %7D)
-;%0A%0A
+%0A      .then(() =%3E %7B%0A    
     db.s
@@ -2215,25 +2215,18 @@
     
-try %7B%0A      await
+    return
  db.
@@ -2247,19 +2247,30 @@
     
-%7D
  
+ %7D)%0A      .
 catch
- 
 (e
-)
+ =%3E
  %7B%0A 
@@ -2270,24 +2270,26 @@
  =%3E %7B%0A      
+  
 assert.equal
@@ -2335,21 +2335,25 @@
 ound%22);%0A
+  
     %7D
+);
 %0A  %7D);%0A%0A
@@ -2409,22 +2409,16 @@
 option%22,
- async
  functio
@@ -2451,37 +2451,38 @@
 on(%22mig1%22);%0A    
-await
+return
  db.syncSchema(%7B
@@ -2496,18 +2496,32 @@
  true %7D)
-;%0A
+.then(() =%3E %7B%0A  
     db.s
@@ -2552,13 +2552,16 @@
     
-await
+  return
  db.
@@ -2593,22 +2593,30 @@
 n: true %7D);%0A
+    %7D);%0A
   %7D);%0A%7D);%0A

Fix automigration-test