Exemplo n.º 1
0
        public virtual void Install(Guid modelKey, string connectionString)
        {
            if (this.Options == null)
            {
                throw new Exception("The 'Options' parameter cannot be null.");
            }

            this.ConnectionString = connectionString;

            //Ensure that there is a database
            using (var context = new DbManagementContext(this.Options))
            {
                context.Database.EnsureCreated();

                var version = context.VersionModel.FirstOrDefault(x => x.ModelKey == modelKey);
                if (version != null)
                {
                    this.CurrentVersion = new Version(version.Version);
                }
                this.LastestVersion = this.CurrentVersion;
            }

            //Run all scripts
            this.RunScripts();

            //Update management tables
            using (var context = new DbManagementContext(this.Options))
            {
                var version = context.VersionModel.FirstOrDefault(x => x.ModelKey == modelKey);
                if (version == null)
                {
                    version = new VersionModel {
                        ModelKey = modelKey
                    };
                    context.Add(version);
                }
                version.LastUpdated = DateTime.Now;
                version.Version     = this.LastestVersion.ToString();
                context.SaveChanges();
            }
        }
Exemplo n.º 2
0
        protected virtual void RunScripts()
        {
            var scripts = GetScripts(this.Assembly);

            var newList = new List <string>();

            newList.AddRange(scripts.Where(x => x.Contains(Folder1)).OrderBy(x => x));

            //Only find those that are versioned. TODO: If any others then error
            var migrations = new Dictionary <Version, string>();

            foreach (var script in scripts.Where(x => x.Contains(Folder2)))
            {
                if (script.Contains(Folder2, StringComparison.CurrentCultureIgnoreCase))
                {
                    var index = script.IndexOf(Folder2, StringComparison.CurrentCultureIgnoreCase);
                    if (index != -1)
                    {
                        var fragment = script.Substring(index + Folder2.Length);
                        if (fragment.EndsWith(".sql"))
                        {
                            fragment = fragment.Substring(0, fragment.Length - 4);
                            if (Version.TryParse(fragment, out Version v))
                            {
                                migrations.Add(v, script);
                            }
                        }
                    }
                }
            }

            //Sort migration scripts
            foreach (var migrationKey in migrations.Keys.OrderBy(x => x))
            {
                newList.Add(migrations[migrationKey]);
                if (this.LastestVersion < migrationKey)
                {
                    this.LastestVersion = migrationKey;
                }
            }

            newList.AddRange(scripts.Where(x => x.Contains(Folder3)).OrderBy(x => x));
            newList.AddRange(scripts.Where(x => x.Contains(Folder4)).OrderBy(x => x));
            newList.AddRange(scripts.Where(x => x.Contains(Folder5)).OrderBy(x => x));

            //TODO: Transaction
            using (var context = new DbManagementContext(this.Options))
            {
                foreach (var resourceFileName in newList)
                {
                    var sql            = string.Empty;
                    var manifestStream = this.Assembly.GetManifestResourceStream(resourceFileName);
                    using (var sr = new System.IO.StreamReader(manifestStream))
                    {
                        sql = sr.ReadToEnd();
                    }

                    this.ExecuteScript(sql, context);

                    var hash    = sql.GetHash();
                    var logItem = context.VersionObject.FirstOrDefault(x => x.Hash == hash);
                    if (logItem == null)
                    {
                        logItem = new VersionObject {
                            Hash = hash
                        };
                        context.Add(logItem);
                    }

                    logItem.Name         = resourceFileName;
                    logItem.CreatedDate  = DateTime.Now;
                    logItem.ModifiedDate = logItem.CreatedDate;
                }

                context.SaveChanges();
            }
        }