### Install Squasher Gem Source: https://github.com/jalkoby/squasher/blob/master/README.md Standalone installation of the Squasher gem using the RubyGems package manager. Ensure Rbenv is rehashed if used. ```bash $ gem install squasher # For Rbenv users: rbenv rehash ``` -------------------------------- ### Squasher Configuration Management (Ruby) Source: https://context7.com/jalkoby/squasher/llms.txt Demonstrates the usage of the `Squasher::Config` class for managing Squasher's configuration settings. This includes setting migration versions, enabling SQL schema format, configuring multi-database setups, specifying Rails engine paths, and checking configuration flags. ```ruby require 'squasher' config = Squasher::Config.new # Set migration version for Rails 5+ config.set(:migration, '5.2') # Results in ActiveRecord::Migration[5.2] # Enable SQL schema format config.set(:sql, true) # Set multi-database format config.set(:multi_db_format, 'rails') config.set(:databases, ['primary', 'analytics']) # Configure for Rails engine config.set(:engine, 'spec/dummy') # Check configuration flags config.set?(:dry) # => false config.set?(:sql) # => true # Get schema file path config.schema_file # => "db/schema.rb" config.schema_file('analytics') # => "db/analytics_schema.rb" ``` -------------------------------- ### Squasher Setup: Configure Runtime Options Source: https://context7.com/jalkoby/squasher/llms.txt Configure Squasher's behavior for squash and clean operations using various options like migration version, SQL format, dry runs, and database settings. This function modifies internal configurations that control how Squasher operates. ```ruby require 'squasher' # Setup with multiple options Squasher.setup({ migration: '6.0', # Rails 6.0 migration syntax sql: true, # Use SQL structure format dry: false, # Not a dry run reuse: false, # Don't reuse database engine: nil, # Not a Rails engine multi_db_format: 'rails', databases: ['primary', 'cache'] }) # Setup modifies internal config config = Squasher.config config.migration_version # => "[6.0]" config.set?(:sql) # => true config.set?(:dry) # => false config.multi_db_format # => "rails" config.databases # => ["primary", "cache"] # Minimal setup Squasher.setup({ migration: '5.2' }) # Flag-only setup Squasher.setup({ dry: true, sql: true }) ``` -------------------------------- ### Configure and Get Migration Folders and Files (Ruby) Source: https://context7.com/jalkoby/squasher/llms.txt Access and retrieve information about migration files and directories using the Squasher configuration object. This allows for dynamic retrieval of migration paths based on specific contexts. ```ruby config.migration_files # => ["db/migrate/20191201_create_users.rb", ...] config.migration_files('analytics') # => ["db/analytics_migrate/...", ...] config.migrations_folder # => "db/migrate" config.migrations_folder('analytics') # => "db/analytics_migrate" ``` -------------------------------- ### Compress Migrations Before Specific Day Source: https://github.com/jalkoby/squasher/blob/master/README.md Compresses all ActiveRecord migrations created before a specific day, month, and year. Example: compress migrations prior to December 19, 2016. ```bash $ squasher 2016/12/19 # prior to 19 December 2016 ``` -------------------------------- ### Compress Migrations Before Specific Month Source: https://github.com/jalkoby/squasher/blob/master/README.md Compresses all ActiveRecord migrations created before a specified month and year. Example: compress migrations prior to December 2016. ```bash $ squasher 2016/12 # prior to December 2016 ``` -------------------------------- ### Interactive User Prompts with Squasher.ask (Ruby) Source: https://context7.com/jalkoby/squasher/llms.txt Engage the user with colorized prompts for confirmation during critical operations, such as keeping a temporary database or applying a cleaning migration. Supports custom prompts with configuration options. ```ruby require 'squasher' # Ask to keep temporary database if Squasher.ask(:keep_database) puts "Keeping database" else Squasher.rake("db:drop") end # Ask to apply cleaning migration if Squasher.ask(:apply_clean) Squasher.clean end # Custom question with parameters Squasher.ask(:use_dbconfig, config: { 'adapter' => 'postgresql' }) ``` -------------------------------- ### Create Squasher Executable in bin folder Source: https://github.com/jalkoby/squasher/blob/master/README.md Generates a command-line executable for Squasher within the application's `bin` directory using Bundler's binstub functionality. This allows direct execution of squasher commands. ```bash $ bundle binstub squasher $ # and you have a runner inside the `bin` folder $ bin/squasher ``` -------------------------------- ### Render Migration Templates with Squasher Render (Ruby) Source: https://context7.com/jalkoby/squasher/llms.txt Generate migration files using ERB templates with the Squasher::Render class. Supports rendering the init_schema migration and squasher_clean migration in both standard Ruby and SQL structure formats. ```ruby require 'squasher' # Setup configuration config = Squasher::Config.new config.set(:migration, '6.1') # Render init_schema migration renderer = Squasher::Render.new(:init_schema, config) migration_content = renderer.render # Render squasher_clean migration clean_renderer = Squasher::Render.new(:squasher_clean, config) clean_migration = clean_renderer.render # For SQL structure format config.set(:sql, true) sql_renderer = Squasher::Render.new(:init_schema, config) sql_migration = sql_renderer.render ``` -------------------------------- ### Squasher Contribution Guidelines Source: https://github.com/jalkoby/squasher/blob/master/README.md Standard contribution workflow for the Squasher project. Involves forking the repository, creating a feature branch, committing changes, pushing the branch, and submitting a pull request. ```git 1. Fork it 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Add some feature'`) 4. Push to the branch (`git push origin my-new-feature`) 5. Create new Pull Request ``` -------------------------------- ### Squash Migrations by Date (CLI) Source: https://context7.com/jalkoby/squasher/llms.txt The primary command-line interface for Squasher. It accepts a date parameter to consolidate all ActiveRecord migrations created before that date into a single 'init schema' migration. Supports various date formats, dry runs, SQL schema format, reuse of temporary databases, Rails engine integration, and multi-database configurations. ```bash # Basic usage - squash all migrations before 2017 $ squasher 2017 # Squash with Rails 5+ migration version syntax $ squasher 2017 -m 5.0 # More specific date formats $ squasher 2016/12 # Prior to December 2016 $ squasher 2016/12/19 # Prior to December 19, 2016 # Dry run mode - preview without deleting old migrations $ squasher 2020 --dry # SQL schema format (for structure.sql instead of schema.rb) $ squasher 2020 --sql # Reuse existing squasher database from previous run $ squasher 2020 --reuse # Run inside a Rails engine $ squasher 2020 -e # Multi-database support (Rails 6+) $ squasher 2020 --multi-db_format=rails --databases=primary,analytics # Custom engine path $ squasher 2020 --engine=spec/dummy ``` -------------------------------- ### Process Migrations with Squasher Worker (Ruby) Source: https://context7.com/jalkoby/squasher/llms.txt Utilize the Squasher::Worker class to automate the process of validating, running, and consolidating database migrations. It handles temporary database creation, schema extraction, and generation of a new init_schema migration. ```ruby require 'squasher' # Configure Squasher first Squasher.setup({ migration: '6.0', dry: false }) # Create worker with cutoff date date = Time.new(2020, 1, 1) worker = Squasher::Worker.new(date) # Process migrations worker.process ``` -------------------------------- ### Config Stub DBConfig: Temporary Database Configuration Source: https://context7.com/jalkoby/squasher/llms.txt Temporarily replace database.yml and schema files with test versions for isolated operations, then automatically restore originals. This is useful for running migrations on a temporary database without affecting your main configuration. ```ruby require 'squasher' config = Squasher::Config.new # Stub configuration for safe operations config.stub_dbconfig do # During this block: # - database.yml is backed up to database.yml.sq # - schema.rb is backed up to schema.rb.sq # - A new database.yml is written with database: 'squasher' # Run migrations on temporary database system('bundle exec rake db:migrate') # Perform other operations schema_content = File.read('db/schema.rb') # When block exits, all files are restored automatically end # Multi-database example config.set(:multi_db_format, 'rails') config.set(:databases, ['primary', 'analytics']) config.stub_dbconfig do # Both primary_schema.rb and analytics_schema.rb are backed up # database.yml contains temp configs for both databases system('bundle exec rake db:migrate:primary') system('bundle exec rake db:migrate:analytics') end # All schema files and database.yml restored ``` -------------------------------- ### Clean Schema Migration Records with Squasher Cleaner (Ruby) Source: https://context7.com/jalkoby/squasher/llms.txt Employ the Squasher::Cleaner class to remove orphaned schema_migration records, ensuring consistency between the database and the migration files on disk. Supports single and multi-database configurations. ```ruby require 'squasher' # Process cleaning for default database cleaner = Squasher::Cleaner.new cleaner.process # For multi-database setup Squasher.setup({ multi_db_format: 'rails', databases: ['primary', 'analytics'] }) Squasher::Cleaner.process # Cleans both databases ``` -------------------------------- ### Clean Schema Migrations (CLI) Source: https://context7.com/jalkoby/squasher/llms.txt Command-line interface for cleaning orphaned records from the schema_migrations table. This command generates or updates a special migration that removes entries from the database's migration tracking table if the corresponding migration file no longer exists on disk, ensuring consistency. ```bash # Generate and run cleaning migration $ squasher clean # This creates a migration like: # db/migrate/20231103142500_squasher_clean.rb # which deletes schema_migration records that no longer have corresponding files ``` -------------------------------- ### Execute Rails Rake Tasks with Squasher (Ruby) Source: https://context7.com/jalkoby/squasher/llms.txt Run various Rails rake tasks, such as database migrations, drops, and creations, within the Squasher workflow. It automatically sets necessary environment variables like RAILS_ENV and DISABLE_DATABASE_ENVIRONMENT_CHECK. ```ruby require 'squasher' # Execute rake task with description Squasher.rake('db:migrate', :db_migrate) # Drop and create database Squasher.rake('db:drop db:create', :db_create) # Migrate to specific version timestamp = '20191231235959' Squasher.rake("db:migrate VERSION=#{timestamp}", :db_migrate) # Multi-database migrations (Rails 6+) Squasher.rake("db:migrate:analytics VERSION=#{timestamp}", :db_migrate) # Clean database records Squasher.rake("db:migrate", :db_cleaning) ``` -------------------------------- ### Programmatic Migration Squashing (Ruby) Source: https://context7.com/jalkoby/squasher/llms.txt The Ruby API for programmatically squashing ActiveRecord migrations. This function takes a date string and squashes all migrations created before that date. It handles date parsing, validation, temporary database creation, schema extraction, and cleanup of old migration files. ```ruby require 'squasher' # Configure options Squasher.setup({ migration: '5.2', dry: false, sql: false }) # Squash migrations before January 1, 2020 Squasher.squash('2020') # Squash with month precision Squasher.squash('2019/06') # Squash with day precision Squasher.squash('2019/06/15') # The method internally: # 1. Parses date string to Time object # 2. Validates migration folder exists # 3. Creates temporary database # 4. Runs migrations up to the specified date # 5. Generates init_schema migration # 6. Removes old migration files # 7. Prompts to keep/drop temp database ``` -------------------------------- ### Compress Migrations Before 2017 (Rails 5+) Source: https://github.com/jalkoby/squasher/blob/master/README.md Command to compress ActiveRecord migrations created before 2017, specifically for Rails 5 and later versions. The `-m 5.0` flag indicates compatibility with Rails 5.0. ```bash $ squasher 2017 -m 5.0 # rails 5+ ``` -------------------------------- ### Programmatic Schema Cleaning (Ruby) Source: https://context7.com/jalkoby/squasher/llms.txt The Ruby API for programmatically cleaning orphaned schema migration records. This function executes the cleaning process, which involves finding or creating a special 'squasher_clean' migration and running it to remove any records in the schema_migrations table that do not have a corresponding migration file. ```ruby require 'squasher' # Run the cleaning process Squasher.clean # This will: # 1. Find or create squasher_clean migration # 2. Execute it to remove orphaned schema_migration records # 3. Update the database to reflect only existing migration files ``` -------------------------------- ### Add Squasher to Gemfile for Development Tools Source: https://github.com/jalkoby/squasher/blob/master/README.md Adds the Squasher gem to the development tools group in a Rails application's Gemfile. This also includes Capistrano and RuboCop for development and deployment utilities. Remember to run `bundle` after modification. ```ruby # Yep, the missing group in most Gemfiles where all utilities should be! group :tools do gem 'squasher', '>= 0.6.0' gem 'capistrano' gem 'rubocop' end ``` -------------------------------- ### Compress Migrations Before 2017 (Rails 3 & 4) Source: https://github.com/jalkoby/squasher/blob/master/README.md Command to compress all ActiveRecord migrations created prior to the year 2017. This command is suitable for Rails versions 3 and 4. ```bash $ squasher 2017 # rails 3 & 4 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.