fix user mover

pull/15686/head
Gonzalo Riestra 4 years ago
parent 249154bf58
commit 92f495dea4

@ -191,21 +191,23 @@ module CartoDB
end
begin
if @target_org_id && @target_is_owner && File.exists?("#{@path}org_#{@target_org_id}.dump")
create_db(@target_dbname, true)
dump = "org_#{@target_org_id}.dump"
create_db(dump)
create_org_oauth_app_user_roles(@target_org_id)
create_org_api_key_roles(@target_org_id)
import_pgdump("org_#{@target_org_id}.dump")
grant_org_oauth_app_user_roles(@target_org_id)
grant_org_api_key_roles(@target_org_id)
elsif File.exists? "#{@path}user_#{@target_userid}.dump"
create_db(@target_dbname, true)
dump = "user_#{@target_userid}.dump"
create_db(dump)
create_user_oauth_app_user_roles(@target_userid)
create_user_api_key_roles(@target_userid)
import_pgdump("user_#{@target_userid}.dump")
import_pgdump(dump)
grant_user_oauth_app_user_roles(@target_userid)
grant_user_api_key_roles(@target_userid)
elsif File.exists? "#{@path}#{@target_username}.schema.sql"
create_db(@target_dbname, false)
create_db
run_file_restore_schema("#{@target_username}.schema.sql")
end
rescue => e
@ -596,21 +598,21 @@ module CartoDB
superuser_pg_conn.query("ALTER USER \"#{user}\" SET search_path= #{search_path}")
end
def create_db(dbname, blank)
# Blank is when the database should be created empty (will receive a pg_dump).
# blank = false: it should have postgis, cartodb/cdb_importer/cdb schemas
def create_db(dump = nil)
# When a dump file is provided, the database should be created empty (will receive a pg_dump).
# dump = nil: it should have postgis, cartodb/cdb_importer/cdb schemas
# connect as superuser (postgres)
@logger.info "Creating user DB #{dbname}..."
@logger.info "Creating user DB #{@target_dbname}..."
begin
if blank
superuser_pg_conn.query("CREATE DATABASE \"#{dbname}\"")
if dump
superuser_pg_conn.query("CREATE DATABASE \"#{@target_dbname}\"")
else
superuser_pg_conn.query("CREATE DATABASE \"#{dbname}\" WITH TEMPLATE template_postgis")
superuser_pg_conn.query("CREATE DATABASE \"#{@target_dbname}\" WITH TEMPLATE template_postgis")
end
# This rescue can be improved a little bit. The way it is it assumes that the error
# will always be that the db already exists
rescue PG::Error => e
if blank
if dump
@logger.error "Error: Database already exists"
raise e
else
@ -618,10 +620,10 @@ module CartoDB
end
end
setup_db(dbname) unless blank
dump ? setup_db_for_migration(dump) : setup_db
end
def setup_db(_dbname)
def setup_db
superuser_user_pg_conn.query("CREATE EXTENSION IF NOT EXISTS postgis")
cartodb_schema = superuser_user_pg_conn.query("SELECT nspname FROM pg_catalog.pg_namespace where nspname = 'cartodb'")
superuser_user_pg_conn.query("CREATE SCHEMA cartodb") if cartodb_schema.count == 0
@ -635,6 +637,19 @@ module CartoDB
raise e
end
def setup_db_for_migration(dump)
source_db_version = get_dump_database_version(superuser_pg_conn, dump).split('.').first
destination_db_version = get_database_version_for_binaries(superuser_pg_conn).split('.').first
return unless source_db_version == '11' && destination_db_version == '12'
superuser_user_pg_conn.query("CREATE EXTENSION IF NOT EXISTS postgis")
superuser_user_pg_conn.query("CREATE EXTENSION IF NOT EXISTS postgis_raster")
superuser_user_pg_conn.query("CREATE EXTENSION IF NOT EXISTS plpython3u")
rescue PG::Error => e
@logger.error "Error: Cannot setup DB for migration"
raise e
end
def update_database_retries(userid, username, db_host, db_name, retries = 1)
update_database(userid, username, db_host, db_name)
rescue => e

@ -1534,6 +1534,7 @@ module CartoDB
'FUNCTION pgis_abs_in(cstring)',
'FUNCTION pgis_abs_out(pgis_abs)',
'FUNCTION pgis_geometry_accum_finalfn(pgis_abs)',
'FUNCTION pgis_geometry_accum_finalfn(internal)',
'FUNCTION pgis_geometry_accum_transfn(pgis_abs,geometry)',
'FUNCTION pgis_geometry_accum_transfn(pgis_abs,geometry,double precision)',
'FUNCTION pgis_geometry_accum_transfn(pgis_abs,geometry,double precision,integer)',
@ -1838,9 +1839,11 @@ module CartoDB
'FUNCTION st_asgeojson(geography)',
'FUNCTION st_asgeojson(geography,integer)',
'FUNCTION st_asgeojson(geography,integer,integer)',
'FUNCTION _st_asgeojson(geography,integer,integer)',
'FUNCTION st_asgeojson(geometry)',
'FUNCTION st_asgeojson(geometry,integer)',
'FUNCTION st_asgeojson(geometry,integer,integer)',
'FUNCTION _st_asgeojson(geometry,integer,integer)',
'FUNCTION st_asgeojson(integer,geography)',
'FUNCTION st_asgeojson(integer,geography,integer)',
'FUNCTION _st_asgeojson(integer,geography,integer,integer)',
@ -1850,6 +1853,7 @@ module CartoDB
'FUNCTION _st_asgeojson(integer,geometry,integer,integer)',
'FUNCTION st_asgeojson(integer,geometry,integer,integer)',
'FUNCTION st_asgeojson(text)',
'FUNCTION _st_asgeojson(text)',
'FUNCTION st_asgml(geography)',
'FUNCTION st_asgml(geography,integer)',
'FUNCTION st_asgml(geography,integer,integer)',
@ -1869,6 +1873,8 @@ module CartoDB
'FUNCTION st_asgml(integer,geometry,integer,integer)',
'FUNCTION _st_asgml(integer,geometry,integer,integer,text)',
'FUNCTION st_asgml(integer,geometry,integer,integer,text)',
'FUNCTION st_asgml(integer,geography,integer,integer,text,text)',
'FUNCTION _st_asgml(integer,geography,integer,integer,text,text)',
'FUNCTION st_asgml(text)',
'FUNCTION st_ashexewkb(geometry)',
'FUNCTION st_ashexewkb(geometry,text)',
@ -3049,6 +3055,7 @@ module CartoDB
'FUNCTION zmin(box3d)',
'FUNCTION st_astext(bytea)',
'FUNCTION st_length_spheroid3d(geometry,spheroid)',
'FUNCTION st_generatepoints(geometry,numeric)',
'OPERATOR CLASS btree_geography_ops',
'OPERATOR CLASS btree_geometry_ops',
'OPERATOR CLASS gist_geography_ops',
@ -3202,9 +3209,9 @@ module CartoDB
return false unless stripped =~ SIGNATURE_RE
match = stripped.match(SIGNATURE_RE)
type = match[:type].strip.gsub(/\s+/, ' ')
name = match[:name].split(' ').last.strip
name = match[:name].split(/[\s.]/).last
arguments = match[:arguments]
arguments = arguments ? arguments.split(',').map { |arg| arg.split('.').last.strip } : nil
arguments = arguments ? arguments.split(',').map { |arg| arg.split(/[\s.]/).last } : nil
[arguments, name, type]
end
end

@ -44,6 +44,13 @@ describe 'UserMigration' do
end
describe 'legacy functions' do
before(:all) do
class DummyTester
include CartoDB::DataMover::LegacyFunctions
end
@dummy_tester = DummyTester.new
end
before :each do
@legacy_functions = CartoDB::DataMover::LegacyFunctions::LEGACY_FUNCTIONS
end
@ -53,27 +60,31 @@ describe 'UserMigration' do
end
it 'loads legacy functions' do
CartoDB::DataMover::LegacyFunctions::LEGACY_FUNCTIONS.count.should eq 2503
CartoDB::DataMover::LegacyFunctions::LEGACY_FUNCTIONS.count.should eq 2510
end
it 'matches functions with attributes qualified with namespace' do
class DummyTester
include CartoDB::DataMover::LegacyFunctions
end
line = '1880; 1255 5950507 FUNCTION asbinary("geometry", "pg_catalog"."text") postgres'
DummyTester.new.remove_line?(line).should be true
@dummy_tester.remove_line?(line).should be true
line2 = '8506; 2753 18284 OPERATOR FAMILY public btree_geography_ops postgres'
DummyTester.new.remove_line?(line2).should be true
@dummy_tester.remove_line?(line2).should be true
line3 = '18305; 0 0 ACL public st_wkbtosql("bytea") postgres'
DummyTester.new.remove_line?(line3).should be true
@dummy_tester.remove_line?(line3).should be true
line4 = '18333; 0 0 ACL public st_countagg("raster", integer, boolean, double precision) postgres'
DummyTester.new.remove_line?(line4).should be false
@dummy_tester.remove_line?(line4).should be false
line5 = '541; 1259 735510 FOREIGN TABLE aggregation agg_admin1 postgres'
DummyTester.new.remove_line?(line5).should be false
@dummy_tester.remove_line?(line5).should be false
line6 = '242; 1255 148122 FUNCTION org00000001-admin st_text(boolean)'
DummyTester.new.remove_line?(line6).should be true
@dummy_tester.remove_line?(line6).should be true
line7 = '5003; 0 0 ACL org000001-admin FUNCTION "st_text"(boolean)'
DummyTester.new.remove_line?(line7).should be true
@dummy_tester.remove_line?(line7).should be true
end
it 'matches functions with attributes qualified with namespace and name' do
line = '5785; 0 0 ACL public FUNCTION "st_asgeojson"'\
'("geog" "public"."geography", "maxdecimaldigits" integer, "options" integer) postgres'
expect(@dummy_tester.remove_line?(line)).to be_true
end
it 'skips importing legacy functions using fixture' do

Loading…
Cancel
Save