How to drop table and recreate in amazon RDS with Elasticbeanstalk?

Question:

My database on Amazon currently has only a little data in it (I am making a web app but it is still in development) and I am looking to delete it, make changes to the schema, and put it back up again. The past few times I have done this, I have completely recreated my elasticbeanstalk app, but there seems like there is a better way. On my local machine, I will take the following steps:

  1. “dropdb databasename” and then “createdb databasename”
  2. python manage.py makemigrations
  3. python manage.py migrate

Is there something like this that I can do on amazon to delete my database and put it back online again without deleting the entire application? When I tried just deleting the RDS instance a while ago and making a new one, I was having problems with elasticbeanstalk.

Asked By: TH22

||

Answers:

The easiest way to accomplish this is to SSH to one of your EC2 instances, that has acccess to the RDS DB, and then connect to the DB from there. Make sure that your python scripts can read your app configuration to access the configured DB, or add arguments for DB hostname. To drop and create your DB, you must just add the necessary arguments to connect to the DB. For example:

$ createdb -h <RDS endpoint> -U <user> -W ebdb

You can also create a RDS snapshot when the DB is empty, and use the RDS instance actions Restore to Point in Time or Migrate Latest Snapshot.

Answered By: quezacoatl

I had the same problem and came up with a workaround. In your python code just add and run the following method when deploying your app the next time:

FOR SQLALCHEMY AFTER VERSION 2.0

from sqlalchemy import create_engine, text

tables = ["table1_name", "table2_name"] # the names of the tables you want to delte
engine = create_engine("sqlite:///example.db") # here you create your engine
    def delete_tables(tables):
        for table in tables:
            sql = text(f"DROP TABLE IF EXISTS {table} CASCADE;") # CASCADE deltes the tables even if they had some connections to other tables
            with engine.connect() as connection:
                with connection.begin():
                    connection.execute(sql)

delete_tables(tables) # Comment this line out after running it once.

FOR SQLALCHEMY BEFORE VERSION 2 (I guess)

    def delete_tables(tables):
        for table in tables:
            engine.execute(f"DROP TABLE IF EXISTS {table} CASCADE;")

delete_tables(tables) # Comment this line out after running it once.

After you deployed and ran this code 1 time, all your tables will be deleted.

IMPORTANT: Delete or comment out this code after that, otherwise you will delete all your tables every time when you deploy your code to AWS

Answered By: Betelgeitze