Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

Commit

Permalink
Copied from main project and duplicated code from non-mono sqllite pr…
Browse files Browse the repository at this point in the history
…oject
  • Loading branch information
droyad committed Jan 29, 2024
1 parent 55ba421 commit 1841545
Show file tree
Hide file tree
Showing 20 changed files with 743 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
DB Operation: Open connection
Info: Beginning database upgrade
Info: Checking whether journal table exists..
DB Operation: Execute scalar command: SELECT count(name) FROM sqlite_master WHERE type = 'table' AND name = 'SchemaVersions'
DB Operation: Dispose command
Info: Journal table does not exist
Info: Executing Database Server script 'Script0001.sql'
Info: Checking whether journal table exists..
DB Operation: Execute scalar command: SELECT count(name) FROM sqlite_master WHERE type = 'table' AND name = 'SchemaVersions'
DB Operation: Dispose command
Info: Creating the [SchemaVersions] table
DB Operation: Execute non query command: CREATE TABLE [SchemaVersions] (
SchemaVersionID INTEGER CONSTRAINT [PK_SchemaVersions_Id] PRIMARY KEY AUTOINCREMENT NOT NULL,
ScriptName TEXT NOT NULL,
Applied DATETIME NOT NULL
)
DB Operation: Dispose command
Info: The [SchemaVersions] table has been created
DB Operation: Execute non query command: script1contents
DB Operation: Dispose command
DB Operation: Create parameter
Info: DB Operation: Add parameter to command: scriptName=Script0001.sql
DB Operation: Create parameter
Info: DB Operation: Add parameter to command: applied=<date>
DB Operation: Execute non query command: insert into [SchemaVersions] (ScriptName, Applied) values (@scriptName, @applied)
DB Operation: Dispose command
Info: Upgrade successful
DB Operation: Dispose connection
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
DB Operation: Open connection
Info: Beginning database upgrade
Info: Checking whether journal table exists..
DB Operation: Execute scalar command: SELECT count(name) FROM sqlite_master WHERE type = 'table' AND name = 'TestSchemaVersions'
DB Operation: Dispose command
Info: Journal table does not exist
Info: Executing Database Server script 'Script0001.sql'
Info: Checking whether journal table exists..
DB Operation: Execute scalar command: SELECT count(name) FROM sqlite_master WHERE type = 'table' AND name = 'TestSchemaVersions'
DB Operation: Dispose command
Info: Creating the [TestSchemaVersions] table
DB Operation: Execute non query command: CREATE TABLE [TestSchemaVersions] (
SchemaVersionID INTEGER CONSTRAINT [PK_TestSchemaVersions_Id] PRIMARY KEY AUTOINCREMENT NOT NULL,
ScriptName TEXT NOT NULL,
Applied DATETIME NOT NULL
)
DB Operation: Dispose command
Info: The [TestSchemaVersions] table has been created
DB Operation: Execute non query command: script1contents
DB Operation: Dispose command
DB Operation: Create parameter
Info: DB Operation: Add parameter to command: scriptName=Script0001.sql
DB Operation: Create parameter
Info: DB Operation: Add parameter to command: applied=<date>
DB Operation: Execute non query command: insert into [TestSchemaVersions] (ScriptName, Applied) values (@scriptName, @applied)
DB Operation: Dispose command
Info: Upgrade successful
DB Operation: Dispose connection
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
DB Operation: Open connection
Info: Beginning database upgrade
Info: Checking whether journal table exists..
DB Operation: Execute scalar command: SELECT count(name) FROM sqlite_master WHERE type = 'table' AND name = 'SchemaVersions'
DB Operation: Dispose command
Info: Journal table does not exist
Info: Executing Database Server script 'Script0001.sql'
Info: Checking whether journal table exists..
DB Operation: Execute scalar command: SELECT count(name) FROM sqlite_master WHERE type = 'table' AND name = 'SchemaVersions'
DB Operation: Dispose command
Info: Creating the [SchemaVersions] table
DB Operation: Execute non query command: CREATE TABLE [SchemaVersions] (
SchemaVersionID INTEGER CONSTRAINT [PK_SchemaVersions_Id] PRIMARY KEY AUTOINCREMENT NOT NULL,
ScriptName TEXT NOT NULL,
Applied DATETIME NOT NULL
)
DB Operation: Dispose command
Info: The [SchemaVersions] table has been created
DB Operation: Execute non query command: print SubstitutedValue
DB Operation: Dispose command
DB Operation: Create parameter
Info: DB Operation: Add parameter to command: scriptName=Script0001.sql
DB Operation: Create parameter
Info: DB Operation: Add parameter to command: applied=<date>
DB Operation: Execute non query command: insert into [SchemaVersions] (ScriptName, Applied) values (@scriptName, @applied)
DB Operation: Dispose command
Info: Upgrade successful
DB Operation: Dispose connection
77 changes: 77 additions & 0 deletions src/Tests/ApprovalFiles/NoPublicApiChanges.Run.Net.verified.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
[assembly: System.CLSCompliantAttribute(true)]
[assembly: System.Runtime.InteropServices.ComVisibleAttribute(false)]
[assembly: System.Runtime.InteropServices.GuidAttribute("bce32c14-27e3-4e0d-bc1f-ec9ccb128f00")]

public static class SQLiteExtensions
{
public static DbUp.Builder.UpgradeEngineBuilder JournalToSQLiteTable(this DbUp.Builder.UpgradeEngineBuilder builder, string table) { }
public static DbUp.Builder.UpgradeEngineBuilder SQLiteDatabase(this DbUp.Builder.SupportedDatabases supported, string connectionString) { }
public static DbUp.Builder.UpgradeEngineBuilder SQLiteDatabase(this DbUp.Builder.SupportedDatabases supported, DbUp.SQLite.Helpers.SharedConnection sharedConnection) { }
}
namespace DbUp.SQLite
{
public class SQLiteConnectionManager : DbUp.Engine.Transactions.DatabaseConnectionManager, DbUp.Engine.Transactions.IConnectionManager
{
public SQLiteConnectionManager(string connectionString) { }
public SQLiteConnectionManager(DbUp.SQLite.Helpers.SharedConnection sharedConnection) { }
public override System.Collections.Generic.IEnumerable<string> SplitScriptIntoCommands(string scriptContents) { }
}
public class SQLiteObjectParser : DbUp.Support.SqlObjectParser, DbUp.Engine.ISqlObjectParser
{
public SQLiteObjectParser() { }
}
public class SQLitePreprocessor : DbUp.Engine.IScriptPreprocessor
{
public SQLitePreprocessor() { }
public string Process(string contents) { }
}
public class SQLiteScriptExecutor : DbUp.Support.ScriptExecutor, DbUp.Engine.IScriptExecutor
{
public SQLiteScriptExecutor(System.Func<DbUp.Engine.Transactions.IConnectionManager> connectionManagerFactory, System.Func<DbUp.Engine.Output.IUpgradeLog> log, string schema, System.Func<bool> variablesEnabled, System.Collections.Generic.IEnumerable<DbUp.Engine.IScriptPreprocessor> scriptPreprocessors, System.Func<DbUp.Engine.IJournal> journalFactory) { }
protected override void ExecuteCommandsWithinExceptionHandler(int index, DbUp.Engine.SqlScript script, System.Action executeCommand) { }
protected override string GetVerifySchemaSql(string schema) { }
}
public class SQLiteTableJournal : DbUp.Support.TableJournal, DbUp.Engine.IJournal
{
public SQLiteTableJournal(System.Func<DbUp.Engine.Transactions.IConnectionManager> connectionManager, System.Func<DbUp.Engine.Output.IUpgradeLog> logger, string table) { }
protected override string CreateSchemaTableSql(string quotedPrimaryKeyName) { }
protected override string DoesTableExistSql() { }
protected override string GetInsertJournalEntrySql(string scriptName, string applied) { }
protected override string GetJournalEntriesSql() { }
}
}
namespace DbUp.SQLite.Helpers
{
public class InMemorySQLiteDatabase : System.IDisposable
{
public InMemorySQLiteDatabase() { }
public string ConnectionString { get; set; }
public DbUp.Helpers.AdHocSqlRunner SqlRunner { get; }
public void Dispose() { }
public DbUp.Engine.Transactions.IConnectionManager GetConnectionManager() { }
}
public class SharedConnection : System.Data.IDbConnection, System.IDisposable
{
public SharedConnection(System.Data.IDbConnection dbConnection) { }
public string ConnectionString { get; set; }
public int ConnectionTimeout { get; }
public string Database { get; }
public System.Data.ConnectionState State { get; }
public System.Data.IDbTransaction BeginTransaction() { }
public System.Data.IDbTransaction BeginTransaction(System.Data.IsolationLevel il) { }
public void ChangeDatabase(string databaseName) { }
public void Close() { }
public System.Data.IDbCommand CreateCommand() { }
public void Dispose() { }
public void DoClose() { }
public void Open() { }
}
public class TemporarySQLiteDatabase : System.IDisposable
{
public TemporarySQLiteDatabase(string name) { }
public DbUp.SQLite.Helpers.SharedConnection SharedConnection { get; }
public DbUp.Helpers.AdHocSqlRunner SqlRunner { get; }
public void Create() { }
public void Dispose() { }
}
}
20 changes: 20 additions & 0 deletions src/Tests/DatabaseSupportTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using DbUp.Builder;
using DbUp.Tests.Common;

namespace DbUp.SQLite.Tests;

public class DatabaseSupportTests : DatabaseSupportTestsBase
{
public DatabaseSupportTests() : base()
{
}

protected override UpgradeEngineBuilder DeployTo(SupportedDatabases to)
=> to.SQLiteDatabase("");

protected override UpgradeEngineBuilder AddCustomNamedJournalToBuilder(UpgradeEngineBuilder builder, string schema, string tableName)
=> builder.JournalTo(
(connectionManagerFactory, logFactory)
=> new SQLiteTableJournal(connectionManagerFactory, logFactory, tableName)
);
}
11 changes: 11 additions & 0 deletions src/Tests/NoPublicApiChanges.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using DbUp.Tests.Common;

namespace DbUp.SQLite.Tests;

public class NoPublicApiChanges : NoPublicApiChangesBase
{
public NoPublicApiChanges()
: base(typeof(SQLiteExtensions).Assembly)
{
}
}
28 changes: 28 additions & 0 deletions src/Tests/SQLiteSupportTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.IO;
using Mono.Data.Sqlite;
using Xunit;

namespace DbUp.SQLite.Tests
{
public class SQLiteSupportTests
{
static readonly string dbFilePath = Path.Combine(Environment.CurrentDirectory, "test.db");

[Fact]
public void CanUseSQLite()
{
var connectionString = string.Format("Data Source={0}; Version=3;", dbFilePath);

if (!File.Exists(dbFilePath))
{
SqliteConnection.CreateFile(dbFilePath);
}

var upgrader = DeployChanges.To
.SQLiteDatabase(connectionString)
.WithScript("Script0001", "CREATE TABLE IF NOT EXISTS Foo (Id int)")
.Build();
}
}
}
63 changes: 63 additions & 0 deletions src/Tests/SQLiteTableJournalTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Collections.Generic;
using System.Data;
using DbUp.Engine;
using DbUp.Engine.Output;
using DbUp.Engine.Transactions;
using DbUp.Tests.Common;
using Mono.Data.Sqlite;
using NSubstitute;
using Shouldly;
using Xunit;

namespace DbUp.SQLite.Tests
{
public class SQLiteTableJournalTests
{
[Fact]
public void dbversion_is_zero_when_journal_table_not_exist()
{
// Given
var dbConnection = Substitute.For<IDbConnection>();
var command = Substitute.For<IDbCommand>();
dbConnection.CreateCommand().Returns(command);
var connectionManager = Substitute.For<IConnectionManager>();
command.ExecuteScalar().Returns(x => { throw new SqliteException("table not found"); });
var consoleUpgradeLog = new ConsoleUpgradeLog();
var journal = new SQLiteTableJournal(() => connectionManager, () => consoleUpgradeLog, "SchemaVersions");

// When
var scripts = journal.GetExecutedScripts();

// Expect
command.DidNotReceive().ExecuteReader();
scripts.ShouldBeEmpty();
}

[Fact]
public void creates_a_new_journal_table_when_not_exist()
{
// Given
var dbConnection = Substitute.For<IDbConnection>();
var connectionManager = new TestConnectionManager(dbConnection);
connectionManager.OperationStarting(new ConsoleUpgradeLog(), new List<SqlScript>());

var command = Substitute.For<IDbCommand>();
var param1 = Substitute.For<IDbDataParameter>();
var param2 = Substitute.For<IDbDataParameter>();
dbConnection.CreateCommand().Returns(command);
command.CreateParameter().Returns(param1, param2);
command.ExecuteScalar().Returns(x => 0);
var consoleUpgradeLog = new ConsoleUpgradeLog();
var journal = new SQLiteTableJournal(() => connectionManager, () => consoleUpgradeLog, "SchemaVersions");

// When
journal.StoreExecutedScript(new SqlScript("test", "select 1"), () => command);

// Expect
command.Received(2).CreateParameter();
param1.ParameterName.ShouldBe("scriptName");
param2.ParameterName.ShouldBe("applied");
command.Received().ExecuteNonQuery();
}
}
}
6 changes: 3 additions & 3 deletions src/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net462;net8</TargetFrameworks>
<TargetFramework>net462</TargetFramework>
<AssemblyName>Tests</AssemblyName>
<RootNamespace>DbUp.SQLite.Tests</RootNamespace>
<!-- <ImplicitUsings>enable</ImplicitUsings> Can't use implict usings with net46 -->
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\dbup-sqlite-mono\dbup-sqlite-mono.csproj"/>
<PackageReference Include="DbUp.Tests.Common" Version="5.0.37"/>
<PackageReference Include="DbUp.Tests.Common" Version="5.0.52-Split.8"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="xunit" Version="2.6.6"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NSubstitute" Version="5.1.0"/>
</ItemGroup>

<ItemGroup>
Expand Down
54 changes: 54 additions & 0 deletions src/dbup-sqlite-mono/Helpers/InMemorySQLiteDatabase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using DbUp.Engine.Transactions;
using DbUp.Helpers;
using Mono.Data.Sqlite;

namespace DbUp.SQLite.Helpers
{
/// <summary>
/// Used to create in-memory SQLite database that is deleted at the end of a test.
/// </summary>
public class InMemorySQLiteDatabase : IDisposable
{
readonly SQLiteConnectionManager connectionManager;
readonly SqliteConnection sharedConnection;

/// <summary>
/// Initializes a new instance of the <see cref="InMemorySQLiteDatabase"/> class.
/// </summary>
public InMemorySQLiteDatabase()
{
var connectionStringBuilder = new SqliteConnectionStringBuilder()
{
DataSource = ":memory:",
Version = 3,
DefaultTimeout = 5,
JournalMode = SQLiteJournalModeEnum.Off,
UseUTF16Encoding = true
};
ConnectionString = connectionStringBuilder.ToString();

connectionManager = new SQLiteConnectionManager(connectionStringBuilder.ConnectionString);
sharedConnection = new SqliteConnection(connectionStringBuilder.ConnectionString);
sharedConnection.Open();
SqlRunner = new AdHocSqlRunner(() => sharedConnection.CreateCommand(), new SQLiteObjectParser(), null, () => true);
}

public string ConnectionString { get; set; }

/// <summary>
/// Gets the connection factory of in-memory database.
/// </summary>
public IConnectionManager GetConnectionManager() => connectionManager;

/// <summary>
/// An adhoc sql runner against the in-memory database
/// </summary>
public AdHocSqlRunner SqlRunner { get; }

/// <summary>
/// Remove the database from memory.
/// </summary>
public void Dispose() => sharedConnection.Dispose();
}
}
Loading

0 comments on commit 1841545

Please sign in to comment.