Skip to content

Commit 2a7cd75

Browse files
authored
Merge pull request rails#48085 from wpank84/patch-1
Update migration examples to showcase database-agnostic raw SQL execution
2 parents cc359c0 + 11efa04 commit 2a7cd75

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

guides/source/active_record_migrations.md

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -724,17 +724,16 @@ class ExampleMigration < ActiveRecord::Migration[7.1]
724724

725725
reversible do |direction|
726726
direction.up do
727-
# add a CHECK constraint
727+
# create a distributors view
728728
execute <<-SQL
729-
ALTER TABLE distributors
730-
ADD CONSTRAINT zipchk
731-
CHECK (char_length(zipcode) = 5) NO INHERIT;
729+
CREATE VIEW distributors_view AS
730+
SELECT id, zipcode
731+
FROM distributors;
732732
SQL
733733
end
734734
direction.down do
735735
execute <<-SQL
736-
ALTER TABLE distributors
737-
DROP CONSTRAINT zipchk
736+
DROP VIEW distributors_view;
738737
SQL
739738
end
740739
end
@@ -774,11 +773,11 @@ class ExampleMigration < ActiveRecord::Migration[7.1]
774773
t.string :zipcode
775774
end
776775

777-
# add a CHECK constraint
776+
# create a distributors view
778777
execute <<-SQL
779-
ALTER TABLE distributors
780-
ADD CONSTRAINT zipchk
781-
CHECK (char_length(zipcode) = 5);
778+
CREATE VIEW distributors_view AS
779+
SELECT id, zipcode
780+
FROM distributors;
782781
SQL
783782

784783
add_column :users, :home_page_url, :string
@@ -790,8 +789,7 @@ class ExampleMigration < ActiveRecord::Migration[7.1]
790789
remove_column :users, :home_page_url
791790

792791
execute <<-SQL
793-
ALTER TABLE distributors
794-
DROP CONSTRAINT zipchk
792+
DROP VIEW distributors_view;
795793
SQL
796794

797795
drop_table :distributors
@@ -832,27 +830,25 @@ The `revert` method also accepts a block of instructions to reverse. This could
832830
be useful to revert selected parts of previous migrations.
833831

834832
For example, let's imagine that `ExampleMigration` is committed and it is later
835-
decided it would be best to use Active Record validations, in place of the
836-
`CHECK` constraint, to verify the zipcode.
833+
decided that a Distributors view is no longer needed.
837834

838835
```ruby
839-
class DontUseConstraintForZipcodeValidationMigration < ActiveRecord::Migration[7.1]
836+
class DontUseDistributorsViewMigration < ActiveRecord::Migration[7.1]
840837
def change
841838
revert do
842839
# copy-pasted code from ExampleMigration
843840
reversible do |direction|
844841
direction.up do
845-
# add a CHECK constraint
842+
# create a distributors view
846843
execute <<-SQL
847-
ALTER TABLE distributors
848-
ADD CONSTRAINT zipchk
849-
CHECK (char_length(zipcode) = 5);
844+
CREATE VIEW distributors_view AS
845+
SELECT id, zipcode
846+
FROM distributors;
850847
SQL
851848
end
852849
direction.down do
853850
execute <<-SQL
854-
ALTER TABLE distributors
855-
DROP CONSTRAINT zipchk
851+
DROP VIEW distributors_view;
856852
SQL
857853
end
858854
end

0 commit comments

Comments
 (0)