Posts

Showing posts from 2019

Deploy Reatjs app on Heroku

1. React JS app npm install -g create-react-app create-react-app my-app cd my-app git init heroku create -b https://github.com/mars/create-react-app-buildpack.git git add . git commit -m "react-create-app on Heroku" git push heroku master heroku open OR if you already have app created then add  mars/create-react-app then set heroku buildpacks:set mars/create-react-app 2. If following error coming then set bundle path:  ls: cannot access '/app/build/static/js/*.js': No such file or directory 2019-12-23T07:49:22.901124+00:00 app[web.1]: Error injecting runtime env: bundle not found '/app/build/static/js/*.js'. See: https://github.com/mars/create-react-app-buildpack/blob/master/README.md#user-content-custom-bundle-location heroku config:set JS_RUNTIME_TARGET_BUNDLE='/app/dist/*.js' Make sure your bundle into directory /app/dist OR if you have your bundle into other directory then set other directory pa

Add Android devices in n file trasfer mode and set USB

For android connect the cellphone in file trasfer mode and set USB debugging mode under developer options add the device to your linux machine  lsusb  list the usb devices connected and shows something like Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub Bus 001 Device 005: ID 04ca:3010 Lite-On Technology Corp. Bus 001 Device 003: ID 04f2:b483 Chicony Electronics Co., Ltd Bus 001 Device 002: ID 04f3:0235 Elan Microelectronics Corp. Bus 001 Device 008: ID *12d1:107e* Huawei Technologies Co., Ltd. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub after this run  echo 'SUBSYSTEM=="usb", ATTR{idVendor}=="12d1", MODE="0666", GROUP="plugdev"' | sudo tee /etc/udev/rules.d/51-android-usb.rules  Finally start the app on your device  ract-native run-android

Add CORS to Nginx on AWS Elastic Beanstalk

Image
Apache Add the following block to your  .htaccess  file: <FilesMatch ".(eot|otf|ttf|woff|woff2)"> Header always set Access-Control-Allow-Origin "*" </FilesMatch> Nginx Add the following location block to your virtual host file (usually within the server directive) and reload Nginx: location ~* \.(eot|otf|ttf|woff|woff2)$ { add_header Access-Control-Allow-Origin *; } Invalidate CloudFront Distribution Once the changes have been made you will need to  invalidate the CloudFront cache  with a path of “/*”. Integrating to CloudFront service It’s simple to config and use CloudFront (a CDN service) for Rails app.  Just one thing from my experience, regarding to CORS issue on EB when your CSS want get loading font files or font-awesome. Because EB run your app with Nginx server serve public static files, then work around would be overwriting web server config in Nginx with  add_header Access-Control-Allow-Origin ‘*’  by crea

Add auto increment column in Postgres in Rails

class AddAutoIncrements < ActiveRecord::Migration def self.up add_column :designs, :custom_id, :integer execute <<-SQL CREATE SEQUENCE custom_id_seq START 1; ALTER SEQUENCE custom_id_seq OWNED BY designs.custom_id; ALTER TABLE designs ALTER COLUMN custom_id SET DEFAULT nextval('custom_id_seq'); SQL end def self.down remove_column :designs, :custom_id execute <<-SQL DROP SEQUENCE custom_id_seq; SQL end end OR Added by Active record: class AddAutoIncrements < ActiveRecord::Migration def self.up add_column :users, :custom_id, :integer ActiveRecord::Base.connection.execute("CREATE SEQUENCE users_id_seq") ActiveRecord::Base.connection.execute("ALTER SEQUENCE users_id_seq OWNED BY users.id") ActiveRecord::Base.connection.execute("ALTER TABLE users ALTER COLUMN id SET DEFAULT nextval('users_id_seq')") end end def self.down remove_column

ERROR: null value in column "id" violates not-null constraint

This is easier: ActiveRecord :: Base . connection . tables . each do | t | ActiveRecord :: Base . connection . reset_pk_sequence !( t ) end If sequence missing then create new sequence to all database table:           ActiveRecord::Base.connection.tables.each do |t|      ActiveRecord::Base.connection.execute("CREATE SEQUENCE #{t}_id_seq")   end

Setup FREE SSL using Let's Encrypt.

Setup SSL There are multiple services out there offering SSL certificates for your domain, many are paid and a couple are free. Setup SSL no matter what service you use.  Let's Encrypt  is very easy, and free, to use. Let's Encrypt is a non-profit organization backed by many tech giants. Installation Run the following commands, and follow the prompts. sudo apt-get update sudo apt-get install software-properties-common sudo add-apt-repository ppa:certbot/certbot sudo apt-get update sudo apt-get install python-certbot-nginx sudo certbot --nginx sudo certbot renew --dry-run If you go to your Vultr instance now using  HTTPS , you will see a secure website. Renewal Your newly issued certificate will expire, and will need to be renewed periodically. The easiest way to do this is to set up a cron job. sudo crontab -e 30 2 * * 1 /usr/bin/certbot renew As a final, but optional step, reboot your VM and make sure all services restart as expected.

Rspec for scopes in rails

 describe "Scopes" do       it "should be active with status_id 3" do       User.active.should include(@user)       end       it "should not be suspended without status_id 4" do           User.suspended.should_not include(@user)       end       it "should not be inactive without status_id 5" do           User.inactive.should_not include(@user)       end       it "should not be hidden without status_id 6" do           User.hidden.should_not include(@user)       end       it "should not be deleted without status_id 7" do           User.deleted.should_not include(@user)       end   end

Find all records with at least one child in rails

### Find all records with at least one child Parent . join ( :children ) . uniq . al l # or Parent . includes ( :children ) . whe re ( " children.id IS NOT NULL" ) # or Rails 5 Parent . left_outer_joins ( : children ) . where . not ( children : { id : nil } ) ### Find all records with no children Parent . includes ( :children ) . whe re ( children : { id : nil } ) # or Rails 5 Parent . left_outer_joins ( : children ) . where ( children : { id : nil } )

Backup heroku database and saved on AWS s3

Add gem "fog" Create rake file lib/tasks/db.rake namespace :db do   desc "Backs up heroku database and saved on AWS s3."   task import_from_heroku: [ :environment, :create ] do     HEROKU_APP_NAME = 'appname' # Change this if app name is not picked up by `heroku` git remote.     dump_filename = "appname_prod_#{Time.now.strftime('%d-%m-%Y')}"     c = Rails.configuration.database_configuration[Rails.env]     heroku_app_flag = HEROKU_APP_NAME ? " --app #{HEROKU_APP_NAME}" : nil     Bundler.with_clean_env do       puts "[1/4] Capturing backup on Heroku"       `heroku pg:backups capture DATABASE_URL#{heroku_app_flag}`       puts "[2/4] Downloading backup onto disk"       `curl -o tmp/#{dump_filename}.dump \`heroku pg:backups public-url #{heroku_app_flag} | cat\``       puts "[3/4] Mounting backup on local database"       # `pg_restore --clean --verbose --no-acl --no-owner -h localhost -d #