From 1ffcd8ef44e260f52acc382aa04757e48063361e Mon Sep 17 00:00:00 2001 From: Eric-Guo Date: Mon, 13 Nov 2023 19:38:49 +0800 Subject: [PATCH] Using pg in yai branch, released new version --- .gitlab-ci.yml | 8 ++++++++ Dockerfile | 2 ++ Gemfile | 3 +-- Gemfile.lock | 11 +++-------- README.md | 25 +++++++++++++++++++------ config/database.yml.sample | 13 +++++++++---- db/schema.rb | 31 +++++++++++++++++-------------- 7 files changed, 59 insertions(+), 34 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 68319f698c..d501adc92b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -2,6 +2,10 @@ image: circleci/ruby:3.0.3-node-browsers variables: + POSTGRES_HOST: postgres + POSTGRES_DB: pagila_test + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres CACHE_FALLBACK_KEY: "$CI_COMMIT_REF_SLUG" before_script: @@ -15,6 +19,8 @@ stages: - deploy test: + services: + - postgres:12.9 stage: test cache: - key: @@ -27,6 +33,8 @@ test: - yarn.lock paths: - ".yarn-cache/" + variables: + DATABASE_URL: postgresql://postgres:postgres@postgres:5432/$POSTGRES_DB script: - bundle install -j $(nproc) - yarn install --cache-folder .yarn-cache diff --git a/Dockerfile b/Dockerfile index 1c8f82f115..4a0683e22e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,6 +13,8 @@ ENV RAILS_ENV="production" \ BUNDLE_PATH="/usr/local/bundle" \ BUNDLE_WITHOUT="development" +# Install libpq to access pg +RUN apt-get update && apt-get install -y libpq-dev # Throw-away build stage to reduce size of final image FROM base as build diff --git a/Gemfile b/Gemfile index edb6be0e85..96b0eda4c7 100644 --- a/Gemfile +++ b/Gemfile @@ -11,8 +11,7 @@ gem 'rails-i18n' # released. gem 'mail', '= 2.8.1' -# Use sqlite3 as the database for Active Record -gem 'sqlite3', '~> 1.7' +gem 'pg' # Use Puma as the app server gem 'puma' # Use jquery as the JavaScript library diff --git a/Gemfile.lock b/Gemfile.lock index 7a5177d658..cecde58d53 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -300,6 +300,7 @@ GEM orm_adapter (0.5.0) ostruct (0.6.1) pagy (9.3.3) + pg (1.5.9) psych (5.2.2) date stringio @@ -354,7 +355,7 @@ GEM rb-fsevent (0.11.2) rb-inotify (0.11.1) ffi (~> 1.0) - rdoc (6.8.1) + rdoc (6.9.1) psych (>= 4.0.0) regexp_parser (2.9.3) reline (0.5.12) @@ -394,12 +395,6 @@ GEM actionpack (>= 6.1) activesupport (>= 6.1) sprockets (>= 3.0.0) - sqlite3 (1.7.3-aarch64-linux) - sqlite3 (1.7.3-arm-linux) - sqlite3 (1.7.3-arm64-darwin) - sqlite3 (1.7.3-x86-linux) - sqlite3 (1.7.3-x86_64-darwin) - sqlite3 (1.7.3-x86_64-linux) sshkit (1.23.2) base64 net-scp (>= 1.1.2) @@ -487,6 +482,7 @@ DEPENDENCIES minitest minitest-ci pagy + pg puma pundit rails (~> 7.1.2) @@ -497,7 +493,6 @@ DEPENDENCIES simplecov spring sprockets - sqlite3 (~> 1.7) terser turbolinks tzinfo-data diff --git a/README.md b/README.md index 86b041d375..0ed381aa10 100644 --- a/README.md +++ b/README.md @@ -40,19 +40,32 @@ rm config/credentials.yml.enc export EDITOR=vim # paste credentials.yml.sample or skip bin/rails credentials:edit +bin/rails db:create bin/rails test:all ``` -# Build & Run in docker mode +# Create postgresql db user in Mac ```bash -docker build --tag ericguo/oauth2id:main . -# or `docker pull ericguo/oauth2id:main` to using existing images -docker run -p 3000:3000 -d --restart always --name oauth2id --env RAILS_MASTER_KEY=YourMasterKey -v ./storage:/rails/storage ericguo/oauth2id:main +sudo su - postgres +createuser oauth2id_prod +psql -d postgres +ALTER ROLE oauth2id_prod LOGIN; +ALTER USER oauth2id_prod PASSWORD 'oauth2id_prod'; +CREATE DATABASE oauth2id_prod WITH ENCODING='UTF8' OWNER=oauth2id_prod; +logout +``` + +# Build & Run in docker mode in OrbStack + +```bash +docker build --tag ericguo/oauth2id:yai . +# or `docker pull ericguo/oauth2id:yai` to using existing images +docker run -p 3000:3000 -d --restart always --name oauth2id-yai --env RAILS_MASTER_KEY=YourMasterKey --env OAUTH2ID_DB_NAME=oauth2id_prod --env OAUTH2ID_DB_HOST=host.docker.internal --env OAUTH2ID_DB_USERNAME=oauth2id_prod --env OAUTH2ID_DB_PASSWORD=oauth2id_prod -v ./storage:/rails/storage ericguo/oauth2id:yai # If can not start in above, do the debug. -docker run --env RAILS_MASTER_KEY=YourMasterKey -v ./storage:/rails/storage -it ericguo/oauth2id:main bash +docker run --env RAILS_MASTER_KEY=YourMasterKey --env OAUTH2ID_DB_NAME=oauth2id_prod --env OAUTH2ID_DB_HOST=host.docker.internal --env OAUTH2ID_DB_USERNAME=oauth2id_prod --env OAUTH2ID_DB_PASSWORD=oauth2id_prod -v ./storage:/rails/storage -it ericguo/oauth2id:yai bash # After success, push manually. -docker push ericguo/oauth2id:main +docker push ericguo/oauth2id:yai ``` # Dev env setup diff --git a/config/database.yml.sample b/config/database.yml.sample index 78fcfad758..d2a8b14e38 100644 --- a/config/database.yml.sample +++ b/config/database.yml.sample @@ -1,13 +1,13 @@ --- default: &default - adapter: sqlite3 + adapter: postgresql pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> timeout: 5000 development: primary: <<: *default - database: storage/oauth2id_dev.sqlite3 + database: <%= ENV.fetch("OAUTH2ID_DEV_DB_NAME") { 'oauth2id_dev' } %> # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". @@ -15,9 +15,14 @@ development: test: primary: <<: *default - database: storage/oauth2id_test.sqlite3 + database: <%= ENV.fetch("OAUTH2ID_TEST_DB_NAME") { 'oauth2id_test' } %> + username: <%= ENV.fetch("OAUTH2ID_DB_USERNAME") { '' } %> + host: <%= ENV.fetch("OAUTH2ID_DB_HOST") { '' } %> production: primary: <<: *default - database: storage/oauth2id_prod.sqlite3 + database: <%= ENV.fetch("OAUTH2ID_DB_NAME") { 'oauth2id_prod' } %> + username: <%= ENV.fetch("OAUTH2ID_DB_USERNAME") { '' } %> + password: <%= ENV.fetch("OAUTH2ID_DB_PASSWORD") { '' } %> + host: <%= ENV.fetch("OAUTH2ID_DB_HOST") { '' } %> diff --git a/db/schema.rb b/db/schema.rb index 6a25fed6c2..4aecb74e2c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,12 +10,15 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_01_05_132232) do +ActiveRecord::Schema[7.1].define(version: 2024_01_05_132232) do + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + create_table "allowlisted_jwts", force: :cascade do |t| t.string "jti", null: false t.string "aud", null: false t.datetime "exp", precision: nil, null: false - t.integer "user_id", null: false + t.bigint "user_id", null: false t.index ["jti"], name: "index_allowlisted_jwts_on_jti", unique: true t.index ["user_id"], name: "index_allowlisted_jwts_on_user_id" end @@ -30,8 +33,8 @@ end create_table "department_users", force: :cascade do |t| - t.integer "department_id", null: false - t.integer "user_id", null: false + t.bigint "department_id", null: false + t.bigint "user_id", null: false t.datetime "created_at", precision: nil, null: false t.datetime "updated_at", precision: nil, null: false t.index ["department_id"], name: "index_department_users_on_department_id" @@ -48,8 +51,8 @@ end create_table "oauth_access_grants", force: :cascade do |t| - t.integer "resource_owner_id", null: false - t.integer "application_id", null: false + t.bigint "resource_owner_id", null: false + t.bigint "application_id", null: false t.string "token", null: false t.integer "expires_in", null: false t.text "redirect_uri", null: false @@ -64,8 +67,8 @@ end create_table "oauth_access_tokens", force: :cascade do |t| - t.integer "resource_owner_id" - t.integer "application_id" + t.bigint "resource_owner_id" + t.bigint "application_id" t.string "token", null: false t.string "refresh_token" t.integer "expires_in" @@ -111,8 +114,8 @@ end create_table "position_users", force: :cascade do |t| - t.integer "position_id", null: false - t.integer "user_id", null: false + t.bigint "position_id", null: false + t.bigint "user_id", null: false t.boolean "main_position", default: false t.datetime "created_at", precision: nil, null: false t.datetime "updated_at", precision: nil, null: false @@ -128,7 +131,7 @@ end create_table "profiles", force: :cascade do |t| - t.integer "user_id" + t.bigint "user_id" t.string "title" t.boolean "gender" t.string "phone" @@ -140,8 +143,8 @@ end create_table "user_allowed_applications", force: :cascade do |t| - t.integer "user_id" - t.integer "oauth_application_id" + t.bigint "user_id" + t.bigint "oauth_application_id" t.boolean "enable", default: true t.datetime "created_at", precision: nil, null: false t.datetime "updated_at", precision: nil, null: false @@ -150,7 +153,7 @@ end create_table "user_sign_in_histories", force: :cascade do |t| - t.integer "user_id" + t.bigint "user_id" t.datetime "sign_in_at", precision: nil t.text "user_agent" t.string "sign_in_ip"