Day14 - [丰收款] 使用Heroku Postgres资料库,存储订单交易资讯

昨天成功将Django网站布署到Heroku上,当然我们会需要撰写BackendURL页面来让API可以被呼叫,但在此之前,有一项技能必须要先点亮才行,就是如何使用Heroku的资料库!不然我们被API呼叫丢进来的PayToken,没办法储存下来。

Heroku不支援原本Django预设的SQLite,因此我们要使用Heroku自带的主流资料库-- Heroku Postgres。

我们继续使用Heroku CLI,输入以下指令可以先确认一下,会看到是否有资料库的add-on。

> heroku addons

https://ithelp.ithome.com.tw/upload/images/20210929/20130354yW6DaPjiDm.png

这边会看到预设已建立好一个Heroku Postgres资料库,但有了这个後要怎麽使用呢?
我们会用两种方式进行资料库的使用:

  1. 规划与资料设计阶段:可以用具备GUI的管理工具pgAdmin,较直觉也省时间。
  2. 给Python程序呼叫使用:使用Python与Postgres间的适配器模组psycopg2

关於Postgres的pgAdmin图形化资料库管理工具下载与安装步骤,我就不细说明。这个到官网去,聪明的你一定可以一路next到底就解决的了。

安装好pgAdmin後,我们需要作资料库连接的初始设定,因为Heroku帮我们建好了资料库,我们也有UI管理工具,接下来就是要取得连接的资讯。

有两种方式可以取得:

  1. 使用Heroku CLI以指令取得,但需要自己拆一下里面各段代表的意义
  2. 登入Heroku Web後,从里面的Data区查察连线资讯细节。

从Heroku CLI查看DB连线资讯

我们使用以下指令,可以查询Database连线资讯

> heroku config

https://ithelp.ithome.com.tw/upload/images/20210929/20130354EeikXcH1tp.png

其中config的字串以下列方式进行解析:

postgres://{user}:{password}@{hostname}:{port}/{database-name}

所以可以找出userpasswordhostnameportdatabase-name这五个资讯备用。

从Heroku Web查看DB连线资讯

在登入後,首页可看到Heroku Postgres的连接,点进去後可看到Datastore相关的资讯。

https://ithelp.ithome.com.tw/upload/images/20210929/20130354acq4toLx2z.png

接着,再点入Settings後,查看View Credentials
https://ithelp.ithome.com.tw/upload/images/20210929/20130354JZze6vDC1a.png

也可以找到对应的UserPasswordHostPortDatabase值。

到pdAdmin新增Server

我们在Servers底下执行Create Server,在对应的栏位输入上面取得的资讯。
https://ithelp.ithome.com.tw/upload/images/20210929/201303548E1axl0JAz.png

另外在SSL Tab中,将SSL mode从Prefer改为你所需要的,例如RequireAllow。依个人所需,其差异如下:

sslmode Eavesdropping protection MITM protection Statement
disable No No I don't care about security, and I don't want to pay the overhead of encryption.
allow Maybe No I don't care about security, but I will pay the overhead of encryption if the server insists on it.
prefer Maybe No I don't care about encryption, but I wish to pay the overhead of encryption if the server supports it.
require Yes No I want my data to be encrypted, and I accept the overhead. I trust that the network will make sure I always connect to the server I want.
verify-ca Yes Depends on CA-policy I want my data encrypted, and I accept the overhead. I want to be sure that I connect to a server that I trust.
verify-full Yes Yes I want my data encrypted, and I accept the overhead. I want to be sure that I connect to a server I trust, and that it's the one I specify.

表面资料来源:Table 31-1. SSL Mode Descriptions

而在Advanced Tab中,在DB restriction将刚刚的Database值输入,这样可以让这个共用Server连上後,只显示我们的这个资料库,不然到时候会有好几千个同样也挂在这个DB Server上面的Database (虽然都无权限存取)。

https://ithelp.ithome.com.tw/upload/images/20210929/20130354exV3u68hhQ.png

成功连上後,我们可以看到左侧树状结构以及进行简单查询也可看到Table资料。
https://ithelp.ithome.com.tw/upload/images/20210929/20130354aaFQXwBo1s.png

建立Order所需Table与Columns

我们需要资料库来记录Order在建立时的初始资料,以及等待API呼叫BackendURL时,可将PayOrder更新到已建立的订单中。我们先建立一个Table叫ks_order_payment专门来处理订单的延伸付款资料,由於目前并不打算建立整电商系统,因此就只模拟情境建立我们所需要的资料即可,想像中会有一个完整的ks_order与ks_order_detail资料表,然後才有这个payment的延伸资料,但我们先不进行实作。

透过UI建立新的Table以及Columns,如下:
https://ithelp.ithome.com.tw/upload/images/20210929/20130354bds2jXJdAm.png

栏位我就不一一说明,这边会以order_no作为Primary Key,然後订单建立顺序的排序会以ks_order_payment来排序,剩下的就是其他我们想记录的各种属性栏位。

UI有SQL功能帮我们产生好语法:

CREATE TABLE IF NOT EXISTS public.ks_order_payment
(
    web_atm_url character varying(250),
    tsno character(14),
    pay_type "char",
    pay_token character varying(100),
    pay_status character varying(20),
    otp_url character varying(250),
    order_no character(13) NOT NULL,
    create_time date,
    card_pay_url character varying(250),
    atm_pay_no character(14),
    amount integer,
    lm_time date,
    PRIMARY KEY (order_no)
)

TABLESPACE pg_default;

ALTER TABLE public.ks_order_payment
    OWNER to xbz_____zbsnz;

等等,听说Django有ORM呢

另一个使用Django的物件关联对映(ORM, Object Relational Mapping)技术,就是以Model类别方式宣告,自动与连线好的资料库进行对应,包含建立所需要的关联式资料库Table,之後针对物件作新增、修改、删除等操作,也会直接反映到资料库中。

这部份,就等明天再来写了!


<<:  学习Python纪录Day14 - JSON档案的写入与读取

>>:  Day 14 - Asynchronous 非同步核心

Progressive Web App Shortcuts: 程序快速启动选单 (9)

什麽是 App Shortcuts App Shortcuts 提供程序快速启动选单,透过这个捷径用...

Day 27-制作购物车之Redux 2

主要呈现实作成果 以下内容有参考教学影片,底下有附网址。 (内容包括我的不专业解说分析及在实作过程中...

Day 23 bert 文字情感分类-2

在这里我们要将训练集替换成中文,但是原本资料集的资料夹格式如下: tf.keras.preproce...

从零开始学3D游戏开发:入门程序实作 Part.6 用脚本计算分数

这是 Roblox 从零开始系列,入门章节的第十二个单元,今天你将学会如何把分数显示在右上角的玩家仪...

D29. 学习基础C、C++语言

D29. C++字串 C++ string的特别用法 str.size():字串长度。 str.em...