rubydb 0.1.3 → 0.1.5
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 +4 -4
- data/.github/PULL_REQUEST_TEMPLATE.md +15 -15
- data/.github/workflows/benchmark.yml +26 -26
- data/.github/workflows/compatibility.yml +63 -63
- data/.github/workflows/fuzz.yml +33 -33
- data/.github/workflows/lint.yml +21 -21
- data/.github/workflows/operations.yml +24 -24
- data/.github/workflows/production-validation.yml +111 -111
- data/.github/workflows/release.yml +77 -77
- data/.github/workflows/security.yml +39 -37
- data/.github/workflows/test.yml +26 -26
- data/.github/workflows/workload.yml +58 -58
- data/.gitignore +12 -5
- data/.rubocop.yml +50 -44
- data/.standard.yml +9 -14
- data/ARCHITECTURE.md +21 -21
- data/CHANGELOG.md +49 -27
- data/CODE_OF_CONDUCT.md +13 -13
- data/CONTRIBUTING.md +29 -29
- data/GOVERNANCE.md +16 -16
- data/Gemfile +18 -17
- data/Gemfile.lock +125 -71
- data/README.md +249 -162
- data/ROADMAP.md +27 -27
- data/Rakefile +71 -71
- data/SECURITY.md +54 -54
- data/SUPPORT.md +14 -14
- data/adapters/activerecord/Gemfile +11 -11
- data/adapters/activerecord/lib/active_record/connection_adapters/rubydb_adapter.rb +877 -880
- data/adapters/activerecord/rubydb-activerecord.gemspec +21 -21
- data/adapters/activerecord/spec/rubydb_adapter_integration_spec.rb +155 -143
- data/adapters/ruby/README.md +18 -18
- data/adapters/sequel/README.md +11 -11
- data/config/monitoring/prometheus-alerts.yml +39 -39
- data/config/production.yml +36 -36
- data/docs/README.md +75 -71
- data/docs/architecture/concurrency.md +14 -14
- data/docs/architecture/current-state.md +125 -125
- data/docs/architecture/execution-engine.md +25 -25
- data/docs/architecture/indexes.md +19 -19
- data/docs/architecture/mvcc.md +19 -19
- data/docs/architecture/overview.md +13 -13
- data/docs/architecture/pages.md +11 -11
- data/docs/architecture/production-roadmap.md +82 -82
- data/docs/architecture/query-planner.md +20 -20
- data/docs/architecture/recovery.md +18 -18
- data/docs/architecture/sql-engine.md +12 -12
- data/docs/architecture/storage-engine.md +14 -14
- data/docs/architecture/transactions.md +10 -10
- data/docs/architecture/wal.md +28 -28
- data/docs/cli-cheatsheet.md +98 -98
- data/docs/cli.md +275 -275
- data/docs/contributing/architecture.md +9 -9
- data/docs/contributing/benchmarking.md +14 -14
- data/docs/contributing/development.md +16 -16
- data/docs/contributing/release-process.md +49 -49
- data/docs/contributing/testing.md +16 -16
- data/docs/debugging.md +229 -229
- data/docs/developer/branching.md +10 -10
- data/docs/developer/database-diff.md +10 -10
- data/docs/developer/local-development.md +17 -17
- data/docs/developer/snapshots.md +9 -9
- data/docs/developer/temporal-data.md +10 -10
- data/docs/developer-guide.md +297 -297
- data/docs/getting-started/first-database.md +16 -16
- data/docs/getting-started/first-query.md +13 -13
- data/docs/getting-started/installation.md +19 -19
- data/docs/getting-started/local-to-production.md +300 -300
- data/docs/getting-started/quickstart.md +17 -17
- data/docs/getting-started/rails.md +16 -16
- data/docs/hardening_backlog.md +93 -93
- data/docs/lessons-learned.md +112 -112
- data/docs/operations/backups.md +33 -33
- data/docs/operations/disaster-recovery.md +31 -31
- data/docs/operations/failover.md +30 -30
- data/docs/operations/monitoring.md +25 -25
- data/docs/operations/production-guide.md +295 -295
- data/docs/operations/production-runbook.md +45 -45
- data/docs/operations/replication.md +33 -33
- data/docs/operations/restore.md +6 -6
- data/docs/operations/runbook.md +34 -34
- data/docs/operations/upgrades.md +14 -14
- data/docs/operations/workload-testing.md +17 -17
- data/docs/production-readiness.md +118 -118
- data/docs/production_validation.md +150 -150
- data/docs/rails/active-record.md +11 -11
- data/docs/rails/compatibility-guide.md +90 -90
- data/docs/rails/database-yml.md +92 -92
- data/docs/rails/installation.md +17 -17
- data/docs/rails/migrations.md +17 -17
- data/docs/rails/production.md +82 -82
- data/docs/rails/troubleshooting.md +18 -18
- data/docs/release.md +25 -25
- data/docs/server/architecture.md +10 -10
- data/docs/server/authentication.md +10 -10
- data/docs/server/configuration.md +16 -16
- data/docs/server/connection-pooling.md +10 -10
- data/docs/server/deployment.md +10 -10
- data/docs/server/protocol.md +12 -12
- data/docs/sql/compatibility-guide.md +82 -82
- data/docs/sql/compatibility.md +39 -39
- data/docs/sql/data-types.md +10 -10
- data/docs/sql/functions.md +9 -9
- data/docs/sql/joins.md +9 -9
- data/docs/sql/operators.md +9 -9
- data/docs/sql/sqlite-compatibility.md +21 -21
- data/docs/sql/syntax.md +10 -10
- data/docs/sql/transactions.md +10 -10
- data/docs/troubleshooting.md +244 -244
- data/lessons/01-foundations.md +73 -0
- data/lessons/02-local-development.md +121 -0
- data/lessons/03-embedded-rubydb.md +99 -0
- data/lessons/04-rails-complex-apps.md +138 -0
- data/lessons/05-rubydb-production-server.md +237 -0
- data/lessons/06-postgresql-massive-apps.md +96 -0
- data/lessons/07-hybrid-microservices.md +94 -0
- data/lessons/08-migrations-backups-recovery.md +86 -0
- data/lessons/09-observability-security-scale.md +87 -0
- data/lessons/10-release-readiness.md +125 -0
- data/lib/rubydb/backup/archive.rb +332 -334
- data/lib/rubydb/backup/backup.rb +400 -401
- data/lib/rubydb/backup/incremental.rb +349 -353
- data/lib/rubydb/backup/restore.rb +289 -290
- data/lib/rubydb/backup/snapshot.rb +265 -267
- data/lib/rubydb/backup/verification.rb +276 -279
- data/lib/rubydb/branching/branch.rb +181 -181
- data/lib/rubydb/branching/branch_manager.rb +307 -311
- data/lib/rubydb/branching/branch_metadata.rb +140 -140
- data/lib/rubydb/branching/checkout.rb +165 -166
- data/lib/rubydb/branching/copy_on_write.rb +272 -272
- data/lib/rubydb/branching/diff.rb +137 -138
- data/lib/rubydb/branching/merge.rb +282 -285
- data/lib/rubydb/build_info.rb +15 -15
- data/lib/rubydb/catalog/catalog.rb +391 -391
- data/lib/rubydb/catalog/column.rb +112 -112
- data/lib/rubydb/catalog/constraint.rb +180 -180
- data/lib/rubydb/catalog/database.rb +184 -184
- data/lib/rubydb/catalog/index.rb +97 -97
- data/lib/rubydb/catalog/schema.rb +103 -103
- data/lib/rubydb/catalog/sequence.rb +90 -90
- data/lib/rubydb/catalog/system_catalog.rb +698 -698
- data/lib/rubydb/catalog/table.rb +178 -178
- data/lib/rubydb/catalog/trigger.rb +102 -102
- data/lib/rubydb/catalog/view.rb +66 -66
- data/lib/rubydb/cli/application.rb +163 -163
- data/lib/rubydb/cli/commands/backup.rb +80 -81
- data/lib/rubydb/cli/commands/branch.rb +72 -72
- data/lib/rubydb/cli/commands/checkout.rb +54 -54
- data/lib/rubydb/cli/commands/create.rb +58 -58
- data/lib/rubydb/cli/commands/diff.rb +76 -77
- data/lib/rubydb/cli/commands/doctor.rb +74 -74
- data/lib/rubydb/cli/commands/drop.rb +57 -57
- data/lib/rubydb/cli/commands/init.rb +101 -102
- data/lib/rubydb/cli/commands/inspect.rb +95 -95
- data/lib/rubydb/cli/commands/merge.rb +63 -63
- data/lib/rubydb/cli/commands/migrate.rb +62 -62
- data/lib/rubydb/cli/commands/restart.rb +42 -39
- data/lib/rubydb/cli/commands/restore.rb +121 -121
- data/lib/rubydb/cli/commands/shell.rb +365 -365
- data/lib/rubydb/cli/commands/snapshot.rb +79 -79
- data/lib/rubydb/cli/commands/start.rb +88 -82
- data/lib/rubydb/cli/commands/status.rb +96 -92
- data/lib/rubydb/cli/commands/stop.rb +47 -47
- data/lib/rubydb/cli/commands/vacuum.rb +58 -58
- data/lib/rubydb/cli/formatter.rb +221 -221
- data/lib/rubydb/cli/output.rb +168 -168
- data/lib/rubydb/client/client.rb +302 -304
- data/lib/rubydb/client/connection.rb +414 -415
- data/lib/rubydb/client/connection_pool.rb +168 -168
- data/lib/rubydb/client/connection_url.rb +96 -96
- data/lib/rubydb/client/prepared_statement.rb +60 -60
- data/lib/rubydb/client/result.rb +123 -123
- data/lib/rubydb/client/statement.rb +52 -52
- data/lib/rubydb/client/transaction.rb +130 -130
- data/lib/rubydb/concurrency/concurrency.rb +19 -19
- data/lib/rubydb/concurrency/deadlock_detector.rb +148 -150
- data/lib/rubydb/concurrency/latch.rb +101 -101
- data/lib/rubydb/concurrency/lock_graph.rb +163 -165
- data/lib/rubydb/concurrency/mutex.rb +181 -183
- data/lib/rubydb/concurrency/rw_lock.rb +180 -180
- data/lib/rubydb/concurrency/scheduler.rb +248 -250
- data/lib/rubydb/concurrency/worker_pool.rb +145 -143
- data/lib/rubydb/configuration/config.rb +170 -170
- data/lib/rubydb/configuration/defaults.rb +179 -179
- data/lib/rubydb/configuration/environment.rb +152 -152
- data/lib/rubydb/configuration/parser.rb +185 -185
- data/lib/rubydb/configuration/validation.rb +221 -221
- data/lib/rubydb/constants.rb +74 -74
- data/lib/rubydb/constraints/check.rb +181 -181
- data/lib/rubydb/constraints/constraint.rb +101 -101
- data/lib/rubydb/constraints/foreign_key.rb +130 -130
- data/lib/rubydb/constraints/not_null.rb +64 -64
- data/lib/rubydb/constraints/primary_key.rb +99 -99
- data/lib/rubydb/constraints/unique.rb +106 -108
- data/lib/rubydb/constraints/validator.rb +349 -350
- data/lib/rubydb/errors/authentication_error.rb +10 -10
- data/lib/rubydb/errors/authorization_error.rb +23 -23
- data/lib/rubydb/errors/client_error.rb +10 -10
- data/lib/rubydb/errors/configuration_error.rb +10 -10
- data/lib/rubydb/errors/connection_error.rb +10 -10
- data/lib/rubydb/errors/constraint_error.rb +23 -23
- data/lib/rubydb/errors/corruption_error.rb +10 -10
- data/lib/rubydb/errors/database_error.rb +10 -10
- data/lib/rubydb/errors/error.rb +20 -20
- data/lib/rubydb/errors/execution_error.rb +10 -10
- data/lib/rubydb/errors/parser_error.rb +10 -10
- data/lib/rubydb/errors/recovery_error.rb +10 -10
- data/lib/rubydb/errors/replication_error.rb +10 -10
- data/lib/rubydb/errors/server_error.rb +6 -6
- data/lib/rubydb/errors/storage_error.rb +10 -10
- data/lib/rubydb/errors/transaction_error.rb +10 -10
- data/lib/rubydb/execution/aggregate_executor.rb +134 -138
- data/lib/rubydb/execution/delete_executor.rb +110 -112
- data/lib/rubydb/execution/distinct_executor.rb +131 -135
- data/lib/rubydb/execution/executor.rb +1195 -1192
- data/lib/rubydb/execution/expression.rb +191 -193
- data/lib/rubydb/execution/index_scan.rb +142 -142
- data/lib/rubydb/execution/insert_executor.rb +215 -217
- data/lib/rubydb/execution/join_executor.rb +243 -249
- data/lib/rubydb/execution/limit_executor.rb +83 -85
- data/lib/rubydb/execution/optimizer.rb +227 -215
- data/lib/rubydb/execution/plan.rb +355 -353
- data/lib/rubydb/execution/planner.rb +542 -536
- data/lib/rubydb/execution/predicate.rb +235 -235
- data/lib/rubydb/execution/scan.rb +49 -49
- data/lib/rubydb/execution/sequential_scan.rb +63 -63
- data/lib/rubydb/execution/sort_executor.rb +180 -185
- data/lib/rubydb/execution/update_executor.rb +160 -162
- data/lib/rubydb/functions/aggregate.rb +70 -70
- data/lib/rubydb/functions/date_functions.rb +274 -278
- data/lib/rubydb/functions/function.rb +85 -85
- data/lib/rubydb/functions/json_functions.rb +231 -215
- data/lib/rubydb/functions/numeric_functions.rb +346 -346
- data/lib/rubydb/functions/scalar.rb +52 -52
- data/lib/rubydb/functions/string_functions.rb +383 -383
- data/lib/rubydb/functions/system_functions.rb +258 -246
- data/lib/rubydb/history/as_of.rb +238 -238
- data/lib/rubydb/history/change.rb +105 -105
- data/lib/rubydb/history/history.rb +131 -131
- data/lib/rubydb/history/history_manager.rb +228 -229
- data/lib/rubydb/history/temporal_query.rb +202 -202
- data/lib/rubydb/history/timeline.rb +144 -144
- data/lib/rubydb/indexes/btree.rb +186 -186
- data/lib/rubydb/indexes/btree_cursor.rb +258 -258
- data/lib/rubydb/indexes/btree_node.rb +384 -385
- data/lib/rubydb/indexes/hash_index.rb +150 -150
- data/lib/rubydb/indexes/index.rb +71 -71
- data/lib/rubydb/indexes/index_manager.rb +408 -406
- data/lib/rubydb/indexes/index_scan.rb +466 -470
- data/lib/rubydb/migrations/migration.rb +253 -254
- data/lib/rubydb/migrations/migration_lock.rb +146 -146
- data/lib/rubydb/migrations/migration_manager.rb +187 -176
- data/lib/rubydb/migrations/migration_version.rb +71 -71
- data/lib/rubydb/migrations/schema_diff.rb +211 -211
- data/lib/rubydb/migrations/schema_version.rb +64 -64
- data/lib/rubydb/monitoring/events.rb +155 -160
- data/lib/rubydb/monitoring/health.rb +216 -222
- data/lib/rubydb/monitoring/logger.rb +188 -193
- data/lib/rubydb/monitoring/metrics.rb +363 -359
- data/lib/rubydb/monitoring/performance.rb +176 -176
- data/lib/rubydb/monitoring/statistics.rb +168 -170
- data/lib/rubydb/mvcc/garbage_collector.rb +199 -199
- data/lib/rubydb/mvcc/mvcc.rb +16 -16
- data/lib/rubydb/mvcc/snapshot.rb +146 -147
- data/lib/rubydb/mvcc/vacuum.rb +180 -180
- data/lib/rubydb/mvcc/version.rb +106 -106
- data/lib/rubydb/mvcc/version_store.rb +396 -398
- data/lib/rubydb/mvcc/visibility.rb +107 -109
- data/lib/rubydb/protocol/capabilities.rb +125 -125
- data/lib/rubydb/protocol/decoder.rb +142 -145
- data/lib/rubydb/protocol/encoder.rb +131 -136
- data/lib/rubydb/protocol/handshake.rb +306 -305
- data/lib/rubydb/protocol/message.rb +121 -121
- data/lib/rubydb/protocol/parameter_binder.rb +101 -0
- data/lib/rubydb/protocol/protocol.rb +276 -277
- data/lib/rubydb/protocol/version.rb +54 -54
- data/lib/rubydb/rails/adapter.rb +245 -239
- data/lib/rubydb/rails/connection.rb +312 -314
- data/lib/rubydb/rails/database_statements.rb +122 -122
- data/lib/rubydb/rails/migration.rb +131 -131
- data/lib/rubydb/rails/quoting.rb +109 -109
- data/lib/rubydb/rails/result.rb +123 -120
- data/lib/rubydb/rails/schema_statements.rb +339 -339
- data/lib/rubydb/rails/transaction.rb +105 -105
- data/lib/rubydb/rails/type.rb +126 -126
- data/lib/rubydb/recovery/checkpoint.rb +261 -257
- data/lib/rubydb/recovery/consistency.rb +457 -467
- data/lib/rubydb/recovery/corruption_detector.rb +5 -5
- data/lib/rubydb/recovery/crash_recovery.rb +381 -387
- data/lib/rubydb/recovery/recovery_manager.rb +204 -206
- data/lib/rubydb/recovery/redo.rb +235 -237
- data/lib/rubydb/recovery/undo.rb +204 -206
- data/lib/rubydb/replication/failover.rb +5 -5
- data/lib/rubydb/replication/fencing.rb +63 -63
- data/lib/rubydb/replication/primary.rb +461 -450
- data/lib/rubydb/replication/replica.rb +382 -384
- data/lib/rubydb/replication/replication_log.rb +194 -200
- data/lib/rubydb/replication/replication_manager.rb +307 -308
- data/lib/rubydb/replication/replication_slot.rb +293 -295
- data/lib/rubydb/replication/replication_stream.rb +198 -201
- data/lib/rubydb/rubydb.rb +564 -560
- data/lib/rubydb/security/access_control.rb +252 -254
- data/lib/rubydb/security/audit_log.rb +209 -213
- data/lib/rubydb/security/authentication.rb +302 -302
- data/lib/rubydb/security/authorization.rb +282 -282
- data/lib/rubydb/security/credentials.rb +192 -196
- data/lib/rubydb/security/password.rb +205 -215
- data/lib/rubydb/security/permissions.rb +74 -74
- data/lib/rubydb/security/role.rb +99 -101
- data/lib/rubydb/security/user.rb +86 -86
- data/lib/rubydb/server/connection.rb +383 -366
- data/lib/rubydb/server/connection_pool.rb +193 -193
- data/lib/rubydb/server/lifecycle.rb +227 -228
- data/lib/rubydb/server/listener.rb +139 -136
- data/lib/rubydb/server/request_handler.rb +277 -276
- data/lib/rubydb/server/server.rb +363 -364
- data/lib/rubydb/server/session.rb +371 -369
- data/lib/rubydb/server/worker.rb +206 -210
- data/lib/rubydb/server/worker_pool.rb +168 -168
- data/lib/rubydb/sql/ast/alter_table.rb +169 -169
- data/lib/rubydb/sql/ast/begin_transaction.rb +47 -47
- data/lib/rubydb/sql/ast/commit.rb +37 -37
- data/lib/rubydb/sql/ast/constraint.rb +92 -83
- data/lib/rubydb/sql/ast/create_database.rb +41 -41
- data/lib/rubydb/sql/ast/create_index.rb +61 -61
- data/lib/rubydb/sql/ast/create_schema.rb +52 -52
- data/lib/rubydb/sql/ast/create_table.rb +187 -187
- data/lib/rubydb/sql/ast/delete.rb +54 -54
- data/lib/rubydb/sql/ast/drop_database.rb +41 -41
- data/lib/rubydb/sql/ast/drop_index.rb +41 -41
- data/lib/rubydb/sql/ast/drop_schema.rb +49 -49
- data/lib/rubydb/sql/ast/drop_table.rb +49 -49
- data/lib/rubydb/sql/ast/explain.rb +64 -64
- data/lib/rubydb/sql/ast/expression.rb +617 -604
- data/lib/rubydb/sql/ast/insert.rb +66 -66
- data/lib/rubydb/sql/ast/node.rb +42 -42
- data/lib/rubydb/sql/ast/rollback.rb +63 -63
- data/lib/rubydb/sql/ast/savepoint.rb +59 -59
- data/lib/rubydb/sql/ast/select.rb +88 -88
- data/lib/rubydb/sql/ast/set_operation.rb +22 -20
- data/lib/rubydb/sql/ast/trigger.rb +35 -29
- data/lib/rubydb/sql/ast/update.rb +88 -88
- data/lib/rubydb/sql/ast/vacuum.rb +19 -19
- data/lib/rubydb/sql/ast/view.rb +38 -32
- data/lib/rubydb/sql/ast/with.rb +32 -32
- data/lib/rubydb/sql/grammar.rb +86 -86
- data/lib/rubydb/sql/keywords.rb +156 -156
- data/lib/rubydb/sql/lexer.rb +209 -214
- data/lib/rubydb/sql/operators.rb +100 -100
- data/lib/rubydb/sql/parser.rb +1167 -1170
- data/lib/rubydb/sql/planner/analyzer.rb +283 -302
- data/lib/rubydb/sql/planner/binder.rb +537 -550
- data/lib/rubydb/sql/planner/type_checker.rb +427 -431
- data/lib/rubydb/sql/token.rb +210 -210
- data/lib/rubydb/storage/buffer_frame.rb +44 -44
- data/lib/rubydb/storage/buffer_pool.rb +155 -155
- data/lib/rubydb/storage/database_lock.rb +74 -74
- data/lib/rubydb/storage/deserializer.rb +332 -342
- data/lib/rubydb/storage/engine.rb +2347 -2330
- data/lib/rubydb/storage/file_manager.rb +191 -187
- data/lib/rubydb/storage/free_space_map.rb +79 -81
- data/lib/rubydb/storage/page.rb +92 -94
- data/lib/rubydb/storage/page_allocator.rb +852 -855
- data/lib/rubydb/storage/page_header.rb +63 -67
- data/lib/rubydb/storage/page_manager.rb +127 -131
- data/lib/rubydb/storage/record.rb +58 -58
- data/lib/rubydb/storage/row.rb +78 -78
- data/lib/rubydb/storage/serializer.rb +51 -51
- data/lib/rubydb/storage/storage_layout.rb +151 -151
- data/lib/rubydb/storage/storage_manager.rb +114 -114
- data/lib/rubydb/storage/tuple.rb +458 -461
- data/lib/rubydb/storage/visibility_map.rb +964 -973
- data/lib/rubydb/transactions/commit_manager.rb +219 -220
- data/lib/rubydb/transactions/isolation.rb +98 -98
- data/lib/rubydb/transactions/lock.rb +76 -76
- data/lib/rubydb/transactions/lock_manager.rb +359 -362
- data/lib/rubydb/transactions/savepoint.rb +142 -143
- data/lib/rubydb/transactions/transaction.rb +214 -215
- data/lib/rubydb/transactions/transaction_id.rb +84 -84
- data/lib/rubydb/transactions/transaction_log.rb +256 -257
- data/lib/rubydb/transactions/transaction_manager.rb +434 -435
- data/lib/rubydb/types/bigint.rb +36 -36
- data/lib/rubydb/types/blob.rb +37 -37
- data/lib/rubydb/types/boolean.rb +34 -34
- data/lib/rubydb/types/date.rb +39 -39
- data/lib/rubydb/types/decimal.rb +48 -48
- data/lib/rubydb/types/float.rb +34 -34
- data/lib/rubydb/types/integer.rb +36 -36
- data/lib/rubydb/types/json.rb +41 -41
- data/lib/rubydb/types/null.rb +34 -34
- data/lib/rubydb/types/smallint.rb +36 -36
- data/lib/rubydb/types/text.rb +37 -37
- data/lib/rubydb/types/time.rb +46 -46
- data/lib/rubydb/types/timestamp.rb +39 -39
- data/lib/rubydb/types/type.rb +119 -119
- data/lib/rubydb/types/uuid.rb +47 -47
- data/lib/rubydb/types/varchar.rb +37 -37
- data/lib/rubydb/version.rb +32 -32
- data/lib/rubydb/wal/archive.rb +190 -193
- data/lib/rubydb/wal/checkpoint.rb +181 -183
- data/lib/rubydb/wal/lsn.rb +94 -94
- data/lib/rubydb/wal/reader.rb +259 -260
- data/lib/rubydb/wal/record.rb +105 -105
- data/lib/rubydb/wal/segment.rb +193 -193
- data/lib/rubydb/wal/wal.rb +480 -452
- data/lib/rubydb/wal/writer.rb +236 -236
- data/lib/rubydb.rb +7 -7
- data/packaging/docker/docker-compose.failover.yml +43 -43
- data/packaging/homebrew/rubydb.rb +19 -19
- data/rubydb.gemspec +60 -57
- data/scripts/benchmark +7 -7
- data/scripts/durability_drill +37 -37
- data/scripts/fuzz +63 -63
- data/scripts/release +45 -40
- data/scripts/release_check +43 -43
- data/scripts/replication_failover_drill +268 -250
- data/scripts/replication_network_failover_drill +287 -255
- data/scripts/restore_drill +45 -45
- data/scripts/security +45 -0
- metadata +54 -1
|
@@ -1,698 +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
|
|
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
|