あじちゃんの備忘録。

〜ここはメモ帳です

gitの複数のブランチを一括で削除する

xargs コマンドを使います。

xargs コマンドとは?

xargs コマンドとは、Linux のコマンドの1つ。
あるコマンドの出力を xargs コマンドに送り込み、別のコマンドの引数として指定することができます。

消したくない branch を除いた全ての branch を削除するとき

git branch | grep -v 消したくないbranch名など | xargs git branch -D

これは、パイプ( | )でくくった部分のコマンドを実行した結果に対して、 xargs 以降のコマンドを適用するという意味になります。 実際に実行したコマンドは以下の画像。(👹<s>-vが抜けてるのであとで書き直す 書き直した)

f:id:azix:20180816174516j:plain

git branch に対するコマンドは自分で判断してね。

【JavaScript】配列の比較について

まとめ

JavaScriptで、配列の比較の際に==又は===演算子を用いると、その配列が参照しているメモリ上のアドレスが同じかどうかでtrue/falseを返す。
Object.is() も同様。

var list_1 = ["mike","saba","tora"];
var list_2 = ["mike","saba","tora"];

console.log(list_1 === list_2);
// -> false
console.log(Object.is(list_1, list_2));
// -> false

配列が全く同じであっても、比較の際に == === Object.is() を用いると false が返る。
これは、比較対象が中の値ではなくポインタのアドレスを見ているため。

じゃあどうやって比較する?

一番楽なのは、toString() してしまう。 もしくは(冗長になってしまうけど)配列を回して比較。

var list_1 = ["mike","saba","tora"];
var list_2 = ["mike","saba","tora"];

console.log(list_1.toString() === list_2.toString());
// -> true
console.log(Object.is(list_1.toString(), list_2.toString()));
// -> true

ただ、Object.is()=== のようにオブジェクトを比較したいときはまたちょっと複雑そう・・・。いつか調べる。

追記

ツイッタで質問があったので。
JavaScriptの配列型にはequalsメソッドが存在しません。

[1, 2] == [1, 2];
// -> false

[1, 2].equals([1, 2]);
// -> Uncaught TypeError: [1,2].equals is not a function

外部キー制約 migration の foreigndrop がうまくいかない

(先に)まとめ

⭐️ 外部キー制約の drop と同時に、制約対象の key を変更や削除する場合はスキーマを分ける必要がある
✏️ 外部キー制約を drop するときは1つのスキーマでまとめて実行できる

テーブル条件

  • テーブル名:animals
  • カラム名:kindId
  • 外部キー設定先テーブル名:animal_kinds
  • 外部キー設定先カラム名:id

作成した migration ファイル

<?php

public function up()
{
    Schema::table('animals', function (Blueprint $table) {
        $table->unsignedBigInteger('kind')->change();
        $table->foreign('kind')->references('id')->on('animal_kinds');
    });
}

public function down()
{
    Schema::table('animals', function (Blueprint $table) {
        $table->dropForeign('animals_kind_foreign');
        $table->bigInteger('kindId')->change();
    })
}

migration を実行し、rollback した際にエラー発生

  [Illuminate\Database\QueryException]                                                                                   
  SQLSTATE[HY000]: General error: 1832 Cannot change column 'kind': used in a foreign key constraint 'animal_kinds' (SQL: ALTER TABLE animal_kinds CHANGE kind kind BIGINT NOT NULL COMMENT '-')                                                 
                                                                                                                         
  [Doctrine\DBAL\Driver\PDOException]                                                                                    
  SQLSTATE[HY000]: General error: 1832 Cannot change column 'kind': used in a foreign key constraint 'animal_kinds'                                                                                        
  [PDOException]                                                                                                         
  SQLSTATE[HY000]: General error: 1832 Cannot change column 'kind': used in a foreign key constraint 'animal_kinds'

down() を以下のように書き換えることで成功

<?php

public function down()
{
    Schema::table('animals', function (Blueprint $table) {
        $table->dropForeign('animals_kind_foreign');
    });
    Schema::table('animals', function (Blueprint $table) {
        $table->bigInteger('kindId')->change();
    });
}