osprey's diary

日々の雑感を書き綴ります

PostgreSQLでテーブルにカラムを追加してみた

とあるシステムに機能追加することになって、どうしても既存のテーブルにカラムを追加する必要が出てきて困ったのでメモ。

絶対停止しちゃダメっていうシステムではないんだけど、なるべく止めないでっていう感じのもの。

 

 

alter table table_a add column colimn_a int not null default '1';

 

みたいにしたら返事が返ってこなくなった。defaultとか指定すると既存のレコードにも影響するから処理が重くなるらしい。※でも自分の環境では指定してもしてなくても返事が無くなった。

 

思い悩んだ挙句、一度既存テーブルのレコードをcsvにエクスポートしてからそのcsvを加工して空のテーブル作って加工したcsvインポートしてrenameでテーブル差し替えるのが時間的には早かった(と思う)。

※もっとスマートなやり方があるような気がしてるけど分からなかった。良いやり方があったら教えていただけると嬉しいです。

 

エクスポートして

copy table_a to '/tmp/db_export.csv' with csv delimiter ',';

 

データ加工して(今回viでやっちゃったけど、pandasとか使うとスマートなのかな)

 

空のテーブル作って

create table table_a_tmp(

column_1 varchar(100) not null unique,
column_2 varchar(512) not null,

...

colimn_a int not null default '1'

);

 作った空のテーブルに加工したファイルインポートして

copy table_a_tmp from '/tmp/db_export.csv' with csv delimiter ',';

 

既存テーブルを別名に退避させてから

alter table table_a rename to table_a_bk;


仮で作ったテーブルをを本番テーブル名に名前変更

alter table table_a_tmp rename to table_a;

 

この後にプログラムファイルを差し替えて動作確認

 

こんな感じ。

python3.7.0rc1をtarballから入れてみた

python3.7を触ってみたかった。今回はtarballから。OSはDebian9.4。

prefixでバージョン分けてたけど、今回は違うやり方を試してみる。

元ネタはこちら

 

必要そうなパッケージはあらかじめ入れておく

# apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev

 

公式サイトからダウンロード

# wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0rc1.tar.xz

 

展開して

# tar Jxvf Python-3.7.0rc1.tar.xz

 

READMEを読む

# less EADME.rst

 

helpも見ておく

# ./configure --help

 

 

configureして

# ./configure --enable-optimizations --with-ensurepip=install

 

makeしてインストールする

makeは-jでjobを並列で走らせると、環境によって早く終わるみたい

ちょっと古いけどこちらが分かりやすかった

manでみても-jは変わってない感じかな

# make -j8 && make altinstall

 

update-alternativesでバージョン切り替え管理できる

ちょっと古いけどこちらが分かりやすかった

# update-alternatives --install /usr/bin/python python /usr/local/bin/python3.6 50
# update-alternatives --install /usr/bin/python python /usr/local/bin/python3.7 60
# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 80
# update-alternatives --install /usr/bin/python python /usr/bin/python3.5 30

必要な時だけ3系に切り替えるのが良いと思う

切り替えるときはこんな感じ

# update-alternatives --config python
There are 4 choices for the alternative python (providing /usr/bin/python).

Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/python2.7 80 auto mode
* 1 /usr/bin/python2.7 80 manual mode
2 /usr/bin/python3.5 30 manual mode
3 /usr/local/bin/python3.6 50 manual mode
4 /usr/local/bin/python3.7 60 manual mode

Press <enter> to keep the current choice[*], or type selection number:

 

 そして早速3.7にしたの忘れててうっかりaptでパッケージインストールしようとして嵌ったorz

そもそも開発するときはvenvで環境分けるから必要なかった。すみませんupdate-alternatives使ってみたかっただけでした。

やっぱり基本に立ち返ってprefixでinstall directory分けて必要な時だけvenvで環境作るのが落ち着くかな。。。

 

 

 

WatchGuard DimensionをWindows10ProのHyper-Vにデプロイしてみる

参考資料は以下。

http://www.watchguard.com/support/release-notes/xtm/11/en-US/EN_ReleaseNotes_Dimension_v2_1/WatchGuard_Dimension_2_1.pdf

 

 

まずはイメージをダウンロードします。

watchguardsupport.secure.force.com

※「Show downloads for:」でDimensionを選択してダウンロードして展開しておいてください。

 

Hyper-Vマネージャを起動、操作→新規→仮想マシンを選択すると、以下の画面が出てくるので、次へと進みます。

f:id:osprey-jp:20171109150206p:plain

 

名前はDimensionにします。

f:id:osprey-jp:20171109150227p:plain

 

 

 

世代は第1世代を選びます。

f:id:osprey-jp:20171109150231p:plain

 

メモリは、最低で2048MBだそうです。ここでは2048MBで。

f:id:osprey-jp:20171109150234p:plain

 

ネットワークは環境に合わせて。

f:id:osprey-jp:20171109150238p:plain

 

仮想ハードディスクの接続のところで、先程ダウンロードして展開したディレクトリの中にあるvhdを選択してください。

f:id:osprey-jp:20171109150241p:plain

 

これでいったん終了。このあとログサーバ用にVHDを作ってつなぎます。

f:id:osprey-jp:20171109150245p:plain

 

さっき作ったのがいるので、操作→設定を選択します。

f:id:osprey-jp:20171109150248p:plain

 

こんな画面が出るので、ハードウェアの追加→SCSIコントローラーを選択します。

f:id:osprey-jp:20171109150253p:plain

 

ハードドライブを追加します。

f:id:osprey-jp:20171109150256p:plain

 

メディアの指定のところで、そのまま新規をクリック。

f:id:osprey-jp:20171109150259p:plain

 

仮想ハードディスクの新規作成ウィザードが出てきます。

f:id:osprey-jp:20171109150302p:plain

 

フォーマットはどっちでもいいです。

f:id:osprey-jp:20171109150306p:plain

 

容量は可変のまま次へ。(固定の方が望ましいかも?)

f:id:osprey-jp:20171109150313p:plain

 

名前と場所を指定してください。

f:id:osprey-jp:20171109150316p:plain

 

ディスクの構成、最低40GBだそうです。ここでは最低で。

f:id:osprey-jp:20171109150325p:plain

 

完了。

f:id:osprey-jp:20171109150329p:plain

 

無事仮想ハードディスクが接続されました。

f:id:osprey-jp:20171109153203p:plain

 

何度か作り直して、その都度忘れるので備忘録として。

aptでpython3.6を入れてみる

前にCentOS7にyumでPython3.6を入れる記事を書いたけど、普段UbuntuDebian使うことのほうが多いのでメモ。

 

Ubuntu16.10とか17.04とか17.10はUniverse Repositoryに含まれてるそうです。

なので普通に

$ sudo apt update
$ sudo apt install python3.6

でOK。

16.04の場合はこんな感じ。

$ sudo add-apt-repository ppa:jonathonf/python-3.6
$ sudo apt update
$ sudo apt install python3.6

2017/9/22時点でPython3.6.2-1だった。

 

Debianの場合はこんな感じ。

# echo 'deb http://ftp.jp.debian.org/debian experimental main' >> /etc/apt/sources.list
# echo 'deb http://ftp.jp.debian.org/debian unstable main' >> /etc/apt/sources.list
# echo 'APT::Default-Release "stable";' | tee -a /etc/apt/apt.conf.d/00local
# apt update
# apt install python3.6

2017/9/22時点でPython3.6.3rc1-2だった。

 

以下のサイトにお世話になりました。感謝。

16.04 - How do I install Python 3.6 using apt-get? - Ask Ubuntu

debian - How to install Python 3.6? - Unix & Linux Stack Exchange

Debianで開発ツールのインストール

Debianで最小インストールしたらmakeもgccも無かったのでメモ。

 

RedHat系だと、

# yum groupinstall "Development Tools"

な感じだけど、Debian系は

# apt install build-essential

らしいです。

 

以下を参考にしました。

www.2daygeek.com

MariaDBでdatabase table の schema 情報の見方

備忘録。スキーマ情報の見方。

 

use <database_name> してから desc <table_name> すると情報が確認できる。

 

$ mysql -u root -p

Enter password:

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 5766

Server version: 5.5.52-MariaDB MariaDB Server

 

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

MariaDB [(none)]> show databases;

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

| Database           |

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

| information_schema |

| owndb              |

| mysql              |

| performance_schema |

| test               |

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

5 rows in set (0.00 sec)

 

MariaDB [(none)]> use owndb;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

 

Database changed

MariaDB [owndb]> show tables;

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

| Tables_in_owndb    |

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

| mt_company         |

| mt_role            |

| mt_staff           |

| mt_user            |

 

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

4 rows in set (0.00 sec)

MariaDB [owndb]> desc mt_company;

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

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

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

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

| ccode             | varchar(6)   | NO   | UNI | NULL    |                |

| cname             | varchar(60)  | NO   |     | NULL    |                |

| email             | varchar(255) | NO   |     | NULL    |                |

| fax               | int(11)      | YES  |     | NULL    |                |

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

5 rows in set (0.01 sec)