Created
May 16, 2024 22:39
-
-
Save RoseAndres/28c5ac2192cc52471d11cff1c3f51a8f to your computer and use it in GitHub Desktop.
Create duplicate table via an ActiveRecord migration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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