Exemple #1
0
		public static void Main(string[] args)
		{

			// Initialize Log4Net
			log4net.Config.XmlConfigurator.Configure();
			
			// Get the schema files
			string createSchemaFile = ConfigurationManager.AppSettings["schema.create.file"];
			string dropSchemaFile = ConfigurationManager.AppSettings["schema.drop.file"];
			if (createSchemaFile == null || createSchemaFile.Trim().Length == 0)
			{
				createSchemaFile = "schema-create.sql";
			}
			if (dropSchemaFile == null || dropSchemaFile.Trim().Length == 0)
			{
				dropSchemaFile = "schema-drop.sql";
			}

			// Show usage information
			string usage = 
@"Usage:

	SchemaExport [-o|-e] 
		-o = outputs DDL to the console
		-e = exports schema to the database
	The following example will display the DDL, AND change the database schema:
		SchemaExport -o -e
	The following example will display the DDL, but NOT change the database schema:
		SchemaExport -o        
";

			string msg = String.Empty;

			// Initialize parameters for SchemaExport
			bool outputSchemaScriipt = false;
			bool exportToDatabase = false;

			foreach (string arg in args)
			{
				switch (arg.ToLower().Trim())
				{
					case "-o":
						outputSchemaScriipt = true;
						break;
					case "-e":
						exportToDatabase = true;
						break;
				}
			}

			//They need to enter one of the switches, otherwise we'll kick them out.
			if (!exportToDatabase && !outputSchemaScriipt)
			{
				Console.WriteLine(usage);
				return;
			}

			string outScript = String.Empty;

			if (exportToDatabase)
			{
				try
				{
					using (DbSupport dbSupport = new DbSupport())
					{
		 					outScript = dbSupport.RegenerateDatabase(createSchemaFile, dropSchemaFile);
					}

					TestDataManager.InsertTestData();

					Console.WriteLine("SchemaExport: Export successful.  Schema create and drop files saved to '{0}' and '{1}'.", createSchemaFile, dropSchemaFile);
				}
				catch (Exception e)
				{
					Console.WriteLine("SchemaExport: Export failed (" + e.Message + ")\n");
					throw (e);
				}
			}	
			else
			{
				// Get the create and drop scripts so we can output them to the console
				using (DbSupport dbSupport = new DbSupport())
				{
					string[] dropSql = dbSupport.GenerateDropSQL();
					string[] createSql = dbSupport.GenerateCreateSQL();
					outScript += String.Join(Environment.NewLine, dropSql);
					outScript += String.Join(Environment.NewLine, createSql);

                    File.WriteAllLines(createSchemaFile, createSql);
                    File.WriteAllLines(dropSchemaFile, dropSql);
				}
			}

			if (outputSchemaScriipt)
			{
				Console.WriteLine(outScript);
			}
		}
Exemple #2
0
        public static void Main(string[] args)
        {
            string createDDL = ConfigurationSettings.AppSettings.Get("schema.create.file");
            string dropDDL = ConfigurationSettings.AppSettings.Get("schema.drop.file");

            string msg = String.Empty;

            // Show usage information
            string usage = @"Usage:
            SchemaExport [-o|-e]
            -o = outputs DDL to the console
            -e = exports schema to the database
            The following example will display the DDL, AND change the database schema:
            SchemaExport -o -e
            The following example will display the DDL, but NOT change the database schema:
            SchemaExport -o

            ";

            // Initialize Log4Net
            log4net.Config.XmlConfigurator.Configure();

            // Initialize parameters for SchemaExport
            bool script = false;
            bool export = false;

            foreach (string arg in args)
            {
                switch (arg)
                {
                    case "-o":
                        script = true;
                        break;
                    case "-e":
                        export = true;
                        break;
                }
            }

            //They need to enter one of the switches, otherwise we'll kick them out.
            if (!export && !script)
            {
                Console.WriteLine(usage);
                return;
            }

            string outScript = String.Empty;
            DbSupport dbSupport = new DbSupport();
            if (export)
            {
                if (createDDL == null || createDDL.Trim().Length == 0)
                    createDDL = "schema-create.sql";
                if (dropDDL == null || dropDDL.Trim().Length == 0)
                    dropDDL = "schema-drop.sql";

                //Use the supplied file name to create a FileInfo object to pass to
                //the DbSupport method.
                FileInfo createFile = new FileInfo(createDDL);
                FileInfo deleteFile = new FileInfo(dropDDL);

                try
                {
                    outScript = dbSupport.RegenerateDatabase(createFile, deleteFile);

                    Console.WriteLine("SchemaExport COMPLETED SUCCESSFULLY!");
                    msg = "\r\n\r\nThe new tables will be in the database defined by the 'hibernate.connection.connection_string' property in the nhibernate.config file.\r\n\r\nAlso, SQL scripts containing the commands required to create and remove all the tables for this version of the schema were saved in the '{0}' and '{1}' files.";
                    Console.WriteLine(String.Format(msg, createFile.FullName, deleteFile.FullName));
                }
                catch (Exception)
                {
                    Console.WriteLine("SchemaExport ENCOUNTERED PROBLEMS!");
                    msg = "\r\n\r\nIn some cases, this will be a non-issue. Look back through the errors generated by SchemaExport (in the log file) to determine the specific failure. You may need to manually drop the tables and rerun this program to have the new schema fully generated.\r\n\r\nA SQL script containing the commands required to remove all the tables added by this version of the schema was saved in the {0} file.";
                    Console.WriteLine(String.Format(msg, deleteFile.FullName));
                }
            }
            else
            {
                //we still need the delete and create scripts to send to the console.
                string[] dropSql = dbSupport.GenerateDropSQL();
                string[] createSql = dbSupport.GenerateCreateSQL();
                outScript += String.Join(Environment.NewLine, dropSql);
                outScript += String.Join(Environment.NewLine, createSql);
            }

            if (script)
                Console.WriteLine(outScript);

            dbSupport.Dispose();
            dbSupport = null;
        }