Skip to content

Commit

Permalink
Merge pull request #153 from ryuzee/new_db_engine
Browse files Browse the repository at this point in the history
[WIP] New db engine
  • Loading branch information
ryuzee committed Feb 21, 2017
2 parents 8271433 + b377f91 commit 9c397a9
Show file tree
Hide file tree
Showing 32 changed files with 222 additions and 148 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ gem 'sdoc', '~> 0.4.0', group: :doc
# Use mysql
gem 'mysql2'

# Use SQLServer
gem 'tiny_tds'
gem 'activerecord-sqlserver-adapter'

# Apply bootstrap
gem 'less-rails'

Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ GEM
activemodel (= 5.0.0.1)
activesupport (= 5.0.0.1)
arel (~> 7.0)
activerecord-sqlserver-adapter (5.0.4)
activerecord (~> 5.0.0)
activesupport (5.0.0.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (~> 0.7)
Expand Down Expand Up @@ -487,6 +489,8 @@ GEM
tilt (2.0.5)
timers (4.1.1)
hitimes
tiny_tds (1.1.0)
mini_portile2 (~> 2.0)
ttfunk (1.4.0)
turbolinks (5.0.1)
turbolinks-source (~> 5)
Expand Down Expand Up @@ -537,6 +541,7 @@ PLATFORMS

DEPENDENCIES
activerecord-nulldb-adapter!
activerecord-sqlserver-adapter
acts-as-taggable-on (~> 4.0)
acts_as_commentable!
annotate
Expand Down Expand Up @@ -610,6 +615,7 @@ DEPENDENCIES
sprockets (= 3.6.3)
sqlite3
therubyracer
tiny_tds
turbolinks
twitter-bootstrap-rails
uglifier (>= 1.3.0)
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ docker pull ryuzee/slidehub:latest
### Run Application

```
/usr/bin/docker run -d \
$CONTAINER_ID=/usr/bin/docker run -d \
--env OSS_REGION=$OSS_REGION \
--env OSS_SQS_URL=$OSS_SQS_URL \
--env OSS_BUCKET_NAME=$OSS_BUCKET_NAME \
Expand Down Expand Up @@ -212,6 +212,14 @@ docker pull ryuzee/slidehub:latest
-P --name slidehub ryuzee/slidehub:latest`
```

Then prepare database as follows.

```
`docker exec $CONTAINER_ID bash -l -c 'bundle exec rake db:create RAILS_ENV=production'`
`docker exec $CONTAINER_ID bash -l -c 'bundle exec rake db:migrate RAILS_ENV=production'`
`docker exec $CONTAINER_ID bash -l -c 'bundle exec rake db:seed RAILS_ENV=production'
```

### For Development mode

You can use docker-compose for development. Try the commands as follows.
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/concerns/slide_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ def set_slide
end

def download_slide
url = CloudConfig::SERVICE.get_slide_download_url(@slide.key)
url = CloudConfig::SERVICE.get_slide_download_url(@slide.object_key)
# @TODO: handle response code
require 'open-uri'
data = open(url).read
send_data data, disposition: 'attachment', filename: "#{@slide.key}#{@slide.extension}"
send_data data, disposition: 'attachment', filename: "#{@slide.object_key}#{@slide.extension}"
end

def slide_position
Expand Down
20 changes: 10 additions & 10 deletions app/controllers/slides_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# category_id :integer not null
# created_at :datetime not null
# modified_at :datetime
# key :string(255) default("")
# object_key :string(255) default("")
# extension :string(10) default(""), not null
# convert_status :integer default(0)
# total_view :integer default(0), not null
Expand Down Expand Up @@ -51,20 +51,20 @@ def new
end

def destroy
CloudConfig::SERVICE.delete_slide(@slide.key)
CloudConfig::SERVICE.delete_generated_files(@slide.key)
CloudConfig::SERVICE.delete_slide(@slide.object_key)
CloudConfig::SERVICE.delete_generated_files(@slide.object_key)
@slide.destroy
redirect_to slides_path
end

def create
key = params[:slide][:key]
slide_params = params.require(:slide).permit(:name, :description, :key, :downloadable, :category_id, :tag_list)
key = params[:slide][:object_key]
slide_params = params.require(:slide).permit(:name, :description, :object_key, :downloadable, :category_id, :tag_list)
@slide = Slide.new(slide_params)
@slide.user_id = current_user.id
if @slide.save
slide = Slide.where('slides.key = ?', key).first
CloudConfig::SERVICE.send_message({ id: slide.id, key: key }.to_json)
slide = Slide.where('slides.object_key = ?', key).first
CloudConfig::SERVICE.send_message({ id: slide.id, object_key: key }.to_json)
redirect_to slide_path(slide.id)
else
render "slides/#{CloudConfig.service_name}/new"
Expand All @@ -76,13 +76,13 @@ def edit
end

def update
slide_params = params.require(:slide).permit(:name, :description, :key, :downloadable, :category_id, :tag_list, :convert_status)
slide_params = params.require(:slide).permit(:name, :description, :object_key, :downloadable, :category_id, :tag_list, :convert_status)
slide_convert_status = params[:slide][:convert_status].to_i

@slide.assign_attributes(slide_params)
if @slide.update_attributes(slide_params)
if slide_convert_status.zero?
CloudConfig::SERVICE.send_message({ id: @slide.id, key: @slide.key }.to_json)
CloudConfig::SERVICE.send_message({ id: @slide.id, object_key: @slide.object_key }.to_json)
end
redirect_to slide_path(@slide.id)
else
Expand All @@ -101,6 +101,6 @@ def owner?
end

def duplicate_key?
redirect_to slides_path if Slide.key_exist?(params[:slide][:key])
redirect_to slides_path if Slide.key_exist?(params[:slide][:object_key])
end
end
16 changes: 8 additions & 8 deletions app/models/slide.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# category_id :integer not null
# created_at :datetime not null
# modified_at :datetime
# key :string(255) default("")
# object_key :string(255) default("")
# extension :string(10) default(""), not null
# convert_status :integer default(0)
# total_view :integer default(0), not null
Expand All @@ -41,8 +41,8 @@ class Slide < ActiveRecord::Base
validates :name, length: { maximum: 255 }
validates :description, presence: true
validates :description, length: { maximum: 2048 }
validates :key, presence: true
validates :key, uniqueness: true
validates :object_key, presence: true
validates :object_key, uniqueness: true
validates :category_id, presence: true
# validates :category_id, :presence => true, :inclusion => { :in => Category.all.map(&:id) }

Expand Down Expand Up @@ -70,7 +70,7 @@ def self.ransackable_attributes(auth_object = nil)
end

def self.key_exist?(key)
Slide.where('slides.key = ?', key).count > 0
Slide.where('slides.object_key = ?', key).count > 0
end

def self.related_slides(category_id, slide_id, limit = 10)
Expand All @@ -82,23 +82,23 @@ def self.related_slides(category_id, slide_id, limit = 10)
end

def thumbnail_url
"#{CloudConfig::SERVICE.resource_endpoint}/#{key}/thumbnail.jpg"
"#{CloudConfig::SERVICE.resource_endpoint}/#{object_key}/thumbnail.jpg"
end

def transcript_url
"#{CloudConfig::SERVICE.resource_endpoint}/#{key}/transcript.txt"
"#{CloudConfig::SERVICE.resource_endpoint}/#{object_key}/transcript.txt"
end

def page_list_url
"#{CloudConfig::SERVICE.resource_endpoint}/#{key}/list.json"
"#{CloudConfig::SERVICE.resource_endpoint}/#{object_key}/list.json"
end

def page_list
len = num_of_pages.abs.to_s.length
result = []
(1..num_of_pages).each do |i|
n = i.to_s.rjust(len, '0')
result.push("#{key}/slide-#{n}.jpg")
result.push("#{object_key}/slide-#{n}.jpg")
end
result
end
Expand Down
6 changes: 3 additions & 3 deletions app/views/admin/slides/_slide_edit_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ $(function(){
<%= f.error_span(:modified_at) %>
</div>
<div class="control-group">
<%= f.label :key, :class => 'control-label' %>
<%= f.label :object_key, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :key, :class => 'form-control' %>
<%= f.text_field :object_key, :class => 'form-control' %>
</div>
<%= f.error_span(:key) %>
<%= f.error_span(:object_key) %>
</div>
<div class="control-group">
<%= f.label :extension, :class => 'control-label' %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/api/v1/slides/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
json.prettify!
json.array!(@slides) do |slide|
json.extract! slide, 'id', 'user_id', 'name', 'description', 'category_id', 'key', 'extension', 'num_of_pages', 'created_at', 'category_name'
json.extract! slide, 'id', 'user_id', 'name', 'description', 'category_id', 'object_key', 'extension', 'num_of_pages', 'created_at', 'category_name'
json.extract! slide.user, 'username'
json.thumbnail_url slide.thumbnail_url
json.transcript_url slide.transcript_url
Expand Down
2 changes: 1 addition & 1 deletion app/views/api/v1/slides/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
json.prettify!
json.extract! @slide, 'id', 'user_id', 'name', 'description', 'category_id', 'key', 'extension', 'num_of_pages', 'created_at', 'category_name'
json.extract! @slide, 'id', 'user_id', 'name', 'description', 'category_id', 'object_key', 'extension', 'num_of_pages', 'created_at', 'category_name'
json.extract! @slide.user, 'username'
json.thumbnail_url @slide.thumbnail_url
json.transcript_url @slide.transcript_url
Expand Down
4 changes: 2 additions & 2 deletions app/views/slides/aws/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</div>

<% policy = CloudConfig::SERVICE.create_policy %>
<input class="fval" type="hidden" id="key" name="key" value='<%= @slide.key %>' class="form-control"/>
<input class="fval" type="hidden" id="object_key" name="object_key" value='<%= @slide.object_key %>' class="form-control"/>
<input class="fval" type="hidden" name="acl" value="<%= policy['acl'] %>" />
<input class="fval" type="hidden" name="success_action_status" value="<%= policy['success_action_status'] %>" />
<input class="fval" type="hidden" id="content_type" name="Content-Type" value="application/octetstream" class="form-control"/>
Expand Down Expand Up @@ -104,7 +104,7 @@

<%= f.hidden_field :id, :value => @slide.id %>
<%= f.hidden_field :convert_status, :id => 'SlideConvertStatus', :value => @slide.convert_status %>
<%= f.hidden_field :key, :id => 'SlideKey', :value => @slide.key %>
<%= f.hidden_field :object_key, :id => 'SlideKey', :value => @slide.object_key %>
<div class="form-group">
<%= f.label t('activerecord.models.tag'), :class => 'control-label col-sm-2' %>
<div class="col-sm-10">
Expand Down
4 changes: 2 additions & 2 deletions app/views/slides/aws/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</div>
</div>
<% policy = CloudConfig::SERVICE.create_policy %>
<input class="fval" type="hidden" id="key" name="key" value='<%= rand_name %>' class="form-control"/>
<input class="fval" type="hidden" id="object_key" name="object_key" value='<%= rand_name %>' class="form-control"/>
<input class="fval" type="hidden" name="acl" value="<%= policy['acl'] %>" />
<input class="fval" type="hidden" name="success_action_status" value="<%= policy['success_action_status'] %>" />
<input class="fval" type="hidden" id="content_type" name="Content-Type" value="application/octetstream" class="form-control"/>
Expand Down Expand Up @@ -92,7 +92,7 @@
<%= f.error_span(:category_id) %>
</div>
</div>
<%= f.hidden_field :key, :id => 'SlideKey' %>
<%= f.hidden_field :object_key, :id => 'SlideKey' %>
<div class="form-group">
<%= f.label t('activerecord.models.tag'), :class => 'control-label col-sm-2' %>
<div class="col-sm-10">
Expand Down
4 changes: 2 additions & 2 deletions app/views/slides/azure/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<% content_for :title, t(:edit_slide) %>
<%= render :partial => 'slides/azure/upload_js', :locals => { :command => 'edit', :rand_name => @slide.key } %>
<%= render :partial => 'slides/azure/upload_js', :locals => { :command => 'edit', :rand_name => @slide.object_key } %>

<div class="row">
<div class="col-md-8">
Expand Down Expand Up @@ -89,7 +89,7 @@

<%= f.hidden_field :id, :value => @slide.id %>
<%= f.hidden_field :convert_status, :id => 'SlideConvertStatus', :value => @slide.convert_status %>
<%= f.hidden_field :key, :id => 'SlideKey', :value => @slide.key %>
<%= f.hidden_field :object_key, :id => 'SlideKey', :value => @slide.object_key %>
<div class="form-group">
<%= f.label t('activerecord.models.tag'), :class => 'control-label col-sm-2' %>
<div class="col-sm-10">
Expand Down
2 changes: 1 addition & 1 deletion app/views/slides/azure/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
<%= f.error_span(:category_id) %>
</div>
</div>
<%= f.hidden_field :key, :id => 'SlideKey' %>
<%= f.hidden_field :object_key, :id => 'SlideKey' %>
<div class="form-group">
<%= f.label t('activerecord.models.tag'), :class => 'control-label col-sm-2' %>
<div class="col-sm-10">
Expand Down
15 changes: 9 additions & 6 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,37 @@
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
adapter: mysql2
adapter: <%= ENV['OSS_DB_ENGINE_DEV'] ||= 'mysql2' %>
encoding: utf8
reconnect: false
database: <%= ENV['OSS_DB_NAME_DEV'] ||= 'openslideshare' %>
database: <%= ENV['OSS_DB_NAME_DEV'] ||= 'slidehub' %>
pool: 5
username: <%= ENV['OSS_DB_USERNAME_DEV'] %>
password: <%= ENV['OSS_DB_PASSWORD_DEV'] %>
host: <%= ENV['OSS_DB_URL_DEV'] %>
port: <%= ENV['OSS_DB_PORT_DEV'] ||= '3306' %>

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: mysql2
adapter: <%= ENV['OSS_DB_ENGINE_TEST'] ||= 'mysql2' %>
encoding: utf8
reconnect: false
database: <%= ENV['OSS_DB_NAME_TEST'] ||= 'test_openslideshare' %>
database: <%= ENV['OSS_DB_NAME_TEST'] ||= 'test_slidehub' %>
pool: 5
username: <%= ENV['OSS_DB_USERNAME_TEST'] %>
password: <%= ENV['OSS_DB_PASSWORD_TEST'] %>
host: <%= ENV['OSS_DB_URL_TEST'] %>
port: <%= ENV['OSS_DB_PORT_TEST'] ||= '3306' %>

production:
adapter: <%= ENV['DB_ADAPTER'] ||= 'mysql2' %>
adapter: <%= ENV['OSS_DB_ENGINE'] ||= 'mysql2' %>
encoding: utf8
reconnect: false
database: <%= ENV['OSS_DB_NAME'] ||= 'openslideshare' %>
database: <%= ENV['OSS_DB_NAME'] ||= 'slidehub' %>
pool: 5
username: <%= ENV['OSS_DB_USERNAME'] %>
password: <%= ENV['OSS_DB_PASSWORD'] %>
host: <%= ENV['OSS_DB_URL'] %>
port: <%= ENV['OSS_DB_PORT'] ||= '3306' %>
5 changes: 5 additions & 0 deletions config/deploy/templates/run_app.sh.erb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ set -- $PORT_STR
PORT=$2
echo "Listening port is $PORT"

echo "Migration started..."
`/usr/bin/docker exec $CONTAINER_ID bash -l -c 'bundle exec rake db:create RAILS_ENV=production'`
`/usr/bin/docker exec $CONTAINER_ID bash -l -c 'bundle exec rake db:migrate RAILS_ENV=production'`
`/usr/bin/docker exec $CONTAINER_ID bash -l -c 'bundle exec rake db:seed RAILS_ENV=production'
CNT=0
while :
do
Expand Down
2 changes: 1 addition & 1 deletion config/locales/translation_en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ en:
downloadable: "Downloadable" #g
embedded_view: "Embedded View" #g
extension: "Extension" #g
key: "Key" #g
object_key: "Key" #g
modified_at: "Modified at" #g
name: "Name" #g
page_view: "Page View" #g
Expand Down
2 changes: 1 addition & 1 deletion config/locales/translation_ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ ja:
download_count: "ダウンロード数"
embedded_view: "埋め込みビュー" #g
extension: "拡張子" #g
key: "キー" #g
object_key: "キー" #g
modified_at: "更新日時" #g
name: "タイトル" #g
page_view: "ページビュー" #g
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20170220075000_rename_slide_key.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RenameSlideKey < ActiveRecord::Migration
def change
rename_column :slides, :key, :object_key
end
end

0 comments on commit 9c397a9

Please sign in to comment.