ReadDbSchema() public method

public ReadDbSchema ( ) : IList
return IList
        static void Main(string[] args)
        {
            const string usage = @"Fluent Migrator Schema Dumper
  Usage:
    SchemaDumper.exe --connection CONNECTION [--file FILE] [--verbose] [--show] [--open]
    SchemaDumper.exe --version
    SchemaDumper.exe --help

  Options:
    --connection CONNECTION -c CONNECTION    The connection string. Required.
    --file FILE -f FILE                      File to output. Optional. [default: schemaDump.cs]
    --show -s                                Show output. Optional.
    --open -o                                Open file. Optional.
    --verbose                                Verbose. Optional.
    --help -h                                Show this screen.
    --version -v                             Show version.
";

            var arguments = new Docopt().Apply(usage, args, version: Assembly.GetExecutingAssembly().GetName().Version, exit: true);
            var file = arguments["--file"].ToString();
            var verbose = arguments["--verbose"].IsTrue;
            var open = arguments["--open"].IsTrue;
            var show = arguments["--show"].IsTrue;
            if (!Path.IsPathRooted(file)) file = Path.Combine(Environment.CurrentDirectory, file);
            var connectionString = arguments["--connection"].ToString();
            if (verbose) WriteLine($"Saving to {file}.");
            try { var builder = new SqlConnectionStringBuilder(connectionString); }
            catch (ArgumentException)
            {
                WriteLine("Connection string is in incorrect format.");
                return;
            }
            using (var connection = new SqlConnection(connectionString))
            {
                try { connection.Open(); }
                catch (SqlException ex)
                {
                    WriteLine($"Connection couldn't be established:\n{ex.Message}");
                    return;
                }
                var consoleAnnouncer = new ConsoleAnnouncer();
                var dumper = new SqlServerSchemaDumper(new SqlServerProcessor(connection, new SqlServer2000Generator(), consoleAnnouncer, new ProcessorOptions(), new SqlServerDbFactory()), consoleAnnouncer);
                var tables = dumper.ReadDbSchema();
                var writer = new RCDumpWriter();
                writer.WriteToFile(tables, file);
            }
            if (show) WriteLine(File.ReadAllText(file));
            if (open) try { Process.Start(file); } catch { }
            if (verbose) WriteLine("Done.");
        }
Ejemplo n.º 2
0
        public static void Main(string[] args)
        {
            var connection = new SqlConnection("data source=172.16.1.186;UID=lssuser;PWD=password1;initial catalog=lss");

            var sb = new StringBuilder();
            var textWriter = new StringWriter(sb);
            var announcer = new TextWriterAnnouncer(textWriter);

            var generator = new SqlServer2008Generator();
            var processor = new SqlServerProcessor(connection, generator, announcer, new ProcessorOptions(), new SqlServerDbFactory());

            var dumper = new SqlServerSchemaDumper(processor, announcer);

            var schema = dumper.ReadDbSchema();
            var schemaWriter = new SchemaWriter();
            schemaWriter.WriteToFile(schema, "C:\\migration.cs");
        }
        /// <summary>
        /// Creates a single column table using the spplied type and retruns its <see cref="ColumnDefinition"/>
        /// </summary>
        /// <param name="type">The Sql Server data type to apply to the column</param>
        /// <returns>The translated <see cref="ColumnDefinition"/></returns>
        private TableDefinition GetTableColumnColumns(string createSql, string name, params IMigrationExpression[] expresions)
        {
            IList<TableDefinition> tables;

              // Act
              using (var connection = new SqlConnection(ConnectionString))
              {
              var processor = new SqlServerProcessor(connection, new SqlServer2005Generator(), new DebugAnnouncer(), new ProcessorOptions());

              if (!string.IsNullOrEmpty(createSql))
                  processor.Execute(createSql);

              foreach (var expresion in expresions)
              {
                  if (expresion is CreateTableExpression)
                      processor.Process((CreateTableExpression)expresion);
                  if (expresion is CreateIndexExpression)
                      processor.Process((CreateIndexExpression)expresion);
                  if (expresion is CreateForeignKeyExpression)
                      processor.Process((CreateForeignKeyExpression)expresion);
              }

              Assert.IsTrue(processor.TableExists(string.Empty, name), "SqlServer");

              var dumper = new SqlServerSchemaDumper(processor, new DebugAnnouncer());
              tables = dumper.ReadDbSchema();

              processor.CommitTransaction();
              }

              if (!string.IsNullOrEmpty(createSql))
              tables.Count.ShouldBe(1);

              return tables.Where(t => t.Name == name).FirstOrDefault();
        }