Skip to content
代码片段 群组 项目
未验证 提交 5db91e48 编辑于 作者: Matt Kasa's avatar Matt Kasa 提交者: GitLab
浏览文件

Merge branch 'morefice/add-postgres-sequences-view' into 'master'

Add PostgresSequence view

See merge request https://gitlab.com/gitlab-org/gitlab/-/merge_requests/139117



Merged-by: default avatarMatt Kasa <mkasa@gitlab.com>
Approved-by: default avatarMarius Bobin <mbobin@gitlab.com>
Approved-by: default avatarMatt Kasa <mkasa@gitlab.com>
Reviewed-by: default avatarMatt Kasa <mkasa@gitlab.com>
Co-authored-by: default avatarMaxime Orefice <morefice@gitlab.com>
No related branches found
No related tags found
无相关合并请求
---
view_name: postgres_sequences
classes:
- Gitlab::Database::PostgresSequence
feature_categories:
- database
description: SQL view to get information about postgres sequences
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/139117
milestone: '16.7'
gitlab_schema: gitlab_shared
# frozen_string_literal: true
class AddPostgresSequencesView < Gitlab::Database::Migration[2.2]
milestone '16.7'
enable_lock_retries!
def up
execute(<<~SQL)
CREATE OR REPLACE VIEW postgres_sequences
AS
SELECT seq_pg_class.relname AS seq_name,
dep_pg_class.relname AS table_name,
pg_attribute.attname AS col_name
FROM pg_class seq_pg_class
INNER JOIN pg_depend ON seq_pg_class.oid = pg_depend.objid
INNER JOIN pg_class dep_pg_class ON pg_depend.refobjid = dep_pg_class.oid
INNER JOIN pg_attribute ON dep_pg_class.oid = pg_attribute.attrelid
AND pg_depend.refobjsubid = pg_attribute.attnum
WHERE seq_pg_class.relkind = 'S'
SQL
end
def down
execute(<<~SQL)
DROP VIEW postgres_sequences;
SQL
end
end
871cc15f04f235ff2719eb334c28041a0f1093653e5ca2fad5e92b911622d221
\ No newline at end of file
......@@ -21617,6 +21617,16 @@ CREATE SEQUENCE postgres_reindex_queued_actions_id_seq
 
ALTER SEQUENCE postgres_reindex_queued_actions_id_seq OWNED BY postgres_reindex_queued_actions.id;
 
CREATE VIEW postgres_sequences AS
SELECT seq_pg_class.relname AS seq_name,
dep_pg_class.relname AS table_name,
pg_attribute.attname AS col_name
FROM (((pg_class seq_pg_class
JOIN pg_depend ON ((seq_pg_class.oid = pg_depend.objid)))
JOIN pg_class dep_pg_class ON ((pg_depend.refobjid = dep_pg_class.oid)))
JOIN pg_attribute ON (((dep_pg_class.oid = pg_attribute.attrelid) AND (pg_depend.refobjsubid = pg_attribute.attnum))))
WHERE (seq_pg_class.relkind = 'S'::"char");
CREATE TABLE programming_languages (
id integer NOT NULL,
name character varying NOT NULL,
# frozen_string_literal: true
module Gitlab
module Database
# Backed by the postgres_sequences view
class PostgresSequence < SharedModel
self.primary_key = :seq_name
scope :by_table_name, ->(table_name) { where(table_name: table_name) }
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Database::PostgresSequence, type: :model, feature_category: :database do
# PostgresSequence does not `behaves_like 'a postgres model'` because it does not correspond 1-1 with a single entry
# in pg_class
let(:schema) { ActiveRecord::Base.connection.current_schema }
let(:table_name) { '_test_table' }
let(:table_name_without_sequence) { '_test_table_without_sequence' }
before do
ActiveRecord::Base.connection.execute(<<~SQL)
CREATE TABLE #{table_name} (
id bigserial PRIMARY KEY NOT NULL
);
CREATE TABLE #{table_name_without_sequence} (
id bigint PRIMARY KEY NOT NULL
);
SQL
end
describe '#by_table_name' do
context 'when table does not have a sequence' do
it 'returns an empty collection' do
expect(described_class.by_table_name(table_name_without_sequence)).to be_empty
end
end
it 'returns the sequence for a given table' do
expect(described_class.by_table_name(table_name).first[:table_name]).to eq(table_name)
end
end
end
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册