forked from andypike/rectify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRakefile
More file actions
69 lines (55 loc) · 1.99 KB
/
Rakefile
File metadata and controls
69 lines (55 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require 'yaml'
require 'active_record'
require 'fileutils'
namespace :db do # rubocop:disable Metrics/BlockLength
desc 'Migrate the database'
task :migrate, [:environment] => :load_config do |_t, _args|
migration_path = 'spec/db/migrate'
ActiveRecord::Migration.verbose = true
context = ActiveRecord::MigrationContext.new(migration_path)
context.migrate
puts '✅ Database migrated.'
end
desc 'Create a db/schema.rb file that is portable against any supported DB'
task :schema, [:environment] => :load_config do
require 'active_record/schema_dumper'
filename = 'spec/db/schema.rb'
FileUtils.mkdir_p(File.dirname(filename))
File.open(filename, 'w:utf-8') do |file|
connection = ActiveRecord::Base.connection
ActiveRecord::SchemaDumper.dump(connection.pool, file)
end
puts '📄 schema.rb dumped.'
end
desc 'Load database configuration'
task :load_config do
env = ENV['RAILS_ENV'] || 'development'
config_path = 'spec/config/database.yml'
raise "❌ Database config file not found: #{config_path}" unless File.exist?(config_path)
db_config = YAML.safe_load_file(config_path, aliases: true)[env]
raise "❌ No configuration found for environment: #{env}" unless db_config
ActiveRecord::Base.establish_connection(db_config)
end
end
namespace :generate do
desc 'Generate migration (usage: rake generate:migration[name])'
task :migration, [:name] do |_, args|
raise '❌ Specify name: rake generate:migration[name]' unless args[:name]
name = args[:name]
timestamp = Time.now.strftime('%Y%m%d%H%M%S')
folder = 'spec/db/migrate'
FileUtils.mkdir_p(folder)
path = File.join(folder, "#{timestamp}_#{name}.rb")
migration_class = name.split('_').map(&:capitalize).join
File.write(
path,
<<~MIGRATION
class #{migration_class} < ActiveRecord::Migration[8.0]
def change
end
end
MIGRATION
)
puts "🛠 Migration created: #{path}"
end
end