Initial commit

This commit is contained in:
Yaser
2026-08-13 19:56:46 +03:30
commit de1d57a67b
474 changed files with 43185 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
class CreateAllTables < ActiveRecord::Migration[5.2]
def change
create_table :users do |t|
t.string :uid, null: false
t.string :email, null: false
t.string :password_digest, null: false
t.string :role, default: "member", null: false
t.integer :level, default: 0, null: false
t.boolean :otp, default: false
t.string :state, default: "pending", null: false
t.timestamps
end
add_index :users, :uid, unique: true
add_index :users, :email, unique: true
create_table :apikeys do |t|
t.bigint :user_id, null: false, unsigned: true
t.string :kid, null: false
t.string :algorithm, null: false
t.string :scope
t.string :state, default: "active", null: false
t.timestamps
t.index [:user_id]
end
create_table :documents do |t|
t.bigint :user_id, null: false, unsigned: true
t.string :upload
t.string :doc_type
t.string :doc_number
t.date :doc_expire
t.text :metadata
t.timestamps
t.index [:user_id]
end
create_table :labels do |t|
t.bigint :user_id, null: false, unsigned: true
t.string :key, null: false
t.string :value, null: false
t.string :scope, default: "public", null: false
t.timestamps
t.index [:user_id]
t.index [:user_id, :key, :scope]
end
create_table :levels do |t|
t.string :key, null: false
t.string :value
t.string :description
t.timestamps
end
create_table :phones do |t|
t.integer :user_id, null: false, unsigned: true
t.string :country, null: false
t.string :number, null: false
t.string :code, limit: 5
t.datetime :validated_at
t.timestamps
t.index [:user_id]
t.index [:number]
end
create_table :profiles do |t|
t.bigint :user_id
t.string :first_name
t.string :last_name
t.date :dob
t.string :address
t.string :postcode
t.string :city
t.string :country
t.text :metadata
t.timestamps
t.index [:user_id]
end
end
end

View File

@@ -0,0 +1,15 @@
class CreateActivities < ActiveRecord::Migration[5.2]
def change
create_table :activities do |t|
t.references :user, null: false
t.string :user_ip, null: false
t.string :user_agent, null: false
t.string :topic, null: false
t.string :action, null: false
t.string :result, null: false
t.text :data, null: true
t.timestamp :created_at # avoid updated_at as records not supposed to be updated
end
end
end

View File

@@ -0,0 +1,8 @@
# frozen_string_literal: true
# Add new field due to referral - affiliate relation
class AddReferralIdToUsersTable < ActiveRecord::Migration[5.2]
def change
add_column :users, :referral_id, :bigint, after: :state
end
end

View File

@@ -0,0 +1,13 @@
class CreatePermissions < ActiveRecord::Migration[5.2]
def change
create_table :permissions do |t|
t.string :action, null: false
t.string :role, null: false
t.string :verb, null: false
t.string :path, null: false
t.timestamps
end
end
end

View File

@@ -0,0 +1,10 @@
class ChangesInActivitiesTable < ActiveRecord::Migration[5.2]
def change
add_column :activities, :target_uid, :string, after: :user_id
add_column :activities, :category, :string, after: :target_uid
add_column :permissions, :topic, :string, after: :path
add_index :activities, :target_uid
add_index :permissions, :topic
end
end

View File

@@ -0,0 +1,12 @@
class CreateRestrictions < ActiveRecord::Migration[5.2]
def change
create_table :restrictions do |t|
t.string :scope, limit: 64, null: false
t.string :value, limit: 64, null: false
t.string :state, limit: 16, default: 'enabled', null: false
t.timestamps
end
end
end

View File

@@ -0,0 +1,5 @@
class AddDataFieldToUsersTable < ActiveRecord::Migration[5.2]
def change
add_column :users, :data, :text, null: true, after: :role
end
end

View File

@@ -0,0 +1,5 @@
class AddStateProfiles < ActiveRecord::Migration[5.2]
def change
add_column :profiles, :state, :integer, unsigned: true, limit: 1, after: :country
end
end

View File

@@ -0,0 +1,12 @@
class CreateDataStorages < ActiveRecord::Migration[5.2]
def change
create_table :data_storages do |t|
t.bigint :user_id, null: false, unsigned: true
t.string :title, limit: 64, null: false
t.text :data, limit: 5120, null: false
t.timestamps
t.index [:user_id, :title], unique: true
end
end
end

View File

@@ -0,0 +1,5 @@
class AddDescriptionToLabels < ActiveRecord::Migration[5.2]
def change
add_column :labels, :description, :string, null: true, after: :scope
end
end

View File

@@ -0,0 +1,5 @@
class AddDefaultStateToProfilesTable < ActiveRecord::Migration[5.2]
def change
change_column_default :profiles, :state, 'drafted'
end
end

View File

@@ -0,0 +1,6 @@
class AddCodeAndTypeInRestrictionsTable < ActiveRecord::Migration[5.2]
def change
add_column :restrictions, :code, :integer, null: true, after: :value
add_column :restrictions, :category, :string, null: false, after: :id
end
end

View File

@@ -0,0 +1,5 @@
class AddEncryptedSecret < ActiveRecord::Migration[5.2]
def change
add_column :apikeys, :secret_encrypted, :string, limit: 1024, after: :scope
end
end

View File

@@ -0,0 +1,7 @@
class AddIdentificatorToDocumentsTable < ActiveRecord::Migration[5.2]
def change
add_column :documents, :identificator, :string, after: :doc_expire
add_column :documents, :doc_issue, :date, before: :doc_expire
add_column :profiles, :applicant_id, :string, after: :user_id
end
end

View File

@@ -0,0 +1,5 @@
class AddAuthorToProfiles < ActiveRecord::Migration[5.2]
def change
add_column :profiles, :author, :string, null: true, after: :user_id
end
end

View File

@@ -0,0 +1,13 @@
class CreateComments < ActiveRecord::Migration[5.2]
def change
create_table :comments do |t|
t.bigint :user_id, null: false, unsigned: true
t.string :author_uid, limit: 16, null: false
t.string :title, limit: 64, null: false
t.text :data, limit: 5120, null: false
t.timestamps
t.index :user_id
end
end
end

View File

@@ -0,0 +1,5 @@
class AddDocCategoryToDocuments < ActiveRecord::Migration[5.2]
def change
add_column :documents, :doc_category, :string
end
end

View File

@@ -0,0 +1,18 @@
class MakeApiKeyKidUnique < ActiveRecord::Migration[5.2]
class APIKey < ActiveRecord::Base
self.table_name = :apikeys
end
def up
duplicated_records = APIKey.select(:kid).group(:kid).having("count(*) > 1")
duplicated_records.each do |api_key|
APIKey.where(kid: api_key.kid).destroy_all
end
add_index :apikeys, :kid, unique: true unless index_exists?(:apikeys, :kid)
end
def down
remove_index :apikeys, :kid if index_exists?(:apikeys, :kid)
end
end

View File

@@ -0,0 +1,50 @@
class AddServiceAccountsAndRelatedChanges < ActiveRecord::Migration[5.2]
class APIKey < ActiveRecord::Base
self.table_name = :apikeys
end
def up
create_table :service_accounts do |t|
t.string :uid, null: false
t.bigint :owner_id, null: false, unsigned: true
t.string :email, null: false
t.string :role, default: "service_account", null: false
t.integer :level, default: 0, null: false
t.string :state, default: "pending", null: false
t.timestamps
end
add_column :apikeys, :key_holder_account_id, :bigint, null: false, unsigned: true, after: :id
add_column :apikeys, :key_holder_account_type, :string, null: false, default: "User", after: :key_holder_account_id
APIKey.find_each do |api_key|
api_key.key_holder_account_type = 'User'
api_key.key_holder_account_id = api_key.user_id
api_key.save!
end
remove_column :apikeys, :user_id
add_index :apikeys, [:key_holder_account_type, :key_holder_account_id], name: :idx_apikey_on_account unless index_exists?(:service_accounts, [:key_holder_account_type, :key_holder_account_id])
end
def down
add_column :apikeys, :user_id, :bigint, unsigned: true, null: false, after: :key_holder_account_id
APIKey.find_each do |api_key|
if api_key.key_holder_account_type == 'ServiceAccount'
api_key.destroy!
next
end
api_key.user_id = api_key.key_holder_account_id
api_key.save!
end
remove_column :apikeys, :key_holder_account_id
remove_column :apikeys, :key_holder_account_type
remove_index :service_accounts, column: [:key_holder_account_type, :key_holder_account_id] if index_exists?(:service_accounts, [:key_holder_account_type, :key_holder_account_id])
drop_table :service_accounts
end
end

View File

@@ -0,0 +1,57 @@
class AddEncryptedValuesToPhonesProfilesDocuments < ActiveRecord::Migration[5.2]
def up
# Phones Table
add_column :phones, :number_encrypted, :string, null: false, after: :code
add_column :phones, :number_index, :bigint, null: false, after: :number_encrypted
add_index :phones, [:number_index]
remove_column :phones, :number, :string
# Profiles Table
add_column :profiles, :first_name_encrypted, :string, limit: 1024, after: :applicant_id
add_column :profiles, :last_name_encrypted, :string, limit: 1024, after: :first_name_encrypted
add_column :profiles, :dob_encrypted, :string, after: :last_name_encrypted
add_column :profiles, :address_encrypted, :string, limit: 1024, after: :dob_encrypted
remove_column :profiles, :first_name, :string
remove_column :profiles, :last_name, :string
remove_column :profiles, :dob, :date
remove_column :profiles, :address, :string
# Documents table
add_column :documents, :doc_number_encrypted, :string, after: :doc_expire
add_column :documents, :doc_number_index, :bigint, after: :doc_number_encrypted
add_index :documents, [:doc_number_index]
remove_column :documents, :doc_number, :string
end
def down
# Phones Table
add_column :phones, :number, :string, null: false, after: :country
remove_index :phones, column: :number_index
remove_column :phones, :number_encrypted, :string
remove_column :phones, :number_index, :bigint
# Profiles Table
add_column :profiles, :first_name, :string, after: :applicant_id
add_column :profiles, :last_name, :string, after: :first_name
add_column :profiles, :dob, :date, after: :last_name
add_column :profiles, :address, :string, after: :dob
remove_column :profiles, :first_name_encrypted, :string
remove_column :profiles, :last_name_encrypted, :string
remove_column :profiles, :dob_encrypted, :string
remove_column :profiles, :address_encrypted, :string
# Documents table
add_column :documents, :doc_number, :string, after: :doc_type
remove_index :documents, column: :doc_number_index
remove_column :documents, :doc_number_encrypted, :string
remove_column :documents, :doc_number_index, :bigint
end
end

View File

@@ -0,0 +1,11 @@
class ChangeDocumentFieldsPosition < ActiveRecord::Migration[5.2]
def up
change_column :documents, :doc_issue, :date, after: :doc_number_index
change_column :documents, :doc_category, :string, after: :doc_issue
end
def down
change_column :documents, :doc_issue, :date, after: :updated_at
change_column :documents, :doc_category, :string, after: :doc_issue
end
end

View File

@@ -0,0 +1,9 @@
class ChangeOwnerIdRequirement < ActiveRecord::Migration[5.2]
def up
change_column :service_accounts, :owner_id, :bigint, null: true, unsigned: true
end
def down
change_column :service_accounts, :owner_id, :bigint, null: false, unsigned: true
end
end

View File

@@ -0,0 +1,5 @@
class AddNationalCodeToProfiles < ActiveRecord::Migration[5.2]
def change
add_column :profiles, :national_code_encrypted, :string
end
end

View File

@@ -0,0 +1,21 @@
class CreateTreasuries < ActiveRecord::Migration[5.2]
def down
drop_table :treasuries
end
def up
create_table :treasuries do |t|
t.bigint :user_id, null: false, unsigned: true
t.string :title, limit: 128, null: true
t.string :data, null: false
t.text :result
t.integer :state, default: 0, null: false, unsigned: true
t.integer :kind, default: 0, null: false, unsigned: true
t.timestamps
end
add_index :treasuries, :user_id
add_index :treasuries, :data, unique: true
add_index :treasuries, %i[user_id title], unique: true
end
end

View File

@@ -0,0 +1,12 @@
class CreateProvinces < ActiveRecord::Migration[5.2]
def down
drop_table :provinces
end
def up
create_table :provinces do |t|
t.string :name
t.timestamps
end
end
end

View File

@@ -0,0 +1,14 @@
class CreateCities < ActiveRecord::Migration[5.2]
def down
drop_table :cities
end
def up
create_table :cities do |t|
t.string :name
t.references :province, foreign_key: true
t.timestamps
end
end
end

View File

@@ -0,0 +1,5 @@
class ChangeCityInProfile < ActiveRecord::Migration[5.2]
def change
add_reference :profiles, :city
end
end

View File

@@ -0,0 +1,13 @@
class AddStepToPhone < ActiveRecord::Migration[5.2]
def up
add_column :phones, :category, :integer, null: false
add_column :phones, :state, :integer, null: true, default: 0
add_column :phones, :step, :integer, null: true, default: 0
end
def down
remove_column :phones, :category
remove_column :phones, :state
remove_column :phones, :step
end
end

View File

@@ -0,0 +1,9 @@
class AddStateToDocuments < ActiveRecord::Migration[5.2]
def up
add_column :documents, :state, :integer, null: false, default: 0
end
def down
remove_column :documents, :state
end
end

View File

@@ -0,0 +1,7 @@
class RemoveIndexDataFromTreasuries < ActiveRecord::Migration[5.2]
def change
remove_index :treasuries, name: 'index_treasuries_on_data'
remove_index :treasuries, name: 'index_treasuries_on_user_id_and_title'
end
end