-
Notifications
You must be signed in to change notification settings - Fork 1
/
DacpacSqlConverter.cs
113 lines (79 loc) · 3.5 KB
/
DacpacSqlConverter.cs
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Management.Automation;
using System.Web.Http.Filters;
using Microsoft.SqlServer.Dac.Model;
using Microsoft.SqlServer.Dac;
namespace DacpacSqlConverter
{
[Cmdlet(VerbsCommon.Format, "DacpacAsSql")]
public class DacpacToSqlCmdlet : Cmdlet
{
public DacQueryScopes QueryScopes
{
get;
set;
}
// Directory for storing the output sql files
[Parameter(Mandatory = true)]
public string OutputDirectory { get; set; }
// The dacpac file being used
[Parameter(Mandatory = true)]
public string InputPath { get; set; }
public void Test()
{
ProcessRecord();
}
protected override void ProcessRecord()
{
string directory = OutputDirectory;
// get the data frm the dacpac and iterate through each file
using (TSqlModel modelFromDacpac = new TSqlModel(InputPath))
{
//list of objects not to be added to source control
string[] objectsNotIncluded = { "RoleMembership", "Synonym", "User", "Login" };
QueryScopes = DacQueryScopes.UserDefined;
IEnumerable<TSqlObject> allObjects = modelFromDacpac.GetObjects(QueryScopes);
//keep a list of all the file names -- if the name isn't there, we will delete the item
List<string> sqlFiles = new List<string>();
foreach (TSqlObject tsqlObject in allObjects)
{
sqlFiles.Add(tsqlObject.Name.ToString());
string script;
// if this is a tsql script, save it to the directory according to its type
if (tsqlObject.TryGetScript(out script) && !objectsNotIncluded.Contains(tsqlObject.ObjectType.Name))
{
Console.WriteLine("Adding " + directory + tsqlObject.ObjectType.Name +"/" + tsqlObject.Name.ToString());
saveToDirectory(directory + tsqlObject.ObjectType.Name, tsqlObject.Name.ToString(), script);
}
}
//get all the files in the directory if they are not in the file list, then delete them
string[] fileNames = Directory.GetFiles(OutputDirectory, "*.sql", SearchOption.AllDirectories);
sqlFiles = sqlFiles.OrderBy(e => e).ToList();
List<string> badItems = new List<string>();
foreach (string fileName in fileNames)
{
string dbObj = fileName.Split(Path.DirectorySeparatorChar).Last().Replace(".sql", string.Empty);
if (!sqlFiles.Contains(dbObj))
{
badItems.Add(dbObj);
Console.WriteLine("Deleting " + fileName);
File.Delete(fileName);
}
}
}
}
private void saveToDirectory(string pathName, string objName, string script)
{
Directory.CreateDirectory(pathName); // If the directory already exists, this method does nothing.
using (StreamWriter sw = new StreamWriter(string.Format("{0}/{1}.sql", pathName, objName)))
{
sw.Write(script);
sw.Flush();
}
}
}
}