@@ -1801,103 +1801,75 @@ def test_check_pending_with_stdlib_logger
1801
1801
def test_unknown_migration_version_should_raise_an_argument_error
1802
1802
assert_raise ( ArgumentError ) { ActiveRecord ::Migration [ 1.0 ] }
1803
1803
end
1804
- end
1805
-
1806
- class MigrationValidationTest < ActiveRecord ::TestCase
1807
- def setup
1808
- @verbose_was , ActiveRecord ::Migration . verbose = ActiveRecord ::Migration . verbose , false
1809
- @schema_migration = ActiveRecord ::Base . connection . schema_migration
1810
- @internal_metadata = ActiveRecord ::Base . connection . internal_metadata
1811
- @active_record_validate_timestamps_was = ActiveRecord . validate_migration_timestamps
1812
- ActiveRecord . validate_migration_timestamps = true
1813
- ActiveRecord ::Base . connection . schema_cache . clear!
1814
-
1815
- @migrations_path = MIGRATIONS_ROOT + "/temp"
1816
- @migrator = ActiveRecord ::MigrationContext . new ( @migrations_path , @schema_migration , @internal_metadata )
1817
- end
1818
1804
1819
- def teardown
1820
- @schema_migration . create_table
1821
- @schema_migration . delete_all_versions
1822
- ActiveRecord . validate_migration_timestamps = @active_record_validate_timestamps_was
1823
- ActiveRecord ::Migration . verbose = @verbose_was
1824
- end
1805
+ class MigrationValidationTest < ActiveRecord :: TestCase
1806
+ def setup
1807
+ @verbose_was , ActiveRecord :: Migration . verbose = ActiveRecord :: Migration . verbose , false
1808
+ @schema_migration = ActiveRecord :: Base . connection . schema_migration
1809
+ @internal_metadata = ActiveRecord ::Base . connection . internal_metadata
1810
+ ActiveRecord :: Base . connection . schema_cache . clear!
1825
1811
1826
- def test_migration_raises_if_timestamp_not_14_digits
1827
- with_temp_migration_file ( @migrations_path , "201801010101010000_test_migration.rb" ) do
1828
- error = assert_raises ( ActiveRecord ::InvalidMigrationTimestampError ) do
1829
- @migrator . up ( 201801010101010000 )
1830
- end
1831
- assert_match ( /Invalid timestamp 201801010101010000 for migration file: test_migration/ , error . message )
1812
+ @migrations_path = MIGRATIONS_ROOT + "/temp"
1813
+ @migrator = ActiveRecord ::MigrationContext . new ( @migrations_path , @schema_migration , @internal_metadata )
1832
1814
end
1833
1815
1834
- with_temp_migration_file ( @migrations_path , "201801010_test_migration.rb" ) do
1835
- error = assert_raises ( ActiveRecord ::InvalidMigrationTimestampError ) do
1836
- @migrator . up ( 201801010 )
1837
- end
1838
- assert_match ( /Invalid timestamp 201801010 for migration file: test_migration/ , error . message )
1816
+ def teardown
1817
+ @schema_migration . create_table
1818
+ @schema_migration . delete_all_versions
1819
+ ActiveRecord ::Migration . verbose = @verbose_was
1839
1820
end
1840
- end
1841
1821
1842
- def test_migration_raises_if_timestamp_is_invalid_date
1843
- # 2023-15-26 is an invalid date
1844
- with_temp_migration_file ( @migrations_path , "20231526101010_test_migration.rb" ) do
1845
- error = assert_raises ( ActiveRecord ::InvalidMigrationTimestampError ) do
1846
- @migrator . up ( 20231526101010 )
1847
- end
1848
- assert_match ( /Invalid timestamp 20231526101010 for migration file: test_migration/ , error . message )
1849
- end
1850
- end
1822
+ def test_copied_migrations_at_timestamp_boundary_are_valid
1823
+ migrations_path_source = MIGRATIONS_ROOT + "/temp_source"
1824
+ migrations_path_dest = MIGRATIONS_ROOT + "/temp_dest"
1825
+ migrations = [ "20180101010101_test_migration.rb" , "20180101010102_test_migration_two.rb" , "20180101010103_test_migration_three.rb" ]
1826
+
1827
+ with_temp_migration_files ( migrations_path_source , migrations ) do
1828
+ travel_to ( Time . utc ( 2023 , 12 , 1 , 10 , 10 , 59 ) ) do
1829
+ ActiveRecord ::Migration . copy ( migrations_path_dest , temp : migrations_path_source )
1830
+
1831
+ assert File . exist? ( migrations_path_dest + "/20231201101059_test_migration.temp.rb" )
1832
+ assert File . exist? ( migrations_path_dest + "/20231201101060_test_migration_two.temp.rb" )
1833
+ assert File . exist? ( migrations_path_dest + "/20231201101061_test_migration_three.temp.rb" )
1834
+
1835
+ migrator = ActiveRecord ::MigrationContext . new ( migrations_path_dest , @schema_migration , @internal_metadata )
1836
+ migrator . up ( 20231201101059 )
1837
+ migrator . up ( 20231201101060 )
1838
+ migrator . up ( 20231201101061 )
1851
1839
1852
- def test_migration_raises_if_timestamp_is_invalid_day_month_combo
1853
- # 2023-02-31 is an invalid date; 02 and 31 are valid month / days individually,
1854
- # but February 31 does not exist.
1855
- with_temp_migration_file ( @migrations_path , "20230231101010_test_migration.rb" ) do
1856
- error = assert_raises ( ActiveRecord ::InvalidMigrationTimestampError ) do
1857
- @migrator . up ( 20230231101010 )
1840
+ assert_equal 20231201101061 , migrator . current_version
1841
+ assert_not migrator . needs_migration?
1842
+ end
1858
1843
end
1859
- assert_match ( /Invalid timestamp 20230231101010 for migration file: test_migration/ , error . message )
1844
+ ensure
1845
+ File . delete ( *Dir [ migrations_path_dest + "/*.rb" ] )
1846
+ Dir . rmdir ( migrations_path_dest ) if Dir . exist? ( migrations_path_dest )
1860
1847
end
1861
- end
1862
1848
1863
- def test_migration_succeeds_despite_invalid_timestamp_if_validate_timestamps_is_false
1864
- validate_migration_timestamps_was = ActiveRecord . validate_migration_timestamps
1865
- ActiveRecord . validate_migration_timestamps = false
1866
- with_temp_migration_file ( @migrations_path , "201801010_test_migration.rb" ) do
1867
- @migrator . up ( 201801010 )
1868
- assert_equal 201801010 , @migrator . current_version
1869
- end
1870
- ensure
1871
- ActiveRecord . validate_migration_timestamps = validate_migration_timestamps_was
1872
- end
1849
+ private
1850
+ def with_temp_migration_files ( migrations_dir , filenames )
1851
+ Dir . mkdir ( migrations_dir ) unless Dir . exist? ( migrations_dir )
1873
1852
1874
- def test_migration_succeeds_despite_invalid_timestamp_if_timestamped_migrations_is_false
1875
- timestamped_migrations_was = ActiveRecord . timestamped_migrations
1876
- ActiveRecord . timestamped_migrations = false
1877
- with_temp_migration_file ( @migrations_path , "201801010_test_migration.rb" ) do
1878
- @migrator . up ( 201801010 )
1879
- assert_equal 201801010 , @migrator . current_version
1880
- end
1881
- ensure
1882
- ActiveRecord . timestamped_migrations = timestamped_migrations_was
1883
- end
1853
+ paths = [ ]
1854
+ filenames . each do |filename |
1855
+ path = File . join ( migrations_dir , filename )
1856
+ paths << path
1884
1857
1885
- private
1886
- def with_temp_migration_file ( migrations_dir , filename )
1887
- Dir . mkdir ( migrations_dir ) unless Dir . exist? ( migrations_dir )
1888
- path = File . join ( migrations_dir , filename )
1858
+ migration_class = filename . match ( ActiveRecord ::Migration ::MigrationFilenameRegexp ) [ 2 ] . camelize
1889
1859
1890
- File . open ( path , "w+" ) do |file |
1891
- file << <<-MIGRATION
1892
- class TestMigration < ActiveRecord::Migration::Current
1893
- def change; end
1894
- end
1895
- MIGRATION
1896
- end
1860
+ File . open ( path , "w+" ) do |file |
1861
+ file << <<~MIGRATION
1862
+ class #{ migration_class } < ActiveRecord::Migration::Current
1863
+ def change; end
1864
+ end
1865
+ MIGRATION
1866
+ end
1867
+ end
1897
1868
1898
- yield
1899
- ensure
1900
- File . delete ( path ) if File . exist? ( path )
1901
- Dir . rmdir ( migrations_dir ) if Dir . exist? ( migrations_dir )
1902
- end
1869
+ yield
1870
+ ensure
1871
+ paths . each { |path | File . delete ( path ) if File . exist? ( path ) }
1872
+ Dir . rmdir ( migrations_dir ) if Dir . exist? ( migrations_dir )
1873
+ end
1874
+ end
1903
1875
end
0 commit comments