protected bool AddColumn(DatabaseConnection connection, string table, string columnName, string type, List <string> existingColumnNames)
        {
            if (existingColumnNames.Contains(columnName, StringComparer.OrdinalIgnoreCase))
            {
                return(false);
            }

            connection.Execute("alter table " + table + " add column " + columnName + " " + type + " NULL");
            return(true);
        }
Example #2
0
        public void UpdateVersion(DatabaseConnection connection, bool dbJustCreated)
        {
            if (dbJustCreated)
            {
                connection.Execute($"PRAGMA user_version = {DbVersion};");
                return;
            }

            int version;

            using (var versionStatement = connection.PrepareStatement("PRAGMA user_version;"))
            {
                version = versionStatement.ExecuteQuery().FirstOrDefault().GetInt(0);
            }

            if (version >= DbVersion)
            {
                return;
            }

            _logger.LogInformation($"InfuseSync: DB version {version} is outdated. Will migrate to version {DbVersion}.");

            for (var migrateVersion = version; migrateVersion < DbVersion; ++migrateVersion)
            {
                var migration = migrations[migrateVersion];
                if (migration != null)
                {
                    _logger.LogInformation($"InfuseSync: Performing migration for DB version {migrateVersion}.");
                    migration.Migrate(connection);
                }
                else
                {
                    _logger.LogInformation($"InfuseSync: Migration not found for DB version {migrateVersion}.");
                }
            }

            connection.Execute($"PRAGMA user_version = {DbVersion};");

            _logger.LogInformation($"InfuseSync: DB migration finished.");
        }