module Cassie::Schema::Versioning
Public Instance Methods
The versions that have been migrated up for the Cassandra database This lists the versions stored in the persistence layer, in reverse chronological order (newest first). @return [Enumerable<Version>]
# File lib/cassie/schema/versioning.rb, line 28 def applied_versions @applied_versions ||= load_applied_versions end
Remove the version from the schema version store. This should only be done if the version has been sucesfully reverted @param [Version] version A persisted version @return [Boolean] whether succesfull or not
# File lib/cassie/schema/versioning.rb, line 70 def forget_version(version) DeleteVersionQuery.new(id: version.id).execute @applied_versions = nil end
Create the keyspace and table for tracking schema versions in the Cassandra database if they don't already exist @return [void]
# File lib/cassie/schema/versioning.rb, line 35 def initialize_versioning create_schema_keyspace unless schema_keyspace_exists? create_versions_table unless versions_table_exists? end
Versions for the {#migration_files} If a migration is applied versions, the object for that version will be the applied version, containing the full information about the applied version @return [Enumeration<Version>]
# File lib/cassie/schema/versioning.rb, line 86 def local_versions @local_versions ||= load_local_versions end
Absolute paths to the migration files in the migration directory @return [Array<String>]
# File lib/cassie/schema/versioning.rb, line 77 def migration_files Dir[root.join(paths[:migrations_directory], "[0-9]*_*.rb")] end
A version with an incremented version number that would be applied after the latest (local or applied) migration. @param [Symbol, nil] bump_type Which semantic version to bump @option bump_type [Symbol] :build Bump the build version @option bump_type [Symbol] :patch Bump the patch version, set build to 0 @option bump_type [Symbol] :minor Bump the minor version, set patch and build to 0 @option bump_type [Symbol] :major Bump the major version, set minor, patch, and build to 0 @option bump_type [nil] nil Default, bumps patch, sets build to 0 @return [Version] The initialized, bumped version
# File lib/cassie/schema/versioning.rb, line 99 def next_version(bump_type=nil) local_max = local_versions.max || Version.new('0') applied_max = applied_versions.max || Version.new('0') max_version = [local_max, applied_max].max max_version.next(bump_type) end
Record a version in the schema version store. This should only be done if the version has been sucesfully migrated @param [Version] The version to record @param [Boolean] set_execution_metadata Determines whether or not to populate
the version object with execution tracking info (+id+, +executed_at+, and +executor+).
@return [Boolean] whether succesfull or not @raises [StandardError] if version could not be recorded. If this happens,
execution_metadata will not be preset on the version object.
# File lib/cassie/schema/versioning.rb, line 48 def record_version(version, set_execution_metadata=true) time = Time.now version.id ||= Cassandra::TimeUuid::Generator.new.at(time) if set_execution_metadata version.executed_at = time version.executor = Etc.getlogin rescue '<unknown>' end InsertVersionQuery.new(version: version).execute! @applied_versions = nil rescue StandardError => e version.id = nil version.executed_at = nil version.executor = nil raise e end
The current schema version @return [Version]
# File lib/cassie/schema/versioning.rb, line 18 def version SelectVersionsQuery.new.fetch_first || Version.new('0') rescue Cassandra::Errors::InvalidError raise uninitialized_error end
Protected Instance Methods
# File lib/cassie/schema/versioning.rb, line 145 def create_schema_keyspace CreateKeyspaceQuery.new(name: Cassie::Schema.schema_keyspace).execute end
# File lib/cassie/schema/versioning.rb, line 149 def create_versions_table CreateVersionsTableQuery.new.execute end
# File lib/cassie/schema/versioning.rb, line 129 def database_versions SelectVersionsQuery.new.fetch end
# File lib/cassie/schema/versioning.rb, line 108 def default_version '0.0.1.0' end
load version migration class from disk
# File lib/cassie/schema/versioning.rb, line 121 def load_applied_versions database_versions.tap do |versions| versions.each{|v| VersionObjectLoader.new(v).load } end rescue Cassandra::Errors::InvalidError => e raise uninitialized_error end
# File lib/cassie/schema/versioning.rb, line 133 def load_local_versions migration_files.map do |filename| VersionFileLoader.new(filename).load end.sort! end
# File lib/cassie/schema/versioning.rb, line 161 def qualified_table_name "#{schema_keyspace}.#{versions_table}" end
# File lib/cassie/schema/versioning.rb, line 112 def schema_keyspace_exists? Cassie.keyspace_exists?(Cassie::Schema.schema_keyspace) end
# File lib/cassie/schema/versioning.rb, line 153 def uninitialized_error UninitializedError.new(uninitialized_message) end
# File lib/cassie/schema/versioning.rb, line 157 def uninitialized_message "Cassie Schema Versions table not found at '#{qualified_table_name}'. Enable versioned migration support with `cassie schema:init`." end
# File lib/cassie/schema/versioning.rb, line 139 def version_exists? !!Cassie::Schema.version rescue Cassie::Schema::UninitializedError false end
# File lib/cassie/schema/versioning.rb, line 116 def versions_table_exists? Cassie.table_exists?(qualified_table_name) end