programing

SQLite에서 Postgre로 변경새로운 Rails 프로젝트의 SQL

stoneblock 2023. 5. 4. 17:56

SQLite에서 Postgre로 변경새로운 Rails 프로젝트의 SQL

데이터베이스가 SQLite(개발 및 운영)에 있는 레일 앱이 있습니다.herku로 이동하기 때문에 데이터베이스를 Postgre로 변환하고 싶습니다.SQL.

어쨌든 로컬, 개발, 데이터베이스는 SQLite에서 변경할 필요가 없다고 들었는데, 변경할 필요가 없는데 운영 환경을 SQLite에서 PostgreSQL로 변경하려면 어떻게 해야 합니까?

전에 이런 일을 해본 적이 있고 도와줄 수 있는 사람이 있습니까?

추신: 이 프로세스를 정확히 무엇이라고 부르는지는 모르겠지만 SQLite에서 Postgre로 데이터베이스를 마이그레이션하는 것에 대해 들어본 적이 있습니다.SQL, 그렇게 해야 하나요?

기본 sqlite를 사용하는 대신 database.yml을 다음과 같이 변경할 수 있습니다.

development:
  adapter: postgresql
  encoding: utf8
  database: project_development
  pool: 5
  username: 
  password:

test: &TEST
  adapter: postgresql
  encoding: utf8
  database: project_test
  pool: 5
  username: 
  password:

production:
  adapter: postgresql
  encoding: utf8
  database: project_production
  pool: 5
  username: 
  password:

cucumber:
  <<: *TEST

아래 단계가 저에게 효과가 있었습니다.Ryan Bates의 Railcast #342에 언급된 Heroku가 만든 탭 보석을 사용합니다.몇 가지 단계가 있지만 완벽하게 작동했으며(날짜도 올바르게 마이그레이션됨), 이전에 Oracle -> DB2 또는 SQL Server -> Oracle 마이그레이션보다 훨씬 쉬웠습니다.

SQLite에는 사용자 ID나 암호가 없지만 탭 보석에는 무언가가 필요합니다.저는 문자 그대로 "사용자"와 "비밀번호"를 사용했습니다.

새 데이터베이스에 대한 Postgres 데이터베이스 사용자 작성

$ createuser f3
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) y

EDIT - 아래의 업데이트된 명령 - 대신 이 명령을 사용합니다.

$ createuser f3 -d -s

필요한 데이터베이스 만들기

$ createdb -Of3 -Eutf8 f3_development
$ createdb -Of3 -Eutf8 f3_test

Gem 파일 업데이트

gem 'sqlite3'
gem 'pg'
gem 'taps'
$ bundle

데이터베이스를 업데이트합니다.yml

#development:
#  adapter: sqlite3
#  database: db/development.sqlite3
#  pool: 5
#  timeout: 5000

development:
  adapter: postgresql
  encoding: unicode
  database: f3_development
  pool: 5
  username: f3
  password:

#test:
#  adapter: sqlite3
#  database: db/test.sqlite3
#  pool: 5
#  timeout: 5000

test:
  adapter: postgresql
  encoding: unicode
  database: f3_test
  pool: 5
  username: f3
  password:

sqlite 데이터베이스에서 탭 서버 시작

$ taps server sqlite://db/development.sqlite3 user password

데이터 마이그레이션

$ taps pull postgres://f3@localhost/f3_development http://user:password@localhost:5000

Rails 웹 서버를 다시 시작합니다.

$ rails s

Gem 파일 정리

#gem 'sqlite3'
gem 'pg'
#gem 'taps'
$ bundle

이제 단일 명령으로 쉽게 실행할 수 있습니다.

bin/rails db:system:change --to=postgresql

헤로쿠로 이동 중이므로 탭을 사용하여 다음 작업을 수행할 수 있습니다.

heroku db:push

이렇게 하면 로컬 개발 SQLite 데이터가 프로덕션으로 푸시되고 herku가 자동으로 포스트그레스로 변환됩니다.

이것은 프로덕션 SQLite db를 herku에 푸시하는 데도 작동해야 하지만 테스트되지 않았습니다.

RAILS_ENV=production heroku db:push

또한 "gem 'pg' 행을 gem 파일에 추가해야 하며, 'pg'는 레일즈의 현재 포스트gem 보석입니다.

config/database.yml 파일을 업데이트하기만 하면 됩니다.

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5

development:
  <<: *default
  database: projectname_development

test:
  <<: *default
  database: projectname_test

production:
  <<: *default
  database: projectname_production
  username: 
  password: 

다음은 실행 시 생성되는 내용입니다.

$ rails new projectname --database=postgresql --skip-test-unit

또한 Gem 파일에 다음을 추가:

gem 'pg'

데이터베이스 업데이트만 하면 됩니다.yml

development: &development
  adapter: postgresql
  database: Your_database_name
  username: user_name
  password: password
  host:     localhost
  schema_search_path: public
  min_messages: warning

test:
  <<: *development
  database: test_database_name

production:
  <<: *development
  database: production_db_name

우리는 레일을 사용하고 있으며 DRY, Convention over Configuration 등과 같은 기본 표준을 따라야 합니다.그래서 위의 코드에서 우리는 같은 코드를 반복하지 않습니다.

위에서 언급했지만, 저는 그것을 지지할 수 있을 만큼의 잠복자로서의 명성을 가지고 있지 않습니다.이 답변을 읽고 있는 레일즈의 신입생들에게 조금 더 관심을 끌기를 바랍니다.

또한 "gem 'pg' 행을 gem 파일에 추가해야 하며, 'pg'는 레일즈의 현재 포스트gem 보석입니다.

이것은 Rails 앱을 Postgres로 마이그레이션하기 위해 선택한 답변에 설명된 database.yml 파일 외에도 중요한 부분입니다.

'sqlite3pg보석 파일에서, 나는 계속해서 받았습니다.sqlite3 error업데이트된 gem 파일을 커밋하는 것을 잊었기 때문에 Heroku 마스터에 푸시할 때.다음을 수행하는 것만으로 이 문제가 해결되었습니다.

git add .
git commit -m 'heroku push'
heroku create 
git push heroku master

이것이 제가 설정한 방식입니다.MRI만 사용하고 Jruby는 사용하지 않는 경우 어댑터 설정의 로직을 건너뛸 수 있습니다.

defaults: &defaults
  adapter: <%= RUBY_ENGINE == 'ruby' ? 'postgresql' : 'jdbcpostgresql' %>
  encoding: unicode
  pool: 5
  timeout: 5000

development:
  database: project_development
  <<: *defaults

test:
  database: project_test
  <<: *defaults

production:
  database: project_production
  <<: *defaults

은 다음을해 볼 수 .sqlite3 development.db .dump | psql dbname username

또는 sqlitetopgscript로 시도해 보십시오. http://trac-hacks.org/browser/sqlitetopgscript/0.10/sqlite2pg

가능한 해결책(헤로쿠가 아닌)은 다음에서 yaml.db를 사용하는 것입니다.

http://www.railslodge.com/plugins/830-yaml-db

오늘도 같은 문제가 있었습니다.레일즈 4.2.8에서 일하고 있습니다.버전을 이었는데, 제 해책은 pgem 버전지는것하, 제경는에우다,0.18.4.

언급URL : https://stackoverflow.com/questions/6710654/change-from-sqlite-to-postgresql-in-a-fresh-rails-project