|
|
@ -320,27 +320,61 @@ module Carto
|
|
|
|
return unless databases.present?
|
|
|
|
return unless databases.present?
|
|
|
|
|
|
|
|
|
|
|
|
databases[:tables].each do |table|
|
|
|
|
databases[:tables].each do |table|
|
|
|
|
check_table(table)
|
|
|
|
if !check_table(table) && !check_view(table) && !check_materilized_view(table)
|
|
|
|
|
|
|
|
raise Carto::UnprocesableEntityError.new("relation \"#{table[:schema]}.#{table[:name]}\" does not exist")
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def check_table(table)
|
|
|
|
def check_table(table)
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
result = db_run(%{
|
|
|
|
result = db_run(%{
|
|
|
|
SELECT *
|
|
|
|
SELECT *
|
|
|
|
FROM
|
|
|
|
FROM
|
|
|
|
pg_tables
|
|
|
|
pg_tables
|
|
|
|
WHERE
|
|
|
|
WHERE
|
|
|
|
schemaname = #{db_connection.quote(table[:schema])} AND
|
|
|
|
schemaname = #{db_connection.quote(table[:schema])} AND
|
|
|
|
tablename = #{db_connection.quote(table[:name])}
|
|
|
|
tablename = #{db_connection.quote(table[:name])}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
rescue StandardError => e
|
|
|
|
rescue StandardError => e
|
|
|
|
raise_unprocessable_entity_error(e)
|
|
|
|
raise_unprocessable_entity_error(e)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
if result && result.count.zero?
|
|
|
|
result && !result.count.zero?
|
|
|
|
raise Carto::UnprocesableEntityError.new("relation \"#{table[:schema]}.#{table[:name]}\" does not exist")
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_view(view)
|
|
|
|
|
|
|
|
begin
|
|
|
|
|
|
|
|
result = db_run(%{
|
|
|
|
|
|
|
|
SELECT *
|
|
|
|
|
|
|
|
FROM
|
|
|
|
|
|
|
|
pg_views
|
|
|
|
|
|
|
|
WHERE
|
|
|
|
|
|
|
|
schemaname = #{db_connection.quote(view[:schema])} AND
|
|
|
|
|
|
|
|
viewname = #{db_connection.quote(view[:name])}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
rescue StandardError => e
|
|
|
|
|
|
|
|
raise_unprocessable_entity_error(e)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
result && !result.count.zero?
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_materilized_view(matview)
|
|
|
|
|
|
|
|
begin
|
|
|
|
|
|
|
|
result = db_run(%{
|
|
|
|
|
|
|
|
SELECT *
|
|
|
|
|
|
|
|
FROM
|
|
|
|
|
|
|
|
pg_matviews
|
|
|
|
|
|
|
|
WHERE
|
|
|
|
|
|
|
|
schemaname = #{db_connection.quote(matview[:schema])} AND
|
|
|
|
|
|
|
|
matviewname = #{db_connection.quote(matview[:name])}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
rescue StandardError => e
|
|
|
|
|
|
|
|
raise_unprocessable_entity_error(e)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
result && !result.count.zero?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def invalidate_cache
|
|
|
|
def invalidate_cache
|
|
|
@ -431,7 +465,7 @@ module Carto
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def drop_db_role
|
|
|
|
def drop_db_role
|
|
|
|
revoke_privileges
|
|
|
|
db_run("DROP OWNED BY \"#{db_role}\"")
|
|
|
|
db_run("DROP ROLE \"#{db_role}\"")
|
|
|
|
db_run("DROP ROLE \"#{db_role}\"")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
@ -497,18 +531,6 @@ module Carto
|
|
|
|
$users_metadata
|
|
|
|
$users_metadata
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def revoke_privileges
|
|
|
|
|
|
|
|
affected_schemas.uniq.each do |schema|
|
|
|
|
|
|
|
|
db_run("REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA \"#{schema}\" FROM \"#{db_role}\"")
|
|
|
|
|
|
|
|
db_run("REVOKE USAGE ON SCHEMA \"#{schema}\" FROM \"#{db_role}\"")
|
|
|
|
|
|
|
|
db_run("REVOKE USAGE, SELECT ON ALL SEQUENCES IN SCHEMA \"#{schema}\" FROM \"#{db_role}\"")
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if user.organization_user?
|
|
|
|
|
|
|
|
db_run("REVOKE ALL ON FUNCTION \"#{user.database_schema}\"._CDB_UserQuotaInBytes() FROM \"#{db_role}\"")
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def grant_aux_write_privileges_for_schema(s)
|
|
|
|
def grant_aux_write_privileges_for_schema(s)
|
|
|
|
db_run("GRANT USAGE ON SCHEMA \"#{s}\" TO \"#{db_role}\"")
|
|
|
|
db_run("GRANT USAGE ON SCHEMA \"#{s}\" TO \"#{db_role}\"")
|
|
|
|
end
|
|
|
|
end
|
|
|
|