-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
database create / drop, migration support with rake tasks
- Loading branch information
Showing
9 changed files
with
193 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# frozen_string_literal: true | ||
|
||
module ClickhouseActiverecord | ||
class Tasks | ||
|
||
delegate :connection, :establish_connection, :clear_active_connections!, to: ActiveRecord::Base | ||
|
||
def initialize(configuration) | ||
@configuration = configuration | ||
end | ||
|
||
def create | ||
establish_master_connection | ||
connection.create_database @configuration["database"] | ||
rescue ActiveRecord::StatementInvalid => e | ||
if e.cause.to_s.include?('already exists') | ||
raise ActiveRecord::Tasks::DatabaseAlreadyExists | ||
else | ||
raise | ||
end | ||
end | ||
|
||
def drop | ||
establish_master_connection | ||
connection.drop_database @configuration["database"] | ||
end | ||
|
||
def purge | ||
clear_active_connections! | ||
drop | ||
create | ||
end | ||
|
||
def migrate | ||
check_target_version | ||
|
||
verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] != "false" : true | ||
scope = ENV["SCOPE"] | ||
verbose_was, ActiveRecord::Migration.verbose = ActiveRecord::Migration.verbose, verbose | ||
binding.pry | ||
connection.migration_context.migrate(target_version) do |migration| | ||
scope.blank? || scope == migration.scope | ||
end | ||
ActiveRecord::Base.clear_cache! | ||
ensure | ||
ActiveRecord::Migration.verbose = verbose_was | ||
end | ||
|
||
private | ||
|
||
def establish_master_connection | ||
establish_connection @configuration | ||
end | ||
|
||
def check_target_version | ||
if target_version && !(ActiveRecord::Migration::MigrationFilenameRegexp.match?(ENV["VERSION"]) || /\A\d+\z/.match?(ENV["VERSION"])) | ||
raise "Invalid format of target version: `VERSION=#{ENV['VERSION']}`" | ||
end | ||
end | ||
|
||
def target_version | ||
ENV["VERSION"].to_i if ENV["VERSION"] && !ENV["VERSION"].empty? | ||
end | ||
end | ||
end |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module ClickhouseActiverecord | ||
VERSION = '0.3.0' | ||
VERSION = '0.3.1' | ||
end |
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,11 @@ | ||
require 'rails/generators/active_record/migration/migration_generator' | ||
|
||
class ClickhouseMigrationGenerator < ActiveRecord::Generators::MigrationGenerator | ||
source_root File.join(File.dirname(ActiveRecord::Generators::MigrationGenerator.instance_method(:create_migration_file).source_location.first), "templates") | ||
|
||
def create_migration_file | ||
set_local_assigns! | ||
validate_file_name! | ||
migration_template @migration_template, "db/migrate_clickhouse/#{file_name}.rb" | ||
end | ||
end |
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