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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
# File 'lib/cli/commands/new_app/files/db_rake.rb', line 16
def self.content(app_name, db_name)
<<~RUBY
# typed: false
# run on the database server once:
#
# CREATE DATABASE #{db_name}_${environment};
require 'zeitwerk/inflector'
require_relative "../../app"
namespace :db do
# RACK_ENV=development bundle exec rake db:create
desc "Create the database"
task :create do
envs = ENV.key?("RACK_ENV") ? [ENV.fetch("RACK_ENV")] : %w[development test]
envs.each do |env|
ENV["RACK_ENV"] = env
db_name = "#{db_name}_\#{env}"
puts("Creating database \#{db_name}...")
reset_memoized_class_level_instance_vars(#{app_name})
url = #{app_name}.default_db_url.dup # frozen string
url.gsub!(db_name, "postgres")
puts("Connecting to \#{url.gsub(%r{://.*@}, "_REDACTED_")}")
db = Sequel.connect(url)
begin
db.execute("CREATE DATABASE \#{db_name}")
puts("Created database \#{db_name}.")
rescue Sequel::DatabaseError, PG::DuplicateDatabase
puts("Database \#{db_name} already exists, skipping.")
end
end
end
desc "Drop the database"
task :drop do
envs = ENV.key?("RACK_ENV") ? [ENV.fetch("RACK_ENV")] : %w[development test]
envs.each do |env|
ENV["RACK_ENV"] = env
db_name = "#{db_name}_\#{env}"
puts("Dropping database \#{db_name}...")
reset_memoized_class_level_instance_vars(#{app_name})
url = #{app_name}.default_db_url.dup # frozen string
url.gsub!(db_name, "postgres")
puts("Connecting to \#{url.gsub(%r{://.*@}, "_REDACTED_")}")
db = Sequel.connect(url)
begin
db.execute("DROP DATABASE \#{db_name} (FORCE)")
puts("Dropped database \#{db_name}.")
rescue Sequel::DatabaseError, PG::DuplicateDatabase
puts("Database \#{db_name} does not exists, nothing to drop.")
end
end
end
desc "Run migrations"
task :migrate do
Sequel.extension(:migration)
envs = ENV.key?("RACK_ENV") ? [ENV.fetch("RACK_ENV")] : %w[development test]
envs.each do |env|
ENV["RACK_ENV"] = env
db_name = "#{db_name}_\#{env}"
reset_memoized_class_level_instance_vars(#{app_name})
db = Sequel.connect(#{app_name}.default_db_url)
Sequel::Migrator.run(db, File.join(#{app_name}.root, "db/migrate"))
current_version = db[:schema_migrations].order(:filename).last[:filename].to_i
puts "Migrated \#{db_name} to version \#{current_version}!"
end
Rake::Task["db:annotate"].invoke
end
desc "Rollback the last migration"
task :rollback do
envs = ENV.key?("RACK_ENV") ? [ENV.fetch("RACK_ENV")] : %w[development test]
Sequel.extension(:migration)
envs.each do |env|
ENV["RACK_ENV"] = env
db_name = "#{db_name}_\#{env}"
reset_memoized_class_level_instance_vars(#{app_name})
db = Sequel.connect(#{app_name}.default_db_url)
steps = (ENV["STEPS"] || 1).to_i + 1
versions = db[:schema_migrations].order(:filename).all
if versions[-steps].nil?
puts "No more migrations to rollback"
else
target_version = versions[-steps][:filename].to_i
Sequel::Migrator.run(db, File.join(#{app_name}.root, "db/migrate"), target: target_version)
puts "Rolled back \#{db_name} \#{steps} steps to version \#{target_version}"
end
end
end
desc "Seed the database"
task :seed do
load File.join(#{app_name}.root, "db/seeds.rb")
end
desc "Generate a new migration file"
task :migration, [:name] do |_t, args|
require "fileutils"
require "time"
# Ensure the migrations directory exists
migrations_dir = File.join(#{app_name}.root, "db/migrate")
FileUtils.mkdir_p(migrations_dir)
# Generate the migration number
migration_number = Time.now.utc.strftime("%Y%m%d%H%M%S")
# Sanitize and format the migration name
formatted_name = args[:name].to_s.gsub(/([a-z])([A-Z])/, '\\1_\\2').downcase
# Combine them to create the filename
filename = "\#{migration_number}_\#{formatted_name}.rb"
file_path = File.join(migrations_dir, filename)
# Define the content of the migration file
content = <<~MIGRATION
# typed: false
# frozen_string_literal: true
Sequel.migration do
up do
# your code here
end
down do
# your code here
end
end
MIGRATION
# Write the migration file
File.write(file_path, content)
puts "Generated migration: db/migrate/\#{filename}"
end
desc "Write the table schema to each model file, or a single file if filename (without extension) is provided"
task :annotate, [:model_file_name] do |_t, args|
require "fileutils"
db = #{app_name}.raw_db_connection
model_file_name = args[:model_file_name]&.to_s
models_dir = #{app_name}.root
Dir.glob("app/models/**/*.rb").each do |model_file|
next if !model_file_name.nil? && model_file == model_file_name
model_path = File.expand_path(model_file, models_dir)
modules = model_file.gsub("app/models/", "").gsub(".rb", "").split("/").map { |mod| Zeitwerk::Inflector.new.camelize(mod, model_path) }
const_name = modules.join("::")
model_klass = Object.const_get(const_name)
next unless model_klass.ancestors.include?(Kirei::Model)
table_name = model_klass.table_name
schema = db.schema(table_name)
schema_comments = format_schema_comments(table_name, schema)
file_contents = File.read(model_path)
# Remove existing schema info comments if present
updated_contents = file_contents.sub(/# == Schema Info\\n(.*?)(\\n#\\n)?\\n(?=\\s*(?:class|module))/m, "")
# Insert the new schema comments before the module/class definition
first_const = modules.first
first_module_or_class = modules.count == 1 ? "class #{first_const}" : "module #{first_const}"
modified_contents = updated_contents.sub(/(A|\n)(#{first_module_or_class})/m, "\\1#{}\n\n\\2")
File.write(model_path, modified_contents)
end
end
end
def reset_memoized_class_level_instance_vars(app)
%i[
@default_db_name
@default_db_url
@raw_db_connection
].each do |ivar|
app.remove_instance_variable(ivar) if app.instance_variable_defined?(ivar)
end
end
def format_schema_comments(table_name, schema)
lines = ["# == Schema Info", "#", "# Table name: \#{table_name}", "#"]
schema.each do |column|
name, info = column
type = "\#{info[:db_type]}(\#{info[:max_length]})" if info[:max_length]
type ||= info[:db_type]
null = info[:allow_null] ? 'null' : 'not null'
primary_key = info[:primary_key] ? ', primary key' : ''
lines << "# \#{name.to_s.ljust(20)}:\#{type} \#{null}\#{primary_key}"
end
lines.join("\\n") + "\\n#"
end
RUBY
end
|