internal Migration(string filePath, Regex commandSplitter)
        {
            var sql = File.ReadAllText(filePath, Encoding.GetEncoding("iso-8859-1"));

            SqlCommands = commandSplitter.Split(sql).Where(s => s.Trim().Length > 0).ToList();
            Hash        = GetHash(sql);
            Filename    = Path.GetFileName(filePath);

            UseTransaction = !sql.StartsWith(NoTransactionToken);

            // check if the migration includes another file
            IList <string> includedRelativeFilepaths = null;

            try
            {
                includedRelativeFilepaths = GetRelativeIncludedFilepaths(sql);
            }
            catch (Exception ex)
            {
                throw new Exception($"Error processing the migration '{Filename}'", ex);
            }

            if (includedRelativeFilepaths.Count > 0)
            {
                SqlCommands.Clear();
                var fileDirectory = Path.GetFullPath(Path.GetDirectoryName(filePath));

                foreach (var includedRelativeFilepath in includedRelativeFilepaths)
                {
                    IncludedFilepath = Path.GetFullPath(Path.Combine(fileDirectory, includedRelativeFilepath));
                    // check if the included file is under the migration's folder
                    if (!CheckIncludedFilepath(fileDirectory, IncludedFilepath))
                    {
                        throw new Exception(
                                  $"The migration '{Filename}' tries to include a file that is not relative to the migration's " +
                                  $"folder: '{IncludedFilepath}'");
                    }

                    // read the included file
                    if (File.Exists(IncludedFilepath))
                    {
                        sql = File.ReadAllText(IncludedFilepath, Encoding.GetEncoding("iso-8859-1"));
                        SqlCommands.AddRange(commandSplitter.Split(sql).Where(s => s.Trim().Length > 0));
                    }
                    else
                    {
                        throw new Exception(
                                  $"The migration '{Filename}' tries to include a file that does not exist: {IncludedFilepath}");
                    }
                }
            }
        }
Exemple #2
0
        public override int SaveTable(DataTable dataTable, string sql)
        {
            //Logger.WriteLine("Save Table with transaction");
            Performance perf = new Performance();

            Logger.WriteLine("Saving " + dataTable.TableName);
            //Logger.WriteLine(sql);
            DataSet myDataSet = new DataSet();

            myDataSet.Tables.Add(dataTable.TableName);

            NpgsqlConnection     conn            = new NpgsqlConnection(ConnectionString);
            NpgsqlCommand        myAccessCommand = new NpgsqlCommand(sql, conn);
            NpgsqlDataAdapter    da = new NpgsqlDataAdapter(myAccessCommand);
            NpgsqlCommandBuilder cb = new NpgsqlCommandBuilder(da);

            da.UpdateCommand = cb.GetUpdateCommand();
            da.InsertCommand = cb.GetInsertCommand();
            da.DeleteCommand = cb.GetDeleteCommand();

            cb.ConflictOption = ConflictOption.OverwriteChanges; // this fixes System.InvalidCastException : Specified cast is not valid.
                                                                 // when reserved word  (group) was a column name

            if (MapToLowerCase)
            {
                var map = da.TableMappings.Add(dataTable.TableName.ToLower(), dataTable.TableName);
                for (int i = 0; i < dataTable.Columns.Count; i++)
                {
                    var cn = dataTable.Columns[i].ColumnName;
                    map.ColumnMappings.Add(cn.ToLower(), cn);
                }
                //PrintMapping(da);
            }

            SqlCommands.Add(sql);
            int recordCount = 0;

            //da.RowUpdating += myDataAdapter_RowUpdating;

            try
            {
                conn.Open();
                var dbTrans = conn.BeginTransaction();
                da.Fill(myDataSet, dataTable.TableName);

                recordCount = da.Update(dataTable);
                dbTrans.Commit();
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }

            Logger.WriteLine("Saved " + recordCount + " records in " + perf.ElapsedSeconds + "seconds");
            if (SqlCommands.Count > 5000)
            {
                SqlCommands.Clear();
            }
            return(recordCount);
        }