rubydb 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitattributes +20 -0
- data/.github/ISSUE_TEMPLATE/bug.yml +0 -0
- data/.github/ISSUE_TEMPLATE/feature.yml +0 -0
- data/.github/ISSUE_TEMPLATE/performance.yml +0 -0
- data/.github/ISSUE_TEMPLATE/security.yml +0 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
- data/.github/dependabot.yml +0 -0
- data/.github/workflows/benchmark.yml +26 -0
- data/.github/workflows/compatibility.yml +63 -0
- data/.github/workflows/fuzz.yml +33 -0
- data/.github/workflows/lint.yml +21 -0
- data/.github/workflows/operations.yml +24 -0
- data/.github/workflows/production-validation.yml +111 -0
- data/.github/workflows/release.yml +77 -0
- data/.github/workflows/security.yml +37 -0
- data/.github/workflows/test.yml +26 -0
- data/.github/workflows/workload.yml +58 -0
- data/.gitignore +43 -0
- data/.rspec +4 -0
- data/.rubocop.yml +44 -0
- data/.ruby-version +1 -0
- data/.standard.yml +14 -0
- data/ARCHITECTURE.md +21 -0
- data/CHANGELOG.md +31 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/CONTRIBUTING.md +29 -0
- data/GOVERNANCE.md +16 -0
- data/Gemfile +17 -0
- data/Gemfile.lock +71 -0
- data/LICENSE +60 -0
- data/README.md +162 -0
- data/ROADMAP.md +27 -0
- data/Rakefile +71 -0
- data/SECURITY.md +54 -0
- data/SUPPORT.md +14 -0
- data/adapters/activerecord/Gemfile +11 -0
- data/adapters/activerecord/README.md +9 -0
- data/adapters/activerecord/lib/active_record/connection_adapters/rubydb_adapter.rb +887 -0
- data/adapters/activerecord/rubydb-activerecord.gemspec +27 -0
- data/adapters/activerecord/spec/rubydb_adapter_integration_spec.rb +151 -0
- data/adapters/ruby/README.md +18 -0
- data/adapters/sequel/README.md +11 -0
- data/bin/rubydb +19 -0
- data/bin/rubydb-server +126 -0
- data/config/development.yml +0 -0
- data/config/monitoring/prometheus-alerts.yml +39 -0
- data/config/production.yml +36 -0
- data/config/test.yml +0 -0
- data/docs/README.md +71 -0
- data/docs/architecture/concurrency.md +14 -0
- data/docs/architecture/current-state.md +125 -0
- data/docs/architecture/execution-engine.md +25 -0
- data/docs/architecture/indexes.md +19 -0
- data/docs/architecture/mvcc.md +19 -0
- data/docs/architecture/overview.md +13 -0
- data/docs/architecture/pages.md +11 -0
- data/docs/architecture/production-roadmap.md +82 -0
- data/docs/architecture/query-planner.md +20 -0
- data/docs/architecture/recovery.md +18 -0
- data/docs/architecture/sql-engine.md +12 -0
- data/docs/architecture/storage-engine.md +14 -0
- data/docs/architecture/transactions.md +10 -0
- data/docs/architecture/wal.md +28 -0
- data/docs/cli-cheatsheet.md +98 -0
- data/docs/cli.md +275 -0
- data/docs/contributing/architecture.md +9 -0
- data/docs/contributing/benchmarking.md +14 -0
- data/docs/contributing/development.md +16 -0
- data/docs/contributing/release-process.md +49 -0
- data/docs/contributing/testing.md +16 -0
- data/docs/debugging.md +229 -0
- data/docs/developer/branching.md +10 -0
- data/docs/developer/database-diff.md +10 -0
- data/docs/developer/local-development.md +17 -0
- data/docs/developer/snapshots.md +9 -0
- data/docs/developer/temporal-data.md +10 -0
- data/docs/developer-guide.md +297 -0
- data/docs/getting-started/first-database.md +16 -0
- data/docs/getting-started/first-query.md +13 -0
- data/docs/getting-started/installation.md +19 -0
- data/docs/getting-started/local-to-production.md +300 -0
- data/docs/getting-started/quickstart.md +17 -0
- data/docs/getting-started/rails.md +16 -0
- data/docs/hardening_backlog.md +93 -0
- data/docs/lessons-learned.md +112 -0
- data/docs/operations/backups.md +33 -0
- data/docs/operations/disaster-recovery.md +31 -0
- data/docs/operations/failover.md +30 -0
- data/docs/operations/monitoring.md +25 -0
- data/docs/operations/production-guide.md +295 -0
- data/docs/operations/production-runbook.md +45 -0
- data/docs/operations/replication.md +33 -0
- data/docs/operations/restore.md +6 -0
- data/docs/operations/runbook.md +34 -0
- data/docs/operations/upgrades.md +14 -0
- data/docs/operations/workload-testing.md +17 -0
- data/docs/production-readiness.md +118 -0
- data/docs/production_validation.md +150 -0
- data/docs/rails/active-record.md +11 -0
- data/docs/rails/compatibility-guide.md +90 -0
- data/docs/rails/database-yml.md +92 -0
- data/docs/rails/installation.md +17 -0
- data/docs/rails/migrations.md +17 -0
- data/docs/rails/production.md +82 -0
- data/docs/rails/troubleshooting.md +18 -0
- data/docs/release.md +59 -0
- data/docs/server/architecture.md +10 -0
- data/docs/server/authentication.md +10 -0
- data/docs/server/configuration.md +16 -0
- data/docs/server/connection-pooling.md +10 -0
- data/docs/server/deployment.md +10 -0
- data/docs/server/protocol.md +12 -0
- data/docs/sql/compatibility-guide.md +82 -0
- data/docs/sql/compatibility.md +39 -0
- data/docs/sql/data-types.md +10 -0
- data/docs/sql/functions.md +9 -0
- data/docs/sql/joins.md +9 -0
- data/docs/sql/operators.md +9 -0
- data/docs/sql/sqlite-compatibility.md +21 -0
- data/docs/sql/syntax.md +10 -0
- data/docs/sql/transactions.md +10 -0
- data/docs/troubleshooting.md +244 -0
- data/exe/rubydb +23 -0
- data/lib/rubydb/backup/archive.rb +334 -0
- data/lib/rubydb/backup/backup.rb +401 -0
- data/lib/rubydb/backup/incremental.rb +353 -0
- data/lib/rubydb/backup/restore.rb +290 -0
- data/lib/rubydb/backup/snapshot.rb +267 -0
- data/lib/rubydb/backup/verification.rb +279 -0
- data/lib/rubydb/branching/branch.rb +181 -0
- data/lib/rubydb/branching/branch_manager.rb +311 -0
- data/lib/rubydb/branching/branch_metadata.rb +140 -0
- data/lib/rubydb/branching/checkout.rb +166 -0
- data/lib/rubydb/branching/copy_on_write.rb +272 -0
- data/lib/rubydb/branching/diff.rb +138 -0
- data/lib/rubydb/branching/merge.rb +285 -0
- data/lib/rubydb/build_info.rb +15 -0
- data/lib/rubydb/catalog/catalog.rb +391 -0
- data/lib/rubydb/catalog/column.rb +112 -0
- data/lib/rubydb/catalog/constraint.rb +180 -0
- data/lib/rubydb/catalog/database.rb +184 -0
- data/lib/rubydb/catalog/index.rb +97 -0
- data/lib/rubydb/catalog/schema.rb +103 -0
- data/lib/rubydb/catalog/sequence.rb +90 -0
- data/lib/rubydb/catalog/system_catalog.rb +698 -0
- data/lib/rubydb/catalog/table.rb +178 -0
- data/lib/rubydb/catalog/trigger.rb +102 -0
- data/lib/rubydb/catalog/view.rb +66 -0
- data/lib/rubydb/cli/application.rb +163 -0
- data/lib/rubydb/cli/commands/backup.rb +81 -0
- data/lib/rubydb/cli/commands/branch.rb +72 -0
- data/lib/rubydb/cli/commands/checkout.rb +54 -0
- data/lib/rubydb/cli/commands/create.rb +58 -0
- data/lib/rubydb/cli/commands/diff.rb +77 -0
- data/lib/rubydb/cli/commands/doctor.rb +74 -0
- data/lib/rubydb/cli/commands/drop.rb +57 -0
- data/lib/rubydb/cli/commands/init.rb +102 -0
- data/lib/rubydb/cli/commands/inspect.rb +95 -0
- data/lib/rubydb/cli/commands/merge.rb +63 -0
- data/lib/rubydb/cli/commands/migrate.rb +62 -0
- data/lib/rubydb/cli/commands/restart.rb +39 -0
- data/lib/rubydb/cli/commands/restore.rb +121 -0
- data/lib/rubydb/cli/commands/shell.rb +365 -0
- data/lib/rubydb/cli/commands/snapshot.rb +79 -0
- data/lib/rubydb/cli/commands/start.rb +82 -0
- data/lib/rubydb/cli/commands/status.rb +92 -0
- data/lib/rubydb/cli/commands/stop.rb +47 -0
- data/lib/rubydb/cli/commands/vacuum.rb +58 -0
- data/lib/rubydb/cli/formatter.rb +221 -0
- data/lib/rubydb/cli/output.rb +168 -0
- data/lib/rubydb/client/client.rb +304 -0
- data/lib/rubydb/client/connection.rb +415 -0
- data/lib/rubydb/client/connection_pool.rb +168 -0
- data/lib/rubydb/client/connection_url.rb +96 -0
- data/lib/rubydb/client/prepared_statement.rb +60 -0
- data/lib/rubydb/client/result.rb +123 -0
- data/lib/rubydb/client/statement.rb +52 -0
- data/lib/rubydb/client/transaction.rb +130 -0
- data/lib/rubydb/concurrency/concurrency.rb +19 -0
- data/lib/rubydb/concurrency/deadlock_detector.rb +150 -0
- data/lib/rubydb/concurrency/latch.rb +101 -0
- data/lib/rubydb/concurrency/lock_graph.rb +165 -0
- data/lib/rubydb/concurrency/mutex.rb +183 -0
- data/lib/rubydb/concurrency/rw_lock.rb +180 -0
- data/lib/rubydb/concurrency/scheduler.rb +250 -0
- data/lib/rubydb/concurrency/worker_pool.rb +143 -0
- data/lib/rubydb/configuration/config.rb +170 -0
- data/lib/rubydb/configuration/defaults.rb +179 -0
- data/lib/rubydb/configuration/environment.rb +152 -0
- data/lib/rubydb/configuration/parser.rb +185 -0
- data/lib/rubydb/configuration/validation.rb +221 -0
- data/lib/rubydb/constants.rb +74 -0
- data/lib/rubydb/constraints/check.rb +181 -0
- data/lib/rubydb/constraints/constraint.rb +101 -0
- data/lib/rubydb/constraints/foreign_key.rb +130 -0
- data/lib/rubydb/constraints/not_null.rb +64 -0
- data/lib/rubydb/constraints/primary_key.rb +99 -0
- data/lib/rubydb/constraints/unique.rb +108 -0
- data/lib/rubydb/constraints/validator.rb +350 -0
- data/lib/rubydb/errors/authentication_error.rb +10 -0
- data/lib/rubydb/errors/authorization_error.rb +23 -0
- data/lib/rubydb/errors/client_error.rb +10 -0
- data/lib/rubydb/errors/configuration_error.rb +10 -0
- data/lib/rubydb/errors/connection_error.rb +10 -0
- data/lib/rubydb/errors/constraint_error.rb +23 -0
- data/lib/rubydb/errors/corruption_error.rb +10 -0
- data/lib/rubydb/errors/database_error.rb +10 -0
- data/lib/rubydb/errors/error.rb +20 -0
- data/lib/rubydb/errors/execution_error.rb +10 -0
- data/lib/rubydb/errors/parser_error.rb +10 -0
- data/lib/rubydb/errors/recovery_error.rb +10 -0
- data/lib/rubydb/errors/replication_error.rb +10 -0
- data/lib/rubydb/errors/server_error.rb +6 -0
- data/lib/rubydb/errors/storage_error.rb +10 -0
- data/lib/rubydb/errors/transaction_error.rb +10 -0
- data/lib/rubydb/execution/aggregate_executor.rb +138 -0
- data/lib/rubydb/execution/delete_executor.rb +112 -0
- data/lib/rubydb/execution/distinct_executor.rb +135 -0
- data/lib/rubydb/execution/executor.rb +1192 -0
- data/lib/rubydb/execution/expression.rb +193 -0
- data/lib/rubydb/execution/index_scan.rb +142 -0
- data/lib/rubydb/execution/insert_executor.rb +217 -0
- data/lib/rubydb/execution/join_executor.rb +249 -0
- data/lib/rubydb/execution/limit_executor.rb +85 -0
- data/lib/rubydb/execution/optimizer.rb +215 -0
- data/lib/rubydb/execution/plan.rb +361 -0
- data/lib/rubydb/execution/planner.rb +546 -0
- data/lib/rubydb/execution/predicate.rb +235 -0
- data/lib/rubydb/execution/scan.rb +49 -0
- data/lib/rubydb/execution/sequential_scan.rb +63 -0
- data/lib/rubydb/execution/sort_executor.rb +185 -0
- data/lib/rubydb/execution/update_executor.rb +162 -0
- data/lib/rubydb/functions/aggregate.rb +70 -0
- data/lib/rubydb/functions/date_functions.rb +278 -0
- data/lib/rubydb/functions/function.rb +85 -0
- data/lib/rubydb/functions/json_functions.rb +215 -0
- data/lib/rubydb/functions/numeric_functions.rb +346 -0
- data/lib/rubydb/functions/scalar.rb +52 -0
- data/lib/rubydb/functions/string_functions.rb +383 -0
- data/lib/rubydb/functions/system_functions.rb +246 -0
- data/lib/rubydb/history/as_of.rb +238 -0
- data/lib/rubydb/history/change.rb +105 -0
- data/lib/rubydb/history/history.rb +131 -0
- data/lib/rubydb/history/history_manager.rb +229 -0
- data/lib/rubydb/history/temporal_query.rb +202 -0
- data/lib/rubydb/history/timeline.rb +144 -0
- data/lib/rubydb/indexes/btree.rb +186 -0
- data/lib/rubydb/indexes/btree_cursor.rb +258 -0
- data/lib/rubydb/indexes/btree_node.rb +385 -0
- data/lib/rubydb/indexes/hash_index.rb +150 -0
- data/lib/rubydb/indexes/index.rb +71 -0
- data/lib/rubydb/indexes/index_manager.rb +406 -0
- data/lib/rubydb/indexes/index_scan.rb +470 -0
- data/lib/rubydb/migrations/migration.rb +254 -0
- data/lib/rubydb/migrations/migration_lock.rb +146 -0
- data/lib/rubydb/migrations/migration_manager.rb +176 -0
- data/lib/rubydb/migrations/migration_version.rb +71 -0
- data/lib/rubydb/migrations/schema_diff.rb +211 -0
- data/lib/rubydb/migrations/schema_version.rb +64 -0
- data/lib/rubydb/monitoring/events.rb +160 -0
- data/lib/rubydb/monitoring/health.rb +222 -0
- data/lib/rubydb/monitoring/logger.rb +193 -0
- data/lib/rubydb/monitoring/metrics.rb +359 -0
- data/lib/rubydb/monitoring/performance.rb +176 -0
- data/lib/rubydb/monitoring/statistics.rb +170 -0
- data/lib/rubydb/mvcc/garbage_collector.rb +199 -0
- data/lib/rubydb/mvcc/mvcc.rb +16 -0
- data/lib/rubydb/mvcc/snapshot.rb +147 -0
- data/lib/rubydb/mvcc/vacuum.rb +180 -0
- data/lib/rubydb/mvcc/version.rb +106 -0
- data/lib/rubydb/mvcc/version_store.rb +398 -0
- data/lib/rubydb/mvcc/visibility.rb +109 -0
- data/lib/rubydb/protocol/capabilities.rb +125 -0
- data/lib/rubydb/protocol/decoder.rb +145 -0
- data/lib/rubydb/protocol/encoder.rb +136 -0
- data/lib/rubydb/protocol/handshake.rb +305 -0
- data/lib/rubydb/protocol/message.rb +121 -0
- data/lib/rubydb/protocol/protocol.rb +277 -0
- data/lib/rubydb/protocol/version.rb +54 -0
- data/lib/rubydb/rails/adapter.rb +239 -0
- data/lib/rubydb/rails/connection.rb +314 -0
- data/lib/rubydb/rails/database_statements.rb +122 -0
- data/lib/rubydb/rails/migration.rb +131 -0
- data/lib/rubydb/rails/quoting.rb +109 -0
- data/lib/rubydb/rails/result.rb +120 -0
- data/lib/rubydb/rails/schema_statements.rb +339 -0
- data/lib/rubydb/rails/transaction.rb +105 -0
- data/lib/rubydb/rails/type.rb +126 -0
- data/lib/rubydb/recovery/checkpoint.rb +257 -0
- data/lib/rubydb/recovery/consistency.rb +467 -0
- data/lib/rubydb/recovery/corruption_detector.rb +5 -0
- data/lib/rubydb/recovery/crash_recovery.rb +387 -0
- data/lib/rubydb/recovery/recovery_manager.rb +206 -0
- data/lib/rubydb/recovery/redo.rb +237 -0
- data/lib/rubydb/recovery/undo.rb +206 -0
- data/lib/rubydb/replication/failover.rb +5 -0
- data/lib/rubydb/replication/fencing.rb +63 -0
- data/lib/rubydb/replication/primary.rb +450 -0
- data/lib/rubydb/replication/replica.rb +384 -0
- data/lib/rubydb/replication/replication_log.rb +200 -0
- data/lib/rubydb/replication/replication_manager.rb +308 -0
- data/lib/rubydb/replication/replication_slot.rb +295 -0
- data/lib/rubydb/replication/replication_stream.rb +201 -0
- data/lib/rubydb/rubydb.rb +560 -0
- data/lib/rubydb/security/access_control.rb +254 -0
- data/lib/rubydb/security/audit_log.rb +213 -0
- data/lib/rubydb/security/authentication.rb +302 -0
- data/lib/rubydb/security/authorization.rb +282 -0
- data/lib/rubydb/security/credentials.rb +196 -0
- data/lib/rubydb/security/password.rb +215 -0
- data/lib/rubydb/security/permissions.rb +74 -0
- data/lib/rubydb/security/role.rb +101 -0
- data/lib/rubydb/security/user.rb +86 -0
- data/lib/rubydb/server/connection.rb +366 -0
- data/lib/rubydb/server/connection_pool.rb +193 -0
- data/lib/rubydb/server/lifecycle.rb +228 -0
- data/lib/rubydb/server/listener.rb +136 -0
- data/lib/rubydb/server/request_handler.rb +276 -0
- data/lib/rubydb/server/server.rb +364 -0
- data/lib/rubydb/server/session.rb +369 -0
- data/lib/rubydb/server/worker.rb +210 -0
- data/lib/rubydb/server/worker_pool.rb +168 -0
- data/lib/rubydb/sql/ast/alter_table.rb +169 -0
- data/lib/rubydb/sql/ast/begin_transaction.rb +47 -0
- data/lib/rubydb/sql/ast/commit.rb +37 -0
- data/lib/rubydb/sql/ast/constraint.rb +83 -0
- data/lib/rubydb/sql/ast/create_database.rb +41 -0
- data/lib/rubydb/sql/ast/create_index.rb +61 -0
- data/lib/rubydb/sql/ast/create_schema.rb +52 -0
- data/lib/rubydb/sql/ast/create_table.rb +187 -0
- data/lib/rubydb/sql/ast/delete.rb +54 -0
- data/lib/rubydb/sql/ast/drop_database.rb +41 -0
- data/lib/rubydb/sql/ast/drop_index.rb +41 -0
- data/lib/rubydb/sql/ast/drop_schema.rb +49 -0
- data/lib/rubydb/sql/ast/drop_table.rb +49 -0
- data/lib/rubydb/sql/ast/explain.rb +64 -0
- data/lib/rubydb/sql/ast/expression.rb +604 -0
- data/lib/rubydb/sql/ast/insert.rb +82 -0
- data/lib/rubydb/sql/ast/node.rb +42 -0
- data/lib/rubydb/sql/ast/rollback.rb +63 -0
- data/lib/rubydb/sql/ast/savepoint.rb +59 -0
- data/lib/rubydb/sql/ast/select.rb +88 -0
- data/lib/rubydb/sql/ast/set_operation.rb +20 -0
- data/lib/rubydb/sql/ast/trigger.rb +29 -0
- data/lib/rubydb/sql/ast/update.rb +88 -0
- data/lib/rubydb/sql/ast/vacuum.rb +19 -0
- data/lib/rubydb/sql/ast/view.rb +32 -0
- data/lib/rubydb/sql/ast/with.rb +32 -0
- data/lib/rubydb/sql/grammar.rb +86 -0
- data/lib/rubydb/sql/keywords.rb +156 -0
- data/lib/rubydb/sql/lexer.rb +214 -0
- data/lib/rubydb/sql/operators.rb +100 -0
- data/lib/rubydb/sql/parser.rb +1185 -0
- data/lib/rubydb/sql/planner/analyzer.rb +302 -0
- data/lib/rubydb/sql/planner/binder.rb +550 -0
- data/lib/rubydb/sql/planner/type_checker.rb +431 -0
- data/lib/rubydb/sql/token.rb +210 -0
- data/lib/rubydb/storage/buffer_frame.rb +44 -0
- data/lib/rubydb/storage/buffer_pool.rb +155 -0
- data/lib/rubydb/storage/database_lock.rb +74 -0
- data/lib/rubydb/storage/deserializer.rb +342 -0
- data/lib/rubydb/storage/engine.rb +2338 -0
- data/lib/rubydb/storage/file_manager.rb +187 -0
- data/lib/rubydb/storage/free_space_map.rb +81 -0
- data/lib/rubydb/storage/page.rb +94 -0
- data/lib/rubydb/storage/page_allocator.rb +855 -0
- data/lib/rubydb/storage/page_header.rb +67 -0
- data/lib/rubydb/storage/page_manager.rb +131 -0
- data/lib/rubydb/storage/record.rb +58 -0
- data/lib/rubydb/storage/row.rb +78 -0
- data/lib/rubydb/storage/serializer.rb +51 -0
- data/lib/rubydb/storage/storage_layout.rb +151 -0
- data/lib/rubydb/storage/storage_manager.rb +114 -0
- data/lib/rubydb/storage/tuple.rb +461 -0
- data/lib/rubydb/storage/visibility_map.rb +984 -0
- data/lib/rubydb/transactions/commit_manager.rb +220 -0
- data/lib/rubydb/transactions/isolation.rb +98 -0
- data/lib/rubydb/transactions/lock.rb +76 -0
- data/lib/rubydb/transactions/lock_manager.rb +362 -0
- data/lib/rubydb/transactions/savepoint.rb +143 -0
- data/lib/rubydb/transactions/transaction.rb +215 -0
- data/lib/rubydb/transactions/transaction_id.rb +84 -0
- data/lib/rubydb/transactions/transaction_log.rb +257 -0
- data/lib/rubydb/transactions/transaction_manager.rb +435 -0
- data/lib/rubydb/types/bigint.rb +36 -0
- data/lib/rubydb/types/blob.rb +37 -0
- data/lib/rubydb/types/boolean.rb +34 -0
- data/lib/rubydb/types/date.rb +39 -0
- data/lib/rubydb/types/decimal.rb +48 -0
- data/lib/rubydb/types/float.rb +34 -0
- data/lib/rubydb/types/integer.rb +36 -0
- data/lib/rubydb/types/json.rb +41 -0
- data/lib/rubydb/types/null.rb +34 -0
- data/lib/rubydb/types/smallint.rb +36 -0
- data/lib/rubydb/types/text.rb +37 -0
- data/lib/rubydb/types/time.rb +46 -0
- data/lib/rubydb/types/timestamp.rb +39 -0
- data/lib/rubydb/types/type.rb +119 -0
- data/lib/rubydb/types/uuid.rb +47 -0
- data/lib/rubydb/types/varchar.rb +37 -0
- data/lib/rubydb/version.rb +33 -0
- data/lib/rubydb/wal/archive.rb +193 -0
- data/lib/rubydb/wal/checkpoint.rb +183 -0
- data/lib/rubydb/wal/lsn.rb +94 -0
- data/lib/rubydb/wal/reader.rb +260 -0
- data/lib/rubydb/wal/record.rb +105 -0
- data/lib/rubydb/wal/segment.rb +193 -0
- data/lib/rubydb/wal/wal.rb +452 -0
- data/lib/rubydb/wal/writer.rb +236 -0
- data/lib/rubydb.rb +7 -0
- data/packaging/docker/Dockerfile +36 -0
- data/packaging/docker/docker-compose.failover.yml +43 -0
- data/packaging/docker/entrypoint.sh +15 -0
- data/packaging/homebrew/rubydb.rb +19 -0
- data/packaging/systemd/rubydb.service +22 -0
- data/rubydb.gemspec +57 -0
- data/scripts/benchmark +7 -0
- data/scripts/durability_drill +37 -0
- data/scripts/format +0 -0
- data/scripts/fuzz +63 -0
- data/scripts/lint +0 -0
- data/scripts/release +50 -0
- data/scripts/release_check +43 -0
- data/scripts/replication_failover_drill +250 -0
- data/scripts/replication_network_failover_drill +255 -0
- data/scripts/restore_drill +45 -0
- data/scripts/security +0 -0
- data/scripts/setup +0 -0
- data/scripts/test +0 -0
- data/scripts/test-chaos +0 -0
- data/scripts/test-crash +0 -0
- data/scripts/test-integration +0 -0
- data/scripts/test-unit +0 -0
- data/visibility_map.json +1 -0
- metadata +538 -0
|
@@ -0,0 +1,698 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RubyDB
|
|
4
|
+
module Catalog
|
|
5
|
+
# SystemCatalog - maintains internal system tables with real data
|
|
6
|
+
# This is the PostgreSQL-style system catalog that tracks all database objects
|
|
7
|
+
class SystemCatalog
|
|
8
|
+
attr_reader :catalog, :system_tables, :oid_counter
|
|
9
|
+
|
|
10
|
+
def initialize(catalog)
|
|
11
|
+
@catalog = catalog
|
|
12
|
+
@system_tables = {}
|
|
13
|
+
@oid_counter = 1000 # Starting OID for system objects
|
|
14
|
+
@type_oids = {}
|
|
15
|
+
@rows = {} # Actual data storage for system tables
|
|
16
|
+
initialize_system_tables
|
|
17
|
+
populate
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def initialize_system_tables
|
|
21
|
+
# pg_namespace - schemas
|
|
22
|
+
@system_tables[:pg_namespace] = Table.new("pg_namespace")
|
|
23
|
+
@system_tables[:pg_namespace].add_column(Column.new("oid", :integer, primary_key: true))
|
|
24
|
+
@system_tables[:pg_namespace].add_column(Column.new("nspname", :text))
|
|
25
|
+
@system_tables[:pg_namespace].add_column(Column.new("nspowner", :integer))
|
|
26
|
+
@system_tables[:pg_namespace].add_column(Column.new("nspcreated", :timestamp))
|
|
27
|
+
|
|
28
|
+
# pg_class - tables, views, indexes, sequences
|
|
29
|
+
@system_tables[:pg_class] = Table.new("pg_class")
|
|
30
|
+
@system_tables[:pg_class].add_column(Column.new("oid", :integer, primary_key: true))
|
|
31
|
+
@system_tables[:pg_class].add_column(Column.new("relname", :text))
|
|
32
|
+
@system_tables[:pg_class].add_column(Column.new("relnamespace", :integer))
|
|
33
|
+
@system_tables[:pg_class].add_column(Column.new("relkind", :text)) # r=table, v=view, i=index, s=sequence, m=materialized
|
|
34
|
+
@system_tables[:pg_class].add_column(Column.new("reltuples", :bigint))
|
|
35
|
+
@system_tables[:pg_class].add_column(Column.new("relpages", :integer))
|
|
36
|
+
@system_tables[:pg_class].add_column(Column.new("relowner", :integer))
|
|
37
|
+
@system_tables[:pg_class].add_column(Column.new("relcreated", :timestamp))
|
|
38
|
+
@system_tables[:pg_class].add_column(Column.new("relmodified", :timestamp))
|
|
39
|
+
|
|
40
|
+
# pg_attribute - columns
|
|
41
|
+
@system_tables[:pg_attribute] = Table.new("pg_attribute")
|
|
42
|
+
@system_tables[:pg_attribute].add_column(Column.new("attrelid", :integer))
|
|
43
|
+
@system_tables[:pg_attribute].add_column(Column.new("attname", :text))
|
|
44
|
+
@system_tables[:pg_attribute].add_column(Column.new("atttypid", :integer))
|
|
45
|
+
@system_tables[:pg_attribute].add_column(Column.new("attnum", :integer))
|
|
46
|
+
@system_tables[:pg_attribute].add_column(Column.new("attlen", :integer))
|
|
47
|
+
@system_tables[:pg_attribute].add_column(Column.new("attnotnull", :boolean))
|
|
48
|
+
@system_tables[:pg_attribute].add_column(Column.new("atthasdef", :boolean))
|
|
49
|
+
@system_tables[:pg_attribute].add_column(Column.new("attdefault", :text))
|
|
50
|
+
@system_tables[:pg_attribute].add_column(Column.new("attisdropped", :boolean))
|
|
51
|
+
|
|
52
|
+
# pg_type - data types
|
|
53
|
+
@system_tables[:pg_type] = Table.new("pg_type")
|
|
54
|
+
@system_tables[:pg_type].add_column(Column.new("oid", :integer, primary_key: true))
|
|
55
|
+
@system_tables[:pg_type].add_column(Column.new("typname", :text))
|
|
56
|
+
@system_tables[:pg_type].add_column(Column.new("typlen", :integer))
|
|
57
|
+
@system_tables[:pg_type].add_column(Column.new("typtype", :text)) # b=base, c=composite, d=domain
|
|
58
|
+
|
|
59
|
+
# pg_index - indexes
|
|
60
|
+
@system_tables[:pg_index] = Table.new("pg_index")
|
|
61
|
+
@system_tables[:pg_index].add_column(Column.new("indexrelid", :integer))
|
|
62
|
+
@system_tables[:pg_index].add_column(Column.new("indrelid", :integer))
|
|
63
|
+
@system_tables[:pg_index].add_column(Column.new("indisunique", :boolean))
|
|
64
|
+
@system_tables[:pg_index].add_column(Column.new("indisprimary", :boolean))
|
|
65
|
+
@system_tables[:pg_index].add_column(Column.new("indkey", :text)) # Array of column numbers
|
|
66
|
+
@system_tables[:pg_index].add_column(Column.new("indpred", :text)) # Partial index predicate
|
|
67
|
+
|
|
68
|
+
# pg_constraint - constraints
|
|
69
|
+
@system_tables[:pg_constraint] = Table.new("pg_constraint")
|
|
70
|
+
@system_tables[:pg_constraint].add_column(Column.new("oid", :integer, primary_key: true))
|
|
71
|
+
@system_tables[:pg_constraint].add_column(Column.new("conname", :text))
|
|
72
|
+
@system_tables[:pg_constraint].add_column(Column.new("contype", :text)) # p=primary, f=foreign, u=unique, c=check
|
|
73
|
+
@system_tables[:pg_constraint].add_column(Column.new("conrelid", :integer))
|
|
74
|
+
@system_tables[:pg_constraint].add_column(Column.new("confrelid", :integer))
|
|
75
|
+
@system_tables[:pg_constraint].add_column(Column.new("conkey", :text)) # Array of column numbers
|
|
76
|
+
@system_tables[:pg_constraint].add_column(Column.new("confkey", :text)) # Array of foreign column numbers
|
|
77
|
+
@system_tables[:pg_constraint].add_column(Column.new("confdeltype", :text)) # a=no action, r=restrict, c=cascade
|
|
78
|
+
@system_tables[:pg_constraint].add_column(Column.new("confupdtype", :text))
|
|
79
|
+
@system_tables[:pg_constraint].add_column(Column.new("condeferrable", :boolean))
|
|
80
|
+
@system_tables[:pg_constraint].add_column(Column.new("condeferred", :boolean))
|
|
81
|
+
|
|
82
|
+
# pg_sequence - sequences
|
|
83
|
+
@system_tables[:pg_sequence] = Table.new("pg_sequence")
|
|
84
|
+
@system_tables[:pg_sequence].add_column(Column.new("seqrelid", :integer))
|
|
85
|
+
@system_tables[:pg_sequence].add_column(Column.new("seqstart", :bigint))
|
|
86
|
+
@system_tables[:pg_sequence].add_column(Column.new("seqincrement", :bigint))
|
|
87
|
+
@system_tables[:pg_sequence].add_column(Column.new("seqmax", :bigint))
|
|
88
|
+
@system_tables[:pg_sequence].add_column(Column.new("seqmin", :bigint))
|
|
89
|
+
@system_tables[:pg_sequence].add_column(Column.new("seqcycle", :boolean))
|
|
90
|
+
@system_tables[:pg_sequence].add_column(Column.new("seqcurrent", :bigint))
|
|
91
|
+
|
|
92
|
+
# pg_view - views
|
|
93
|
+
@system_tables[:pg_view] = Table.new("pg_view")
|
|
94
|
+
@system_tables[:pg_view].add_column(Column.new("viewrelid", :integer))
|
|
95
|
+
@system_tables[:pg_view].add_column(Column.new("viewdefinition", :text))
|
|
96
|
+
@system_tables[:pg_view].add_column(Column.new("viewmaterialized", :boolean))
|
|
97
|
+
|
|
98
|
+
# pg_trigger - triggers
|
|
99
|
+
@system_tables[:pg_trigger] = Table.new("pg_trigger")
|
|
100
|
+
@system_tables[:pg_trigger].add_column(Column.new("oid", :integer, primary_key: true))
|
|
101
|
+
@system_tables[:pg_trigger].add_column(Column.new("tgname", :text))
|
|
102
|
+
@system_tables[:pg_trigger].add_column(Column.new("tgrelid", :integer))
|
|
103
|
+
@system_tables[:pg_trigger].add_column(Column.new("tgenabled", :boolean))
|
|
104
|
+
@system_tables[:pg_trigger].add_column(Column.new("tgtype", :integer)) # Bitmask: 1=insert, 2=update, 4=delete
|
|
105
|
+
@system_tables[:pg_trigger].add_column(Column.new("tgtiming", :integer)) # 1=before, 2=after, 3=instead_of
|
|
106
|
+
@system_tables[:pg_trigger].add_column(Column.new("tgcondition", :text))
|
|
107
|
+
@system_tables[:pg_trigger].add_column(Column.new("tgdefinition", :text))
|
|
108
|
+
|
|
109
|
+
# pg_database - databases
|
|
110
|
+
@system_tables[:pg_database] = Table.new("pg_database")
|
|
111
|
+
@system_tables[:pg_database].add_column(Column.new("oid", :integer, primary_key: true))
|
|
112
|
+
@system_tables[:pg_database].add_column(Column.new("datname", :text))
|
|
113
|
+
@system_tables[:pg_database].add_column(Column.new("datowner", :integer))
|
|
114
|
+
@system_tables[:pg_database].add_column(Column.new("datcreated", :timestamp))
|
|
115
|
+
|
|
116
|
+
# pg_user - users (simplified)
|
|
117
|
+
@system_tables[:pg_user] = Table.new("pg_user")
|
|
118
|
+
@system_tables[:pg_user].add_column(Column.new("usename", :text, primary_key: true))
|
|
119
|
+
@system_tables[:pg_user].add_column(Column.new("usesysid", :integer))
|
|
120
|
+
@system_tables[:pg_user].add_column(Column.new("usecreatedb", :boolean))
|
|
121
|
+
@system_tables[:pg_user].add_column(Column.new("usesuper", :boolean))
|
|
122
|
+
|
|
123
|
+
# Initialize rows storage
|
|
124
|
+
@system_tables.each_key do |name|
|
|
125
|
+
@rows[name] = []
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
build_type_oids
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def build_type_oids
|
|
132
|
+
@type_oids = {
|
|
133
|
+
integer: 20,
|
|
134
|
+
bigint: 21,
|
|
135
|
+
smallint: 22,
|
|
136
|
+
float: 23,
|
|
137
|
+
decimal: 24,
|
|
138
|
+
boolean: 25,
|
|
139
|
+
text: 26,
|
|
140
|
+
varchar: 27,
|
|
141
|
+
blob: 28,
|
|
142
|
+
date: 29,
|
|
143
|
+
time: 30,
|
|
144
|
+
timestamp: 31,
|
|
145
|
+
json: 32,
|
|
146
|
+
uuid: 33,
|
|
147
|
+
char: 34,
|
|
148
|
+
numeric: 35,
|
|
149
|
+
double_precision: 36,
|
|
150
|
+
real: 37,
|
|
151
|
+
serial: 38,
|
|
152
|
+
bigserial: 39
|
|
153
|
+
}
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# Populate system tables from catalog
|
|
157
|
+
def populate
|
|
158
|
+
clear_all_tables
|
|
159
|
+
|
|
160
|
+
# Populate pg_type with built-in types
|
|
161
|
+
populate_types
|
|
162
|
+
|
|
163
|
+
# Populate pg_database
|
|
164
|
+
populate_databases
|
|
165
|
+
|
|
166
|
+
# Populate pg_namespace
|
|
167
|
+
populate_namespaces
|
|
168
|
+
|
|
169
|
+
# Populate pg_user (default users)
|
|
170
|
+
populate_users
|
|
171
|
+
|
|
172
|
+
# Populate pg_class, pg_attribute, etc.
|
|
173
|
+
populate_catalog_objects
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def clear_all_tables
|
|
177
|
+
@rows.each_value(&:clear)
|
|
178
|
+
@system_tables.each_value { |t| t.row_count = 0 }
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def populate_types
|
|
182
|
+
@type_oids.each do |type_name, oid|
|
|
183
|
+
row = {
|
|
184
|
+
"oid" => oid,
|
|
185
|
+
"typname" => type_name.to_s,
|
|
186
|
+
"typlen" => type_size(type_name),
|
|
187
|
+
"typtype" => "b"
|
|
188
|
+
}
|
|
189
|
+
insert_row(:pg_type, row)
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def type_size(type_name)
|
|
194
|
+
case type_name
|
|
195
|
+
when :integer, :serial then 4
|
|
196
|
+
when :bigint, :bigserial then 8
|
|
197
|
+
when :smallint then 2
|
|
198
|
+
when :float, :double_precision, :real then 8
|
|
199
|
+
when :boolean then 1
|
|
200
|
+
when :date then 4
|
|
201
|
+
when :timestamp, :time then 8
|
|
202
|
+
when :char then 1
|
|
203
|
+
else 0 # variable length
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def populate_databases
|
|
208
|
+
@catalog.databases.each do |db_name, db|
|
|
209
|
+
oid = next_oid
|
|
210
|
+
row = {
|
|
211
|
+
"oid" => oid,
|
|
212
|
+
"datname" => db_name,
|
|
213
|
+
"datowner" => 1,
|
|
214
|
+
"datcreated" => db.created_at.iso8601
|
|
215
|
+
}
|
|
216
|
+
insert_row(:pg_database, row)
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def populate_namespaces
|
|
221
|
+
# public schema
|
|
222
|
+
row = {
|
|
223
|
+
"oid" => next_oid,
|
|
224
|
+
"nspname" => "public",
|
|
225
|
+
"nspowner" => 1,
|
|
226
|
+
"nspcreated" => Time.now.iso8601
|
|
227
|
+
}
|
|
228
|
+
insert_row(:pg_namespace, row)
|
|
229
|
+
|
|
230
|
+
# information_schema
|
|
231
|
+
row = {
|
|
232
|
+
"oid" => next_oid,
|
|
233
|
+
"nspname" => "information_schema",
|
|
234
|
+
"nspowner" => 1,
|
|
235
|
+
"nspcreated" => Time.now.iso8601
|
|
236
|
+
}
|
|
237
|
+
insert_row(:pg_namespace, row)
|
|
238
|
+
|
|
239
|
+
# pg_catalog (self)
|
|
240
|
+
row = {
|
|
241
|
+
"oid" => next_oid,
|
|
242
|
+
"nspname" => "pg_catalog",
|
|
243
|
+
"nspowner" => 1,
|
|
244
|
+
"nspcreated" => Time.now.iso8601
|
|
245
|
+
}
|
|
246
|
+
insert_row(:pg_namespace, row)
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
def populate_users
|
|
250
|
+
# Default admin user
|
|
251
|
+
row = {
|
|
252
|
+
"usename" => "postgres",
|
|
253
|
+
"usesysid" => 1,
|
|
254
|
+
"usecreatedb" => true,
|
|
255
|
+
"usesuper" => true
|
|
256
|
+
}
|
|
257
|
+
insert_row(:pg_user, row)
|
|
258
|
+
|
|
259
|
+
# Default application user
|
|
260
|
+
row = {
|
|
261
|
+
"usename" => "rubydb",
|
|
262
|
+
"usesysid" => 2,
|
|
263
|
+
"usecreatedb" => true,
|
|
264
|
+
"usesuper" => false
|
|
265
|
+
}
|
|
266
|
+
insert_row(:pg_user, row)
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
def populate_catalog_objects
|
|
270
|
+
return unless @catalog.current_database
|
|
271
|
+
|
|
272
|
+
db = @catalog.current_database
|
|
273
|
+
|
|
274
|
+
# Populate tables
|
|
275
|
+
db.tables.each do |table|
|
|
276
|
+
sync_table(table)
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
# Populate sequences
|
|
280
|
+
db.sequences.each do |seq|
|
|
281
|
+
sync_sequence(seq)
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
# Populate views
|
|
285
|
+
db.views.each do |view|
|
|
286
|
+
sync_view(view)
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
# Populate triggers
|
|
290
|
+
db.triggers.each do |trigger|
|
|
291
|
+
sync_trigger(trigger)
|
|
292
|
+
end
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
def sync_table(table)
|
|
296
|
+
# Get namespace OID
|
|
297
|
+
ns_oid = get_namespace_oid("public")
|
|
298
|
+
oid = next_oid
|
|
299
|
+
|
|
300
|
+
# Add to pg_class
|
|
301
|
+
row = {
|
|
302
|
+
"oid" => oid,
|
|
303
|
+
"relname" => table.name,
|
|
304
|
+
"relnamespace" => ns_oid,
|
|
305
|
+
"relkind" => "r",
|
|
306
|
+
"reltuples" => table.row_count || 0,
|
|
307
|
+
"relpages" => table.storage_size || 0,
|
|
308
|
+
"relowner" => 1,
|
|
309
|
+
"relcreated" => table.created_at.iso8601,
|
|
310
|
+
"relmodified" => table.modified_at.iso8601
|
|
311
|
+
}
|
|
312
|
+
insert_row(:pg_class, row)
|
|
313
|
+
|
|
314
|
+
# Store the OID for reference
|
|
315
|
+
table.instance_variable_set(:@system_oid, oid)
|
|
316
|
+
|
|
317
|
+
# Add columns to pg_attribute
|
|
318
|
+
table.columns.each_with_index do |col, idx|
|
|
319
|
+
type_oid = @type_oids[col.type_class] || 0
|
|
320
|
+
row = {
|
|
321
|
+
"attrelid" => oid,
|
|
322
|
+
"attname" => col.name,
|
|
323
|
+
"atttypid" => type_oid,
|
|
324
|
+
"attnum" => idx + 1,
|
|
325
|
+
"attlen" => type_size(col.type_class),
|
|
326
|
+
"attnotnull" => !col.nullable?,
|
|
327
|
+
"atthasdef" => col.has_default?,
|
|
328
|
+
"attdefault" => col.default.to_s,
|
|
329
|
+
"attisdropped" => false
|
|
330
|
+
}
|
|
331
|
+
insert_row(:pg_attribute, row)
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
# Add constraints to pg_constraint
|
|
335
|
+
table.constraints.each do |constraint|
|
|
336
|
+
sync_constraint(constraint, oid)
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
# Add indexes to pg_index
|
|
340
|
+
table.indexes.each do |index|
|
|
341
|
+
sync_index(index, oid)
|
|
342
|
+
end
|
|
343
|
+
end
|
|
344
|
+
|
|
345
|
+
def sync_constraint(constraint, rel_oid)
|
|
346
|
+
constr_oid = next_oid
|
|
347
|
+
|
|
348
|
+
base_row = {
|
|
349
|
+
"oid" => constr_oid,
|
|
350
|
+
"conname" => constraint.name,
|
|
351
|
+
"conrelid" => rel_oid,
|
|
352
|
+
"confrelid" => 0,
|
|
353
|
+
"conkey" => "",
|
|
354
|
+
"confkey" => "",
|
|
355
|
+
"confdeltype" => "",
|
|
356
|
+
"confupdtype" => "",
|
|
357
|
+
"condeferrable" => false,
|
|
358
|
+
"condeferred" => false
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
case constraint
|
|
362
|
+
when PrimaryKeyConstraint
|
|
363
|
+
row = base_row.merge(
|
|
364
|
+
"contype" => "p",
|
|
365
|
+
"conkey" => constraint.columns.join(",")
|
|
366
|
+
)
|
|
367
|
+
when ForeignKeyConstraint
|
|
368
|
+
row = base_row.merge(
|
|
369
|
+
"contype" => "f",
|
|
370
|
+
"confrelid" => get_table_oid(constraint.reference_table),
|
|
371
|
+
"conkey" => constraint.columns.join(","),
|
|
372
|
+
"confkey" => constraint.reference_columns.join(","),
|
|
373
|
+
"confdeltype" => map_delete_action(constraint.on_delete),
|
|
374
|
+
"confupdtype" => map_update_action(constraint.on_update)
|
|
375
|
+
)
|
|
376
|
+
when UniqueConstraint
|
|
377
|
+
row = base_row.merge(
|
|
378
|
+
"contype" => "u",
|
|
379
|
+
"conkey" => constraint.columns.join(",")
|
|
380
|
+
)
|
|
381
|
+
when CheckConstraint
|
|
382
|
+
row = base_row.merge(
|
|
383
|
+
"contype" => "c"
|
|
384
|
+
)
|
|
385
|
+
else
|
|
386
|
+
return
|
|
387
|
+
end
|
|
388
|
+
|
|
389
|
+
insert_row(:pg_constraint, row)
|
|
390
|
+
end
|
|
391
|
+
|
|
392
|
+
def sync_index(index, rel_oid)
|
|
393
|
+
# Create index entry in pg_class
|
|
394
|
+
idx_oid = next_oid
|
|
395
|
+
ns_oid = get_namespace_oid("public")
|
|
396
|
+
|
|
397
|
+
row = {
|
|
398
|
+
"oid" => idx_oid,
|
|
399
|
+
"relname" => index.name,
|
|
400
|
+
"relnamespace" => ns_oid,
|
|
401
|
+
"relkind" => "i",
|
|
402
|
+
"reltuples" => 0,
|
|
403
|
+
"relpages" => 0,
|
|
404
|
+
"relowner" => 1,
|
|
405
|
+
"relcreated" => index.created_at.iso8601,
|
|
406
|
+
"relmodified" => index.modified_at.iso8601
|
|
407
|
+
}
|
|
408
|
+
insert_row(:pg_class, row)
|
|
409
|
+
|
|
410
|
+
# Get column positions from pg_attribute
|
|
411
|
+
column_numbers = index.columns.map do |col_name|
|
|
412
|
+
# Find the column position
|
|
413
|
+
attrs = query(:pg_attribute, attrelid: rel_oid, attname: col_name)
|
|
414
|
+
if attrs.any?
|
|
415
|
+
attrs.first["attnum"]
|
|
416
|
+
else
|
|
417
|
+
0
|
|
418
|
+
end
|
|
419
|
+
end
|
|
420
|
+
|
|
421
|
+
# Add to pg_index
|
|
422
|
+
row = {
|
|
423
|
+
"indexrelid" => idx_oid,
|
|
424
|
+
"indrelid" => rel_oid,
|
|
425
|
+
"indisunique" => index.unique,
|
|
426
|
+
"indisprimary" => false,
|
|
427
|
+
"indkey" => column_numbers.join(","),
|
|
428
|
+
"indpred" => index.options[:where] || ""
|
|
429
|
+
}
|
|
430
|
+
insert_row(:pg_index, row)
|
|
431
|
+
end
|
|
432
|
+
|
|
433
|
+
def sync_sequence(seq)
|
|
434
|
+
ns_oid = get_namespace_oid("public")
|
|
435
|
+
oid = next_oid
|
|
436
|
+
|
|
437
|
+
# Add to pg_class
|
|
438
|
+
row = {
|
|
439
|
+
"oid" => oid,
|
|
440
|
+
"relname" => seq.name,
|
|
441
|
+
"relnamespace" => ns_oid,
|
|
442
|
+
"relkind" => "s",
|
|
443
|
+
"reltuples" => 0,
|
|
444
|
+
"relpages" => 0,
|
|
445
|
+
"relowner" => 1,
|
|
446
|
+
"relcreated" => seq.created_at.iso8601,
|
|
447
|
+
"relmodified" => seq.modified_at.iso8601
|
|
448
|
+
}
|
|
449
|
+
insert_row(:pg_class, row)
|
|
450
|
+
|
|
451
|
+
# Add to pg_sequence
|
|
452
|
+
row = {
|
|
453
|
+
"seqrelid" => oid,
|
|
454
|
+
"seqstart" => seq.start_value,
|
|
455
|
+
"seqincrement" => seq.increment,
|
|
456
|
+
"seqmax" => seq.max_value,
|
|
457
|
+
"seqmin" => seq.min_value,
|
|
458
|
+
"seqcycle" => seq.cycle,
|
|
459
|
+
"seqcurrent" => seq.current_value
|
|
460
|
+
}
|
|
461
|
+
insert_row(:pg_sequence, row)
|
|
462
|
+
end
|
|
463
|
+
|
|
464
|
+
def sync_view(view)
|
|
465
|
+
ns_oid = get_namespace_oid("public")
|
|
466
|
+
oid = next_oid
|
|
467
|
+
|
|
468
|
+
# Add to pg_class
|
|
469
|
+
row = {
|
|
470
|
+
"oid" => oid,
|
|
471
|
+
"relname" => view.name,
|
|
472
|
+
"relnamespace" => ns_oid,
|
|
473
|
+
"relkind" => view.materialized ? "m" : "v",
|
|
474
|
+
"reltuples" => 0,
|
|
475
|
+
"relpages" => 0,
|
|
476
|
+
"relowner" => 1,
|
|
477
|
+
"relcreated" => view.created_at.iso8601,
|
|
478
|
+
"relmodified" => view.modified_at.iso8601
|
|
479
|
+
}
|
|
480
|
+
insert_row(:pg_class, row)
|
|
481
|
+
|
|
482
|
+
# Add to pg_view
|
|
483
|
+
row = {
|
|
484
|
+
"viewrelid" => oid,
|
|
485
|
+
"viewdefinition" => view.query,
|
|
486
|
+
"viewmaterialized" => view.materialized
|
|
487
|
+
}
|
|
488
|
+
insert_row(:pg_view, row)
|
|
489
|
+
end
|
|
490
|
+
|
|
491
|
+
def sync_trigger(trigger)
|
|
492
|
+
oid = next_oid
|
|
493
|
+
table_oid = get_table_oid(trigger.table_name)
|
|
494
|
+
|
|
495
|
+
# Convert event types to bitmask
|
|
496
|
+
event_bitmask = 0
|
|
497
|
+
event_bitmask |= 1 if trigger.on_insert?
|
|
498
|
+
event_bitmask |= 2 if trigger.on_update?
|
|
499
|
+
event_bitmask |= 4 if trigger.on_delete?
|
|
500
|
+
|
|
501
|
+
# Convert timing
|
|
502
|
+
timing_value = case trigger.timing
|
|
503
|
+
when :before then 1
|
|
504
|
+
when :after then 2
|
|
505
|
+
when :instead_of then 3
|
|
506
|
+
else 2
|
|
507
|
+
end
|
|
508
|
+
|
|
509
|
+
row = {
|
|
510
|
+
"oid" => oid,
|
|
511
|
+
"tgname" => trigger.name,
|
|
512
|
+
"tgrelid" => table_oid || 0,
|
|
513
|
+
"tgenabled" => trigger.enabled,
|
|
514
|
+
"tgtype" => event_bitmask,
|
|
515
|
+
"tgtiming" => timing_value,
|
|
516
|
+
"tgcondition" => trigger.condition || "",
|
|
517
|
+
"tgdefinition" => trigger.definition
|
|
518
|
+
}
|
|
519
|
+
insert_row(:pg_trigger, row)
|
|
520
|
+
end
|
|
521
|
+
|
|
522
|
+
# Helper methods
|
|
523
|
+
|
|
524
|
+
def insert_row(table_name, row_data)
|
|
525
|
+
table = @system_tables[table_name]
|
|
526
|
+
return unless table
|
|
527
|
+
|
|
528
|
+
table.row_count += 1
|
|
529
|
+
@rows[table_name] ||= []
|
|
530
|
+
@rows[table_name] << row_data
|
|
531
|
+
end
|
|
532
|
+
|
|
533
|
+
def next_oid
|
|
534
|
+
@oid_counter += 1
|
|
535
|
+
@oid_counter
|
|
536
|
+
end
|
|
537
|
+
|
|
538
|
+
def get_namespace_oid(name)
|
|
539
|
+
rows = @rows[:pg_namespace] || []
|
|
540
|
+
row = rows.find { |r| r["nspname"] == name }
|
|
541
|
+
row ? row["oid"] : 1
|
|
542
|
+
end
|
|
543
|
+
|
|
544
|
+
def get_table_oid(name)
|
|
545
|
+
rows = @rows[:pg_class] || []
|
|
546
|
+
row = rows.find { |r| r["relname"] == name && r["relkind"] == "r" }
|
|
547
|
+
row ? row["oid"] : 0
|
|
548
|
+
end
|
|
549
|
+
|
|
550
|
+
def map_delete_action(action)
|
|
551
|
+
case action&.to_s&.upcase
|
|
552
|
+
when "CASCADE" then "c"
|
|
553
|
+
when "RESTRICT" then "r"
|
|
554
|
+
when "SET NULL" then "n"
|
|
555
|
+
when "SET DEFAULT" then "d"
|
|
556
|
+
else "a"
|
|
557
|
+
end
|
|
558
|
+
end
|
|
559
|
+
|
|
560
|
+
def map_update_action(action)
|
|
561
|
+
case action&.to_s&.upcase
|
|
562
|
+
when "CASCADE" then "c"
|
|
563
|
+
when "RESTRICT" then "r"
|
|
564
|
+
when "SET NULL" then "n"
|
|
565
|
+
when "SET DEFAULT" then "d"
|
|
566
|
+
else "a"
|
|
567
|
+
end
|
|
568
|
+
end
|
|
569
|
+
|
|
570
|
+
# Query system tables
|
|
571
|
+
|
|
572
|
+
def query(table_name, conditions = {})
|
|
573
|
+
rows = @rows[table_name.to_sym] || []
|
|
574
|
+
rows.select do |row|
|
|
575
|
+
conditions.all? { |key, value| row[key.to_s] == value }
|
|
576
|
+
end
|
|
577
|
+
end
|
|
578
|
+
|
|
579
|
+
def find_table_oid(table_name)
|
|
580
|
+
result = query(:pg_class, relname: table_name.to_s, relkind: "r")
|
|
581
|
+
result.first&.fetch("oid")
|
|
582
|
+
end
|
|
583
|
+
|
|
584
|
+
def find_index_oid(index_name)
|
|
585
|
+
result = query(:pg_class, relname: index_name.to_s, relkind: "i")
|
|
586
|
+
result.first&.fetch("oid")
|
|
587
|
+
end
|
|
588
|
+
|
|
589
|
+
def find_sequence_oid(sequence_name)
|
|
590
|
+
result = query(:pg_class, relname: sequence_name.to_s, relkind: "s")
|
|
591
|
+
result.first&.fetch("oid")
|
|
592
|
+
end
|
|
593
|
+
|
|
594
|
+
def find_view_oid(view_name)
|
|
595
|
+
result = query(:pg_class, relname: view_name.to_s, relkind: ["v", "m"])
|
|
596
|
+
result.first&.fetch("oid")
|
|
597
|
+
end
|
|
598
|
+
|
|
599
|
+
def find_columns_for_table(table_name)
|
|
600
|
+
oid = find_table_oid(table_name)
|
|
601
|
+
return [] unless oid
|
|
602
|
+
|
|
603
|
+
query(:pg_attribute, attrelid: oid).reject { |r| r["attisdropped"] }
|
|
604
|
+
end
|
|
605
|
+
|
|
606
|
+
def find_constraints_for_table(table_name)
|
|
607
|
+
oid = find_table_oid(table_name)
|
|
608
|
+
return [] unless oid
|
|
609
|
+
|
|
610
|
+
query(:pg_constraint, conrelid: oid)
|
|
611
|
+
end
|
|
612
|
+
|
|
613
|
+
def find_indexes_for_table(table_name)
|
|
614
|
+
oid = find_table_oid(table_name)
|
|
615
|
+
return [] unless oid
|
|
616
|
+
|
|
617
|
+
query(:pg_index, indrelid: oid)
|
|
618
|
+
end
|
|
619
|
+
|
|
620
|
+
def find_triggers_for_table(table_name)
|
|
621
|
+
oid = find_table_oid(table_name)
|
|
622
|
+
return [] unless oid
|
|
623
|
+
|
|
624
|
+
query(:pg_trigger, tgrelid: oid)
|
|
625
|
+
end
|
|
626
|
+
|
|
627
|
+
def find_tables_in_schema(schema_name = "public")
|
|
628
|
+
ns_oid = get_namespace_oid(schema_name)
|
|
629
|
+
query(:pg_class, relnamespace: ns_oid, relkind: "r")
|
|
630
|
+
end
|
|
631
|
+
|
|
632
|
+
def find_all_tables
|
|
633
|
+
query(:pg_class, relkind: "r")
|
|
634
|
+
end
|
|
635
|
+
|
|
636
|
+
def table_exists?(table_name)
|
|
637
|
+
!find_table_oid(table_name).nil?
|
|
638
|
+
end
|
|
639
|
+
|
|
640
|
+
def index_exists?(index_name)
|
|
641
|
+
!find_index_oid(index_name).nil?
|
|
642
|
+
end
|
|
643
|
+
|
|
644
|
+
def column_exists?(table_name, column_name)
|
|
645
|
+
oid = find_table_oid(table_name)
|
|
646
|
+
return false unless oid
|
|
647
|
+
|
|
648
|
+
query(:pg_attribute, attrelid: oid, attname: column_name).any?
|
|
649
|
+
end
|
|
650
|
+
|
|
651
|
+
def type_exists?(type_name)
|
|
652
|
+
@type_oids.key?(type_name.to_sym)
|
|
653
|
+
end
|
|
654
|
+
|
|
655
|
+
def get_type_oid(type_name)
|
|
656
|
+
@type_oids[type_name.to_sym]
|
|
657
|
+
end
|
|
658
|
+
|
|
659
|
+
# Serialization
|
|
660
|
+
|
|
661
|
+
def serialize
|
|
662
|
+
{
|
|
663
|
+
oid_counter: @oid_counter,
|
|
664
|
+
type_oids: @type_oids,
|
|
665
|
+
tables: @rows.transform_values(&:dup)
|
|
666
|
+
}
|
|
667
|
+
end
|
|
668
|
+
|
|
669
|
+
def self.deserialize(data, catalog)
|
|
670
|
+
system_catalog = new(catalog)
|
|
671
|
+
system_catalog.instance_variable_set(:@oid_counter, data[:oid_counter] || 1000)
|
|
672
|
+
system_catalog.instance_variable_set(:@type_oids, data[:type_oids] || {})
|
|
673
|
+
|
|
674
|
+
data[:tables].each do |name, rows|
|
|
675
|
+
system_catalog.instance_variable_get(:@rows)[name.to_sym] = rows.dup
|
|
676
|
+
table = system_catalog.system_tables[name.to_sym]
|
|
677
|
+
table.row_count = rows.size if table
|
|
678
|
+
end
|
|
679
|
+
|
|
680
|
+
system_catalog
|
|
681
|
+
end
|
|
682
|
+
|
|
683
|
+
def to_s
|
|
684
|
+
"SystemCatalog with #{@system_tables.size} system tables and #{total_rows} total rows"
|
|
685
|
+
end
|
|
686
|
+
|
|
687
|
+
def inspect
|
|
688
|
+
to_s
|
|
689
|
+
end
|
|
690
|
+
|
|
691
|
+
private
|
|
692
|
+
|
|
693
|
+
def total_rows
|
|
694
|
+
@rows.values.sum(&:size)
|
|
695
|
+
end
|
|
696
|
+
end
|
|
697
|
+
end
|
|
698
|
+
end
|