From d227e03fff285380fa73f8d631206b46e993df44 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 --- Dockerfile | 2 ++ Gemfile | 3 +-- Gemfile.lock | 9 ++------- README.md | 25 +++++++++++++++++++------ config/database.yml.sample | 13 +++++++++---- db/schema.rb | 29 ++++++++++++++++------------- 6 files changed, 49 insertions(+), 32 deletions(-) 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 de74de092e..5ed4060f17 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' +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 36ebac62e6..73c5d8d605 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -282,6 +282,7 @@ GEM racc (~> 1.4) orm_adapter (0.5.0) pagy (6.2.0) + pg (1.5.4) psych (5.1.2) stringio public_suffix (5.0.4) @@ -374,12 +375,6 @@ GEM actionpack (>= 5.2) activesupport (>= 5.2) sprockets (>= 3.0.0) - sqlite3 (1.6.9-aarch64-linux) - sqlite3 (1.6.9-arm-linux) - sqlite3 (1.6.9-arm64-darwin) - sqlite3 (1.6.9-x86-linux) - sqlite3 (1.6.9-x86_64-darwin) - sqlite3 (1.6.9-x86_64-linux) sshkit (1.21.6) net-scp (>= 1.1.2) net-ssh (>= 2.8.0) @@ -465,6 +460,7 @@ DEPENDENCIES minitest minitest-ci pagy + pg puma pundit rails (~> 7.1.1) @@ -475,7 +471,6 @@ DEPENDENCIES simplecov spring sprockets - sqlite3 terser turbolinks tzinfo-data diff --git a/README.md b/README.md index 7ebb60229d..b7283f499a 100644 --- a/README.md +++ b/README.md @@ -14,19 +14,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 ``` # First user is admin 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 9883c5b7fc..7476c8d20f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,18 +11,21 @@ # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema[7.1].define(version: 2021_03_26_082831) 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 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" @@ -39,8 +42,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 @@ -55,8 +58,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" @@ -93,8 +96,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 @@ -110,7 +113,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" @@ -122,8 +125,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 @@ -132,7 +135,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"