Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified example_scripts/Dbdeploy.NAnt.dll
Binary file not shown.
Binary file modified example_scripts/Net.Sf.Dbdeploy.dll
Binary file not shown.
18 changes: 18 additions & 0 deletions example_scripts/Resources/postgres_apply.vm
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#foreach($script in $scripts)

-- START CHANGE SCRIPT ${script}

INSERT INTO ${changeLogTableName} (Folder, ScriptNumber, ScriptName, StartDate, AppliedBy, ScriptStatus, ScriptOutput)
VALUES ('${script.Folder}', ${script.ScriptNumber}, '${script.ScriptName}', CURRENT_TIMESTAMP, USER, 1, '')${separator}${delimiter}

${script.getContent()}

UPDATE ${changeLogTableName}
SET CompleteDate = getdate(), ScriptStatus = 1, ScriptOutput = ''
WHERE Folder = '$script.Folder' AND ScriptNumber = $script.ScriptNumber$!{separator}${delimiter}

COMMIT${separator}${delimiter}

-- END CHANGE SCRIPT ${script}

#end
13 changes: 13 additions & 0 deletions example_scripts/Resources/postgres_undo.vm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#foreach($script in $scripts)

-- START UNDO OF CHANGE SCRIPT ${script}

${script.getUndoContent()}

DELETE FROM ${changeLogTableName} WHERE Folder = '$script.Folder' AND ScriptNumber = $script.ScriptNumber${separator}${delimiter}

COMMIT${separator}${delimiter}

-- END UNDO OF CHANGE SCRIPT ${script}

#end
6 changes: 6 additions & 0 deletions example_scripts/dbproviders.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@
assemblyName="MySql.Data, Version=1.0.7.30072, Culture=neutral, PublicKeyToken=c5687fc88969c44d"
connectionClass="MySql.Data.MySqlClient.MySqlConnection"
/>
<provider
name="postgres"
description="Npgsql, Version=2.2.3.0"
assemblyName="Npgsql, Version=2.2.3.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7"
connectionClass="Npgsql.NpgsqlConnection"
/>
</providers>
4 changes: 2 additions & 2 deletions example_scripts/example.build
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<project name="dbdeploy_example" default="generate-script" basedir="." xmlns="http://nant.sf.net/release/0.85/nant.xsd">
<loadtasks assembly="./Dbdeploy.NAnt.dll" />
<target name="generate-script" description="generate a sql upgrade script">
<dbdeploy dbType="mssql"
dbConnection="Server=.\SQLEXPRESS;Initial Catalog=DBDEPLOY;User Id=DBDeployUser;Password=Password01"
<dbdeploy dbType="postgres"
dbConnection="User ID=postgres;Password=C0mplexPwd;Host=localhost;Port=5432;Database=dummy;"
dir="."
outputFile="output.sql"
undoOutputFile="output-undo.sql" />
Expand Down
15 changes: 15 additions & 0 deletions scripts/CreateSchemaVersionTable.postgres.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE $(QualifiedTableName) (
ChangeId bigserial,
Folder character varying(256) NOT NULL,
ScriptNumber smallint NOT NULL,
ScriptName character varying(512) NOT NULL,
StartDate timestamp NOT NULL,
CompleteDate timestamp NULL,
AppliedBy character varying(128) NOT NULL,
ScriptStatus smallint NOT NULL,
ScriptOutput text NOT NULL
);

ALTER TABLE $(QualifiedTableName) ADD CONSTRAINT PK_$(TableName) PRIMARY KEY (ChangeId);

CREATE INDEX IX_$(TableName) ON $(QualifiedTableName) (Folder, ScriptNumber)
2 changes: 1 addition & 1 deletion src/Dbdeploy.Console/OptionsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private static OptionSet Initialize(DbDeployConfig config, ConfigFileInfo config
options
.Add(
"d|dbms=",
"DBMS type ('mssql', 'mysql' or 'ora')",
"DBMS type ('mssql', 'mysql', 'ora' or 'postgres')",
s => config.Dbms = s)

.Add(
Expand Down
2 changes: 1 addition & 1 deletion src/Dbdeploy.NAnt/NAntTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public void PrintUsage()
string message = "\n\nDbdeploy Ant Task Usage"
+ "\n======================="
+ "\n\n\t<dbdeploy"
+ "\n\t\tdbType=\"[DATABASE TYPE - mssql/mysql/ora]\" *"
+ "\n\t\tdbType=\"[DATABASE TYPE - mssql/mysql/ora/postgres]\" *"
+ "\n\t\tdbConnection=\"[DATABASE CONNECTION STRING]\" *"
+ "\n\t\ttemplatedir=\"[DIRECTORY FOR DBMS TEMPLATE SCRIPTS, IF NOT USING BUILT-IN]\""
+ "\n\t\tdir=\"[YOUR SCRIPT FOLDER]\" *"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private static T GetValue<T>(IDataReader reader, string name)
var columnValue = reader[name];
if (columnValue != DBNull.Value)
{
value = (T)columnValue;
value = (T)Convert.ChangeType(columnValue, typeof(T));
}

return value;
Expand Down
4 changes: 3 additions & 1 deletion src/Net.Sf.Dbdeploy/Database/DbmsFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ public virtual IDbmsSyntax CreateDbmsSyntax()
return new MsSqlDbmsSyntax();
case "mysql":
return new MySqlDbmsSyntax();
case "postgres":
return new PostgresDbmsSyntax();
default:
throw new ArgumentException("Supported dbms: ora, mssql, mysql");
throw new ArgumentException("Supported dbms: ora, mssql, mysql, postgres");
}
}

Expand Down
11 changes: 3 additions & 8 deletions src/Net.Sf.Dbdeploy/Database/DbmsSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,12 @@ public virtual string TableExists(string tableName)
if (!string.IsNullOrWhiteSpace(tableInfo.Schema))
{
syntax = string.Format(CultureInfo.InvariantCulture,
@"SELECT table_schema
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = '{0}'
AND TABLE_NAME = '{1}'", tableInfo.Schema, tableInfo.TableName);
@"SELECT table_schema FROM INFORMATION_SCHEMA.TABLES WHERE lower(TABLE_SCHEMA) = lower('{0}') AND lower(TABLE_NAME) = lower('{1}')", tableInfo.Schema, tableInfo.TableName);
}
else
{
syntax = string.Format(CultureInfo.InvariantCulture,
@"SELECT table_schema
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = '{0}'", tableName);
syntax = string.Format(CultureInfo.InvariantCulture,
@"SELECT table_schema FROM INFORMATION_SCHEMA.TABLES WHERE lower(TABLE_NAME) = lower('{0}')", tableName);
}

return syntax;
Expand Down
43 changes: 43 additions & 0 deletions src/Net.Sf.Dbdeploy/Database/PostgresDbmsSyntax.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace Net.Sf.Dbdeploy.Database
{
/// <summary>
/// Postgres syntax.
/// </summary>
public class PostgresDbmsSyntax : DbmsSyntax
{
/// <summary>
/// Initializes a new instance of the <see cref="PostgresDbmsSyntax" /> class.
/// </summary>
public PostgresDbmsSyntax()
: base("postgres")
{
}

public override string DefaultSchema
{
get { return "public"; }
}

/// <summary>
/// Gets the get timestamp.
/// </summary>
/// <value>
/// The get timestamp.
/// </value>
public override string CurrentTimestamp
{
get { return "CURRENT_TIMESTAMP"; }
}

/// <summary>
/// Gets the syntax to get the current user.
/// </summary>
/// <value>
/// The current user syntax.
/// </value>
public override string CurrentUser
{
get { return "USER"; }
}
}
}
12 changes: 12 additions & 0 deletions src/Net.Sf.Dbdeploy/Net.Sf.Dbdeploy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
<Compile Include="Configuration\IDbDeployConfigurationManager.cs" />
<Compile Include="Configuration\Parser.cs" />
<Compile Include="Database\ChangeEntry.cs" />
<Compile Include="Database\PostgresDbmsSyntax.cs" />
<Compile Include="Database\SqlCmd\ConnectionStringInfo.cs" />
<Compile Include="Database\SqlCmd\ConnectionStringParser.cs" />
<Compile Include="Database\SqlCmd\SqlCmdExecutor.cs" />
Expand Down Expand Up @@ -189,6 +190,12 @@
<EmbeddedResource Include="Resources\SQLCMD.rll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="Resources\postgres_apply.vm">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="Resources\postgres_undo.vm">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="..\dbproviders.xml">
Expand All @@ -211,6 +218,11 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\CreateSchemaVersionTable.postgres.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE $(QualifiedTableName) (
ChangeId serial,
Folder character varying(256) NOT NULL,
ScriptNumber smallint NOT NULL,
ScriptName character varying(512) NOT NULL,
StartDate timestamp NOT NULL,
CompleteDate timestamp NULL,
AppliedBy character varying(128) NOT NULL,
ScriptStatus char NOT NULL,
ScriptOutput text NOT NULL
);

ALTER TABLE $(QualifiedTableName) ADD CONSTRAINT PK_$(TableName) PRIMARY KEY (ChangeId);

CREATE INDEX IX_$(TableName) ON $(QualifiedTableName) (Folder, ScriptNumber)
18 changes: 18 additions & 0 deletions src/Net.Sf.Dbdeploy/Resources/postgres_apply.vm
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#foreach($script in $scripts)

-- START CHANGE SCRIPT ${script}

INSERT INTO ${changeLogTableName} (Folder, ScriptNumber, ScriptName, StartDate, AppliedBy, ScriptStatus, ScriptOutput)
VALUES ('${script.Folder}', ${script.ScriptNumber}, '${script.ScriptName}', CURRENT_TIMESTAMP, USER, 1, '')${separator}${delimiter}

${script.getContent()}

UPDATE ${changeLogTableName}
SET CompleteDate = getdate(), ScriptStatus = 1, ScriptOutput = ''
WHERE Folder = '$script.Folder' AND ScriptNumber = $script.ScriptNumber$!{separator}${delimiter}

COMMIT${separator}${delimiter}

-- END CHANGE SCRIPT ${script}

#end
13 changes: 13 additions & 0 deletions src/Net.Sf.Dbdeploy/Resources/postgres_undo.vm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#foreach($script in $scripts)

-- START UNDO OF CHANGE SCRIPT ${script}

${script.getUndoContent()}

DELETE FROM ${changeLogTableName} WHERE Folder = '$script.Folder' AND ScriptNumber = $script.ScriptNumber${separator}${delimiter}

COMMIT${separator}${delimiter}

-- END UNDO OF CHANGE SCRIPT ${script}

#end
1 change: 1 addition & 0 deletions src/Test.Net.Sf.Dbdeploy/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
<add key="ConnString" value="Server=.;Initial Catalog=DBDEPLOY;User Id=DBDeployUser;Password=Password01"/>
<add key="ConnString-2ARMITAGE" value="Server=.;Initial Catalog=DBDEPLOY;Trusted_Connection=true"/>
<add key="OracleConnString" value="data source=ORACLE;user id=scott;password=tiger"/>
<add key="PostgresConnString" value="Server=127.0.0.1;Port=5432;User Id=postgres;Password=C0mplexPwd;Database=dummy;" />
</appSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ namespace Net.Sf.Dbdeploy.Database
[TestFixture]
public abstract class AbstractDatabaseSchemaVersionManagerTest
{
public const string TableName = "ChangeLog";

protected DatabaseSchemaVersionManager databaseSchemaVersion;

private IDbmsSyntax syntax;

protected string TableName { get; set; }

public AbstractDatabaseSchemaVersionManagerTest()
{
TableName = "ChangeLog";
}

[SetUp]
protected void SetUp()
{
Expand Down Expand Up @@ -124,7 +129,7 @@ public virtual void TestShouldReturnEmptySetWhenTableHasNoRows()
/// <param name="tableName">Name of the table.</param>
protected virtual void EnsureTableDoesNotExist(string tableName)
{
ExecuteSql("DROP TABLE " + TableName);
ExecuteSql("DROP TABLE " + tableName);
}

protected void ExecuteSql(string sql)
Expand Down
Loading