Skip to content

Commit

Permalink
Actually fix the template file
Browse files Browse the repository at this point in the history
  • Loading branch information
fynsta committed Apr 4, 2024
1 parent 321ec27 commit 8928149
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/squasher/cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def process_database(database = nil)
FileUtils.rm(prev_migration)
end
File.open(migration_file, 'wb') do |stream|
stream << ::Squasher::Render.render(MIGRATION_NAME, config)
stream << ::Squasher::Render.render(MIGRATION_NAME, config, database)
end

if database.nil?
Expand Down
21 changes: 15 additions & 6 deletions lib/squasher/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def inspect
end
end

attr_reader :schema_file, :migration_version, :multi_db_format, :databases
attr_reader :migration_version, :multi_db_format, :databases

def initialize
@root_path = Dir.pwd.freeze
Expand All @@ -59,9 +59,6 @@ def set(key, value)
elsif key == :migration
Squasher.error(:invalid_migration_version, value: value) unless value.to_s =~ /\A\d.\d\z/
@migration_version = "[#{value}]"
elsif key == :sql
@schema_file = File.join(@app_path, 'db', 'structure.sql')
@flags << key
elsif key == :multi_db_format
Squasher.error(:invalid_multi_db_format, value: value) unless %w[rails multiverse].include?(value)
@multi_db_format = value
Expand All @@ -76,6 +73,19 @@ def set?(k)
@flags.include?(k)
end

def schema_files
return [schema_file] unless @multi_db_format == 'rails'

@databases.map { |db| schema_file(db) }
end

def schema_file(database = nil)
prefix = database.nil? || database == 'primary' ? '' : "#{ database }_"
file = set?(:sql) ? 'structure.sql' : 'schema.rb'

File.join(@app_path, 'db', "#{ prefix }#{ file }")
end

def migration_files(database = nil)
Dir.glob(File.join(migrations_folder(database), '**.rb'))
end
Expand Down Expand Up @@ -112,7 +122,7 @@ def dbconfig?
def stub_dbconfig
return unless dbconfig?

list = [dbconfig_file, schema_file]
list = [dbconfig_file, *schema_files]
list.each do |file|
next unless File.exist?(file)
FileUtils.mv file, "#{ file }.sq"
Expand Down Expand Up @@ -179,7 +189,6 @@ def dbconfig

def set_app_path(path)
@app_path = path
@schema_file = File.join(path, 'db', 'schema.rb')
@dbconfig_file = File.join(path, 'config', 'database.yml')
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/squasher/render.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ def self.render(*args)

attr_reader :name, :config

def initialize(name, config)
def initialize(name, config, database = nil)
@name = name
@config = config
@database = database
end

def render
Expand All @@ -22,7 +23,7 @@ def render
end

def each_schema_line(&block)
File.open(config.schema_file, 'r') do |stream|
File.open(config.schema_file(@database), 'r') do |stream|
if @config.set?(:sql)
stream_structure(stream, &block)
else
Expand Down
4 changes: 2 additions & 2 deletions lib/squasher/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ def get_timestamp(file)

def clean_migrations(database = nil)
path = config.migration_file(finish_timestamp(database), :init_schema, database)
File.open(path, 'wb') { |io| io << Render.render(:init_schema, config) }
migrations(database).each { |file| FileUtils.rm(file) }
migrations(database).each { |file| FileUtils.rm(file) } # Remove all migrations before creating the new one
File.open(path, 'wb') { |io| io << Render.render(:init_schema, config, database) }
end

def before_date?(timestamp)
Expand Down

0 comments on commit 8928149

Please sign in to comment.