個人開発② ~Gitリポジトリ、DB設計~

 今回は以前も行なったGitへのリポジトリとアプリのDB設計について書いていきます。

 

 

〜今回の記事の内容 〜

 

 コンセプト、構想を練る

     ⬇︎

   アプリを立ち上げ

     ⬇︎

   ☆Gitを利用して管理

             ⬇︎

☆DB設計(データベース設計)

 

 

 

Git、GitHubを利用した管理

以前も記事にして書いてましたが、再びここで書いておきます。

 

 

GItHub Dasktopをまだダウンロードしていない方はこちらからダウンロード出来ます

Mac版GitHub Desktopのダウンロードリンク

 

natori-gorira.hatenablog.com

 

 

※上記と内容はほぼ一緒です 

 

左上の「Current Repository」→「Add」→「Add Existing Repository」を選択します。

 

f:id:natori_gorira:20200610110653p:plain

 

f:id:natori_gorira:20200610111435p:plain

Chooseをクリック

f:id:natori_gorira:20200610111553p:plain

ローカルリポジトリにしたいファイルをクリック

f:id:natori_gorira:20200610111646p:plain

Add Repositoryをクリック

 

①にコミット名、②をクリックしてコミットします。

 

f:id:natori_gorira:20200610113448p:plain

コミット名は分かりやすい名前をオススメします

 


Publish repositoryをクリックします。

GitHub Desktop では、Publish ボタンを押すとリモートリポジトリが作られていない場合にリモートリポジトリも作ってくれます。

 

f:id:natori_gorira:20200610120409p:plain

 

 

モーダルが現れます。公開するかのチェックですが、後からGIthubで変更も出来ます。

決めたら、Publish repositoryをクリックします。

f:id:natori_gorira:20200610115739p:plain

公開したくない場合はチェックを入れます



ここまで完了すれば、GitHubリポジトリが作られています。

 

あとはブランチを作成する準備として現在の情報をコミットします

写真は開発途中のものです

f:id:natori_gorira:20200618104740p:plain

コミット名は”first commit”にしました

 

ここまででGitのリポジトリで管理することができます

次にDB設計に移ります

 

 

DB設計(データベース設計)

 DB設計は個人的に苦手意識を持っており、ここでつまづくと後からの修正が大変なのではないか?と先入観を持っていました。

実際にはDB設計はしっかり作る必要はあるが、開発で度々変更出来ることを知り、知らないから勝手不安になっていただけでした(笑)

 まずは”lucidchart(ルシッドチャート)”というアプリを使い、ER図を作っていくところからスタートしました。

 

 

Lucidchartは、ユーザーがチャートや図表の描画、改訂、共有を共同で行えるようにするために使用されるWebベースの独自のプラットフォームです。

wikipediaより抜粋

 

f:id:natori_gorira:20200618110707p:plain

 

f:id:natori_gorira:20200618110734p:plain

 

公式ホームページ

https://www.lucidchart.com/pages/ja/landing?utm_source=google&utm_medium=cpc&utm_campaign=ja_japan_desktop_branded_x_exact_lucidchart&km_CPC_CampaignId=1532901510&km_CPC_AdGroupID=58843219819&km_CPC_Keyword=lucidchart&km_CPC_MatchType=e&km_CPC_ExtensionID=&km_CPC_Network=g&km_CPC_AdPosition=&km_CPC_Creative=302449566445&km_CPC_TargetID=aud-561781099678:kwd-33511936169&km_CPC_Country=1009129&km_CPC_Device=c&km_CPC_placement=&km_CPC_target=&mkwid=sKcclI4jM_pcrid_302449566445_pkw_lucidchart_pmt_e_pdv_c_slid__pgrid_58843219819_ptaid_aud-561781099678:kwd-33511936169_&gclid=CjwKCAjw_qb3BRAVEiwAvwq6VgQBxR43RaIGtTwN2Gxwp6_Yk7C4xXesJDXF_g-MQswr_ZQCnJujWRoCefMQAvD_BwE

 

lucidcharは制限はありますが、無料で利用することができ、オンライン上で管理されているので、チーム開発などでメンバーとER図の共有が出来ます

 

 

今回lucidcharを利用し作成したER図が下記のものになります

f:id:natori_gorira:20200618111540p:plain

 

こちらは拡大したものです

f:id:natori_gorira:20200618111613p:plain

userテーブル、profileテーブル、commentテーブルを拡大しています

 

README(リードミーに書き込むために

カラム名|単位|制約、外部キー|、

1対多、多対多、などの必要な情報を考えながら書いていきます

 

上記はモデルにしたアプリで書いていたので、後々変わってくるだろうと思い

READMEにはまず確定しているテーブルを書き込みました

 

右側は後から変更した内容です

 
## DB設計
### Usersテーブル
|Column|Type|Options|
|------|----|-------|
|nickname|string|null: false|
|last_name|string||
|first_name|string||
|last_name_kane|string||
|first_name_kane|string||
|birth_year|integer|| 後の変更点削除
|birth_manth|integer|| 後の変更点削除
|birth_day|integer|| 後の変更点|birthday|date||
|email|string|null: false, unique: true|
|password|string||

#### Association
- has_many :comments 後の変更点削除
- has_many :items
- has_many :likes 後の変更点削除
- has_many :sns_credentials 後の変更点削除
- has_many :trading_partners 後の変更点削除
- has_one :credit_card 後の変更点- has_one :card
- has_one :profile

## profilesテーブル
|Column|Type|Options|
|------|----|-------|
|body|text||
|phone_number|integer|unique:true|
|zipcode|integer||
|prefecture|integer||
|city|string||
|block|string||
|building|string||
|user_id|references|null:false,index:true,foreign_key:true|

#### Association
- belongs_to user
 
※credit_cardテーブルはcardテーブルになり、内容も大きく変わることになりました
理由としてクレジットカードの情報は個人では管理してはいけないとのことでした
 
<!-- ## credit_cardsテーブル
|Column|Type|Options|
|------|----|-------|
|authorization_code|integer|null:false,unique:true|
|security_code|integer|null:false|
|month|integer|null:false|
|year|integer|null:false|
|user_id|references|null:false,index:true,foreign_key:true| -->
 
## cardsテーブル
|Column|Type|Options|
|------|----|-------|
|user_id|integer|null:false|
|customer_id|string|null:false|
|card_id|string|null:false|

#### Association
- belongs_to user

### Itemsテーブル
|Column|Type|Options|
|------|----|-------|
|name|string|null: false, index: true|
|price|integer|null: false|
|description|text|null: false|
|condition|integer|null: false|
|shipping_fee|integer|null: false| 後の変更点削除
|shipping_from|integer|null: false|
|days_before_shipping|integer|null: false|
|shipping_method|integer|null: false|
|trade_status|integer|null: false| 後の変更点削除
|brand_id|references|index: true, foreign_key: true| 後の変更点削除
|category_id|references|null: false. index:true, foreign_key:true|
後の変更点削除
|user_id|references|null: false. index:true, foreign_key:true|
|size_id|references|null: false. index:true, foreign_key:true|
後の変更点削除

#### Association
- belongs_to :brand 後の変更点削除
- belongs_to :category 後の変更点削除
- belongs_to :user
- belongs_to size 後の変更点削除
- has_many :comments 後の変更点削除
- has_many :images
- has_many :likes 後の変更点削除
- has_one :order 後の変更点削除

### Imagesテーブル
|Column|Type|Options|
|------|----|-------|
|name|string|null: false| 後の変更点|image|string|null: false|
|item_id|references|null: false, index: true, foreign_key: true|

- belong_to :item
 

 

 

 現在完成している部分のみですが、今後完成したものを後日載せようと思います、ひとまず、DB設計はこれで進めていきます

 

 

 

今回はここまで!続きはまた次回書いていきます!