Skip to content

Instantly share code, notes, and snippets.

@RoseAndres
Created May 16, 2024 22:39
Show Gist options
  • Save RoseAndres/28c5ac2192cc52471d11cff1c3f51a8f to your computer and use it in GitHub Desktop.
Save RoseAndres/28c5ac2192cc52471d11cff1c3f51a8f to your computer and use it in GitHub Desktop.
Create duplicate table via an ActiveRecord migration
class ExampleMigration < ActiveRecord::Migration[5.2]
def up
# ? Create the new table with the same structure as the old table
# ? https://www.postgresql.org/docs/9.3/sql-createtable.html
execute <<-SQL
CREATE TABLE new_table (LIKE old_table INCLUDING ALL);
SQL
# * removing any unneeded columns will also remove related indexes and constraints
remove_column :new_table, :old_column
end
def down
drop_table :new_table
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment