bio photo

Email

MySql - Migrations

Concept of migration files, how to generate and edit them through rails commands. And - how their nomenclature explained. Source.

We need to create migrations for backend.timesvr.com/myadmin database changes, in particular for introducing pools.

To access database structure via production MySQL - source

mysql -u root -p

show databases;
use timesvr_production;
show tables;
desc task_pools;

# will show:

+------------+----------+------+-----+---------+----------------+
| Field      | Type     | Null | Key | Default | Extra          |
+------------+----------+------+-----+---------+----------------+
| id         | int(11)  | NO   | PRI | NULL    | auto_increment |
| task_id    | int(11)  | YES  | MUL | NULL    |                |
| pool_id    | int(11)  | YES  | MUL | NULL    |                |
| created_at | datetime | YES  |     | NULL    |                |
| updated_at | datetime | YES  |     | NULL    |                |
+------------+----------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

Back on local we generate model migration:

$bundle exec rails generate model task_pools

This will create a migration file in db/migrate folder of app and will look like this:

class CreateTaskPools < ActiveRecord::Migration
  def change
    create_table :task_pools do |t|

      t.timestamps
    end
  end
end

#filling out corresponding fields in this migration file,

class CreateTaskPools < ActiveRecord::Migration
  def change
    create_table :task_pools do |t|

      t.references :task
      t.references :pool

      t.timestamps
    end
  end
end

#note "id", "created_at" and "updated_at" are autogenerated hence ignored. task_id and pool_id are referenced fields from field "task" and "pool" hence nomenclature "t.reference :task".

#Now run migration to see if the migration file was created correctly. It is. 

$bundle exec rake db:migrate 

Next we compare database entries for conflicts on both VPS and local

$bundle exec rake db:migrate:down VERSION=20170403002821 $bundle exec rake db:migrate

That didn’t work. So trying the redo migrate command. Editing timestamp_create_aide_pools.rb file to make table edits and then running:

$ bundle exec rake db:migrate:redo VERSION=20170403002821

IT WORKED!

We are having issues with MUL keys, here are resources to experiment with:

MUL keys are there in in two database fileds of two separate tables in production they're not there in local. Below. I read up on them and it seems two solutions are possible - either add them through SQL commands (complex) or (possibly) by editing migration files with -> "unique = true" commands. I'll experiment with this. Below are the tables with MUL key difference.


desc task_pools;

linode

+------------+----------+------+-----+---------+----------------+

| Field      | Type     | Null | Key | Default | Extra          |

+------------+----------+------+-----+---------+----------------+

| id         | int(11)  | NO   | PRI | NULL    | auto_increment |

| task_id    | int(11)  | YES  | MUL | NULL    |                |

| pool_id    | int(11)  | YES  | MUL | NULL    |                |

| created_at | datetime | YES  |     | NULL    |                |

| updated_at | datetime | YES  |     | NULL    |                |

+------------+----------+------+-----+---------+----------------+

5 rows in set (0.00 sec) 



desc task_pools;

+------------+----------+------+-----+---------+----------------+

| Field      | Type     | Null | Key | Default | Extra          |

+------------+----------+------+-----+---------+----------------+

| id         | int(11)  | NO   | PRI | NULL    | auto_increment |

| task_id    | int(11)  | YES  |     | NULL    |                |

| pool_id    | int(11)  | YES  |     | NULL    |                |

| created_at | datetime | YES  |     | NULL    |                |

| updated_at | datetime | YES  |     | NULL    |                |

+------------+----------+------+-----+---------+----------------+

5 rows in set (0.00 sec)

Ngrok

It’s in your local downloads folder.

Run app locally in terminal.

In another terminal window run in downloads

  ./ngrok http 3001 (or which ever port you are serving from)

Share https (ngrok.io) url with tester

Getting Sign Up + Sign In to work on LocalHost - Create Coupon

1. Under /db/migrate/006_create_coupons.rb

class CreateCoupons < ActiveRecord::Migration
  def self.up
    create_table :coupons do |t|
      t.string :code

      # Settings for Personal Users
      t.float :personal_a1 #Trial period 1 price
      t.integer :personal_p1 #Trial period 1 duration
      t.string :personal_t1 #length unit (D W M Y) for period 1

      t.float :personal_a2 #Trial period 2 price
      t.integer :personal_p2 #Trial period 2 duration
      t.string :personal_t2 #length unit (D W M Y) for period 2

      t.float :personal_a3 #Regular subscription price.	  
      t.integer :personal_p3 #Subscription duration
      t.string :personal_t3 #length unit (D W M Y) for regular subscription
      # End of settings for Personal Users #

      # Settings for Dedicated Users #
      t.float :dedicated_weekly_a1 #Trial period 1 price
      t.integer :dedicated_weekly_p1 #Trial period 1 duration
      t.string :dedicated_weekly_t1 #length unit (D W M Y) for period 1

      t.float :dedicated_weekly_a3 #Regular subscription price.	  
      t.integer :dedicated_weekly_p3 #Subscription duration
      t.string :dedicated_weekly_t3 #length unit (D W M Y) for period 2

      t.float :dedicated_monthly_a1 #Trial period 1 price
      t.integer :dedicated_monthly_p1 #Trial period 1 duration
      t.string :dedicated_monthly_t1 #length unit (D W M Y) for period 1

      t.float :dedicated_monthly_a3 #Regular subscription price.	  
      t.integer :dedicated_monthly_p3 #Subscription duration
      t.string :dedicated_monthly_t3 #length unit (D W M Y) for period 2
      
      #values for hourly rates
      t.float :dedicated_10_hourly 
      t.float :dedicated_20_hourly
      t.float :dedicated_35_hourly
      t.float :dedicated_50_hourly
      # End of settings for Dedicated Users #
      
      # whether the coupon is for internal use only
      t.boolean :internal, :default => 0, :null => false
      
      t.datetime :expiry
      t.timestamps
    end
  end

  def self.down
    drop_table :coupons
  end
end

2. All A1, P1, T1 and A3, P3, T3 need to have non-nil and non zero values

This way,

coupon = Coupon.new
coupon.internal = true
coupon.personal_a1 = 1
...
coupon.save

TimeSvr main index css/html update

popup Form

modal to rails - 1 modal to rails - 2

There is a splash header html and header html. Logic of which gets displayed on index.html is dictated here:

app/views/layouts/application.html.erb

and like so:

<% if @splash_page == true %>
  <%= render :partial => 'main/splash_header' %>
<% else %>
  <%= render :partial => 'main/header' %>
<% end %>

Also, in app/controllers/main_controller.rb -> if you’re logged in and splash_page true, you are redirected to dashboard

def index
  @splash_page = true
  redirect_to :controller => 'dashboard', :action => :index if logged_in?
end

Make copies of all css/html/navigation files and start with blank canvas

$ cp _footer.html.erb _footer_old.html.erb
$ cp _splash_header.html.erb _splash_header_old.html.erb
$ cp _nav_links.html.erb _nav_links_old.html.erb
$ cp _header.html.erb _header_old.html.erb
$ cp index.html.erb index_old.html.erb
$ cp index.css index_old.css
$ cp main.css main_old.css

TimeSvr.com Mac configuration

To load database when cloning existing rails app, do:

bundle exec rake db:schema:load

and not rake db:migrate (this runs all migrations..database changes..from start of app till current time. We don’t want that as it creates conflicts)