-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
363 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
module ClickhouseActiverecord | ||
|
||
class SchemaMigration < ::ActiveRecord::SchemaMigration | ||
class << self | ||
|
||
def create_table | ||
unless table_exists? | ||
version_options = connection.internal_string_options_for_primary_key | ||
|
||
connection.create_table(table_name, id: false, options: 'ReplacingMergeTree(ver) PARTITION BY version ORDER BY (version)') do |t| | ||
t.string :version, version_options | ||
t.column :active, 'Int8', null: false, default: '1' | ||
t.datetime :ver, null: false, default: -> { 'now()' } | ||
end | ||
end | ||
end | ||
|
||
def all_versions | ||
from("#{table_name} FINAL").where(active: 1).order(:version).pluck(:version) | ||
end | ||
end | ||
end | ||
|
||
class InternalMetadata < ::ActiveRecord::InternalMetadata | ||
class << self | ||
def create_table | ||
unless table_exists? | ||
key_options = connection.internal_string_options_for_primary_key | ||
|
||
connection.create_table(table_name, id: false, options: 'MergeTree() PARTITION BY toDate(created_at) ORDER BY (created_at)') do |t| | ||
t.string :key, key_options | ||
t.string :value | ||
t.timestamps | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
class MigrationContext < ::ActiveRecord::MigrationContext #:nodoc: | ||
attr_reader :migrations_paths, :schema_migration | ||
|
||
def initialize(migrations_paths, schema_migration) | ||
@migrations_paths = migrations_paths | ||
@schema_migration = schema_migration | ||
end | ||
|
||
def down(target_version = nil) | ||
selected_migrations = if block_given? | ||
migrations.select { |m| yield m } | ||
else | ||
migrations | ||
end | ||
|
||
ClickhouseActiverecord::Migrator.new(:down, selected_migrations, schema_migration, target_version).migrate | ||
end | ||
|
||
def get_all_versions | ||
if schema_migration.table_exists? | ||
schema_migration.all_versions.map(&:to_i) | ||
else | ||
[] | ||
end | ||
end | ||
|
||
end | ||
|
||
class Migrator < ::ActiveRecord::Migrator | ||
|
||
def initialize(direction, migrations, schema_migration, target_version = nil) | ||
@direction = direction | ||
@target_version = target_version | ||
@migrated_versions = nil | ||
@migrations = migrations | ||
@schema_migration = schema_migration | ||
|
||
validate(@migrations) | ||
|
||
@schema_migration.create_table | ||
ClickhouseActiverecord::InternalMetadata.create_table | ||
end | ||
|
||
def record_version_state_after_migrating(version) | ||
if down? | ||
migrated.delete(version) | ||
@schema_migration.create!(version: version.to_s, active: 0) | ||
else | ||
super | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.