Пример #1
0
        public static void SeedInTransaction(this SqliteConnection db, IConfiguration configSeed,
                                             SeedStrategy strategy = SeedStrategy.InsertIfNotExists)
        {
            var t = db.BeginTransaction();

            switch (strategy)
            {
            case SeedStrategy.InsertIfNotExists:
                InsertIfNotExists();
                break;

            case SeedStrategy.Initialize:
                var count = db.ExecuteScalar <int>("SELECT COUNT(1) FROM 'Configuration'");
                if (count == 0)
                {
                    InsertIfNotExists();
                }
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            t.Commit();

            void InsertIfNotExists()
            {
                foreach (var entry in configSeed.AsEnumerable())
                {
                    db.Execute("INSERT OR IGNORE INTO 'Configuration' ('Key', 'Value') VALUES (@Key, @Value)",
                               new { entry.Key, entry.Value }, t);
                }
            }
        }
Пример #2
0
 public SqliteConfigurationSource(string dataFilePath, SaveConfigurationOptions saveConfig,
                                  IConfiguration configSeed = null, SeedStrategy strategy = SeedStrategy.Initialize)
 {
     SaveConfig        = saveConfig;
     ConfigSeed        = configSeed;
     DataFilePath      = dataFilePath;
     DataDirectoryPath = new FileInfo(DataFilePath).Directory?.FullName;
     DataFileName      = Path.GetFileName(DataFilePath);
     SeedStrategy      = strategy;
 }
Пример #3
0
        public static void MigrateToLatest(string dataFilePath, SaveConfigurationOptions saveConfig,
                                           IConfiguration configSeed = null, SeedStrategy strategy = SeedStrategy.InsertIfNotExists)
        {
            try
            {
                var connectionString = $"Data Source={dataFilePath}";

                if (saveConfig.CreateIfNotExists)
                {
                    CreateIfNotExists(connectionString);
                }

                var builder = new SqliteConnectionStringBuilder(connectionString)
                {
                    Mode = SqliteOpenMode.ReadWrite
                };
                using (var db = new SqliteConnection(builder.ConnectionString))
                {
                    db.Open();

                    if (saveConfig.MigrateOnStartup)
                    {
                        MigrateUp(db);
                    }

                    if (configSeed != null)
                    {
                        db.SeedInTransaction(configSeed, strategy);
                    }
                }
            }
            catch (SqliteException e)
            {
                Trace.TraceError("Error migrating configuration database: {0}", e);
                throw;
            }
        }
        public static Container MigrateToLatest(string slot, CosmosStorageOptions options,
                                                IConfiguration configSeed = null, SeedStrategy strategy = SeedStrategy.InsertIfNotExists)
        {
            var optionsMonitor = new OptionsMonitorShim <CosmosStorageOptions>(options);

            var runner = new CosmosMigrationRunner(slot, optionsMonitor);

            var container = runner.CreateContainerIfNotExistsAsync().GetAwaiter().GetResult();

            if (configSeed != null && strategy != SeedStrategy.None)
            {
                var repository = new CosmosRepository(slot, container, optionsMonitor, null);

                switch (strategy)
                {
                case SeedStrategy.InsertIfNotExists:
                {
                    InsertIfNotExists(repository);
                    break;
                }

                case SeedStrategy.Initialize:
                {
                    var count = repository.CountAsync <ConfigurationDocument>().GetAwaiter().GetResult();
                    if (count == 0)
                    {
                        InsertIfNotExists(repository);
                    }

                    break;
                }

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            void InsertIfNotExists(ICosmosRepository repository)
            {
                var manifest = repository.RetrieveAsync <ConfigurationDocument>()
                               .GetAwaiter().GetResult().Select(x => x.Key).ToImmutableHashSet();

                var changedKeys = new HashSet <string>();

                foreach (var(k, v) in configSeed.AsEnumerable())
                {
                    if (manifest.Contains(k))
                    {
                        continue;
                    }

                    repository.CreateAsync(new ConfigurationDocument {
                        Key = k, Value = v
                    })
                    .GetAwaiter().GetResult();

                    changedKeys.Add(k);
                }

                Trace.TraceInformation(changedKeys.Count > 0
                                        ? $"Configuration updated the following keys: {string.Join(",", changedKeys)}"
                                        : "Configuration is up to date.");
            }

            return(container);
        }