Commit Graph

39 Commits

Author SHA1 Message Date
Nick Borromeo
0f09dfca36
Guard against using VERSION with db:rollback (#41430)
* Guard against using VERSION with db:rollback

I recently ran a migration that I needed to rollback, and admittedly, I often forget the proper incantation for this 😅
so the first thing I tried was to run `bin/rake db:rollback VERSION=123454679`. I had hoped that this reverted my
migration back and at first glance I thought it worked. However on closer inspection I realized that it was a different
migration, which initially confused me.

So I looked over the docs and saw that I was using the rake task incorrectly, and promptly corrected my mistake.

Proposal

Looking at the how the `:down` task is defined we see

8dc7439058/activerecord/lib/active_record/railties/databases.rake (L206-L211)

This got me thinking that maybe it would be helpful to have the opposite of this guard defined in `rollback` so that if
`VERSION` is passed it will raise an exception instead of just negecting the extra argument. This could help the user
realize that something went wrong instead of just seeing output and assuming that the rollback happened.

Change

We now raise an execption if `VERSION` is passed when attempting to rollback a migration

* update test name and fix failing test

* remove byebug

[Nick Borromeo + Kate Travers + Rafael Mendonça França]
2021-02-15 19:30:05 -05:00
Yasuo Honda
3cafe4ad2f Revert "Avoid chdir error in Railties tests on Ruby master"
This reverts commit ae5ecfe26c8fdc194d5b3cd16fd13b64574bcfd3.
2020-12-26 15:01:24 +09:00
Eugene Kenny
ae5ecfe26c Avoid chdir error in Railties tests on Ruby master
5d7953f86b
https://buildkite.com/rails/rails/builds/71925#9839c015-e562-4145-932c-75b9c118bfca/1043-1055
2020-10-04 00:26:03 +01:00
Ryuta Kamizono
5324f2cb09 Revert "Merge pull request #37215 from utilum/avoid_test_flunking_on_warning"
This reverts commit ed78e96408f3f83e779a71c65b86aeb1cfc5616e, reversing
changes made to eca6c273fe2729b9634907562c2717cf86443b6b.
2019-12-25 17:13:09 +09:00
utilum
e0b8c918a0 Avoid flunking tests on warning in output 2019-09-28 12:52:31 +02:00
Ryuta Kamizono
1dc17e7b2e Fix CustomCops/AssertNot to allow it to have failure message
Follow up of #32605.
2018-05-13 11:32:47 +09:00
eileencodes
e0ad907ade Add test to properly test down with a block
down is only called with a block from the rake tasks where it passes a
`SCOPE`. Technically this was tested but since we don't run all the
migrations we're not actually testing the down works with a `SCOPE`. To
ensure we're testing both we can run `db:migrate` again to migrate users
and then run `down` with a scope to test that only the bukkits migration
is reverted.

Updates test to prevent having to fix regressions like we did in
4d4db4c.
2018-01-18 13:09:15 -05:00
eileencodes
a2827ec981 Refactor migration to move migrations paths to connection
Rails has some support for multiple databases but it can be hard to
handle migrations with those. The easiest way to implement multiple
databases is to contain migrations into their own folder ("db/migrate"
for the primary db and "db/seconddb_migrate" for the second db). Without
this you would need to write code that allowed you to switch connections
in migrations. I can tell you from experience that is not a fun way to
implement multiple databases.

This refactoring is a pre-requisite for implementing other features
related to parallel testing and improved handling for multiple
databases.

The refactoring here moves the class methods from the `Migrator` class
into it's own new class `MigrationContext`. The goal was to move the
`migrations_paths` method off of the `Migrator` class and onto the
connection. This allows users to do the following in their
`database.yml`:

```
development:
  adapter: mysql2
  username: root
  password:

development_seconddb:
  adapter: mysql2
  username: root
  password:
  migrations_paths: "db/second_db_migrate"
```

Migrations for the `seconddb` can now be store in the
`db/second_db_migrate` directory. Migrations for the primary database
are stored in `db/migrate`".

The refactoring here drastically reduces the internal API for migrations
since we don't need to pass `migrations_paths` around to every single
method. Additionally this change does not require any Rails applications
to make changes unless they want to use the new public API. All of the
class methods from the `Migrator` class were `nodoc`'d except for the
`migrations_paths` and `migrations_path` getter/setters respectively.
2018-01-18 08:55:03 -05:00
bogdanvlviv
90fe2a42f0
Fix bin/rails db:migrate with specified VERSION
Ensure that `bin/rails db:migrate` with specified `VERSION` reverts
all migrations only if `VERSION` is `0`.
Raise error if target migration doesn't exist.
2017-11-06 22:40:10 +00:00
bogdanvlviv
ff67743fb2
Remove redundant execution of Dir.chdir(app_path) { } in railties' tests 2017-10-08 23:04:04 +03:00
Matthew Draper
802ce8a239 Run in-app rails commands via fork+load where possible
While this avoids shell argument parsing, we still pass through
everything in our stack.
2017-09-04 20:19:39 +09:30
Pat Allan
acea68de02 Adding frozen_string_literal pragma to Railties. 2017-08-14 19:08:09 +02:00
Matthew Draper
87b3e226d6 Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"
This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing
changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
2017-07-02 02:15:17 +09:30
Kir Shatrov
cfade1ec7e Enforce frozen string in Rubocop 2017-07-01 02:11:03 +03:00
Rafael Mendonça França
15a4d3c383
Also raise error when VERSION is nil
Fix #28905
2017-04-27 10:21:28 -07:00
bogdanvlviv
bb9d6eb094 Add additional raise UnknownMigrationVersionError
Raise error on the movement of migrations
when the current migration does not exist.
2017-04-19 23:37:58 +03:00
bogdanvlviv
b77d2aa0c3 Fix bin/rails db:forward first migration 2017-04-19 21:32:26 +03:00
Ryuta Kamizono
a315846923 Remove duplicated "test" prefix 2017-04-07 08:40:52 +09:00
Philippe Guay
14739b5e27 Fixes #28359
Add stronger assertions to rake migration tasks to make sure the user is providing a numeric VERSION
An empty string was getting converted to version = 0. This would in turn pass the presence check.

Address linting warning

Add test for rake task and refactor code to meet expectations
In particular passing VERSION=0 should not raise an error.

Addressed Comments for PR #28485. Trimmed empty lines + change of wording for error message

Adjust test for change of wording in error message

Change condition to follow rails idioms
2017-03-26 21:06:02 -04:00
Rafael Mendonça França
55f9b8129a
Add three new rubocop rules
Style/SpaceBeforeBlockBraces
Style/SpaceInsideBlockBraces
Style/SpaceInsideHashLiteralBraces

Fix all violations in the repository.
2016-08-16 04:30:11 -03:00
Xavier Noria
80e66cc4d9 normalizes indentation and whitespace across the project 2016-08-06 20:16:27 +02:00
Xavier Noria
783763bde9 applies new string literal convention in railties/test
The current code base is not uniform. After some discussion,
we have chosen to go with double quotes by default.
2016-08-06 19:16:09 +02:00
Prathamesh Sonpatki
d93427840e
Remove unused boot_rails method and it's usage
- The `boot_rails` method from abstract_unit.rb is empty after 2abcdfd978fdcd491576a237e8c6b.
- So let's remove it and its usage.
2016-07-04 09:34:21 +05:30
Vipul A M
e3b04447f4 Be consistent in testing outputs from railties test and use /bin/rails everywhere(the default behaviour now) instead of mix of /bin/rake /bin/rails everywhere
[Ryo Hashimoto & Vipul A M]
2016-01-24 20:01:02 +05:30
Matthew Draper
a6d4e5e532 Internal test migrations use the private 'Current' version
Apart from specific versioning support, our tests should focus on the
behaviour of whatever version they're accompanying, regardless of when
they were written.

Application code should *not* do this.
2015-12-15 17:18:09 +10:30
yui-knk
a7beeb7faa Make db:migrate:status to render 1_some.rb format migrate files.
`1_valid_people_have_last_names.rb` and
`20150823202140_create_users.rb` are valid migration file name.
But `1_valid_people_have_last_names.rb` is rendered as
`********** NO FILE **********` when `rake db:migrate:status`.

Fix to this bug, this commit includes

* define some API private methdos and a Constant
  `match_to_migration_filename?`, `parse_migration_filename`, and
  `MigrationFilenameRegexp`
* use these methods in `db:migrate:status` task

Example:

These files are in `db/migrate`

* 1_valid_people_have_last_names.rb
* 20150819202140_irreversible_migration.rb
* 20150823202140_add_admin_flag_to_users.rb
* 20150823202141_migration_tests.rb
* 2_we_need_reminders.rb
* 3_innocent_jointable.rb

we can migrate all of them.

Before

```shell
$ bundle exec rake db:migrate:status

...

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     001             ********** NO FILE **********
   up     002             ********** NO FILE **********
   up     003             ********** NO FILE **********
   up     20150819202140  Irreversible migration
   up     20150823202140  Add admin flag to users
   up     20150823202141  Migration tests
```

After

```shell
$ bundle exec rake db:migrate:status

...

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     001             Valid people have last names
   up     002             We need reminders
   up     003             Innocent jointable
   up     20150819202140  Irreversible migration
   up     20150823202140  Add admin flag to users
   up     20150823202141  Migration tests
```
2015-11-02 23:06:57 +09:00
Yves Senn
2183caa24a dump_schema_after_migration applies migration tasks other than db:migrate
Closes #20743.

The task `db:_dump` now only dumps the schema if
`ActiveRecord::Base.dump_schema_after_migration` is true. This has
effects:

- `db:migrate:up`
- `db:migrate:down`
- `db:forward`
- `db:rollback`
2015-06-30 16:36:03 +02:00
Yves Senn
a8f250a22c tests, railties tests should use bin/ executables when possible.
We recommend using the `bin/` executables in our docs and guides.
Let's make sure that our tests execute the same code path.
2015-06-30 11:19:01 +02:00
Viktar Basharymau
0813607c70 Add a test for db:migrate:status to check missing file scenario 2014-06-27 18:28:12 +03:00
Paul B
d1fa1fc35a Return a non zero code when db has never been setup on status 2014-05-07 18:33:18 -03:00
Emil Soman
8806768e9f Add config to disable schema dump after migration
* Add a config on Active Record named `dump_schema_after_migration`
* Schema dump doesn't happen if the config is set to false
* Set default value of the config to true
* Set config in generated production environment file to false
* Update configuration guide
* Update CHANGELOG
2014-02-06 17:38:31 +05:30
Marc-Andre Lafortune
a4932d6a63 Fixes for PR [#8267]
* Fix Migration#reversible by not using `transaction`.

* Adapt mysql adapter to updated api for remove_column

* Update test after aedcd683684d08eaf30623a4b48ce31a31426372
2012-12-22 20:40:42 -05:00
Carlos Antonio da Silva
73e8e70066 Use one system call whenever possible, group rake and Dir.chdir calls 2012-03-08 19:57:17 -03:00
Peter Mitchell
910c59fffe Correctly print names of non-timestamped migrations with db:migrate:status 2012-02-17 16:58:11 +01:00
Aaron Patterson
8f309e3105 convert railties to use AS::TestCase 2012-01-05 17:30:17 -08:00
José Valim
283a087634 Clean up the cache before the request in case we are running in the reload_classes_only_on_change schema. 2011-12-15 18:48:10 +01:00
Piotr Sarnacki
866d2dbd87 Fix indentation 2011-12-09 22:00:56 +01:00
Piotr Sarnacki
35a1744a45 Allow to run migrations with given scope, with SCOPE=<scope>
Scope in migrations can be defined by adding suffix in filename,
like: 01_a_migration.blog.rb. Such migration have blog scope.

Scope is automatically added while copying migrations from engine,
so if you want to revert all of the migrations from given engine,
you can just run db:migrate with SCOPE, like:

    rake db:migrate SCOPE=blog
2011-12-09 22:00:51 +01:00
Kazimierz Kiełkowicz
c6e6ce437c Moves migrations tests from railties/test/application/rake_test.rb to railties/test/application/rake/migrations_test.rb 2011-12-07 17:34:29 +01:00