Ejemplo n.º 1
0
        public void Migrate(string connectionString, MigrationContext migrationContext)
        {
            var sw = Stopwatch.StartNew();

            _announcer.Heading("Migrating " + connectionString);

            var assembly = Assembly.GetExecutingAssembly();

            var runnerContext = new RunnerContext(_announcer)
            {
                Namespace = "NzbDrone.Core.Datastore.Migration",
                ApplicationContext = migrationContext
            };

            var options = new MigrationOptions { PreviewOnly = false, Timeout = 60 };
            var factory = new NzbDroneSqliteProcessorFactory();
            var processor = factory.Create(connectionString, _announcer, options);
            var runner = new MigrationRunner(assembly, runnerContext, processor);

            if (migrationContext.DesiredVersion.HasValue)
            {
                runner.MigrateUp(migrationContext.DesiredVersion.Value, true);
            }
            else
            {
                runner.MigrateUp(true);
            }

            sw.Stop();

            _announcer.ElapsedTime(sw.Elapsed);
        }
Ejemplo n.º 2
0
        public IDatabase Create(MigrationContext migrationContext)
        {
            string connectionString;

            switch (migrationContext.MigrationType)
            {
                case MigrationType.Main:
                    {
                        connectionString = _connectionStringFactory.MainDbConnectionString;
                        break;
                    }
                case MigrationType.Log:
                    {
                        connectionString = _connectionStringFactory.LogDbConnectionString;
                        break;
                    }
                default:
                    {
                        throw new ArgumentException("Invalid MigrationType");
                    }
            }

            _migrationController.Migrate(connectionString, migrationContext);

            var db = new Database(migrationContext.MigrationType.ToString(), () =>
                {
                    var dataMapper = new DataMapper(SQLiteFactory.Instance, connectionString)
                    {
                        SqlMode = SqlModes.Text,
                    };

                    return dataMapper;
                });

            return db;
        }
Ejemplo n.º 3
0
        public IDatabase Create(MigrationContext migrationContext)
        {
            string connectionString;

            switch (migrationContext.MigrationType)
            {
                case MigrationType.Main:
                    {
                        connectionString = _connectionStringFactory.MainDbConnectionString;
                        break;
                    }
                case MigrationType.Log:
                    {
                        connectionString = _connectionStringFactory.LogDbConnectionString;
                        break;
                    }
                default:
                    {
                        throw new ArgumentException("Invalid MigrationType");
                    }
            }

            try
            {
                _migrationController.Migrate(connectionString, migrationContext);
            }
            catch (SQLiteException ex)
            {
                var fileName = _connectionStringFactory.GetDatabasePath(connectionString);

                if (migrationContext.MigrationType == MigrationType.Log)
                {
                    Logger.Error(ex, "Logging database is corrupt, attempting to recreate it automatically");

                    try
                    {
                        _diskProvider.DeleteFile(fileName + "-shm");
                        _diskProvider.DeleteFile(fileName + "-wal");
                        _diskProvider.DeleteFile(fileName + "-journal");
                        _diskProvider.DeleteFile(fileName);
                    }
                    catch (Exception)
                    {
                        Logger.Error("Unable to recreate logging database automatically. It will need to be removed manually.");
                    }

                    _migrationController.Migrate(connectionString, migrationContext);
                }

                else
                {
                    if (OsInfo.IsOsx)
                    {
                        throw new CorruptDatabaseException("Database file: {0} is corrupt, restore from backup if available. See: https://github.com/Sonarr/Sonarr/wiki/FAQ#i-use-sonarr-on-a-mac-and-it-suddenly-stopped-working-what-happened", ex, fileName);
                    }

                    throw new CorruptDatabaseException("Database file: {0} is corrupt, restore from backup if available. See: https://github.com/Sonarr/Sonarr/wiki/FAQ#i-am-getting-an-error-database-disk-image-is-malformed", ex, fileName);
                }
            }

            var db = new Database(migrationContext.MigrationType.ToString(), () =>
                {
                    var dataMapper = new DataMapper(SQLiteFactory.Instance, connectionString)
                    {
                        SqlMode = SqlModes.Text,
                    };

                    return dataMapper;
                });

            return db;
        }