github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/scripts/psql-migration-test.sh (about)

     1  TMPDIR=`mktemp -d 2>/dev/null || mktemp -d -t 'tmpConfigDir'`
     2  DUMPDIR=`mktemp -d 2>/dev/null || mktemp -d -t 'dumpDir'`
     3  
     4  cp config/config.json $TMPDIR
     5  
     6  echo "Creating databases"
     7  docker exec mattermost-postgres sh -c 'exec echo "CREATE DATABASE migrated; CREATE DATABASE latest;" | exec psql -U mmuser mattermost_test'
     8  
     9  echo "Importing postgres dump from version 5.0"
    10  docker exec -i mattermost-postgres psql -U mmuser -d migrated < $(pwd)/scripts/mattermost-postgresql-5.0.sql
    11  
    12  echo "Setting up config for db migration"
    13  make ARGS="config set SqlSettings.DataSource 'postgres://mmuser:mostest@localhost:5432/migrated?sslmode=disable&connect_timeout=10' --config $TMPDIR/config.json" run-cli
    14  make ARGS="config set SqlSettings.DriverName 'postgres' --config $TMPDIR/config.json" run-cli
    15  
    16  echo "Running the migration"
    17  make ARGS="version --config $TMPDIR/config.json" run-cli
    18  
    19  echo "Setting up config for fresh db setup"
    20  make ARGS="config set SqlSettings.DataSource 'postgres://mmuser:mostest@localhost:5432/latest?sslmode=disable&connect_timeout=10' --config $TMPDIR/config.json" run-cli
    21  
    22  echo "Setting up fresh db"
    23  make ARGS="version --config $TMPDIR/config.json" run-cli
    24  
    25  for i in "ChannelMembers MentionCountRoot" "ChannelMembers MsgCountRoot" "Channels TotalMsgCountRoot"; do
    26      a=( $i );
    27      echo "Ignoring known Postgres mismatch: ${a[0]}.${a[1]}"
    28      docker exec mattermost-postgres psql -U mmuser -d migrated -c "ALTER TABLE ${a[0]} DROP COLUMN ${a[1]};"
    29      docker exec mattermost-postgres psql -U mmuser -d latest -c "ALTER TABLE ${a[0]} DROP COLUMN ${a[1]};"
    30  done
    31  
    32  echo "Generating dump"
    33  docker exec mattermost-postgres pg_dump --schema-only -d migrated -U mmuser > $DUMPDIR/migrated.sql
    34  docker exec mattermost-postgres pg_dump --schema-only -d latest -U mmuser > $DUMPDIR/latest.sql
    35  
    36  echo "Removing databases created for db comparison"
    37  docker exec mattermost-postgres sh -c 'exec echo "DROP DATABASE migrated; DROP DATABASE latest;" | exec psql -U mmuser mattermost_test'
    38  
    39  echo "Generating diff"
    40  git diff --word-diff=color $DUMPDIR/migrated.sql $DUMPDIR/latest.sql > $DUMPDIR/diff.txt
    41  diffErrorCode=$?
    42  
    43  if [ $diffErrorCode -eq 0 ]; then
    44      echo "Both schemas are same"
    45  else
    46      echo "Schema mismatch"
    47      cat $DUMPDIR/diff.txt
    48  fi
    49  rm -rf $TMPDIR $DUMPDIR
    50  
    51  exit $diffErrorCode