Permalink
Browse files

purgedb for postgre verified for primary keys

  • Loading branch information...
1 parent 7fd7ed0 commit 01738ac72edc409b2788dcff7674b53961b3bfe2 @amirrajan amirrajan committed Aug 21, 2013
View
@@ -1,4 +1,5 @@
using System.Linq;
+using System.Text.RegularExpressions;
using System.Reflection;
using NSpec;
using NSpec.Domain;
@@ -61,4 +62,20 @@ public static string Print(this string s)
return s;
}
+
+ public static string File(this string s, params string[] otherStrings)
+ {
+ var path = System.IO.Path.GetTempFileName();
+ s += string.Join("\n", otherStrings);
+ System.IO.File.WriteAllText(path, s);
+ System.Diagnostics.Process.Start(path);
+ return s;
+ }
+
+ public static string ToSingleLine(this string s)
+ {
+ var single = Regex.Replace(s, @"[ ]{2,}", "");
+ single = single.Trim().Replace(Environment.NewLine, "");
+ return single;
+ }
}
@@ -191,6 +191,8 @@
<Compile Include="describe_Memoize.cs" />
<Compile Include="describe_Seed\schemas.cs" />
<Compile Include="describe_Seed_for_Postgres\describe_PurgeDb.cs" />
+ <Compile Include="describe_Seed_for_Postgres\when_creating_table.cs" />
+ <Compile Include="describe_Seed_for_Postgres\_seed.cs" />
<Compile Include="describe_StrongParameterss.cs" />
<Compile Include="describe_DynamicModel\describe_Validation\Classes\Essay.cs" />
<Compile Include="describe_DynamicModel\describe_Validation\Classes\FailedValidation.cs" />
@@ -0,0 +1,46 @@
+using System;
+using System.Collections.Generic;
+using System.Text.RegularExpressions;
+using System.Linq;
+using System.Text;
+using NSpec;
+using System.Data;
+using Npgsql;
+
+namespace Oak.Tests.describe_Seed_for_Postgres
+{
+ class _seed : nspec
+ {
+ protected Seed seed;
+ protected string command;
+ protected dynamic[] columns;
+ ConnectionProfile connectionProfile;
+ string connectionString = "Server=127.0.0.1;Port=5432;User Id=postgres;Password=Postgres1234;Database=Oak;";
+
+ void before_each()
+ {
+ connectionProfile = new ConnectionProfile
+ {
+ ConnectionString = connectionString,
+ ProviderName = "Npgsql"
+ };
+
+ seed = new Seed(connectionProfile);
+ }
+
+
+ public void CommandShouldBe(string expected)
+ {
+ command.should_be(ToSingleLine(expected));
+ }
+
+ public string ToSingleLine(string s)
+ {
+ var single = Regex.Replace(s, @"[ ]{2,}", "");
+
+ single = single.Trim().Replace(Environment.NewLine, "");
+
+ return single;
+ }
+ }
+}
@@ -26,7 +26,26 @@ void before_each()
void it_can_drop_public_tables()
{
- seed.ExecuteNonQuery(seed.CreateTable("People", new { Name = "varchar(255)" }));
+ seed.ExecuteNonQuery(
+ seed.CreateTable(
+ "People",
+ new { Name = "varchar(255)" }
+ )
+ );
+
+ TableExists("People").should_be(true);
+
+ seed.PurgeDb();
+
+ TableExists("People").should_be(false);
+ }
+
+ void it_can_drop_public_table_with_primary_key()
+ {
+ seed.ExecuteNonQuery(seed.CreateTable(
+ "People",
+ seed.Id(),
+ new { Name = "varchar(255)" }));
TableExists("People").should_be(true);
@@ -0,0 +1,39 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using NSpec;
+using System.Data;
+using Npgsql;
+
+namespace Oak.Tests.describe_Seed_for_Postgres
+{
+ class when_creating_table : _seed
+ {
+ void act_each()
+ {
+ command = seed.CreateTable("Users", columns);
+ }
+
+ void generating_a_table_where_column_is_an_identity_column_and_primary_key()
+ {
+ before = () =>
+ columns = new dynamic[]
+ {
+ new { Id = "SERIAL", Identity = true, PrimaryKey = true },
+ };
+
+ it["contains identity definition"] = () =>
+ CommandShouldBe(@"
+ CREATE TABLE public.Users
+ (
+ Id SERIAL NOT NULL,
+ CONSTRAINT PK_Users PRIMARY KEY
+ (
+ Id
+ )
+ )
+ ");
+ }
+ }
+}
View
@@ -13,6 +13,7 @@ namespace Oak
public class Seed
{
public virtual ConnectionProfile ConnectionProfile { get; set; }
+ SyntaxVariations syntax;
public Seed()
: this(null)
@@ -22,12 +23,10 @@ public Seed()
public Seed(ConnectionProfile connectionProfile)
{
if (connectionProfile == null) connectionProfile = new ConnectionProfile();
+
ConnectionProfile = connectionProfile;
- }
- public bool IsPostgres()
- {
- return ConnectionProfile.ProviderName == "Npgsql";
+ syntax = new SyntaxVariations(connectionProfile);
}
/// <summary>
@@ -39,7 +38,7 @@ public bool IsPostgres()
/// <returns>Returns the sql command generated by seed.</returns>
public string CreateTable(string table, params dynamic[] columns)
{
- return CreateTable(Dbo(), table, columns);
+ return CreateTable(syntax.Dbo(), table, columns);
}
/// <summary>
@@ -69,7 +68,7 @@ public string CreateTable(string schema, string table, params dynamic[] columns)
public string DropTable(string table)
{
- return "drop table " + table;
+ return "drop table " + syntax.Escape(table);
}
/// <summary>
@@ -114,14 +113,14 @@ public string AddColumns(string schema, string table, params dynamic[] columns)
string ColumnStringFor(object column)
{
- var name = Escape(column.Name());
+ var name = syntax.Escape(column.Name());
var type = column.SqlType();
var defaultValue = column.DefaultValue();
var isIdentity = column.IsIdentityColumn();
var isPrimaryKey = column.IsPrimaryKey();
var isForeignKey = column.IsForeignKeyColumn();
- string identityAsString = isIdentity ? " IDENTITY(1,1)" : "";
+ string identityAsString = isIdentity ? syntax.IdentityScript() : "";
string foreignKeyString = isForeignKey ? " FOREIGN KEY REFERENCES " + column.ForeignKey() : "";
return "{0} {1} {2} {3}{4}{5}"
@@ -135,28 +134,14 @@ string ColumnStringFor(object column)
.Trim();
}
- string Escape(string s)
- {
- if(IsPostgres()) return s;
-
- return "[" + s + "]";
- }
-
- string Dbo()
- {
- if (IsPostgres()) return "public";
-
- return "dbo";
- }
string CreateTableCommand(string schema, string table, List<string> otherColumns, List<string> primaryKeyColumns)
{
- var primaryKeyColumnScript = string.Join(",", primaryKeyColumns.Select(s => "[{0}] ASC".With(s)));
+ var primaryKeyColumnScript = string.Join(",", primaryKeyColumns.Select(syntax.ConstraintColumn));
var columns = string.Join(",", otherColumns);
- var primaryKeyScript =
- ", CONSTRAINT [PK_{0}] PRIMARY KEY CLUSTERED ({1})".With(table, primaryKeyColumnScript ?? string.Empty);
+ var primaryKeyScript = syntax.ConstraintPrimaryKey(table, primaryKeyColumnScript);
if (!primaryKeyColumns.Any()) primaryKeyScript = "";
@@ -165,8 +150,8 @@ string CreateTableCommand(string schema, string table, List<string> otherColumns
string FullyQualifedTableName(string schema, string table)
{
- schema = Escape(schema);
- table = Escape(table);
+ schema = syntax.Escape(schema);
+ table = syntax.Escape(table);
if(string.IsNullOrEmpty(schema)) return table;
@@ -183,7 +168,7 @@ public void PurgeDb()
private void DropCustomSchemas()
{
- if(IsPostgres()) return; //TODO
+ if(syntax.IsPostgres()) return; //TODO
var reader = @"SELECT name FROM sys.schemas".ExecuteReader(ConnectionProfile);
@@ -220,7 +205,7 @@ List<string> DefaultSchemas()
void DropAllForeignKeys()
{
- if(IsPostgres()) return; //TODO
+ if(syntax.IsPostgres()) return; //TODO
var reader = @"
select name as constraint_name,
@@ -236,7 +221,7 @@ void DropAllForeignKeys()
void DropAllPrimaryKeys()
{
- if(IsPostgres()) return; //TODO
+ if(syntax.IsPostgres()) return; //TODO
var reader = @"
select name as constraint_name,
@@ -257,7 +242,7 @@ void DropAllTables()
object_schema_name(id) as table_schema
from sysobjects where xtype = 'u'";
- if(IsPostgres())
+ if(syntax.IsPostgres())
{
query = @"
select table_name,
@@ -270,7 +255,7 @@ from information_schema.tables
while (reader.Read())
{
- "drop table {0}.{1} ".With(Escape(reader["table_schema"] as string), Escape(reader["table_name"] as string)).ExecuteNonQuery(ConnectionProfile);
+ "drop table {0}.{1} ".With(syntax.Escape(reader["table_schema"] as string), syntax.Escape(reader["table_name"] as string)).ExecuteNonQuery(ConnectionProfile);
}
}
@@ -309,6 +294,16 @@ void WriteSqlFile(string path, string name, string content)
public object Id()
{
+ if(syntax.IsPostgres())
+ {
+ return new
+ {
+ Id = "SERIAL",
+ Identity = true,
+ PrimaryKey = true
+ };
+ }
+
return new { Id = "int", Identity = true, PrimaryKey = true };
}
@@ -406,6 +401,64 @@ public void ExecuteTo(IEnumerable<Func<dynamic>> scripts, Func<dynamic> method)
}
}
}
+
+ public class SyntaxVariations
+ {
+ ConnectionProfile connectionProfile;
+ public SyntaxVariations(ConnectionProfile connectionProfile)
+ {
+ this.connectionProfile = connectionProfile;
+ }
+
+ public bool IsPostgres()
+ {
+ return connectionProfile.ProviderName == "Npgsql";
+ }
+
+ public string Escape(string s)
+ {
+ if(IsPostgres()) return s;
+
+ return "[" + s + "]";
+ }
+
+ public string Dbo()
+ {
+ if (IsPostgres()) return "public";
+
+ return "dbo";
+ }
+
+ public string IdentityScript()
+ {
+ if (IsPostgres()) return "";
+
+ return " IDENTITY(1,1)";
+ }
+
+ public string PrimaryKeyConstraintName(string forTable)
+ {
+ if (IsPostgres()) return "PK_{0}".With(forTable);
+
+ return "[PK_{0}]".With(forTable);
+ }
+
+ public string ConstraintPrimaryKey(string table, string primaryKeyColumnScript)
+ {
+ if (IsPostgres()) return ", CONSTRAINT {0} PRIMARY KEY ({1})".With(PrimaryKeyConstraintName(table), primaryKeyColumnScript ?? string.Empty);
+
+ return ", CONSTRAINT {0} PRIMARY KEY CLUSTERED ({1})".With(PrimaryKeyConstraintName(table), primaryKeyColumnScript ?? string.Empty);
+ }
+
+ public string ConstraintColumn(string column)
+ {
+ var escaped = Escape(column);
+
+ if(IsPostgres()) return escaped;
+
+ return escaped + " ASC";
+ }
+ }
}
namespace Oak.Extensions
Binary file not shown.

0 comments on commit 01738ac

Please sign in to comment.