Blacker.MangaScraper.Library.SQLite.SchemaManager.CheckSchema C# (CSharp) Méthode

CheckSchema() public méthode

This method checks whether the database uses latest schema and if it doesn't it will automatically initiate schema upgrade.
public CheckSchema ( SQLiteConnection connection ) : void
connection System.Data.SQLite.SQLiteConnection Connection to the SQLite database
Résultat void
        public void CheckSchema(SQLiteConnection connection)
        {
            long version = GetSchemaVersion(connection);

            if (version == SchemaVersion) // database uses current schema version bail
            {
                return;
            }

            if (version > SchemaVersion)
            {
                throw new InvalidOperationException(
                    String.Format("Unsupported schema version. Database uses newer schema ({0}) than application ({1}).", version, SchemaVersion));
            }

            lock (_syncRoot)
            {
                // reload the schema version to make sure that the database schema was not already updated
                version = GetSchemaVersion(connection);

                // database has not yet been initialized
                if (version == 0)
                {
                    InitDatabase(connection);

                    // since we have created new database we don't need to execute any migration logic
                    return;
                }

                string backupName = String.Format("backup-v{0}-{1}.sqlite", version, DateTime.Now.ToString("yyyyMMdd-HHmmssFF"));

                if (!BackupDatabase(connection, backupName))
                {
                    _log.Warn("Unable to perform backup. Will continue with upgrade.");
                }

                IDictionary<long, ISchemaMigrator> migrators =
                    ReflectionHelper.GetInstances<ISchemaMigrator>(this.GetType().Assembly, Enumerable.Empty<Type>())
                                    .ToDictionary(sm => sm.FromVersion);

                int loopCount = 0; // this will ensure that we will not end up looping forever
                do
                {
                    ISchemaMigrator migrator;

                    migrators.TryGetValue(version, out migrator);

                    if (migrator != null)
                    {
                        migrator.Migrate(connection);
                    }
                    else
                    {
                        System.Diagnostics.Debug.Assert(false, "Migrator for this version of schema not found.");

                        _log.WarnFormat("Migrator from database schema version {0} was not found.", version);
                    }

                    version = GetSchemaVersion(connection);
                } while (version < SchemaVersion && ++loopCount < SchemaVersion);
            }
        }

Usage Example

        protected SQLiteConnection GetConnection()
        {
            try
            {
                var dir = Path.GetDirectoryName(Configuration.LibraryConfiguration.Instance.StoragePath);

                if (String.IsNullOrEmpty(dir))
                {
                    throw new InvalidOperationException("Invalid path to the database.");
                }

                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }

                var connection = new SQLiteConnection(ConnectionString);
                connection.Open();

                _schemaManager.CheckSchema(connection);

                return(connection);
            }
            catch (Exception ex)
            {
                _log.Error("Unable to establish proper connection to database.", ex);

                throw new StorageException("Unable to establish proper connection to database.", ex);
            }
        }