Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent db:schema:load to protected environments #24399

Merged
merged 1 commit into from Apr 6, 2016
Merged

Prevent db:schema:load to protected environments #24399

merged 1 commit into from Apr 6, 2016

Conversation

jch
Copy link
Contributor

@jch jch commented Apr 2, 2016

Follow up to #22967 to protect against loading a schema on accident in production.

cc @schneems

Follow up to #22967 to protect against
loading a schema on accident in production.

cc @schneems
@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @sgrif (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@schneems
Copy link
Member

schneems commented Apr 4, 2016

When loading the schema can you take destructive actions?

@jch
Copy link
Contributor Author

jch commented Apr 4, 2016

@schneems when I tested locally, it blows away the existing data and creates fresh tables.

@schneems
Copy link
Member

schneems commented Apr 6, 2016

Can confirm

$ rails -v
riRails 5.0.0.beta3
2.3.0  /tmp
$ rails new foo
      create
      create  README.md
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      create  app
      create  app/assets/config/manifest.js
      create  app/assets/javascripts/application.js
      create  app/assets/javascripts/cable.coffee
      create  app/assets/stylesheets/application.css
      create  app/channels/application_cable/channel.rb
      create  app/channels/application_cable/connection.rb
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/jobs/application_job.rb
      create  app/mailers/application_mailer.rb
      create  app/models/application_record.rb
      create  app/views/layouts/application.html.erb
      create  app/views/layouts/mailer.html.erb
      create  app/views/layouts/mailer.text.erb
      create  app/assets/images/.keep
      create  app/assets/javascripts/channels
      create  app/assets/javascripts/channels/.keep
      create  app/controllers/concerns/.keep
      create  app/models/concerns/.keep
      create  bin
      create  bin/bundle
      create  bin/rails
      create  bin/rake
      create  bin/setup
      create  bin/update
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/secrets.yml
      create  config/cable.yml
      create  config/puma.rb
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/active_record_belongs_to_required_by_default.rb
      create  config/initializers/application_controller_renderer.rb
      create  config/initializers/assets.rb
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/callback_terminator.rb
      create  config/initializers/cookies_serializer.rb
      create  config/initializers/cors.rb
      create  config/initializers/filter_parameter_logging.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/per_form_csrf_tokens.rb
      create  config/initializers/request_forgery_protection.rb
      create  config/initializers/session_store.rb
      create  config/initializers/wrap_parameters.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  lib
      create  lib/tasks
      create  lib/tasks/.keep
      create  lib/assets
      create  lib/assets/.keep
      create  log
      create  log/.keep
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/apple-touch-icon-precomposed.png
      create  public/apple-touch-icon.png
      create  public/favicon.ico
      create  public/robots.txt
      create  test/fixtures
      create  test/fixtures/.keep
      create  test/fixtures/files
      create  test/fixtures/files/.keep
      create  test/controllers
      create  test/controllers/.keep
      create  test/mailers
      create  test/mailers/.keep
      create  test/models
      create  test/models/.keep
      create  test/helpers
      create  test/helpers/.keep
      create  test/integration
      create  test/integration/.keep
      create  test/test_helper.rb
      create  tmp
      create  tmp/.keep
      create  tmp/cache
      create  tmp/cache/assets
      create  vendor/assets/javascripts
      create  vendor/assets/javascripts/.keep
      create  vendor/assets/stylesheets
      create  vendor/assets/stylesheets/.keep
      remove  config/initializers/cors.rb
         run  bundle install
cd foo
Fetching gem metadata from https://rubygems.org/..rails. ...g .mi.g...r
Fetching version metadata from https://rubygems.org/.ation add..
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies..............
Using concurrent-ruby 1.0.1
Using i18n 0.7.0
Using minitest 5.8.4
Using thread_safe 0.3.5
Using builder 3.2.2
Using erubis 2.7.0
Using mini_portile2 2.0.0
Using json 1.8.3
Using nio4r 1.2.1
Using websocket-extensions 0.1.2
Using mime-types-data 3.2016.0221
Using arel 7.0.0
Using bundler 1.11.2
Using byebug 8.2.2
Using coffee-script-source 1.10.0
Using execjs 2.6.0
Using method_source 0.8.2
Using thor 0.19.1
Using debug_inspector 0.0.2
Using ffi 1.9.10
Using multi_json 1.11.2
Using rb-fsevent 0.9.7
Installing rake 11.1.2
Installing puma 3.3.0 with native extensions
Installing sass 3.4.22
Using tilt 2.0.2
Using spring 1.6.4
Using sqlite3 1.3.11
Installing turbolinks-source 5.0.0.beta4
Using tzinfo 1.2.2
Using nokogiri 1.6.7.2
Using rack 2.0.0.alpha
Using websocket-driver 0.6.3
Using mime-types 3.0
Using coffee-script 2.4.1
Installing uglifier 3.0.0
Using rb-inotify 0.9.7
Using turbolinks 5.0.0.beta2
Using activesupport 5.0.0.beta3
Using loofah 2.0.3
Using rack-test 0.6.3
Using sprockets 3.5.2
Using mail 2.6.4
Using listen 3.0.6
Using rails-deprecated_sanitizer 1.0.3
Using globalid 0.3.6
Using activemodel 5.0.0.beta3
Using jbuilder 2.4.1
Using rails-html-sanitizer 1.0.3
Using spring-watcher-listen 2.0.0
Using rails-dom-testing 1.0.7
Using activejob 5.0.0.beta3
Using activerecord 5.0.0.beta3
Using actionview 5.0.0.beta3
Using actionpack 5.0.0.beta3
Using actioncable 5.0.0.beta3
Using actionmailer 5.0.0.beta3
Using railties 5.0.0.beta3
Using sprockets-rails 3.0.4
Using coffee-rails 4.1.1
Using jquery-rails 4.1.1
Using web-console 3.1.1
Using rails 5.0.0.beta3
Using sass-rails 5.0.4
Bundle complete! 15 Gemfile dependencies, 64 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.
         run  bundle exec spring binstub --all
* bin/rake: spring inserted
* bin/rails: spring inserted
2.3.0  /tmp
$ cd foo
2.3.0  /tmp/foo
$ rails g scaffold user name email
      invoke  active_record
      create    db/migrate/20160406173152_create_users.rb
      create    app/models/user.rb
      invoke    test_unit
      create      test/models/user_test.rb
      create      test/fixtures/users.yml
      invoke  resource_route
       route    resources :users
      invoke  scaffold_controller
      create    app/controllers/users_controller.rb
      invoke    erb
      create      app/views/users
      create      app/views/users/index.html.erb
      create      app/views/users/edit.html.erb
      create      app/views/users/show.html.erb
      create      app/views/users/new.html.erb
      create      app/views/users/_form.html.erb
      invoke    test_unit
      create      test/controllers/users_controller_test.rb
      invoke    helper
      create      app/helpers/users_helper.rb
      invoke      test_unit
      invoke    jbuilder
      create      app/views/users/index.json.jbuilder
      create      app/views/users/show.json.jbuilder
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/users.coffee
      invoke    scss
      create      app/assets/stylesheets/users.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.scss
2.3.0  /tmp/foo
$ rake db:migrate
rails console
== 20160406173152 CreateUsers: migrating ======================================
-- create_table(:users)
   -> 0.0013s
== 20160406173152 CreateUsers: migrated (0.0014s) =============================

2.3.0  /tmp/foo
$ rails console
User.newLoading development environment (Rails 5.0.0.beta3)
irb(main):001:0> User.new(name: 'schneems').save
   (0.0ms)  begin transaction
  SQL (0.4ms)  INSERT INTO "users" ("name", "created_at", "updated_at") VALUES (?, ?, ?)  [["name", "schneems"], ["created_at", 2016-04-06 17:32:08 UTC], ["updated_at", 2016-04-06 17:32:08 UTC]]
   (0.8ms)  commit transaction
=> true
irb(main):002:0> User.count
   (0.2ms)  SELECT COUNT(*) FROM "users"
=> 1
irb(main):003:0> exit
2.3.0  /tmp/foo
$ rake db:schema:load
-- create_table("users", {:force=>:cascade})
   -> 0.0263s
-- initialize_schema_migrations_table()
   -> 0.0039s
-- create_table("users", {:force=>:cascade})
   -> 0.0025s
-- initialize_schema_migrations_table()
   -> 0.0016s
2.3.0  /tmp/foo
$ rails c
Loading development environment (Rails 5.0.0.beta3)
irb(main):001:0> User.count
   (0.1ms)  SELECT COUNT(*) FROM "users"
=> 0

@schneems
Copy link
Member

schneems commented Apr 6, 2016

Thanks for the PR!

@schneems schneems merged commit c908a90 into rails:master Apr 6, 2016
@jch jch deleted the protect-against-db-schema-load-in-production branch April 7, 2016 19:06
prathamesh-sonpatki added a commit to prathamesh-sonpatki/rails that referenced this pull request Apr 10, 2016
…file

- Check for protected environments while running `db:structure:load`
  similar to how `db:schema:load` behaves.
- Followup of rails#24399.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants