替換數(shù)據(jù)庫

看到資料表明可能是數(shù)據(jù)庫問題 我就查了如何正確從sqlite3 切換成Postgres

找了一篇參考資料

https://medium.com/geekculture/ruby-on-rails-switch-from-sqlite3-to-postgres-590009645c25

其中不熟悉的一個步驟是把 config/database.yml里的內(nèi)容從


# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  adapter: sqlite3
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

# 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:
  <<: *default
  database: db/test.sqlite3

production:
  <<: *default
  database: db/production.sqlite3

替換成

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: MyTrip_development

# 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:
  <<: *default
  database: MyTrip_test

production:
  <<: *default
  database: MyTrip_production
  username: deploy
  password:

注解掉的說明文字內(nèi)容還是sqlite3的相關(guān)文字沒改 先這樣吧

以及查資料到后面發(fā)現(xiàn) username: deploy用替換 貌似不是 deploy

因?yàn)橐暮脦滋幎铱雌饋韘tring更合理 所以先跳過
報(bào)錯了 但我不死心

在改 t.string over to t.text前 我想先嘗試rake三兄弟

夠狠的!三兄弟都執(zhí)行不了...強(qiáng)啊

第一個文檔改動存照 方便改回去

第二個文檔改了

t.string :title

第三個文檔

t.string  :address
t.string  :price
t.string  :best_time

改了居然還是報(bào)錯 果然問題出在數(shù)據(jù)庫設(shè)置啊啊俺饪浮瞎惫!

嘗試解決數(shù)據(jù)庫問題過程中運(yùn)行了

brew upgrade

看到一些提示 記錄以備后用

==> Pouring libxml2--2.9.12.big_sur.bottle.tar.gz
==> Caveats
libxml2 is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have libxml2 first in your PATH, run:
  echo 'export PATH="/usr/local/opt/libxml2/bin:$PATH"' >> ~/.zshrc

For compilers to find libxml2 you may need to set:
  export LDFLAGS="-L/usr/local/opt/libxml2/lib"
  export CPPFLAGS="-I/usr/local/opt/libxml2/include"

For pkg-config to find libxml2 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/libxml2/lib/pkgconfig"

隔一段居然還有報(bào)錯

Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink lib/libImath.dylib
Target /usr/local/lib/libImath.dylib
is a symlink belonging to ilmbase. You can unlink it:
  brew unlink ilmbase

To force the link and overwrite all conflicting files:
  brew link --overwrite imath

To list all files that would be deleted:
  brew link --overwrite --dry-run imath

Possible conflicting files are:
/usr/local/lib/libImath.dylib -> /usr/local/Cellar/ilmbase/2.5.5_1/lib/libImath.dylib

以及

Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink include/OpenEXR/Iex.h
Target /usr/local/include/OpenEXR/Iex.h
is a symlink belonging to ilmbase. You can unlink it:
  brew unlink ilmbase

To force the link and overwrite all conflicting files:
  brew link --overwrite openexr

To list all files that would be deleted:
  brew link --overwrite --dry-run openexr

Possible conflicting files are:
一堆的文檔...就沒有復(fù)制...

我估計(jì)是系統(tǒng)老是自己升級導(dǎo)致的...不要亂升級系統(tǒng)真是老師當(dāng)時的良心建議啊...

然后發(fā)現(xiàn)自己兩個數(shù)據(jù)庫都安裝了 果然...

==> mysql
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start
==> postgresql
To migrate existing data from a previous major version of PostgreSQL run:
  brew postgresql-upgrade-database

This formula has created a default database cluster with:
  initdb --locale=C -E UTF-8 /usr/local/var/postgres
For more details, read:
  https://www.postgresql.org/docs/13/app-initdb.html

To have launchd start postgresql now and restart at login:
  brew services start postgresql
Or, if you don't want/need a background service you can just run:
  pg_ctl -D /usr/local/var/postgres start

最后還有這個眼熟的朋友

==> redis
To have launchd start redis now and restart at login:
  brew services start redis
Or, if you don't want/need a background service you can just run:
  redis-server /usr/local/etc/redis.conf

一通報(bào)錯折騰之后 找到個能運(yùn)行起來的解法 https://stackoverflow.com/questions/27700596/homebrew-postgres-broken

rm -rf /usr/local/var/postgres  
mkdir /usr/local/var/postgres
chmod 0700 /usr/local/var/postgres
initdb -D /usr/local/var/postgres

記錄有用的信息內(nèi)容 注意其中我的 user不是云服務(wù)器上的那個

The files belonging to this database system will be owned by user "RealAnalysis".
This user must also own the server process.

The database cluster will be initialized with locales
  COLLATE:  C
  CTYPE:    UTF-8
  MESSAGES: C
  MONETARY: C
  NUMERIC:  C
  TIME:     C
The default database encoding has accordingly been set to "UTF8".
initdb: could not find suitable text search configuration for locale "UTF-8"
The default text search configuration will be set to "simple".

Data page checksums are disabled.

fixing permissions on existing directory /usr/local/var/postgres ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Shanghai
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /usr/local/var/postgres -l logfile start

注意到user的不同 所以先跑去 config/database.yml把數(shù)據(jù)庫的 user改了

然后就能運(yùn)行

rake db:create

psql -l

就可以查看數(shù)據(jù)庫情況 比如 owner具體為何

雖然過程沒辦法完全順利的按照 https://medium.com/geekculture/ruby-on-rails-switch-from-sqlite3-to-postgres-590009645c25 走下來 但是貌似也算順利替換成pg數(shù)據(jù)庫了杭攻?

直接用PG數(shù)據(jù)庫 不用換

真是良心建議 建立以后會部署的項(xiàng)目時 直接執(zhí)行

rails new my_app_name --database=postgresql

記得my_app_name替換成你的新項(xiàng)目名稱哈 這樣直接用 pg 數(shù)據(jù)庫就不會有部署的時候要換數(shù)據(jù)庫的吐血了

最后 給你解釋一下全程疑惑的一點(diǎn)

解惑一下

還有一篇不錯的參考

https://dev.to/forksofpower/make-the-move-from-sqlite3-to-postgres-in-rails-6-34m2

里面有解釋為啥換數(shù)據(jù)庫的時候 建議把 t.string 改成t.text

以及查到一個看著挺靠譜的 database.yml 模板當(dāng)參考

default: &default
  adapter: postgresql
  encoding: unicode
  password: xxxxx #<-- which you have defiend while installing postgresql
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: project_development

test:
  <<: *default
  database: project_test

production:
  <<: *default
  database: project_production
  username: username (我不確定要不要改 是否要跟云端的一致)
  password: <%= ENV['PROJECT_DATABASE_PASSWORD'] %>

又一堆連環(huán)報(bào)錯 一個個修過去 然后執(zhí)行了

bundle install --path vendor/bundle

后 提示記錄一下 說不定后面可以用

==== Release notes for capistrano-passenger ====
passenger once had only one way to restart: `touch tmp/restart.txt`
Beginning with passenger v4.0.33, a new way was introduced: `passenger-config restart-app`

The new way to restart was not initially practical for everyone,
since for versions of passenger prior to v5.0.10,
it required your deployment user to have sudo access for some server configurations.

capistrano-passenger gives you the flexibility to choose your restart approach, or to rely on reasonable defaults.

If you want to restart using `touch tmp/restart.txt`, add this to your config/deploy.rb:

    set :passenger_restart_with_touch, true

If you want to restart using `passenger-config restart-app`, add this to your config/deploy.rb:

    set :passenger_restart_with_touch, false # Note that `nil` is NOT the same as `false` here

If you don't set `:passenger_restart_with_touch`, capistrano-passenger will check what version of passenger you are running
and use `passenger-config restart-app` if it is available in that version.

If you are running passenger in standalone mode, it is possible for you to put passenger in your
Gemfile and rely on capistrano-bundler to install it with the rest of your bundle.
If you are installing passenger during your deployment AND you want to restart using `passenger-config restart-app`,
you need to set `:passenger_in_gemfile` to `true` in your `config/deploy.rb`.

好奇心起來 執(zhí)行了一下

 rake db:migrate RAILS_ENV=production

報(bào)錯提示為

Caused by:
PG::ConnectionBad: FATAL:  database "MyTrip_production" does not exist

解法

rake db:create:all

就能補(bǔ)上創(chuàng)建不存在的 database "MyTrip_production"

然后就能順利的跑

 rake db:migrate RAILS_ENV=production

還找到個官網(wǎng)的參考資料 如何設(shè)置 config/database.yml

https://guides.rubyonrails.org/configuring.html#configuring-a-postgresql-database

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末督笆,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子谆构,更是在濱河造成了極大的恐慌谅河,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,589評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件叶骨,死亡現(xiàn)場離奇詭異茫多,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)忽刽,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,615評論 3 396
  • 文/潘曉璐 我一進(jìn)店門天揖,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人跪帝,你說我怎么就攤上這事今膊。” “怎么了伞剑?”我有些...
    開封第一講書人閱讀 165,933評論 0 356
  • 文/不壞的土叔 我叫張陵斑唬,是天一觀的道長。 經(jīng)常有香客問我纸泄,道長赖钞,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,976評論 1 295
  • 正文 為了忘掉前任聘裁,我火速辦了婚禮雪营,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘衡便。我一直安慰自己献起,他們只是感情好洋访,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,999評論 6 393
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著谴餐,像睡著了一般姻政。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上岂嗓,一...
    開封第一講書人閱讀 51,775評論 1 307
  • 那天汁展,我揣著相機(jī)與錄音,去河邊找鬼厌殉。 笑死食绿,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的公罕。 我是一名探鬼主播器紧,決...
    沈念sama閱讀 40,474評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼楼眷!你這毒婦竟也來了铲汪?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,359評論 0 276
  • 序言:老撾萬榮一對情侶失蹤罐柳,失蹤者是張志新(化名)和其女友劉穎掌腰,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體硝清,經(jīng)...
    沈念sama閱讀 45,854評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡辅斟,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,007評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了芦拿。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片士飒。...
    茶點(diǎn)故事閱讀 40,146評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖蔗崎,靈堂內(nèi)的尸體忽然破棺而出酵幕,到底是詐尸還是另有隱情,我是刑警寧澤缓苛,帶...
    沈念sama閱讀 35,826評論 5 346
  • 正文 年R本政府宣布芳撒,位于F島的核電站,受9級特大地震影響未桥,放射性物質(zhì)發(fā)生泄漏笔刹。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,484評論 3 331
  • 文/蒙蒙 一冬耿、第九天 我趴在偏房一處隱蔽的房頂上張望舌菜。 院中可真熱鬧,春花似錦亦镶、人聲如沸日月。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,029評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽爱咬。三九已至尺借,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間精拟,已是汗流浹背燎斩。 一陣腳步聲響...
    開封第一講書人閱讀 33,153評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留串前,地道東北人瘫里。 一個月前我還...
    沈念sama閱讀 48,420評論 3 373
  • 正文 我出身青樓实蔽,卻偏偏與公主長得像荡碾,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子局装,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,107評論 2 356

推薦閱讀更多精彩內(nèi)容