forked from bittercoder/Migrator.NET
-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathProgram.cs
More file actions
41 lines (38 loc) · 1.92 KB
/
Copy pathProgram.cs
File metadata and controls
41 lines (38 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using DotNetProjects.Migrator;
using DotNetProjects.Migrator.Framework;
using DotNetProjects.Migrator.Framework.Fluent;
using DotNetProjects.Migrator.Providers;
using Microsoft.Data.Sqlite;
using var connection = new SqliteConnection("Data Source=:memory:;Foreign Keys=True");
connection.Open();
using var provider = ProviderFactory.Create(ProviderTypes.SQLite, connection, null, "demo");
var runner = new Migrator(provider, false, typeof(CreateUsers), typeof(AddUserEmail));
runner.Options.Tags.Add("core");
runner.Options.TransactionMode = MigrationTransactionMode.WholeSession;
Console.WriteLine(runner.PreviewSql(2, ProviderTypes.SQLite));
if (provider.TableExists("Users") || provider.TableExists(provider.SchemaInfoTable)) throw new Exception("Preview wrote to the database.");
runner.MigrateToLastVersion();
if (!provider.ColumnExists("Users", "Name")) throw new Exception("Migration failed.");
if (!provider.ColumnExists("Users", "Email") || !provider.IndexExists("Users", "IX_Users_Email")) throw new Exception("Column/index migration failed.");
runner.MigrateTo(0);
if (provider.TableExists("Users")) throw new Exception("Automatic reversal failed.");
Console.WriteLine("Quick-start migration, preview and reversal passed.");
[Migration(1, Scope = "demo"), Tags("core")]
public class CreateUsers : AutoReversingMigration
{
public override void BuildUp(MigrationBuilder migration)
{
migration.Create.Table("Users")
.WithColumn("Id").AsInt32().WithPrimaryKey("PK_Id", "Id")
.WithColumn("Name").AsString(255).NotNullable();
}
}
[Migration(2, Scope = "demo"), Tags("core")]
public class AddUserEmail : AutoReversingMigration
{
public override void BuildUp(MigrationBuilder migration)
{
migration.Create.Column("Email").OnTable("Users").AsString(320).Nullable();
migration.Create.Index("IX_Users_Email").OnTable("Users").WithColumns("Email");
}
}